add sync download

This commit is contained in:
2025-05-12 04:30:17 +08:00
parent 4aec2bc231
commit eaccbf5ada
14 changed files with 219 additions and 32 deletions

View File

@@ -1,3 +1,4 @@
import { getBufferHash, getHash } from '@/uitls/hash.ts';
import FormData from 'form-data';
export const handleResponse = async (err: any, res: any) => {
return new Promise((resolve) => {
@@ -37,6 +38,7 @@ export const getFormParams = (opts: UploadOptions, headers: any): FormData.Submi
...headers,
},
};
console.log('getFormParams', value);
return value;
};
type UploadOptions = {
@@ -44,14 +46,25 @@ type UploadOptions = {
file?: string | Buffer | File;
token?: string;
form?: FormData;
needHash?: boolean;
};
export const upload = (opts: UploadOptions): Promise<{ code?: number; message?: string; [key: string]: any }> => {
const form = opts?.form || new FormData();
if (!opts.form) {
let hash = '';
let value: any;
let type = 'string';
if (typeof opts.file === 'string') {
form.append('file', Buffer.from(opts.file));
value = Buffer.from(opts.file);
} else {
form.append('file', opts.file);
type = 'buffer';
value = opts.file;
}
form.append('file', value);
if (opts.needHash) {
hash = getBufferHash(value);
opts.url = new URL(opts.url.toString());
opts.url.searchParams.append('hash', hash);
}
}
const headers = form.getHeaders();