doomistorage 1.0.3 → 1.0.5
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/dist/cosfile.d.ts +4 -1
- package/dist/cosfile.js +21 -19
- package/dist/file.d.ts +4 -1
- package/dist/filehelper.d.ts +3 -1
- package/dist/filehelper.js +19 -1
- package/dist/localfile.d.ts +4 -1
- package/dist/localfile.js +18 -13
- package/package.json +1 -1
- package/src/cosfile.ts +17 -15
- package/src/file.ts +22 -22
- package/src/filehelper.ts +20 -4
- package/src/localfile.ts +13 -9
package/dist/cosfile.d.ts
CHANGED
package/dist/cosfile.js
CHANGED
|
@@ -103,26 +103,28 @@ class CosFile extends file_1.FileBase {
|
|
|
103
103
|
* @param filepath
|
|
104
104
|
*/
|
|
105
105
|
deleteFile(filepath) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
return new Promise(reslove => {
|
|
108
|
+
///如果是数组,则一次性删除多个文件
|
|
109
|
+
if (Array.isArray(filepath)) {
|
|
110
|
+
const params = {
|
|
111
|
+
Bucket: this.bucket,
|
|
112
|
+
Region: this.region,
|
|
113
|
+
Objects: filepath.map(item => { return { Key: item }; }),
|
|
114
|
+
};
|
|
115
|
+
return this.cos.deleteMultipleObject(params, (err) => {
|
|
116
|
+
return reslove({ successed: err == null, error: err });
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const params = {
|
|
120
|
+
Bucket: this.bucket,
|
|
121
|
+
Region: this.region,
|
|
122
|
+
Key: filepath,
|
|
123
|
+
};
|
|
124
|
+
this.cos.deleteObject(params, (err) => {
|
|
125
|
+
return reslove({ successed: err == null, error: err });
|
|
126
|
+
});
|
|
116
127
|
});
|
|
117
|
-
}
|
|
118
|
-
const params = {
|
|
119
|
-
Bucket: this.bucket,
|
|
120
|
-
Region: this.region,
|
|
121
|
-
Key: filepath,
|
|
122
|
-
};
|
|
123
|
-
this.cos.deleteObject(params, (err) => {
|
|
124
|
-
if (err)
|
|
125
|
-
console.log('delete file error', err);
|
|
126
128
|
});
|
|
127
129
|
}
|
|
128
130
|
}
|
package/dist/file.d.ts
CHANGED
|
@@ -41,7 +41,10 @@ export declare abstract class FileBase {
|
|
|
41
41
|
* 删除指定位置的文件
|
|
42
42
|
* @param filepath
|
|
43
43
|
*/
|
|
44
|
-
abstract deleteFile(filepath: string | string[]):
|
|
44
|
+
abstract deleteFile(filepath: string | string[]): Promise<{
|
|
45
|
+
successed: boolean;
|
|
46
|
+
error?: any;
|
|
47
|
+
}>;
|
|
45
48
|
/**
|
|
46
49
|
* 创建多级目录
|
|
47
50
|
* @param dirpath
|
package/dist/filehelper.d.ts
CHANGED
|
@@ -43,7 +43,9 @@ declare class FileUtility {
|
|
|
43
43
|
* 删除指定位置的文件
|
|
44
44
|
* @param filePath
|
|
45
45
|
*/
|
|
46
|
-
DeleteFile(filePath: string | string[]):
|
|
46
|
+
DeleteFile(filePath: string | string[]): Promise<{
|
|
47
|
+
successed: boolean;
|
|
48
|
+
}>;
|
|
47
49
|
/**
|
|
48
50
|
* 批量下载指定的文件并上传到存储中
|
|
49
51
|
* 替代原来file中的对应方法save2localForRemoteImage
|
package/dist/filehelper.js
CHANGED
|
@@ -17,6 +17,19 @@ const cosfile_1 = require("./cosfile");
|
|
|
17
17
|
const localfile_1 = require("./localfile");
|
|
18
18
|
const axios_1 = __importDefault(require("axios"));
|
|
19
19
|
const path_1 = __importDefault(require("path"));
|
|
20
|
+
const fs_1 = __importDefault(require("fs"));
|
|
21
|
+
/**
|
|
22
|
+
* 读取系统配置文件的腾讯云设置
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
function getConfigurationSetting(settingName) {
|
|
26
|
+
let configfilename = process.env["CONFIGFILE"] || 'configuration.json';
|
|
27
|
+
let configfile = path_1.default.join(process.cwd(), configfilename);
|
|
28
|
+
if (!fs_1.default.existsSync(configfile))
|
|
29
|
+
return null;
|
|
30
|
+
const config = require(configfile)[settingName || 'tencentCOS'];
|
|
31
|
+
return config;
|
|
32
|
+
}
|
|
20
33
|
/**
|
|
21
34
|
* 文件处理器类型
|
|
22
35
|
*/
|
|
@@ -36,6 +49,9 @@ function FileHelper(provider, apiOption) {
|
|
|
36
49
|
filehandler = new localfile_1.LocalFile();
|
|
37
50
|
break;
|
|
38
51
|
case exports.FileProviderEnum.TENCENTCOS:
|
|
52
|
+
if (!apiOption || typeof (apiOption) == 'string') {
|
|
53
|
+
apiOption = getConfigurationSetting(apiOption);
|
|
54
|
+
}
|
|
39
55
|
filehandler = new cosfile_1.CosFile(apiOption);
|
|
40
56
|
break;
|
|
41
57
|
default: filehandler = new localfile_1.LocalFile();
|
|
@@ -82,7 +98,9 @@ class FileUtility {
|
|
|
82
98
|
* @param filePath
|
|
83
99
|
*/
|
|
84
100
|
DeleteFile(filePath) {
|
|
85
|
-
this
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
return yield this.fileHandler.deleteFile(filePath);
|
|
103
|
+
});
|
|
86
104
|
}
|
|
87
105
|
/**
|
|
88
106
|
* 批量下载指定的文件并上传到存储中
|
package/dist/localfile.d.ts
CHANGED
package/dist/localfile.js
CHANGED
|
@@ -68,20 +68,25 @@ class LocalFile extends file_1.FileBase {
|
|
|
68
68
|
* @param filepath
|
|
69
69
|
*/
|
|
70
70
|
deleteFile(filepath) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
return new Promise(reslove => {
|
|
73
|
+
if (Array.isArray(filepath)) {
|
|
74
|
+
for (const f of filepath) {
|
|
75
|
+
fs_1.default.unlink(f, (err) => {
|
|
76
|
+
if (err)
|
|
77
|
+
console.log('delete file error', err);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
fs_1.default.unlink(filepath, (err) => {
|
|
83
|
+
if (err)
|
|
84
|
+
console.log('delete file error', err);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return reslove({ successed: true });
|
|
83
88
|
});
|
|
84
|
-
}
|
|
89
|
+
});
|
|
85
90
|
}
|
|
86
91
|
}
|
|
87
92
|
exports.LocalFile = LocalFile;
|
package/package.json
CHANGED
package/src/cosfile.ts
CHANGED
|
@@ -90,26 +90,28 @@ export class CosFile extends FileBase {
|
|
|
90
90
|
* 删除指定位置的文件
|
|
91
91
|
* @param filepath
|
|
92
92
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
async deleteFile(filepath: string | string[]): Promise<{ successed: boolean,error?:any }> {
|
|
94
|
+
return new Promise(reslove => {
|
|
95
|
+
///如果是数组,则一次性删除多个文件
|
|
96
|
+
if (Array.isArray(filepath)) {
|
|
97
|
+
const params = {
|
|
98
|
+
Bucket: this.bucket,
|
|
99
|
+
Region: this.region,
|
|
100
|
+
Objects: filepath.map(item => { return { Key: item } }),
|
|
101
|
+
};
|
|
102
|
+
return this.cos.deleteMultipleObject(params, (err: any) => {
|
|
103
|
+
return reslove({ successed: err == null, error: err })
|
|
104
|
+
})
|
|
105
|
+
}
|
|
97
106
|
const params = {
|
|
98
107
|
Bucket: this.bucket,
|
|
99
108
|
Region: this.region,
|
|
100
|
-
|
|
109
|
+
Key: filepath,
|
|
101
110
|
};
|
|
102
|
-
|
|
103
|
-
|
|
111
|
+
this.cos.deleteObject(params, (err: any) => {
|
|
112
|
+
return reslove({ successed: err == null, error: err })
|
|
104
113
|
})
|
|
105
|
-
}
|
|
106
|
-
const params = {
|
|
107
|
-
Bucket: this.bucket,
|
|
108
|
-
Region: this.region,
|
|
109
|
-
Key: filepath,
|
|
110
|
-
};
|
|
111
|
-
this.cos.deleteObject(params, (err: any) => {
|
|
112
|
-
if (err) console.log('delete file error', err)
|
|
113
114
|
})
|
|
115
|
+
|
|
114
116
|
}
|
|
115
117
|
}
|
package/src/file.ts
CHANGED
|
@@ -10,8 +10,8 @@ export abstract class FileBase {
|
|
|
10
10
|
* @param {*} fileName
|
|
11
11
|
* @param {*} userInfo
|
|
12
12
|
*/
|
|
13
|
-
getSaveFileName(saveOption: FileConfig, fileName:string, userInfo:any = {}):string {
|
|
14
|
-
let saveFolder = this.getSaveFolder(saveOption,userInfo)
|
|
13
|
+
getSaveFileName(saveOption: FileConfig, fileName: string, userInfo: any = {}): string {
|
|
14
|
+
let saveFolder = this.getSaveFolder(saveOption, userInfo)
|
|
15
15
|
// console.log('getSaveFileName', fileName)
|
|
16
16
|
let _fileName = this.getSaveOnlyFileName(saveOption, fileName, userInfo);
|
|
17
17
|
return path.join(saveFolder, _fileName);
|
|
@@ -21,7 +21,7 @@ export abstract class FileBase {
|
|
|
21
21
|
* @param {*} saveOption
|
|
22
22
|
* @param {*} userInfo
|
|
23
23
|
*/
|
|
24
|
-
getSaveFolder(saveOption: FileConfig,userInfo:any ={}):string {
|
|
24
|
+
getSaveFolder(saveOption: FileConfig, userInfo: any = {}): string {
|
|
25
25
|
let subFolder = '';
|
|
26
26
|
///分目录存储
|
|
27
27
|
switch ((saveOption.subFolder || '').toLowerCase()) {
|
|
@@ -30,21 +30,21 @@ export abstract class FileBase {
|
|
|
30
30
|
///按日建造多级目录
|
|
31
31
|
case 'mutilbydate': subFolder = Moment().year() + path.sep + Moment().month() + path.sep + Moment().day(); break;
|
|
32
32
|
///用自己的id(当前登录的id)建造目录
|
|
33
|
-
case 'identity': subFolder =
|
|
33
|
+
case 'identity': subFolder = userInfo.id ?? UUIDV4(); break;
|
|
34
34
|
}
|
|
35
|
-
let saveFolder = path.join(saveOption.saveDir, subFolder+'');
|
|
35
|
+
let saveFolder = path.join(saveOption.saveDir, subFolder + '');
|
|
36
36
|
if (userInfo && userInfo.subfolder) saveFolder = path.join(saveFolder, userInfo.subfolder);
|
|
37
37
|
///如果包含当前调用用户的信息,则替换整个路径中的@@关键字
|
|
38
|
-
if (userInfo){
|
|
39
|
-
const matched=saveFolder.match(/@.*?@/g);
|
|
40
|
-
if (matched && matched.length>0)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
38
|
+
if (userInfo) {
|
|
39
|
+
const matched = saveFolder.match(/@.*?@/g);
|
|
40
|
+
if (matched && matched.length > 0)
|
|
41
|
+
matched.forEach(ele => {
|
|
42
|
+
const matchValue = ele.substring(1, ele.length - 1);
|
|
43
|
+
if (matchValue.indexOf(' ') >= 0 || matchValue.indexOf(':') >= 0 || matchValue.indexOf('=') >= 0) return;
|
|
44
|
+
let keyName = ele.substring(1, ele.length - 1);
|
|
45
|
+
const keyValue = userInfo[keyName] || ''
|
|
46
|
+
saveFolder = saveFolder.replace(ele, keyValue);
|
|
47
|
+
});
|
|
48
48
|
}
|
|
49
49
|
return saveFolder;
|
|
50
50
|
}
|
|
@@ -55,7 +55,7 @@ export abstract class FileBase {
|
|
|
55
55
|
* @param {*} userInfo
|
|
56
56
|
*/
|
|
57
57
|
getSaveOnlyFileName(saveOption: FileConfig, fileName: string, userInfo: any = {}): string {
|
|
58
|
-
let _fileName='';
|
|
58
|
+
let _fileName = '';
|
|
59
59
|
switch ((saveOption.fileName || 'keep').toLowerCase()) {
|
|
60
60
|
///保持和原有文件一致的文件名
|
|
61
61
|
case "keep": _fileName = fileName; break;
|
|
@@ -87,20 +87,20 @@ export abstract class FileBase {
|
|
|
87
87
|
* 删除指定位置的文件
|
|
88
88
|
* @param filepath
|
|
89
89
|
*/
|
|
90
|
-
abstract deleteFile(filepath: string | string[]):
|
|
91
|
-
|
|
90
|
+
abstract deleteFile(filepath: string | string[]): Promise<{ successed: boolean, error?: any }>;
|
|
91
|
+
|
|
92
92
|
/**
|
|
93
93
|
* 创建多级目录
|
|
94
94
|
* @param dirpath
|
|
95
95
|
* @param mode
|
|
96
96
|
* @returns
|
|
97
97
|
*/
|
|
98
|
-
mkdirsSync(dirpath:string, mode:any=null):boolean {
|
|
98
|
+
mkdirsSync(dirpath: string, mode: any = null): boolean {
|
|
99
99
|
if (!fs.existsSync(dirpath)) {
|
|
100
100
|
let pathtmp = '', splitPath = dirpath.split(path.sep);
|
|
101
|
-
for (const dirname of splitPath){
|
|
102
|
-
if (dirname.length==0) {
|
|
103
|
-
pathtmp=path.sep;
|
|
101
|
+
for (const dirname of splitPath) {
|
|
102
|
+
if (dirname.length == 0) {
|
|
103
|
+
pathtmp = path.sep;
|
|
104
104
|
continue;
|
|
105
105
|
}
|
|
106
106
|
if (pathtmp)
|
package/src/filehelper.ts
CHANGED
|
@@ -4,7 +4,18 @@ import { FileBase } from "./file";
|
|
|
4
4
|
import { LocalFile } from "./localfile";
|
|
5
5
|
import axios from 'axios';
|
|
6
6
|
import path from 'path';
|
|
7
|
-
|
|
7
|
+
import fs from 'fs';
|
|
8
|
+
/**
|
|
9
|
+
* 读取系统配置文件的腾讯云设置
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
function getConfigurationSetting(settingName?:string):any{
|
|
13
|
+
let configfilename = process.env["CONFIGFILE"] || 'configuration.json'
|
|
14
|
+
let configfile = path.join(process.cwd(), configfilename);
|
|
15
|
+
if (!fs.existsSync(configfile)) return null;
|
|
16
|
+
const config = require(configfile)[settingName || 'tencentCOS'];
|
|
17
|
+
return config;
|
|
18
|
+
}
|
|
8
19
|
/**
|
|
9
20
|
* 文件处理器类型
|
|
10
21
|
*/
|
|
@@ -22,7 +33,12 @@ export function FileHelper(provider: FileProviderEnum, apiOption?: any): FileUti
|
|
|
22
33
|
let filehandler: FileBase
|
|
23
34
|
switch (provider) {
|
|
24
35
|
case FileProviderEnum.LOCAL: filehandler = new LocalFile(); break;
|
|
25
|
-
case FileProviderEnum.TENCENTCOS:
|
|
36
|
+
case FileProviderEnum.TENCENTCOS:
|
|
37
|
+
if (!apiOption || typeof (apiOption)=='string'){
|
|
38
|
+
apiOption = getConfigurationSetting(apiOption)
|
|
39
|
+
}
|
|
40
|
+
filehandler = new CosFile(apiOption);
|
|
41
|
+
break;
|
|
26
42
|
default: filehandler = new LocalFile();
|
|
27
43
|
}
|
|
28
44
|
return new FileUtility(filehandler)
|
|
@@ -73,8 +89,8 @@ class FileUtility {
|
|
|
73
89
|
* 删除指定位置的文件
|
|
74
90
|
* @param filePath
|
|
75
91
|
*/
|
|
76
|
-
DeleteFile(filePath: string | string[]):
|
|
77
|
-
|
|
92
|
+
async DeleteFile(filePath: string | string[]): Promise<{ successed: boolean}> {
|
|
93
|
+
return await this.fileHandler.deleteFile(filePath);
|
|
78
94
|
}
|
|
79
95
|
/**
|
|
80
96
|
* 批量下载指定的文件并上传到存储中
|
package/src/localfile.ts
CHANGED
|
@@ -46,18 +46,22 @@ export class LocalFile extends FileBase {
|
|
|
46
46
|
* 删除指定文件
|
|
47
47
|
* @param filepath
|
|
48
48
|
*/
|
|
49
|
-
deleteFile(filepath: string | string[]) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
async deleteFile(filepath: string | string[]): Promise<{ successed: boolean, error?: any }> {
|
|
50
|
+
return new Promise(reslove=>{
|
|
51
|
+
if (Array.isArray(filepath)) {
|
|
52
|
+
for (const f of filepath) {
|
|
53
|
+
fs.unlink(f, (err: any) => {
|
|
54
|
+
if (err) console.log('delete file error', err)
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
fs.unlink(filepath, (err: any) => {
|
|
53
59
|
if (err) console.log('delete file error', err)
|
|
54
60
|
})
|
|
55
61
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
})
|
|
60
|
-
}
|
|
62
|
+
return reslove({ successed: true })
|
|
63
|
+
})
|
|
64
|
+
|
|
61
65
|
|
|
62
66
|
}
|
|
63
67
|
}
|