doomistorage 1.0.2 → 1.0.4

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.
@@ -44,6 +44,16 @@ declare class FileUtility {
44
44
  * @param filePath
45
45
  */
46
46
  DeleteFile(filePath: string | string[]): void;
47
+ /**
48
+ * 批量下载指定的文件并上传到存储中
49
+ * 替代原来file中的对应方法save2localForRemoteImage
50
+ * 用于百度UEditor的保存文件
51
+ * @param urlArray
52
+ * @param saveOption
53
+ * @param userInfo
54
+ * @returns
55
+ */
56
+ BatchDownloadImage(urlArray: string[], saveOption: FileConfig, userInfo?: any): Promise<FileResult[]>;
47
57
  /**
48
58
  * 下载文件并且保存
49
59
  * @param saveDestination
@@ -53,7 +63,7 @@ declare class FileUtility {
53
63
  * @param allowWebPFormat
54
64
  * @returns
55
65
  */
56
- DownloadFile(fileUrl: string, savefileName: string, savesetting: FileConfig, userInfo?: any, allowWebPFormat?: boolean): Promise<FileResult>;
66
+ DownloadFile(fileUrl: string, savefileName: string | null, savesetting: FileConfig, userInfo?: any, allowWebPFormat?: boolean): Promise<FileResult>;
57
67
  /**
58
68
  * 预测生成文件保存后的文件路径
59
69
  * @param saveOption
@@ -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();
@@ -84,6 +100,24 @@ class FileUtility {
84
100
  DeleteFile(filePath) {
85
101
  this.fileHandler.deleteFile(filePath);
86
102
  }
103
+ /**
104
+ * 批量下载指定的文件并上传到存储中
105
+ * 替代原来file中的对应方法save2localForRemoteImage
106
+ * 用于百度UEditor的保存文件
107
+ * @param urlArray
108
+ * @param saveOption
109
+ * @param userInfo
110
+ * @returns
111
+ */
112
+ BatchDownloadImage(urlArray, saveOption, userInfo = {}) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ let promiseArr = [];
115
+ for (const url of urlArray) {
116
+ promiseArr.push(this.DownloadFile(url, null, saveOption, userInfo));
117
+ }
118
+ return yield Promise.all(promiseArr);
119
+ });
120
+ }
87
121
  /**
88
122
  * 下载文件并且保存
89
123
  * @param saveDestination
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomistorage",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.js",
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
  */
