Skip to content

Layer tus-js-client on top of Axios #8281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cvat-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cvat-core",
"version": "15.1.0",
"version": "15.1.1",
"type": "module",
"description": "Part of Computer Vision Tool which presents an interface for client-side integration",
"main": "src/api.ts",
Expand Down
87 changes: 87 additions & 0 deletions cvat-core/src/axios-tus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (C) 2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

import Axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import * as tus from 'tus-js-client';

class AxiosHttpResponse implements tus.HttpResponse {
readonly #axiosResponse: AxiosResponse;

constructor(axiosResponse: AxiosResponse) {
this.#axiosResponse = axiosResponse;
}

getStatus(): number {
return this.#axiosResponse.status;
}
getHeader(header: string): string | undefined {
return this.#axiosResponse.headers[header.toLowerCase()];
}
getBody(): string {
return this.#axiosResponse.data;
}
getUnderlyingObject(): AxiosResponse {
return this.#axiosResponse;
}
}

class AxiosHttpRequest implements tus.HttpRequest {
readonly #axiosConfig: AxiosRequestConfig;
readonly #abortController: AbortController;

constructor(method: string, url: string) {
this.#abortController = new AbortController();
this.#axiosConfig = {
method,
url,
headers: {},
signal: this.#abortController.signal,
validateStatus: () => true,
};
}

getMethod(): string {
return this.#axiosConfig.method;
}
getURL(): string {
return this.#axiosConfig.url;
}

setHeader(header: string, value: string): void {
this.#axiosConfig.headers[header.toLowerCase()] = value;
}
getHeader(header: string): string | undefined {
return this.#axiosConfig.headers[header.toLowerCase()];
}

setProgressHandler(handler: (bytesSent: number) => void): void {
this.#axiosConfig.onUploadProgress = (progressEvent) => {
handler(progressEvent.loaded);
};
}

async send(body: any): Promise<tus.HttpResponse> {
const axiosResponse = await Axios({ ...this.#axiosConfig, data: body });
return new AxiosHttpResponse(axiosResponse);
}

async abort(): Promise<void> {
this.#abortController.abort();
}

getUnderlyingObject(): AxiosRequestConfig {
return this.#axiosConfig;
}
}

class AxiosHttpStack implements tus.HttpStack {
createRequest(method: string, url: string): tus.HttpRequest {
return new AxiosHttpRequest(method, url);
}
getName(): string {
return 'AxiosHttpStack';
}
}

export const axiosTusHttpStack = new AxiosHttpStack();
12 changes: 2 additions & 10 deletions cvat-core/src/server-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { ChunkQuality } from 'cvat-data';

import './axios-config';
import { axiosTusHttpStack } from './axios-tus';
import {
SerializedLabel, SerializedAnnotationFormats, ProjectsFilter,
SerializedProject, SerializedTask, TasksFilter, SerializedUser, SerializedOrganization,
Expand Down Expand Up @@ -117,7 +118,6 @@
}

async function chunkUpload(file: File, uploadConfig): Promise<{ uploadSentSize: number; filename: string }> {
const params = enableOrganization();
const {
endpoint, chunkSize, totalSize, onUpdate, metadata, totalSentSize,
} = uploadConfig;
Expand All @@ -130,9 +130,7 @@
filetype: file.type,
...metadata,
},
headers: {
Authorization: Axios.defaults.headers.common.Authorization,
},
httpStack: axiosTusHttpStack,
chunkSize,
retryDelays: [2000, 4000, 8000, 16000, 32000, 64000],
onShouldRetry(err: tus.DetailedError | Error): boolean {
Expand All @@ -151,12 +149,6 @@
onError(error) {
reject(error);
},
onBeforeRequest(req) {
const xhr = req.getUnderlyingObject();
const { org } = params;
req.setHeader('X-Organization', org);
xhr.withCredentials = true;
},
onProgress(bytesUploaded) {
if (onUpdate && Number.isInteger(totalSentSize) && Number.isInteger(totalSize)) {
const currentUploadedSize = totalSentSize + bytesUploaded;
Expand Down Expand Up @@ -252,7 +244,7 @@
return new ServerError(message, 0);
}

function prepareData(details) {

Check warning on line 247 in cvat-core/src/server-proxy.ts

View workflow job for this annotation

GitHub Actions / Linter

Missing return type on function
const data = new FormData();
for (const [key, value] of Object.entries(details)) {
if (Array.isArray(value)) {
Expand Down Expand Up @@ -294,7 +286,7 @@
return requestId++;
}

async function get(url: string, requestConfig) {

Check warning on line 289 in cvat-core/src/server-proxy.ts

View workflow job for this annotation

GitHub Actions / Linter

Missing return type on function
return new Promise((resolve, reject) => {
const newRequestId = getRequestId();
requests[newRequestId] = { resolve, reject };
Expand Down Expand Up @@ -849,7 +841,7 @@
save_images: saveImages,
};
return new Promise<string | void>((resolve, reject) => {
async function request() {

Check warning on line 844 in cvat-core/src/server-proxy.ts

View workflow job for this annotation

GitHub Actions / Linter

Missing return type on function
Axios.post(baseURL, {}, {
params,
})
Expand Down Expand Up @@ -941,7 +933,7 @@
const url = `${backendAPI}/tasks/${id}/backup/export`;

return new Promise<string | void>((resolve, reject) => {
async function request() {

Check warning on line 936 in cvat-core/src/server-proxy.ts

View workflow job for this annotation

GitHub Actions / Linter

Missing return type on function
try {
const response = await Axios.post(url, {}, {
params,
Expand Down Expand Up @@ -1020,7 +1012,7 @@
const url = `${backendAPI}/projects/${id}/backup/export`;

return new Promise<string | void>((resolve, reject) => {
async function request() {

Check warning on line 1015 in cvat-core/src/server-proxy.ts

View workflow job for this annotation

GitHub Actions / Linter

Missing return type on function
try {
const response = await Axios.post(url, {}, {
params,
Expand Down Expand Up @@ -1145,7 +1137,7 @@
message: 'CVAT is uploading task data to the server',
}));

async function bulkUpload(taskId, files) {

Check warning on line 1140 in cvat-core/src/server-proxy.ts

View workflow job for this annotation

GitHub Actions / Linter

Missing return type on function
const fileBulks = files.reduce((fileGroups, file) => {
const lastBulk = fileGroups[fileGroups.length - 1];
if (chunkSize - lastBulk.size >= file.size) {
Expand Down
3 changes: 2 additions & 1 deletion cvat/apps/engine/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from unittest import mock
from textwrap import dedent
from typing import Optional, Callable, Dict, Any, Mapping
from urllib.parse import urljoin

import django_rq
from attr.converters import to_bool
Expand Down Expand Up @@ -315,7 +316,7 @@ def init_tus_upload(self, request):

return self._tus_response(
status=status.HTTP_201_CREATED,
extra_headers={'Location': '{}{}'.format(location, tus_file.file_id),
extra_headers={'Location': urljoin(location, tus_file.file_id),
'Upload-Filename': tus_file.filename})

def append_tus_chunk(self, request, file_id):
Expand Down
Loading