doomistorage 1.0.9 → 2.0.0

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/package.json CHANGED
@@ -1,26 +1,43 @@
1
1
  {
2
2
  "name": "doomistorage",
3
- "version": "1.0.9",
3
+ "version": "2.0.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
- "types": "./dist/index.d.js",
6
+ "module": "./dist/index.esm.js",
7
+ "types": "./dist/index.d.ts",
7
8
  "scripts": {
8
- "build": "tsc --outDir dist"
9
+ "build": "rm -rf dist && rollup -c",
10
+ "build:tsc": "rm -rf dist && tsc --outDir dist"
9
11
  },
10
12
  "keywords": [],
11
13
  "author": "",
12
14
  "license": "ISC",
13
15
  "devDependencies": {
16
+ "@rollup/plugin-commonjs": "^29.0.0",
17
+ "@rollup/plugin-json": "^6.1.0",
18
+ "@rollup/plugin-node-resolve": "^16.0.3",
19
+ "@rollup/plugin-typescript": "^12.3.0",
20
+ "@types/jest": "^27.5.2",
14
21
  "@types/multer": "^1.4.7",
15
22
  "@types/node": "^18.15.3",
16
23
  "@types/node-uuid": "^0.0.29",
17
- "typescript": "^5.0.2"
24
+ "jest": "^27.3.1",
25
+ "jest-html-reporters": "^2.1.6",
26
+ "rollup": "^2.79.2",
27
+ "rollup-plugin-terser": "^7.0.2",
28
+ "ts-jest": "^27.0.7",
29
+ "tslib": "^2.8.1",
30
+ "typescript": "^4.4.4"
18
31
  },
19
32
  "dependencies": {
20
- "axios": "^1.8.4",
33
+ "@azure/storage-blob": "^12.29.1",
21
34
  "cos-nodejs-sdk-v5": "^2.16.0-beta.8",
22
35
  "moment": "^2.29.4",
23
36
  "multer": "^1.4.5-lts.1",
37
+ "tencentcloud-sdk-nodejs-sts": "^4.1.100",
24
38
  "uuid": "^11.1.0"
39
+ },
40
+ "peerDependencies": {
41
+ "axios": "*"
25
42
  }
26
43
  }
