doomistorage 1.0.4 → 1.0.6
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 +22 -19
- package/dist/file.d.ts +4 -1
- package/dist/filehelper.d.ts +4 -2
- package/dist/filehelper.js +18 -7
- package/dist/localfile.d.ts +4 -1
- package/dist/localfile.js +18 -13
- package/package.json +1 -1
- package/src/cosfile.ts +18 -15
- package/src/file.ts +22 -22
- package/src/filehelper.ts +15 -9
- package/src/localfile.ts +13 -9
package/dist/cosfile.d.ts
CHANGED
package/dist/cosfile.js
CHANGED
|
@@ -61,6 +61,7 @@ class CosFile extends file_1.FileBase {
|
|
|
61
61
|
saveFileStream(file, fileName, saveOption, userInfo) {
|
|
62
62
|
return __awaiter(this, void 0, void 0, function* () {
|
|
63
63
|
const destinationFileName = this.getSaveFileName(saveOption, fileName, userInfo);
|
|
64
|
+
console.log('InStorage saveFileStream', saveOption);
|
|
64
65
|
return new Promise((resolve) => {
|
|
65
66
|
if (saveOption.reRead) {
|
|
66
67
|
let dataArr = [], //存储读取的结果集合
|
|
@@ -103,26 +104,28 @@ class CosFile extends file_1.FileBase {
|
|
|
103
104
|
* @param filepath
|
|
104
105
|
*/
|
|
105
106
|
deleteFile(filepath) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
return new Promise(reslove => {
|
|
109
|
+
///如果是数组,则一次性删除多个文件
|
|
110
|
+
if (Array.isArray(filepath)) {
|
|
111
|
+
const params = {
|
|
112
|
+
Bucket: this.bucket,
|
|
113
|
+
Region: this.region,
|
|
114
|
+
Objects: filepath.map(item => { return { Key: item }; }),
|
|
115
|
+
};
|
|
116
|
+
return this.cos.deleteMultipleObject(params, (err) => {
|
|
117
|
+
return reslove({ successed: err == null, error: err });
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const params = {
|
|
121
|
+
Bucket: this.bucket,
|
|
122
|
+
Region: this.region,
|
|
123
|
+
Key: filepath,
|
|
124
|
+
};
|
|
125
|
+
this.cos.deleteObject(params, (err) => {
|
|
126
|
+
return reslove({ successed: err == null, error: err });
|
|
127
|
+
});
|
|
116
128
|
});
|
|
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
129
|
});
|
|
127
130
|
}
|
|
128
131
|
}
|
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
|
@@ -13,7 +13,7 @@ export type FileProviderEnum = typeof FileProviderEnum[keyof typeof FileProvider
|
|
|
13
13
|
* @param destination
|
|
14
14
|
* @returns
|
|
15
15
|
*/
|
|
16
|
-
export declare function FileHelper(provider
|
|
16
|
+
export declare function FileHelper(provider?: FileProviderEnum, apiOption?: any): FileUtility;
|
|
17
17
|
/**
|
|
18
18
|
* 文件帮助工具
|
|
19
19
|
*/
|
|
@@ -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
|
@@ -18,17 +18,24 @@ const localfile_1 = require("./localfile");
|
|
|
18
18
|
const axios_1 = __importDefault(require("axios"));
|
|
19
19
|
const path_1 = __importDefault(require("path"));
|
|
20
20
|
const fs_1 = __importDefault(require("fs"));
|
|
21
|
+
let config;
|
|
21
22
|
/**
|
|
22
23
|
* 读取系统配置文件的腾讯云设置
|
|
23
24
|
* @returns
|
|
24
25
|
*/
|
|
25
|
-
function getConfigurationSetting(settingName) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
function getConfigurationSetting(settingName, isSection = true) {
|
|
27
|
+
var _a;
|
|
28
|
+
if (!config) {
|
|
29
|
+
let configfilename = process.env["CONFIGFILE"] || 'configuration.json';
|
|
30
|
+
let configfile = path_1.default.join(process.cwd(), configfilename);
|
|
31
|
+
if (!fs_1.default.existsSync(configfile))
|
|
32
|
+
return null;
|
|
33
|
+
config = require(configfile);
|
|
34
|
+
}
|
|
35
|
+
if (!config)
|
|
29
36
|
return null;
|
|
30
|
-
const config = require(configfile)[settingName || 'tencentCOS'];
|
|
31
|
-
return config;
|
|
37
|
+
//const config = require(configfile)[settingName || 'tencentCOS'];
|
|
38
|
+
return isSection ? config[settingName || 'tencentCOS'] : ((_a = config.appsetting[settingName || 'destination']) !== null && _a !== void 0 ? _a : 'tencentcos');
|
|
32
39
|
}
|
|
33
40
|
/**
|
|
34
41
|
* 文件处理器类型
|
|
@@ -43,6 +50,8 @@ exports.FileProviderEnum = {
|
|
|
43
50
|
* @returns
|
|
44
51
|
*/
|
|
45
52
|
function FileHelper(provider, apiOption) {
|
|
53
|
+
if (!provider)
|
|
54
|
+
provider = getConfigurationSetting('destination', false);
|
|
46
55
|
let filehandler;
|
|
47
56
|
switch (provider) {
|
|
48
57
|
case exports.FileProviderEnum.LOCAL:
|
|
@@ -98,7 +107,9 @@ class FileUtility {
|
|
|
98
107
|
* @param filePath
|
|
99
108
|
*/
|
|
100
109
|
DeleteFile(filePath) {
|
|
101
|
-
this
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
return yield this.fileHandler.deleteFile(filePath);
|
|
112
|
+
});
|
|
102
113
|
}
|
|
103
114
|
/**
|
|
104
115
|
* 批量下载指定的文件并上传到存储中
|
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
|
@@ -49,6 +49,7 @@ export class CosFile extends FileBase {
|
|
|
49
49
|
*/
|
|
50
50
|
async saveFileStream(file: any, fileName: any, saveOption: FileConfig, userInfo: any): Promise<FileResult> {
|
|
51
51
|
const destinationFileName = this.getSaveFileName(saveOption, fileName, userInfo);
|
|
52
|
+
console.log('InStorage saveFileStream', saveOption)
|
|
52
53
|
return new Promise((resolve) => {
|
|
53
54
|
if (saveOption.reRead) {
|
|
54
55
|
let dataArr: any = [],//存储读取的结果集合
|
|
@@ -90,26 +91,28 @@ export class CosFile extends FileBase {
|
|
|
90
91
|
* 删除指定位置的文件
|
|
91
92
|
* @param filepath
|
|
92
93
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
async deleteFile(filepath: string | string[]): Promise<{ successed: boolean,error?:any }> {
|
|
95
|
+
return new Promise(reslove => {
|
|
96
|
+
///如果是数组,则一次性删除多个文件
|
|
97
|
+
if (Array.isArray(filepath)) {
|
|
98
|
+
const params = {
|
|
99
|
+
Bucket: this.bucket,
|
|
100
|
+
Region: this.region,
|
|
101
|
+
Objects: filepath.map(item => { return { Key: item } }),
|
|
102
|
+
};
|
|
103
|
+
return this.cos.deleteMultipleObject(params, (err: any) => {
|
|
104
|
+
return reslove({ successed: err == null, error: err })
|
|
105
|
+
})
|
|
106
|
+
}
|
|
97
107
|
const params = {
|
|
98
108
|
Bucket: this.bucket,
|
|
99
109
|
Region: this.region,
|
|
100
|
-
|
|
110
|
+
Key: filepath,
|
|
101
111
|
};
|
|
102
|
-
|
|
103
|
-
|
|
112
|
+
this.cos.deleteObject(params, (err: any) => {
|
|
113
|
+
return reslove({ successed: err == null, error: err })
|
|
104
114
|
})
|
|
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
115
|
})
|
|
116
|
+
|
|
114
117
|
}
|
|
115
118
|
}
|
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
|
@@ -5,16 +5,21 @@ import { LocalFile } from "./localfile";
|
|
|
5
5
|
import axios from 'axios';
|
|
6
6
|
import path from 'path';
|
|
7
7
|
import fs from 'fs';
|
|
8
|
+
let config:any;
|
|
8
9
|
/**
|
|
9
10
|
* 读取系统配置文件的腾讯云设置
|
|
10
11
|
* @returns
|
|
11
12
|
*/
|
|
12
|
-
function getConfigurationSetting(settingName?:string):any{
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
function getConfigurationSetting(settingName?:string,isSection:boolean=true):any{
|
|
14
|
+
if (!config){
|
|
15
|
+
let configfilename = process.env["CONFIGFILE"] || 'configuration.json'
|
|
16
|
+
let configfile = path.join(process.cwd(), configfilename);
|
|
17
|
+
if (!fs.existsSync(configfile)) return null;
|
|
18
|
+
config = require(configfile);
|
|
19
|
+
}
|
|
20
|
+
if (!config) return null;
|
|
21
|
+
//const config = require(configfile)[settingName || 'tencentCOS'];
|
|
22
|
+
return isSection ? config[settingName || 'tencentCOS'] : (config.appsetting[settingName ||'destination']??'tencentcos') ;
|
|
18
23
|
}
|
|
19
24
|
/**
|
|
20
25
|
* 文件处理器类型
|
|
@@ -29,7 +34,8 @@ export type FileProviderEnum = typeof FileProviderEnum[keyof typeof FileProvider
|
|
|
29
34
|
* @param destination
|
|
30
35
|
* @returns
|
|
31
36
|
*/
|
|
32
|
-
export function FileHelper(provider
|
|
37
|
+
export function FileHelper(provider?: FileProviderEnum, apiOption?: any): FileUtility {
|
|
38
|
+
if (!provider) provider = getConfigurationSetting('destination',false)
|
|
33
39
|
let filehandler: FileBase
|
|
34
40
|
switch (provider) {
|
|
35
41
|
case FileProviderEnum.LOCAL: filehandler = new LocalFile(); break;
|
|
@@ -89,8 +95,8 @@ class FileUtility {
|
|
|
89
95
|
* 删除指定位置的文件
|
|
90
96
|
* @param filePath
|
|
91
97
|
*/
|
|
92
|
-
DeleteFile(filePath: string | string[]):
|
|
93
|
-
|
|
98
|
+
async DeleteFile(filePath: string | string[]): Promise<{ successed: boolean}> {
|
|
99
|
+
return await this.fileHandler.deleteFile(filePath);
|
|
94
100
|
}
|
|
95
101
|
/**
|
|
96
102
|
* 批量下载指定的文件并上传到存储中
|
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
|
}
|