更新 @kevisual/api 版本至 0.0.34,增强文件上传功能,支持 Blob 类型,新增创建文件夹和重命名功能
This commit is contained in:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user