@kevisual/api 0.0.31 → 0.0.32
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
|
@@ -62,15 +62,21 @@ export class QueryResources {
|
|
|
62
62
|
const url = `${this.prefix}${filepath}`;
|
|
63
63
|
return this.get({}, { url, method: 'GET', headers: this.header(opts?.headers, false), isText: true });
|
|
64
64
|
}
|
|
65
|
-
async uploadFile(filepath: string, content: string, opts?: DataOpts): Promise<Result<any>> {
|
|
65
|
+
async uploadFile(filepath: string, content: string | Blob, opts?: DataOpts): Promise<Result<any>> {
|
|
66
66
|
const pathname = `${this.prefix}${filepath}`;
|
|
67
67
|
const filename = path.basename(pathname);
|
|
68
68
|
const type = getContentType(filename);
|
|
69
69
|
const url = new URL(pathname, window.location.origin);
|
|
70
|
-
const
|
|
70
|
+
const hashResult = hashContent(content);
|
|
71
|
+
// Blob 类型时 hashContent 返回 Promise
|
|
72
|
+
const hash = hashResult instanceof Promise ? await hashResult : hashResult;
|
|
71
73
|
url.searchParams.set('hash', hash);
|
|
72
74
|
const formData = new FormData();
|
|
73
|
-
|
|
75
|
+
if (content instanceof Blob) {
|
|
76
|
+
formData.append('file', content);
|
|
77
|
+
} else {
|
|
78
|
+
formData.append('file', new Blob([content], { type }));
|
|
79
|
+
}
|
|
74
80
|
return adapter({
|
|
75
81
|
url: url.toString(),
|
|
76
82
|
headers: { ...this.header(opts?.headers, false) },
|
|
@@ -1,14 +1,40 @@
|
|
|
1
1
|
import MD5 from 'crypto-js/md5';
|
|
2
2
|
|
|
3
|
-
export const hashContent = (str: string | Buffer): string => {
|
|
3
|
+
export const hashContent = (str: string | Blob | Buffer): Promise<string> | string => {
|
|
4
4
|
if (typeof str === 'string') {
|
|
5
5
|
return MD5(str).toString();
|
|
6
|
+
} else if (str instanceof Blob) {
|
|
7
|
+
return hashBlob(str);
|
|
6
8
|
} else if (Buffer.isBuffer(str)) {
|
|
7
9
|
return MD5(str.toString()).toString();
|
|
8
10
|
}
|
|
9
|
-
console.error('hashContent error: input must be a string or Buffer');
|
|
11
|
+
console.error('hashContent error: input must be a string, Blob, or Buffer');
|
|
10
12
|
return '';
|
|
11
13
|
};
|
|
14
|
+
|
|
15
|
+
export const hashBlob = (blob: Blob): Promise<string> => {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const reader = new FileReader();
|
|
18
|
+
reader.onload = async () => {
|
|
19
|
+
try {
|
|
20
|
+
const content = reader.result;
|
|
21
|
+
if (typeof content === 'string') {
|
|
22
|
+
resolve(MD5(content).toString());
|
|
23
|
+
} else if (content) {
|
|
24
|
+
const contentString = new TextDecoder().decode(content);
|
|
25
|
+
resolve(MD5(contentString).toString());
|
|
26
|
+
} else {
|
|
27
|
+
reject(new Error('Empty content'));
|
|
28
|
+
}
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error('hashBlob error', error);
|
|
31
|
+
reject(error);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
reader.onerror = (error) => reject(error);
|
|
35
|
+
reader.readAsArrayBuffer(blob);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
12
38
|
export const hashFile = (file: File): Promise<string> => {
|
|
13
39
|
return new Promise((resolve, reject) => {
|
|
14
40
|
const reader = new FileReader();
|