@kevisual/api 0.0.28 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/api",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "description": "",
5
5
  "main": "mod.ts",
6
6
  "scripts": {
@@ -27,7 +27,9 @@
27
27
  "@kevisual/types": "^0.0.12",
28
28
  "@kevisual/use-config": "^1.0.28",
29
29
  "@types/bun": "^1.3.6",
30
+ "@types/crypto-js": "^4.2.2",
30
31
  "@types/node": "^25.0.10",
32
+ "crypto-js": "^4.2.0",
31
33
  "dotenv": "^17.2.3",
32
34
  "fast-glob": "^3.3.3"
33
35
  },
@@ -37,7 +39,8 @@
37
39
  "es-toolkit": "^1.44.0",
38
40
  "eventemitter3": "^5.0.4",
39
41
  "fuse.js": "^7.1.0",
40
- "nanoid": "^5.1.6"
42
+ "nanoid": "^5.1.6",
43
+ "path-browserify-esm": "^1.0.6"
41
44
  },
42
45
  "exports": {
43
46
  ".": "./mod.ts",
@@ -46,6 +49,7 @@
46
49
  "./config": "./query/query-config/query-config.ts",
47
50
  "./proxy": "./query/query-proxy/index.ts",
48
51
  "./secret": "./query/query-secret/index.ts",
52
+ "./resources": "./query/query-resources/index.ts",
49
53
  "./query/*": "./query/*"
50
54
  }
51
55
  }
@@ -1,4 +1,6 @@
1
1
  import { adapter, DataOpts, Result } from '@kevisual/query';
2
+ import path from 'path-browserify-esm';
3
+ import { hashContent } from './utils';
2
4
 
3
5
  type QueryResourcesOptions = {
4
6
  prefix?: string;
@@ -20,6 +22,9 @@ export class QueryResources {
20
22
  setUsername(username: string) {
21
23
  this.prefix = `/${username}/resources/`;
22
24
  }
25
+ setPrefix(prefix: string) {
26
+ this.prefix = prefix;
27
+ }
23
28
  header(headers?: Record<string, string>, json = true): Record<string, string> {
24
29
  const token = this.storage.getItem('token');
25
30
  const _headers: Record<string, string> = {
@@ -54,18 +59,93 @@ export class QueryResources {
54
59
  });
55
60
  }
56
61
  async fetchFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
57
- return fetch(`${this.prefix}${filepath}`, {
58
- method: 'GET',
59
- headers: this.header(opts?.headers, false),
60
- }).then(async (res) => {
61
- if (!res.ok) {
62
- return {
63
- code: 500,
64
- success: false,
65
- message: `Failed to fetch file: ${res.status} ${res.statusText}`,
66
- } as Result<any>;
67
- }
68
- return { code: 200, data: await res.text(), success: true } as Result<any>;
62
+ const url = `${this.prefix}${filepath}`;
63
+ return this.get({}, { url, method: 'GET', headers: this.header(opts?.headers, false), isText: true });
64
+ }
65
+ async uploadFile(filepath: string, content: string, opts?: DataOpts): Promise<Result<any>> {
66
+ const pathname = `${this.prefix}${filepath}`;
67
+ const filename = path.basename(pathname);
68
+ const type = getContentType(filename);
69
+ const url = new URL(pathname, window.location.origin);
70
+ const hash = hashContent(content);
71
+ url.searchParams.set('hash', hash);
72
+ const formData = new FormData();
73
+ formData.append('file', new Blob([content], { type }));
74
+ return adapter({
75
+ url: url.toString(),
76
+ headers: { ...this.header(opts?.headers, false) },
77
+ isPostFile: true,
78
+ method: 'POST',
79
+ body: formData,
69
80
  });
70
81
  }
71
82
  }
83
+
84
+ export const getContentType = (filename: string): string => {
85
+ const ext = path.extname(filename);
86
+ let type = 'text/plain';
87
+
88
+ switch (ext) {
89
+ case '':
90
+ type = 'application/octet-stream';
91
+ break;
92
+ case '.json':
93
+ type = 'application/json';
94
+ break;
95
+ case '.txt':
96
+ type = 'text/plain';
97
+ break;
98
+ case '.csv':
99
+ type = 'text/csv';
100
+ break;
101
+ case '.md':
102
+ type = 'text/markdown';
103
+ break;
104
+ case '.html':
105
+ case '.htm':
106
+ type = 'text/html';
107
+ break;
108
+ case '.xml':
109
+ type = 'application/xml';
110
+ break;
111
+ case '.js':
112
+ type = 'application/javascript';
113
+ break;
114
+ case '.css':
115
+ type = 'text/css';
116
+ break;
117
+ case '.ts':
118
+ type = 'application/typescript';
119
+ break;
120
+ case '.pdf':
121
+ type = 'application/pdf';
122
+ break;
123
+ case '.zip':
124
+ type = 'application/zip';
125
+ break;
126
+ case '.docx':
127
+ type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
128
+ break;
129
+ case '.xlsx':
130
+ type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
131
+ break;
132
+ case '.mp3':
133
+ type = 'audio/mpeg';
134
+ break;
135
+ case '.mp4':
136
+ type = 'video/mp4';
137
+ break;
138
+ case '.png':
139
+ case '.jpg':
140
+ case '.jpeg':
141
+ case '.gif':
142
+ case '.webp':
143
+ type = `image/${ext.slice(1)}`;
144
+ break;
145
+ case '.svg':
146
+ type = 'image/svg+xml';
147
+ break;
148
+ }
149
+
150
+ return type;
151
+ };
@@ -0,0 +1,42 @@
1
+ import MD5 from 'crypto-js/md5';
2
+
3
+ export const hashContent = (str: string | Buffer): string => {
4
+ if (typeof str === 'string') {
5
+ return MD5(str).toString();
6
+ } else if (Buffer.isBuffer(str)) {
7
+ return MD5(str.toString()).toString();
8
+ }
9
+ console.error('hashContent error: input must be a string or Buffer');
10
+ return '';
11
+ };
12
+ export const hashFile = (file: File): Promise<string> => {
13
+ return new Promise((resolve, reject) => {
14
+ const reader = new FileReader();
15
+
16
+ reader.onload = async (event) => {
17
+ try {
18
+ const content = event.target?.result;
19
+ if (content instanceof ArrayBuffer) {
20
+ const contentString = new TextDecoder().decode(content);
21
+ const hashHex = MD5(contentString).toString();
22
+ resolve(hashHex);
23
+ } else if (typeof content === 'string') {
24
+ const hashHex = MD5(content).toString();
25
+ resolve(hashHex);
26
+ } else {
27
+ throw new Error('Invalid content type');
28
+ }
29
+ } catch (error) {
30
+ console.error('hashFile error', error);
31
+ reject(error);
32
+ }
33
+ };
34
+
35
+ reader.onerror = (error) => {
36
+ reject(error);
37
+ };
38
+
39
+ // 读取文件为 ArrayBuffer
40
+ reader.readAsArrayBuffer(file);
41
+ });
42
+ };