160 lines
4.1 KiB
TypeScript
160 lines
4.1 KiB
TypeScript
import { adapter, DataOpts, Result } from '@kevisual/query';
|
|
import path from 'path-browserify-esm';
|
|
import { hashContent } from './utils';
|
|
|
|
type QueryResourcesOptions = {
|
|
prefix?: string;
|
|
storage?: Storage;
|
|
username?: string;
|
|
[key: string]: any;
|
|
};
|
|
export class QueryResources {
|
|
prefix: string; // root/resources
|
|
storage: Storage;
|
|
constructor(opts: QueryResourcesOptions) {
|
|
if (opts.username) {
|
|
this.prefix = `/${opts.username}/resources/`;
|
|
} else {
|
|
this.prefix = opts.prefix || '';
|
|
}
|
|
this.storage = opts.storage || localStorage;
|
|
}
|
|
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> = {
|
|
'Content-Type': 'application/json',
|
|
...headers,
|
|
};
|
|
if (!json) {
|
|
delete _headers['Content-Type'];
|
|
}
|
|
if (!token) {
|
|
return _headers;
|
|
}
|
|
return {
|
|
..._headers,
|
|
Authorization: `Bearer ${token}`,
|
|
};
|
|
}
|
|
async get(data: any, opts: DataOpts): Promise<any> {
|
|
return adapter({
|
|
url: opts.url!,
|
|
method: 'GET',
|
|
body: data,
|
|
...opts,
|
|
headers: this.header(opts?.headers),
|
|
});
|
|
}
|
|
async getList(prefix: string, data?: { recursive?: boolean }, opts?: DataOpts): Promise<Result<any[]>> {
|
|
return this.get(data, {
|
|
url: `${this.prefix}${prefix}`,
|
|
body: data,
|
|
...opts,
|
|
});
|
|
}
|
|
async fetchFile(filepath: string, opts?: DataOpts): Promise<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,
|
|
});
|
|
}
|
|
async deleteFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
|
|
const url = `${this.prefix}${filepath}`;
|
|
return adapter({
|
|
url,
|
|
method: 'DELETE' as any,
|
|
headers: this.header(opts?.headers),
|
|
});
|
|
}
|
|
}
|
|
|
|
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;
|
|
};
|