@@ -18,12 +29,17 @@ export type FileProviderEnum = typeof FileProviderEnum[keyof typeof FileProvider
18
29
  * @param destination
19
30
  * @returns
20
31
  */
21
- export function FileHelper(provider: FileProviderEnum, apiOption?:any): FileUtility {
22
- let filehandler:FileBase
32
+ export function FileHelper(provider: FileProviderEnum, apiOption?: any): FileUtility {
33
+ let filehandler: FileBase
23
34
  switch (provider) {
24
- case FileProviderEnum.LOCAL: filehandler = new LocalFile(); break;
25
- case FileProviderEnum.TENCENTCOS: filehandler = new CosFile(apiOption); break;
26
- default: filehandler = new LocalFile();
35
+ case FileProviderEnum.LOCAL: filehandler = new LocalFile(); break;
36
+ case FileProviderEnum.TENCENTCOS:
37
+ if (!apiOption || typeof (apiOption)=='string'){
38
+ apiOption = getConfigurationSetting(apiOption)
39
+ }
40
+ filehandler = new CosFile(apiOption);
41
+ break;
42
+ default: filehandler = new LocalFile();
27
43
  }
28
44
  return new FileUtility(filehandler)
29
45
  }
@@ -32,7 +48,7 @@ export function FileHelper(provider: FileProviderEnum, apiOption?:any): FileUtil
32
48
  /**
33
49
  * 文件帮助工具
34
50
  */
35
- class FileUtility {
51
+ class FileUtility {
36
52
  private fileHandler: FileBase;
37
53
  constructor(handler: FileBase) {
38
54
  this.fileHandler = handler;
@@ -53,7 +69,7 @@ export function FileHelper(provider: FileProviderEnum, apiOption?:any): FileUtil
53
69
  userInfo: any = {}): Promise<FileResult> {
54
70
  return await this.fileHandler.saveFileStream(file, fileName, saveOption, userInfo);
55
71
  }
56
-
72
+
57
73
  /**
58
74
  * 保存字符流到对应的存储设备
59
75
  * @param bufferData
@@ -67,15 +83,31 @@ export function FileHelper(provider: FileProviderEnum, apiOption?:any): FileUtil
67
83
  fileName: string,
68
84
  saveOption: FileConfig,
69
85
  userInfo: any = {}): Promise<FileResult> {
70
- return await this.fileHandler.saveString2File(bufferData,fileName, saveOption, userInfo);
86
+ return await this.fileHandler.saveString2File(bufferData, fileName, saveOption, userInfo);
71
87
  }
72
88
  /**
73
89
  * 删除指定位置的文件
74
90
  * @param filePath
75
91
  */
76
- DeleteFile(filePath:string|string[]):void{
92
+ DeleteFile(filePath: string | string[]): void {
77
93
  this.fileHandler.deleteFile(filePath);
78
94
  }
95
+ /**
96
+ * 批量下载指定的文件并上传到存储中
97
+ * 替代原来file中的对应方法save2localForRemoteImage
98
+ * 用于百度UEditor的保存文件
99
+ * @param urlArray
100
+ * @param saveOption
101
+ * @param userInfo
102
+ * @returns
103
+ */
104
+ async BatchDownloadImage(urlArray: string[], saveOption: FileConfig, userInfo: any = {}): Promise<FileResult[]> {
105
+ let promiseArr = [];
106
+ for (const url of urlArray) {
107
+ promiseArr.push(this.DownloadFile(url, null, saveOption, userInfo))
108
+ }
109
+ return await Promise.all(promiseArr);
110
+ }
79
111
  /**
80
112
  * 下载文件并且保存
81
113
  * @param saveDestination
@@ -85,17 +117,17 @@ export function FileHelper(provider: FileProviderEnum, apiOption?:any): FileUtil
85
117
  * @param allowWebPFormat
86
118
  * @returns
87
119
  */
88
- async DownloadFile(fileUrl: string, savefileName:string, savesetting: FileConfig, userInfo: any = {}, allowWebPFormat = false): Promise<FileResult> {
120
+ async DownloadFile(fileUrl: string, savefileName: string | null, savesetting: FileConfig, userInfo: any = {}, allowWebPFormat = false): Promise<FileResult> {
89
121
  if (!allowWebPFormat && fileUrl.indexOf('&tp=webp') >= 0) fileUrl = fileUrl.replace('&tp=webp', '');
90
- let fileName = savefileName ?savefileName:path.basename(fileUrl);
91
- try{
122
+ let fileName = savefileName ? savefileName : path.basename(fileUrl);
123
+ try {
92
124
  const downloadResult = await axios.get(fileUrl, { responseType: 'arraybuffer' });
93
- if (downloadResult.data){
94
- return this.SaveString2File(downloadResult.data, fileName, savesetting,userInfo);
125
+ if (downloadResult.data) {
126
+ return this.SaveString2File(downloadResult.data, fileName, savesetting, userInfo);
95
127
  }
96
128
  return { successed: false, error: '下载文件中没有任何内容' }
97
- }catch(error:any){
98
- return { successed: false, error: error}
129
+ } catch (error: any) {
130
+ return { successed: false, error: error }
99
131
  }
100
132
  }
101
133
  /**
@@ -104,26 +136,28 @@ export function FileHelper(provider: FileProviderEnum, apiOption?:any): FileUtil
104
136
  * @param fileName
105
137
  * @param userInfo
106
138
  */
107
- getSaveFileName(saveOption: FileConfig, fileName: string, userInfo: any = {}):string{
108
- return this.fileHandler.getSaveFileName(saveOption, fileName, userInfo);
109
- }
139
+ getSaveFileName(saveOption: FileConfig, fileName: string, userInfo: any = {}): string {
140
+ return this.fileHandler.getSaveFileName(saveOption, fileName, userInfo);
141
+ }
142
+
143
+ /**
144
+ * 预测生成文件保存后的文件路径
145
+ * @param saveOption
146
+ * @param fileName
147
+ * @param userInfo
148
+ */
149
+ getSaveFolder(saveOption: FileConfig, userInfo: any = {}): string {
150
+ return this.fileHandler.getSaveFolder(saveOption, userInfo);
151
+ }
152
+ /**
153
+ * 预测生成文件保存后的文件名称
154
+ * @param saveOption
155
+ * @param fileName
156
+ * @param userInfo
157
+ */
158
+ getSaveOnlyFileName(saveOption: FileConfig, fileName: string, userInfo: any = {}): string {
159
+ return this.fileHandler.getSaveOnlyFileName(saveOption, fileName, userInfo);
160
+ }
161
+
110
162
 
111
- /**
112
- * 预测生成文件保存后的文件路径
113
- * @param saveOption
114
- * @param fileName
115
- * @param userInfo
116
- */
117
- getSaveFolder(saveOption: FileConfig, userInfo: any = {}): string {
118
- return this.fileHandler.getSaveFolder(saveOption, userInfo);
119
- }
120
- /**
121
- * 预测生成文件保存后的文件名称
122
- * @param saveOption
123
- * @param fileName
124
- * @param userInfo
125
- */
126
- getSaveOnlyFileName(saveOption: FileConfig, fileName: string, userInfo: any = {}): string {
127
- return this.fileHandler.getSaveOnlyFileName(saveOption,fileName,userInfo);
128
- }
129
163
  }