@@ -0,0 +1,36 @@
1
+ import json from '@rollup/plugin-json';
2
+ import { nodeResolve } from '@rollup/plugin-node-resolve';
3
+ import commonjs from '@rollup/plugin-commonjs';
4
+ import { terser } from "rollup-plugin-terser";
5
+ import typescript from '@rollup/plugin-typescript'; // 若用 TS 则添加
6
+ const packageJson = require("./package.json");
7
+ export default {
8
+ input: 'src/index.ts', // 入口文件(TS 则为 src/index.ts)
9
+ output: [
10
+ {
11
+ // dir: 'dist', // 输出目录(与 tsconfig.outDir 一致)
12
+ file: packageJson.main,
13
+ sourcemap: true,
14
+ format: 'cjs',
15
+ compact: true, // 紧凑模式(减少空格)
16
+ plugins: [terser()], // 压缩代码,
17
+ },
18
+ // 输出 ESM 格式(供 import 使用)
19
+ {
20
+ file: packageJson.module,
21
+ format: 'esm',
22
+ sourcemap: true,
23
+ compact: true,
24
+ plugins: [ terser()]
25
+ }
26
+ ],
27
+ plugins: [
28
+ json(), // 处理 JSON 文件
29
+ nodeResolve({ preferBuiltins: true }), // 优先使用 Node.js 内置模块
30
+ commonjs(), // 转换 CommonJS 为 ESM
31
+ typescript() // 若用 TS,需配置 tsconfig.json
32
+ ],
33
+ external: [...Object.keys(packageJson.peerDependencies || {}),
34
+ ...Object.keys(packageJson.dependencies || {})
35
+ ]
36
+ };
@@ -0,0 +1,83 @@
1
+ /**
2
+ * 微软Azure Blob存储
3
+ */
4
+ import { FileConfig, FileResult } from "./declare";
5
+ import { FileBase } from "./file";
6
+ import { StorageSharedKeyCredential, BlobServiceClient, generateBlobSASQueryParameters, ContainerSASPermissions, SASProtocol } from "@azure/storage-blob";
7
+
8
+
9
+ export class AzureBlob extends FileBase {
10
+
11
+ private blobServiceClient;
12
+ private containerClient;
13
+ /**
14
+ * account : 账号
15
+ * accountKey : 密钥
16
+ * container : 容器名称 相当于腾讯云的存储桶
17
+ * @param config
18
+ */
19
+ constructor(private config:{ account: string, accountKey:string,container:string }) {
20
+ super();
21
+ const sharedKeyCredential = new StorageSharedKeyCredential(config.account, config.accountKey);
22
+ this.blobServiceClient = new BlobServiceClient(
23
+ `https://${config.account}.blob.core.windows.net`,
24
+ sharedKeyCredential,
25
+ );
26
+ this.containerClient = this.blobServiceClient.getContainerClient(config.container);
27
+ }
28
+ async getTemporaryToken(expiresSeconds?: number, permission?: string): Promise<string> {
29
+ const sharedKeyCredential = new StorageSharedKeyCredential(this.config.account, this.config.accountKey);
30
+ expiresSeconds = expiresSeconds || 86400; /// 默认一天的有效期
31
+ permission = permission || "rw"; ///read & write
32
+ const containerSAS = generateBlobSASQueryParameters(
33
+ {
34
+ containerName: this.config.container, // Required
35
+ permissions: ContainerSASPermissions.parse("rw"), // Required
36
+ startsOn: new Date(), // Optional 从当前开始计算
37
+ expiresOn: new Date(new Date().valueOf() + expiresSeconds * 1000), // Required. Date type
38
+ // ipRange: { start: "0.0.0.0", end: "255.255.255.255" }, // Optional
39
+ // protocol: SASProtocol.HttpsAndHttp, // Optional
40
+ // version: "2016-05-31", // Optional
41
+ },
42
+ sharedKeyCredential,
43
+ ).toString();
44
+ return containerSAS;
45
+ }
46
+ /**
47
+ *
48
+ * @param data
49
+ * @param fileName
50
+ * @param saveOption
51
+ * @param userInfo
52
+ */
53
+ async saveString2File(data: string | Buffer, fileName: string, saveOption: FileConfig, userInfo: any = {}): Promise<FileResult> {
54
+ if (!data) data = '';
55
+ const destinationFileName = this.getSaveFileName(saveOption, fileName, userInfo);
56
+ const blockBlobClient = this.containerClient.getBlockBlobClient(destinationFileName);
57
+ const uploadBlobResponse = await blockBlobClient.upload(data, typeof data === "string"? Buffer.byteLength(data) : data.byteLength);
58
+ return { successed: !uploadBlobResponse.errorCode, filePath: destinationFileName };
59
+ }
60
+ /**
61
+ *
62
+ * @param file
63
+ * @param fileName
64
+ * @param saveOption
65
+ * @param userInfo
66
+ * @returns
67
+ */
68
+ async saveFileStream(file: any, fileName: any, saveOption: FileConfig, userInfo: any): Promise<FileResult> {
69
+ const destinationFileName = this.getSaveFileName(saveOption, fileName, userInfo);
70
+ const blockBlobClient = this.containerClient.getBlockBlobClient(destinationFileName);
71
+ const uploadBlobResponse = await blockBlobClient.upload(file, file.byteCount || file.length);
72
+ return { successed: !uploadBlobResponse.errorCode, filePath: destinationFileName };
73
+ }
74
+ /**
75
+ * 删除指定位置的文件
76
+ * @param filepath
77
+ */
78
+ async deleteFile(filepath: string | string[]): Promise<{ successed: boolean,error?:any }> {
79
+ if (!Array.isArray(filepath)) filepath = [filepath];
80
+ for(const file of filepath) this.containerClient.deleteBlob(file)
81
+ return { successed: true }
82
+ }
83
+ }
package/src/cosfile.ts CHANGED
@@ -1,12 +1,17 @@
1
1
  import { FileConfig, FileResult } from "./declare";
