更新 @kevisual/api 版本至 0.0.34,增强文件上传功能,支持 Blob 类型,新增创建文件夹和重命名功能

This commit is contained in:
2026-01-30 15:48:25 +08:00
parent 97e7a53a2b
commit e2b7d62693
3 changed files with 59 additions and 6 deletions

View File

@@ -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<Result<any>> {
async uploadFile(filepath: string, content: string | Blob, opts?: DataOpts): Promise<Result<any>> {
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<Result<any>> {
const filepath = folderpath.endsWith('/') ? `${folderpath}keep.txt` : `${folderpath}/keep.txt`;
return this.uploadFile(filepath, '文件夹占位,其他文件不存在,文件夹不存在,如果有其他文件夹,删除当前文件夹占位文件即可', opts);
}
async rename(oldpath: string, newpath: string, opts?: DataOpts): Promise<Result<any>> {
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<Result<any>> {
const url = `${this.prefix}${filepath}`;
return adapter({