import { password } from "~/store" import { Resp, TaskInfo } from "~/types" import { r } from "~/utils" import { SetUpload, Upload } from "./types" import { calculateHash } from "./util" type UploadResp = Resp<{ task?: TaskInfo }> export const FormUpload: Upload = async ( uploadPath: string, file: File, setUpload: SetUpload, asTask = false, overwrite = false, rapid = false, ) => { let oldTimestamp = new Date().valueOf() let oldLoaded = 0 const form = new FormData() form.append("file", file) let headers: { [k: string]: any } = { "File-Path": encodeURIComponent(uploadPath), "As-Task": asTask, "Content-Type": "multipart/form-data", "Last-Modified": file.lastModified, Password: password(), Overwrite: overwrite.toString(), } if (rapid) { const { md5, sha1, sha256 } = await calculateHash(file) headers["X-File-Md5"] = md5 headers["X-File-Sha1"] = sha1 headers["X-File-Sha256"] = sha256 } const resp: UploadResp = await r.put("/fs/form", form, { headers: headers, onUploadProgress: (progressEvent) => { if (progressEvent.total) { const complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0 setUpload("progress", complete) const timestamp = new Date().valueOf() const duration = (timestamp - oldTimestamp) / 1000 if (duration > 1) { const loaded = progressEvent.loaded - oldLoaded const speed = loaded / duration const remain = progressEvent.total - progressEvent.loaded const remainTime = remain / speed setUpload("speed", speed) console.log(remainTime) oldTimestamp = timestamp oldLoaded = progressEvent.loaded } if (complete === 100) { setUpload("status", "backending") } } }, }) if (resp.code === 200) { return { task: resp.data?.task, } } else { return { error: new Error(resp.message), } } }