UNPKG

@ithinkdt/cloud

Version:

iThinkDT Cloud

81 lines (72 loc) 2.95 kB
import { $fetch } from '@ithinkdt/core' export function file(CTX, getToken) { return { fileUploader: async ( file, { access = 'INTERNAL', name, action: _action, field = 'file', data = {}, onUploadProgress }, ) => { let action = _action if (!_action) { // TODO 计算 MD5、分片 /* const md5 = await spark.md5(file) const { isSkip, id: fileId, fileName = file.name, fileSize = file.size, extendName: fileType = file.type } = await $fetch(`${ctx.FILE}cer/file/transfer/quickupload`, { identifier: md5 }) if (isSkip) return { fileId, fileName, fileSize, fileType } */ action = `${CTX.FILE}cer/file/transfer/upload` } const fd = new FormData() fd.append('filename', name || file.name) fd.append('accessRight', access) for (const k of Object.keys(data)) { if (k === 'filename') fd.set(k, data[k]) else fd.append(k, data[k]) } fd.append(field, file) return await $fetch .post(action, fd, { onUploadProgress, }) .then((data) => { return { ...data[0], fileId: data[0].id, } }) }, filePreview: (fileId, credentials = true) => `${CTX._BASE + CTX.FILE}${credentials ? 'cli/' : 'pub/'}file/transfer/preview?id=${fileId}` + (credentials ? `&access_token=${encodeURIComponent(`Bearer ${getToken()}`)}` : ''), fileDownload: (fileId, credentials = true) => `${CTX._BASE + CTX.FILE}${credentials ? 'cli/' : 'pub/'}file/transfer/download?id=${fileId}` + (credentials ? `&access_token=${encodeURIComponent(`Bearer ${getToken()}`)}` : ''), fileInfo: (fileIds) => $fetch .get(`${CTX.FILE}cli/file/transfer/fileinfo`, { ids: fileIds, }) .then((data) => fileIds.map((id) => { const it = data[id] || {} it.id = id it.fileId = id return it }), ), fileRemove: (fileIds) => { return Promise.resolve(fileIds) // return Promise.all( // fileIds.map((id) => // $fetch.post( // `${CTX.FILE}cli/file/transfer/delete`, // { // id, // }, // { reqType: 'urlencoded' }, // ), // ), // ) }, } }