UNPKG

@cmstops/pro-compo

Version:

[物料平台文档中心](https://arco.design/docs/material/guide)

85 lines (84 loc) 2.47 kB
import * as TUS from "tus-js-client"; import { DEFAULT_UPLOAD_URL, DEFAULT_UPLOAD_CHUNK_SIZE } from "../config.js"; function uploadByTUS(file, progressCallback) { return new Promise((resolve, reject) => { const upload = new TUS.Upload(file, { ...uploadConfig(file), onBeforeRequest: (req) => { const xhr = req.getUnderlyingObject(); xhr.withCredentials = true; }, onError: (error) => { reject(error); }, onProgress: (bytesSent, bytesTotal) => { progressCallback && progressCallback(bytesSent, bytesTotal); }, onSuccess: () => { resolve(upload); } }); upload.start(); }); } function uploadConfig(file) { const fType = getFileType(file); return { endpoint: `${DEFAULT_UPLOAD_URL}/upload/`, chunkSize: DEFAULT_UPLOAD_CHUNK_SIZE, retryDelays: [0, 1e3, 3e3, 5e3], overridePatchMethod: true, removeFingerprintOnSuccess: true, metadata: { filename: file.name, filetype: fType, platform: "holly" } }; } function getFileType(file) { let fType = file.type; const ext = file.name.length > 4 ? file.name.substring(file.name.length - 4) : ".null"; if (ext.toLocaleLowerCase() === ".mkv") fType = "video/x-matroska"; if (ext.toLocaleLowerCase() === ".zip") fType = "application/zip"; if (ext.toLocaleLowerCase() === ".rar") fType = "application/x-rar"; return fType; } class TusUploadTask { constructor(file, platformKey) { this.file = file; this.platformKey = platformKey || "holly"; } start(callback) { return new Promise((resolve, reject) => { const upload = new TUS.Upload(this.file, { endpoint: `${DEFAULT_UPLOAD_URL}/upload/`, chunkSize: DEFAULT_UPLOAD_CHUNK_SIZE || 5242880, retryDelays: [0, 1e3, 3e3, 5e3], overridePatchMethod: true, removeFingerprintOnSuccess: true, metadata: { filename: this.file.name, filetype: getFileType(this.file), platform: this.platformKey }, onBeforeRequest: (req) => { const xhr = req.getUnderlyingObject(); xhr.withCredentials = true; }, onProgress: callback, onSuccess: () => resolve(upload.url), onError: (error) => reject(error) }); this.upload = upload; upload.start(); }); } abort() { return this.upload.abort(); } } export { TusUploadTask, uploadByTUS };