From e2b7d62693e8a85e9f2726b8a09ad79cee54f50b Mon Sep 17 00:00:00 2001 From: abearxiong Date: Fri, 30 Jan 2026 15:48:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20@kevisual/api=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=87=B3=200.0.34=EF=BC=8C=E5=A2=9E=E5=BC=BA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20Blob=20=E7=B1=BB=E5=9E=8B=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=88=9B=E5=BB=BA=E6=96=87=E4=BB=B6=E5=A4=B9=E5=92=8C?= =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- query/query-resources/index.ts | 33 ++++++++++++++++++++++++++++++--- query/query-resources/utils.ts | 30 ++++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index dbf6446..5e6fef5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/api", - "version": "0.0.30", + "version": "0.0.34", "description": "", "main": "mod.ts", "scripts": { diff --git a/query/query-resources/index.ts b/query/query-resources/index.ts index da1ed0a..1ca49b5 100644 --- a/query/query-resources/index.ts +++ b/query/query-resources/index.ts @@ -62,23 +62,50 @@ export class QueryResources { const url = `${this.prefix}${filepath}`; return this.get({}, { url, method: 'GET', headers: this.header(opts?.headers, false), isText: true }); } - async uploadFile(filepath: string, content: string, opts?: DataOpts): Promise> { + async uploadFile(filepath: string, content: string | Blob, opts?: DataOpts): Promise> { const pathname = `${this.prefix}${filepath}`; const filename = path.basename(pathname); const type = getContentType(filename); const url = new URL(pathname, window.location.origin); - const hash = hashContent(content); + const hashResult = hashContent(content); + // Blob 类型时 hashContent 返回 Promise + const hash = hashResult instanceof Promise ? await hashResult : hashResult; url.searchParams.set('hash', hash); const formData = new FormData(); - formData.append('file', new Blob([content], { type })); + if (content instanceof Blob) { + formData.append('file', content); + } else { + formData.append('file', new Blob([content], { type })); + } return adapter({ url: url.toString(), headers: { ...this.header(opts?.headers, false) }, + params: { + hash: hash, + }, isPostFile: true, method: 'POST', body: formData, }); } + async createFolder(folderpath: string, opts?: DataOpts): Promise> { + const filepath = folderpath.endsWith('/') ? `${folderpath}keep.txt` : `${folderpath}/keep.txt`; + return this.uploadFile(filepath, '文件夹占位,其他文件不存在,文件夹不存在,如果有其他文件夹,删除当前文件夹占位文件即可', opts); + } + async rename(oldpath: string, newpath: string, opts?: DataOpts): Promise> { + const pathname = `${this.prefix}${oldpath}`; + const newName = `${this.prefix}${newpath}`; + const params = { + newName: newName, + }; + const url = pathname + return adapter({ + url, + method: 'PUT' as any, + headers: this.header(opts?.headers), + params, + }); + } async deleteFile(filepath: string, opts?: DataOpts): Promise> { const url = `${this.prefix}${filepath}`; return adapter({ diff --git a/query/query-resources/utils.ts b/query/query-resources/utils.ts index 895734f..7cd1ce3 100644 --- a/query/query-resources/utils.ts +++ b/query/query-resources/utils.ts @@ -1,14 +1,40 @@ import MD5 from 'crypto-js/md5'; -export const hashContent = (str: string | Buffer): string => { +export const hashContent = (str: string | Blob | Buffer): Promise | string => { if (typeof str === 'string') { return MD5(str).toString(); + } else if (str instanceof Blob) { + return hashBlob(str); } else if (Buffer.isBuffer(str)) { return MD5(str.toString()).toString(); } - console.error('hashContent error: input must be a string or Buffer'); + console.error('hashContent error: input must be a string, Blob, or Buffer'); return ''; }; + +export const hashBlob = (blob: Blob): Promise => { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = async () => { + try { + const content = reader.result; + if (typeof content === 'string') { + resolve(MD5(content).toString()); + } else if (content) { + const contentString = new TextDecoder().decode(content); + resolve(MD5(contentString).toString()); + } else { + reject(new Error('Empty content')); + } + } catch (error) { + console.error('hashBlob error', error); + reject(error); + } + }; + reader.onerror = (error) => reject(error); + reader.readAsArrayBuffer(blob); + }); +}; export const hashFile = (file: File): Promise => { return new Promise((resolve, reject) => { const reader = new FileReader();