@kevisual/api 0.0.31 → 0.0.33
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) },
|
|
@@ -86,6 +92,18 @@ export class QueryResources {
|
|
|
86
92
|
const filepath = folderpath.endsWith('/') ? `${folderpath}keep.txt` : `${folderpath}/keep.txt`;
|
|
87
93
|
return this.uploadFile(filepath, '文件夹占位,其他文件不存在,文件夹不存在,如果有其他文件夹,删除当前文件夹占位文件即可', opts);
|
|
88
94
|
}
|
|
95
|
+
async rename(oldpath: string, newpath: string, opts?: DataOpts): Promise<Result<any>> {
|
|
96
|
+
const params = {
|
|
97
|
+
newName: newpath,
|
|
98
|
+
};
|
|
99
|
+
const url = `${this.prefix}${oldpath}`;
|
|
100
|
+
return adapter({
|
|
101
|
+
url,
|
|
102
|
+
method: 'PUT' as any,
|
|
103
|
+
headers: this.header(opts?.headers),
|
|
104
|
+
params,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
89
107
|
async deleteFile(filepath: string, opts?: DataOpts): Promise<Result<any>> {
|
|
90
108
|
const url = `${this.prefix}${filepath}`;
|
|
91
109
|
return adapter({
|
|
@@ -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();
|