更新 @kevisual/api 版本至 0.0.34,增强文件上传功能,支持 Blob 类型,新增创建文件夹和重命名功能
This commit is contained in:
@@ -1,14 +1,40 @@
|
||||
import MD5 from 'crypto-js/md5';
|
||||
|
||||
export const hashContent = (str: string | Buffer): string => {
|
||||
export const hashContent = (str: string | Blob | Buffer): Promise<string> | string => {
|
||||
if (typeof str === 'string') {
|
||||
return MD5(str).toString();
|
||||
} else if (str instanceof Blob) {
|
||||
return hashBlob(str);
|
||||
} else if (Buffer.isBuffer(str)) {
|
||||
return MD5(str.toString()).toString();
|
||||
}
|
||||
console.error('hashContent error: input must be a string or Buffer');
|
||||
console.error('hashContent error: input must be a string, Blob, or Buffer');
|
||||
return '';
|
||||
};
|
||||
|
||||
export const hashBlob = (blob: Blob): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = async () => {
|
||||
try {
|
||||
const content = reader.result;
|
||||
if (typeof content === 'string') {
|
||||
resolve(MD5(content).toString());
|
||||
} else if (content) {
|
||||
const contentString = new TextDecoder().decode(content);
|
||||
resolve(MD5(contentString).toString());
|
||||
} else {
|
||||
reject(new Error('Empty content'));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('hashBlob error', error);
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsArrayBuffer(blob);
|
||||
});
|
||||
};
|
||||
export const hashFile = (file: File): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
|
||||
Reference in New Issue
Block a user