import _ from 'lodash'; import * as qiniuJS from 'qiniu-js'; import type { Config as QiniuConfig } from 'qiniu-js/esm/upload/base'; import type { CompressOptions } from 'qiniu-js/esm/utils/compress'; import { v4 as uuidv4 } from 'uuid'; // import type { CreateFileForm } from '@/api/v0/files'; // import type { QiniuTokenResponse } from '@/api/v0/qiniu'; // import { getQiniuToken } from '@/services/api'; // import { createFile, getFileUploadKey } from '@/services/files'; // import storage from '@/utils/storage'; import { computeChecksumMd5 } from './utils'; export const lastToken = async (onRefreshQiniuToken?: (token) => void) => { console.info(onRefreshQiniuToken); // const token = storage.getQiniuToken(); // if (token.expire_at === undefined || token.expire_at.getTime() - new Date().getTime() <= 10 * 60 * 1000) { // return await getQiniuToken().then((data) => { // token.value = data.value; // token.expire_at = data.expire_at; // storage.setQiniuToken(token); // if (onRefreshQiniuToken) { // onRefreshQiniuToken(token); // } // return data.value; // }); // } return new Promise((resolve) => { resolve(''); }); }; const qiniuConfig: QiniuConfig = { useCdnDomain: true, region: qiniuJS.region.z0, chunkSize: 4, concurrentRequestLimit: 5, checkByMD5: true, debugLogLevel: 'OFF', }; export const defaultOnComplete = (file, uname, res) => { console.info(file, uname, res); // const qUrl = storage.getQiniuUrl(); // if (file && uname && res) { // const fileType = file.type.split('/')[0]; // const fileSubType = file.type.split('/')[1]; // const fileForm: CreateFileForm = { // title: file.name, // type: fileType, // sub_type: fileSubType, // size: file.size, // source_url: qiniuUrl(res.key, qUrl), // initialState?.settings.qiniu.url // hash: res.hash, // key: res.key, // unnotify: true, // space_id: '', // permission_user_ids: [], // }; // if (fileType === 'image') { // request // .get(qiniuImageInfo(res.key, qUrl)) // .then((response) => { // fileForm.file_info = response; // if (_.has(fileForm.file_info, 'size')) { // fileForm.size = _.get(fileForm.file_info, 'size'); // } // saveFiles(uname, fileForm); // }) // .catch(console.error); // } else if (fileType === 'audio' || fileType === 'video') { // request // .get(qiniuAVInfo(res.key, qUrl)) // .then((response) => { // fileForm.file_info = response; // saveFiles(uname, fileForm); // }) // .catch(console.error); // } else { // saveFiles(uname, fileForm); // } // } }; export const qiniuUpload = async ( file: File, filename: string, username?: string | undefined, tags?: string[], onComplete?: (res: any) => void, onError?: (err?: Error) => void, onNext?: (res: any) => void, onRefreshQiniuToken?: (token) => void ) => { const token = await lastToken(onRefreshQiniuToken); if (token) { const observable = qiniuJS.upload( file, filename, token, { fname: filename, }, qiniuConfig ); observable.subscribe({ next: onNext, error: (e: any) => { if (e && e.data && e.data.error === 'expired token') { lastToken(onRefreshQiniuToken); } if (onError) onError(e); }, complete(res) { // if (process.env.NODE_ENV === 'development') { // deleteQiniuFileAfterDays(res.key, 7).catch((e) => { // message.error(e.message); // }); // 设置文件7天过期 // } if (onComplete) { onComplete(res); } else if (username) { defaultOnComplete(file, username, res); } }, }); } else if (onError) { onError(Error('Token Undefined!')); } }; export const qiniuUploadMD5 = async ( file: File, username?: string | undefined, tags?: string[], onComplete?: (res: Qiniu.UploadRes) => void, onError?: (err?: Error) => void, onNext?: (res: any) => void ) => { computeChecksumMd5(file) .then((md5: any) => qiniuUpload(file, _.replace(md5, /\//g, '='), username, tags, onComplete, onError, onNext)) .catch((reason) => { if (onError) onError(reason); }); }; export const qiniuUploadCompressImage = async ( file: File, compressOption: CompressOptions, filename?: string | undefined, username?: string | undefined, tags?: string[], onComplete?: (res: Qiniu.UploadRes) => void, onError?: (err?: Error) => void, onNext?: (res: any) => void, onRefreshQiniuToken?: (token) => void ) => { qiniuJS .compressImage(file, compressOption) .then((data) => { const newFileName = `${uuidv4()}-${file.name.replace(/[^a-zA-Z0-9-_.]/g, '')}`; const fileToUpdate = new File([data.dist], newFileName, { type: data.dist.type, }); computeChecksumMd5(fileToUpdate).then((md5: any) => { qiniuUpload( fileToUpdate, filename ? `${filename}_compressed` : _.replace(md5, /\//g, '='), username, tags, onComplete, onError, onNext, onRefreshQiniuToken ); }); }) .catch(onError); };