2
2
  import { FileBase } from "./file";
3
3
  import COS from 'cos-nodejs-sdk-v5';
4
+ import tencentcloud from "tencentcloud-sdk-nodejs-sts";
5
+
6
+ const DEFAULT_PERMISSON = {
7
+ version:"2.0",statement:[{effect:"allow",action:["name/cos:PutObject","name/cos:DeleteObject"],"resource":"*"}]
8
+ }
4
9
 
5
10
  export class CosFile extends FileBase {
6
11
  private bucket: string; ///存储桶名称
7
12
  private cos: COS; ///腾讯云COS对象
8
13
  private region: string; ///存储桶所在的地区
9
- constructor(config: any) {
14
+ constructor(private config: any) {
10
15
  super();
11
16
  this.cos = new COS({
12
17
  SecretId: config.SecretId,
@@ -15,6 +20,30 @@ export class CosFile extends FileBase {
15
20
  this.bucket = config.bucket;
16
21
  this.region = config.region;
17
22
  }
23
+ async getTemporaryToken(expiresSeconds?: number, permission?: string): Promise<any> {
24
+ const StsClient = tencentcloud.sts.v20180813.Client;
25
+ permission = permission || JSON.stringify(DEFAULT_PERMISSON);
26
+ const clientConfig = {
27
+ credential: {
28
+ secretId: this.config.SecretId,
29
+ secretKey: this.config.SecretKey,
30
+ },
31
+ region: this.config.region,
32
+ profile: {
33
+ httpProfile: {
34
+ endpoint: "sts.tencentcloudapi.com",
35
+ },
36
+ },
37
+ };
38
+ const client = new StsClient(clientConfig);
39
+ const params = {
40
+ "Name": "TemporaryToken",
41
+ "Policy": permission,
42
+ "DurationSeconds": expiresSeconds? expiresSeconds : 7200,
43
+ };
44
+ const token = await client.GetFederationToken(params);
45
+ return token;
46
+ }
18
47
  /**
19
48
  *
20
49
  * @param data
@@ -74,7 +103,7 @@ export class CosFile extends FileBase {
74
103
  const fileParam = {
75
104
  Bucket: this.bucket,
76
105
  Region: this.region,
77
- // ContentDisposition: "form-data; name=\"file\"; filename=\"" + encodeURIComponent(destinationFileName) + "\"",
106
+ // ContentDisposition: "form-data; name="file"; filename="" + encodeURIComponent(destinationFileName) + """,
78
107
  Key: destinationFileName,
79
108
  Body: file,
80
109
  ContentLength: file.byteCount || file.length
package/src/file.ts CHANGED
@@ -88,7 +88,10 @@ export abstract class FileBase {
88
88
  * @param filepath
89
89
  */
90
90
  abstract deleteFile(filepath: string | string[]): Promise<{ successed: boolean, error?: any }>;
91
-
91
+ /**
92
+ * 获取到临时操作的token
93
+ */
94
+ abstract getTemporaryToken(expiresSeconds?:number,permission?:string):Promise<any>;
92
95
  /**
93
96
  * 创建多级目录
94
97
  * @param dirpath
@@ -114,114 +117,4 @@ export abstract class FileBase {
114
117
  }
115
118
  return true;
116
119
  }
117
-
118
- /**
119
- * 删除文件夹
120
- * @param {*} path
121
- */
122
- // deleteFolder(path:string) {
123
- // if( fs.existsSync(path) ) {
124
- // const files = fs.readdirSync(path);
125
- // files.forEach(function(file,index){
126
- // var curPath = path + "/" + file;
127
- // if(fs.statSync(curPath).isDirectory()) { // recurse
128
- // fileUtility.deleteFolder(curPath);
129
- // } else { // delete file
130
- // fs.unlinkSync(curPath);
131
- // }
132
- // });
133
- // fs.rmdirSync(path);
134
- // }
135
- // };
136
-
137
-
138
-
139
- /**
140
- * 从远程下载文件并保存到腾讯云本地
141
- * @param {*} saveDestination
142
- * @param {*} fileUrl
143
- * @param {*} savesetting
144
- * @param {*} keyid
145
- */
146
- // static downloadFile4upload(saveDestination,fileUrl,savesetting,userinfo,allowWebPFormat = false) {
147
- // let originUrl = fileUrl;
148
- // ///是否允许WebP格式的文件被下载
149
- // if(!allowWebPFormat &&fileUrl.indexOf('&tp=webp')>=0) fileUrl = fileUrl.replace('&tp=webp','');
150
- // let fileOption = urlUtility.parse(fileUrl);
151
- // let filename = path.basename(fileOption.path);
152
- // if(filename.indexOf('?')>=0) filename=filename.substr(0,filename.indexOf('?'));
153
- // if (path.extname(filename)==''){
154
- // let param = querystring.parse(fileOption.query)
155
- // if (param.wx_fmt)
156
- // filename=filename+"."+param.wx_fmt;
157
- // else
158
- // filename=filename+".jpeg";
159
- // }
160
- // const http =fileOption.protocol==='https:'? require('https'):require('http'); //require("../rpc/rpcUtility");
161
- // return new Promise((resolve, reject) => {
162
- // try{
163
- // http.get(fileUrl,(res) => {
164
- // var dataArr = [], len = 0;
165
- // res.on('data',(chunk)=>{
166
- // dataArr.push(chunk);
167
- // len += chunk.length;
168
- // })
169
- // res.on("end", function (err) {
170
- // File.uploadFile(saveDestination, filename, Buffer.concat(dataArr, len),savesetting,userinfo, function (file, result) {
171
- // result.source = originUrl;
172
- // if (result.successed) result.state = "SUCCESS";
173
- // resolve(result);
174
- // });
175
- // });
176
- // })
177
- // }
178
- // catch(err){
179
- // console.log('download file error :',err);
180
- // reject({successed:false,error:err});
181
- // }
182
- // })
183
- // }
184
- // static save2localForRemoteImage(sourceUrlArr, saveOption, userInfo, callback) {
185
- // File.promiseAll(sourceUrlArr, saveOption, userInfo).then(function (values) {
186
- // callback(null, values);
187
- // }).catch(function (err) {
188
- // callback(new Error("抓取报错"));
189
- // });
190
- // }
191
- // static promiseAll(urlArr, saveOption, userInfo) {
192
- // let promiseArr = [];
193
- // for (let url of urlArr) {
194
- // try {
195
- // promiseArr.push(File.downloadFile4upload('tencentcos',url,saveOption,userInfo))
196
- // } catch (error) {
197
- // console.log('error happened',error)
198
- // }
199
- // }
200
- // return Promise.all(promiseArr);
201
- // }
202
- /**
203
- * 上传本地目录至远程服务器
204
- * @param {*} localFile
205
- * @param {*} saveDestination
206
- * @param {*} savesetting
207
- * @param {*} keyid
208
- */
209
- // uploadlocalFolder(path,saveDestination,savesetting,keyParam){
210
- // if( fs.existsSync(path) ) {
211
- // const files = fs.readdirSync(path);
212
- // files.forEach(function(file,index){
213
- // var curPath = path + "/" + file;
214
- // if(fs.statSync(curPath).isDirectory()) { // recurse
215
- // this.uploadlocalFolder(curPath);
216
- // } else { // delete file
217
- // //fs.unlinkSync(curPath);
218
- // let data = fs.readFileSync(curPath);
219
- // File.uploadFile(saveDestination, path.basename(curPath), data,savesetting,keyParam,function (file, result) {
220
- // resolve(result);
221
- // });
222
- // }
223
- // });
224
- // fs.rmdirSync(path);
225
- // }
226
- // }
227
120
  }
package/src/filehelper.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CosFile } from "./cosfile";
2
+ import { AzureBlob } from "./azureblob";
2
3
  import { FileConfig, FileResult } from "./declare";
3
4
  import { FileBase } from "./file";
4
5
  import { LocalFile } from "./localfile";
@@ -27,6 +28,7 @@ function getConfigurationSetting(settingName?:string,isSection:boolean=true):any
27
28
  export const FileProviderEnum = {
28
29
  LOCAL: 'local',
29
30
  TENCENTCOS: 'tencentcos',
31
+ Azure:'azure'
30
32
  } as const;
31
33
  export type FileProviderEnum = typeof FileProviderEnum[keyof typeof FileProviderEnum];
32
34
  /**
@@ -45,6 +47,12 @@ export function FileHelper(provider?: FileProviderEnum, apiOption?: any): FileUt
45
47
  }
46
48
  filehandler = new CosFile(apiOption);
47
49
  break;
50
+ case FileProviderEnum.Azure:
51
+ if (!apiOption || typeof (apiOption) == 'string') {
52
+ apiOption = getConfigurationSetting('azurestorge')
53
+ }
54
+ filehandler = new AzureBlob(apiOption);
55
+ break;
48
56
  default: filehandler = new LocalFile();
49
57
  }
50
58
  return new FileUtility(filehandler)
@@ -59,6 +67,13 @@ class FileUtility {
59
67
  constructor(handler: FileBase) {
60
68
  this.fileHandler = handler;
61
69
  }
70
+
71
+ /**
72
+ * 获取文件存储的临时token
73
+ */
74
+ async getTemporaryToken(expiresSeconds: number=0, permission?: string):Promise<any>{
75
+ return await this.fileHandler.getTemporaryToken(expiresSeconds, permission);
76
+ }
62
77
  /**
63
78
  * 读取文件并保存至目标路径
64
79
  * @param file 文件对象
@@ -76,6 +91,7 @@ class FileUtility {
76
91
  return await this.fileHandler.saveFileStream(file, fileName, saveOption, userInfo);
77
92
  }
78
93
 
94
+
79
95
  /**
80
96
  * 保存字符流到对应的存储设备
81
97
  * @param bufferData
package/src/localfile.ts CHANGED
@@ -3,6 +3,7 @@ import fs from 'fs';
3
3
  import path from 'path';
4
4
  import { FileConfig, FileResult } from "./declare";
5
5
  export class LocalFile extends FileBase {
6
+
6
7
 
7
8
  /**
8
9
  * 直接保存字符串
@@ -19,11 +20,15 @@ export class LocalFile extends FileBase {
19
20
  ///创建本地文件夹
20
21
  if (!this.mkdirsSync(_saveDir)) return { successed: false };
21
22
  return new Promise(resolve => {
22
- fs.writeFile(fullFileName, data, (error) => {
23
+ fs.writeFile(fullFileName, data as any, (error) => {
23
24
  return resolve({ successed: error == null, filePath: fullFileName })
24
25
  })
25
26
  });
26
27
  }
28
+
29
+ async getTemporaryToken(expiresSeconds?: number, permission?: string): Promise<string> {
30
+ throw new Error("Method not implemented.");
31
+ }
27
32
  /**
28
33
  * 读取文件对象并保存至本地存储
29
34
  * @param file
@@ -0,0 +1,31 @@
1
+ {
2
+ "files": [
3
+ "src/index.ts"
4
+ ],
5
+ "compilerOptions": {
6
+ "target": "es2015",
7
+ "module": "commonjs",
8
+ "declaration": true,
9
+ "outDir": "./dist",
10
+ "noEmit": false,
11
+ "strict": true,
12
+ "noImplicitAny": true,
13
+ "strictNullChecks": true,
14
+ "strictFunctionTypes": true,
15
+ "strictBindCallApply": true,
16
+ "strictPropertyInitialization": true,
17
+ "noImplicitThis": true,
18
+ "alwaysStrict": true,
19
+ "noUnusedLocals": true,
20
+ "noUnusedParameters": true,
21
+ "noImplicitReturns": true,
22
+ "noFallthroughCasesInSwitch": true,
23
+ "noUncheckedIndexedAccess": true,
24
+ "noImplicitOverride": true,
25
+ "moduleResolution": "node",
26
+ "noPropertyAccessFromIndexSignature": true,
27
+ "esModuleInterop": true,
28
+ "forceConsistentCasingInFileNames": true,
29
+ "skipLibCheck": true
30
+ }
31
+ }
package/tsconfig.json CHANGED
@@ -1,31 +1,19 @@
1
1
  {
2
- "files": [
3
- "src/index.ts"
4
- ],
5
- "compilerOptions": {
6
- "target": "es2015",
7
- "module": "commonjs",
8
- "declaration": true,
9
- "outDir": "./dist",
10
- "noEmit": false,
11
- "strict": true,
12
- "noImplicitAny": true,
13
- "strictNullChecks": true,
14
- "strictFunctionTypes": true,
15
- "strictBindCallApply": true,
16
- "strictPropertyInitialization": true,
17
- "noImplicitThis": true,
18
- "alwaysStrict": true,
19
- "noUnusedLocals": true,
20
- "noUnusedParameters": true,
21
- "noImplicitReturns": true,
22
- "noFallthroughCasesInSwitch": true,
23
- "noUncheckedIndexedAccess": true,
24
- "noImplicitOverride": true,
25
- "moduleResolution": "node",
26
- "noPropertyAccessFromIndexSignature": true,
27
- "esModuleInterop": true,
28
- "forceConsistentCasingInFileNames": true,
29
- "skipLibCheck": true
30
- }
31
- }
2
+ "compilerOptions": {
3
+ "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
4
+ "module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
5
+ "declaration": true /* Generates corresponding '.d.ts' file. */,
6
+ "declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
7
+ "sourceMap": true /* Generates corresponding '.map' file. */,
8
+ "outDir": "dist" /* Redirect output structure to the directory. */,
9
+ "declarationDir": "./dist/types",
10
+ "noEmit": false,
11
+ "emitDeclarationOnly": false,
12
+ "strict": true /* Enable all strict type-checking options. */,
13
+ "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
14
+ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
15
+ "skipLibCheck": true /* Skip type checking of declaration files. */,
16
+ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
17
+ },
18
+ "include": ["src"]
19
+ }
package/dist/cosfile.d.ts DELETED
@@ -1,34 +0,0 @@
1
- /// <reference types="node" />
2
- import { FileConfig, FileResult } from "./declare";
3
- import { FileBase } from "./file";
4
- export declare class CosFile extends FileBase {
5
- private bucket;
6
- private cos;
7
- private region;
8
- constructor(config: any);
9
- /**
10
- *
11
- * @param data
12
- * @param fileName
13
- * @param saveOption
14
- * @param userInfo
15
- */
16
- saveString2File(data: string | Buffer, fileName: string, saveOption: FileConfig, userInfo?: any): Promise<FileResult>;
17
- /**
18
- *
19
- * @param file
20
- * @param fileName
21
- * @param saveOption
22
- * @param userInfo
23
- * @returns
24
- */
25
- saveFileStream(file: any, fileName: any, saveOption: FileConfig, userInfo: any): Promise<FileResult>;
26
- /**
27
- * 删除指定位置的文件
28
- * @param filepath
29
- */
30
- deleteFile(filepath: string | string[]): Promise<{
31
- successed: boolean;
32
- error?: any;
33
- }>;
34
- }