更新 @kevisual/api 版本至 0.0.29,添加路径处理和哈希功能,新增随机 ID 生成器

This commit is contained in:
2026-01-30 14:23:51 +08:00
parent 0086aeced2
commit ef5d3c3805
5 changed files with 190 additions and 14 deletions

View File

@@ -1,4 +1,6 @@
import { adapter, DataOpts, Result } from '@kevisual/query';
import path from 'path-browserify-esm';
import { hashContent } from './utils';
type QueryResourcesOptions = {
prefix?: string;
@@ -20,6 +22,9 @@ export class QueryResources {
setUsername(username: string) {
this.prefix = `/${username}/resources/`;
}
setPrefix(prefix: string) {
this.prefix = prefix;
}
header(headers?: Record<string, string>, json = true): Record<string, string> {
const token = this.storage.getItem('token');
const _headers: Record<string, string> = {
@@ -54,18 +59,93 @@ export class QueryResources {
});
}
async fetchFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
return fetch(`${this.prefix}${filepath}`, {
method: 'GET',
headers: this.header(opts?.headers, false),
}).then(async (res) => {
if (!res.ok) {
return {
code: 500,
success: false,
message: `Failed to fetch file: ${res.status} ${res.statusText}`,
} as Result<any>;
}
return { code: 200, data: await res.text(), success: true } as Result<any>;
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>> {
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);
url.searchParams.set('hash', hash);
const formData = new FormData();
formData.append('file', new Blob([content], { type }));
return adapter({
url: url.toString(),
headers: { ...this.header(opts?.headers, false) },
isPostFile: true,
method: 'POST',
body: formData,
});
}
}
export const getContentType = (filename: string): string => {
const ext = path.extname(filename);
let type = 'text/plain';
switch (ext) {
case '':
type = 'application/octet-stream';
break;
case '.json':
type = 'application/json';
break;
case '.txt':
type = 'text/plain';
break;
case '.csv':
type = 'text/csv';
break;
case '.md':
type = 'text/markdown';
break;
case '.html':
case '.htm':
type = 'text/html';
break;
case '.xml':
type = 'application/xml';
break;
case '.js':
type = 'application/javascript';
break;
case '.css':
type = 'text/css';
break;
case '.ts':
type = 'application/typescript';
break;
case '.pdf':
type = 'application/pdf';
break;
case '.zip':
type = 'application/zip';
break;
case '.docx':
type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case '.xlsx':
type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case '.mp3':
type = 'audio/mpeg';
break;
case '.mp4':
type = 'video/mp4';
break;
case '.png':
case '.jpg':
case '.jpeg':
case '.gif':
case '.webp':
type = `image/${ext.slice(1)}`;
break;
case '.svg':
type = 'image/svg+xml';
break;
}
return type;
};