64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import { Upload, type HttpRequest, type HttpResponse, type OnSuccessPayload } from "tus-js-client";
|
|
import { CS_TUS_UPLOAD, CS_URL } from "../config";
|
|
import type { FilesType } from "@/components/ui/form/fcltReg/pbnstReg/mngReg/mngRegForm";
|
|
|
|
type UploadOptions = {
|
|
handleUploadStart?: (upload: Upload) => void;
|
|
handleProgress?: (percentage: string) => void;
|
|
handleSuccess?: (payload :OnSuccessPayload) => void;
|
|
handleResponse?: (req:HttpRequest, res: ReturnFileType) => void;
|
|
};
|
|
export type ReturnFileType={
|
|
atflCd:String,
|
|
atflSn:number,
|
|
atflId:String,
|
|
physFileNm:String
|
|
}
|
|
export const TusClientUpload = (oFile: FilesType, options?: UploadOptions): Promise<ReturnFileType> => {
|
|
return new Promise((resolve, reject) => {
|
|
const Authorization = "Authorization";
|
|
const AUTH_USER_ID = "token-id";
|
|
|
|
//CS_URL
|
|
const uploader = new Upload(oFile.file, {
|
|
endpoint: 'http://localhost:3001' + CS_TUS_UPLOAD,
|
|
chunkSize: 10 * 1024 * 1024,
|
|
headers: {
|
|
Authorization: "",
|
|
AUTH_USER_ID: "",
|
|
},
|
|
metadata: {
|
|
filename: oFile.file.name,
|
|
filetype: oFile.file.type,
|
|
},
|
|
onError(error) {
|
|
console.error(`Failed because: ${error}`);
|
|
reject(error);
|
|
},
|
|
onProgress(bytesUploaded, bytesTotal) {
|
|
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
|
|
|
|
console.log(`Uploaded ${bytesUploaded} of ${bytesTotal} bytes (${percentage}%)`);
|
|
options?.handleProgress?.(percentage);
|
|
},
|
|
onAfterResponse(req, res: HttpResponse) {
|
|
const fileInfo = res.getHeader("X-File-Info");
|
|
if (fileInfo === undefined || fileInfo === null) return;
|
|
console.log("onAfterResponse fileInfo", fileInfo);
|
|
resolve(JSON.parse(fileInfo) as ReturnFileType);
|
|
},
|
|
onSuccess(payload: OnSuccessPayload) {
|
|
options?.handleSuccess?.(payload);
|
|
},
|
|
});
|
|
|
|
options?.handleUploadStart?.(uploader);
|
|
uploader.start();
|
|
console.log("upload start >>>>>", uploader);
|
|
|
|
// setUpload(uploader);
|
|
});
|
|
}
|
|
//
|
|
//
|