54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { Upload, type HttpRequest, type HttpResponse, type OnSuccessPayload } from "tus-js-client";
|
|
import { CS_TUS_UPLOAD, CS_URL } from "../config";
|
|
|
|
type UploadOptions = {
|
|
handleUploadStart?: (upload: Upload) => void;
|
|
handleProgress?: (percentage: string) => void;
|
|
handleSuccess?: (payload :OnSuccessPayload) => void;
|
|
handleResponse?: (req:HttpRequest, res: HttpResponse) => void;
|
|
};
|
|
|
|
export function TusClientUpload(oFile: File, setUpload: (upload: Upload) => void, options?: UploadOptions) {
|
|
const Authorization = "Authorization";
|
|
const AUTH_USER_ID = "token-id";
|
|
|
|
//CS_URL
|
|
const uploader = new Upload(oFile, {
|
|
endpoint: 'http://localhost:3001' + CS_TUS_UPLOAD,
|
|
chunkSize: 10* 1024 * 1024,
|
|
headers: {
|
|
Authorization: "",
|
|
AUTH_USER_ID: "",
|
|
},
|
|
metadata: {
|
|
filename: oFile.name,
|
|
filetype: oFile.type,
|
|
},
|
|
onError(error) {
|
|
console.error(`Failed because: ${error}`);
|
|
},
|
|
onProgress(bytesUploaded, bytesTotal) {
|
|
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
|
|
options?.handleProgress?.(percentage);
|
|
},
|
|
onAfterResponse(req, res) {
|
|
const fileInfo = res.getHeader("X-File-Info");
|
|
if (fileInfo === undefined || fileInfo === null) return;
|
|
|
|
options?.handleResponse?.(res);
|
|
},
|
|
onSuccess(payload :OnSuccessPayload) {
|
|
options?.handleSuccess?.(payload);
|
|
},
|
|
});
|
|
uploader.start();
|
|
|
|
options?.handleUploadStart?.(uploader);
|
|
|
|
console.log("upload start >>>>>", uploader);
|
|
|
|
|
|
// setUpload(uploader);
|
|
}
|
|
//
|