更新 @kevisual/api 版本至 0.0.29,添加路径处理和哈希功能,新增随机 ID 生成器
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
42
query/query-resources/utils.ts
Normal file
42
query/query-resources/utils.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import MD5 from 'crypto-js/md5';
|
||||
|
||||
export const hashContent = (str: string | Buffer): string => {
|
||||
if (typeof str === 'string') {
|
||||
return MD5(str).toString();
|
||||
} else if (Buffer.isBuffer(str)) {
|
||||
return MD5(str.toString()).toString();
|
||||
}
|
||||
console.error('hashContent error: input must be a string or Buffer');
|
||||
return '';
|
||||
};
|
||||
export const hashFile = (file: File): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = async (event) => {
|
||||
try {
|
||||
const content = event.target?.result;
|
||||
if (content instanceof ArrayBuffer) {
|
||||
const contentString = new TextDecoder().decode(content);
|
||||
const hashHex = MD5(contentString).toString();
|
||||
resolve(hashHex);
|
||||
} else if (typeof content === 'string') {
|
||||
const hashHex = MD5(content).toString();
|
||||
resolve(hashHex);
|
||||
} else {
|
||||
throw new Error('Invalid content type');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('hashFile error', error);
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
|
||||
reader.onerror = (error) => {
|
||||
reject(error);
|
||||
};
|
||||
|
||||
// 读取文件为 ArrayBuffer
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user