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 CHANGED
@@ -27,5 +27,8 @@ export declare class CosFile extends FileBase {
27
27
  * 删除指定位置的文件
28
28
  * @param filepath
29
29
  */
30
- deleteFile(filepath: string | string[]): void;
30
+ deleteFile(filepath: string | string[]): Promise<{
31
+ successed: boolean;
32
+ error?: any;
33
+ }>;
31
34
  }
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
- if (Array.isArray(filepath)) {
108
- const params = {
109
- Bucket: this.bucket,
110
- Region: this.region,
111
- Objects: filepath.map(item => { return { Key: item }; }),
112
- };
113
- return this.cos.deleteMultipleObject(params, (err) => {
114
- if (err)
115
- console.log('delete file error', err);
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[]): void;
44
+ abstract deleteFile(filepath: string | string[]): Promise<{
45
+ successed: boolean;
46
+ error?: any;
47
+ }>;
45
48
  /**
46
49
  * 创建多级目录
47
50
  * @param dirpath
@@ -43,7 +43,9 @@ declare class FileUtility {
43
43
  * 删除指定位置的文件
44
44
  * @param filePath
45
45
  */
46
- DeleteFile(filePath: string | string[]): void;
46
+ DeleteFile(filePath: string | string[]): Promise<{
47
+ successed: boolean;
48
+ }>;
47
49
  /**
48
50
  * 批量下载指定的文件并上传到存储中
49
51
  * 替代原来file中的对应方法save2localForRemoteImage
@@ -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.fileHandler.deleteFile(filePath);
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ return yield this.fileHandler.deleteFile(filePath);
103
+ });
86
104
  }
87
105
  /**
88
106
  * 批量下载指定的文件并上传到存储中
@@ -23,5 +23,8 @@ export declare class LocalFile extends FileBase {
23
23
  * 删除指定文件
24
24
  * @param filepath
25
25
  */
26
- deleteFile(filepath: string | string[]): void;
26
+ deleteFile(filepath: string | string[]): Promise<{
27
+ successed: boolean;
28
+ error?: any;
29
+ }>;
27
30
  }
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
- if (Array.isArray(filepath)) {
72
- for (const f of filepath) {
73
- fs_1.default.unlink(f, (err) => {
74
- if (err)
75
- console.log('delete file error', err);
76
- });
77
- }
78
- }
79
- else {
80
- fs_1.default.unlink(filepath, (err) => {
81
- if (err)
82
- console.log('delete file error', err);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomistorage",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.js",
package/src/cosfile.ts CHANGED
@@ -90,26 +90,28 @@ export class CosFile extends FileBase {
90
90
  * 删除指定位置的文件
91
91
  * @param filepath
92
92
  */
93
-
94
- deleteFile(filepath: string|string[]): void {
95
- ///如果是数组,则一次性删除多个文件
96
- if (Array.isArray(filepath)){
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
- Objects: filepath.map(item => { return { Key: item }}),
109
+ Key: filepath,
101
110
  };
102
- return this.cos.deleteMultipleObject(params,(err:any)=>{
103
- if (err) console.log('delete file error', err)
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 = userInfo.id??UUIDV4(); break;
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
- 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
- });
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[]):void;
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: filehandler = new CosFile(apiOption); break;
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[]): void {
77
- this.fileHandler.deleteFile(filePath);
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
- if (Array.isArray(filepath)) {
51
- for (const f of filepath) {
52
- fs.unlink(f, (err: any) => {
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
- } else {
57
- fs.unlink(filepath, (err: any) => {
58
- if (err) console.log('delete file error', err)
59
- })
60
- }
62
+ return reslove({ successed: true })
63
+ })
64
+
61
65
 
62
66
  }
63
67
  }