103 lines
2.9 KiB
TypeScript
103 lines
2.9 KiB
TypeScript
import type { FilesType } from "@/components/ui/form/fcltReg/pbnstReg/mngReg/mngRegForm";
|
|
import toast from "react-hot-toast";
|
|
import {
|
|
Upload,
|
|
type DetailedError,
|
|
type HttpRequest,
|
|
type HttpResponse,
|
|
type OnSuccessPayload,
|
|
} from "tus-js-client";
|
|
import { fetchType, putFectch } from "../api";
|
|
import { CS_TUS_UPLOAD } from "../config";
|
|
|
|
type UploadOptions = {
|
|
handleUploadStart?: (upload: Upload) => void;
|
|
handleProgress?: (percentage: string) => void;
|
|
handleSuccess?: (payload: OnSuccessPayload) => void;
|
|
handleResponse?: (req: HttpRequest, res: ReturnFileType) => void;
|
|
};
|
|
export type ReturnFileType = {
|
|
orgnlFileNm: string;
|
|
atflFlieSz: number;
|
|
atflType: string;
|
|
dwnldNocs: number;
|
|
delYn: string;
|
|
lnkgDmnId: string | null;
|
|
lnkgDmnNm: string | null;
|
|
regDt: Date | null;
|
|
atflCd: string;
|
|
atflSn: number;
|
|
atflId: string;
|
|
physFileNm: string;
|
|
};
|
|
export const TusClientUpload = (oFile: FilesType, options?: UploadOptions) =>
|
|
new Promise<ReturnFileType>((resolve, reject) => {
|
|
try {
|
|
let fileInfo: string | undefined;
|
|
//CS_URL
|
|
const uploader = new Upload(oFile.file, {
|
|
endpoint: CS_TUS_UPLOAD,
|
|
chunkSize: 10 * 1024 * 1024,
|
|
headers: {
|
|
Authorization: "",
|
|
AUTH_USER_ID: "",
|
|
},
|
|
metadata: {
|
|
filename: oFile.file.name,
|
|
filetype: oFile.file.type,
|
|
},
|
|
onError(error) {
|
|
reject(error);
|
|
},
|
|
onProgress(bytesUploaded, bytesTotal) {
|
|
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
|
|
options?.handleProgress?.(percentage);
|
|
},
|
|
onAfterResponse(req, res: HttpResponse) {
|
|
fileInfo = res.getHeader("X-File-Info");
|
|
if (fileInfo === undefined || fileInfo === null) return;
|
|
},
|
|
onSuccess(payload: OnSuccessPayload) {
|
|
options?.handleSuccess?.(payload);
|
|
if (fileInfo) {
|
|
|
|
|
|
const WrTableInfo = {
|
|
lnkgDmnNm: "tbWrRuseFcltFile",
|
|
lnkgDmnId: "fileTkn",
|
|
};
|
|
|
|
console.log('fileInfo ', fileInfo)
|
|
const fileParse = JSON.parse(fileInfo) as ReturnFileType;
|
|
putFectch(
|
|
`/attach-service/api/v1/atfl/attachments/${fileParse.atflCd}/info`,
|
|
WrTableInfo,
|
|
fetchType.CS
|
|
)
|
|
.then(() => {
|
|
resolve(fileParse);
|
|
})
|
|
.catch(() => {
|
|
reject({
|
|
causingError: {
|
|
cause: "파일 업로드 업데이트 실패",
|
|
},
|
|
} as DetailedError);
|
|
});
|
|
} else reject({} as DetailedError);
|
|
},
|
|
});
|
|
|
|
options?.handleUploadStart?.(uploader);
|
|
uploader.start();
|
|
} catch (e) {
|
|
console.log(e);
|
|
reject(e as DetailedError);
|
|
|
|
toast.error(String(e));
|
|
}
|
|
// setUpload(uploader);
|
|
});
|
|
//
|
|
//
|