doomistorage 1.0.0 → 1.0.2

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
@@ -22,5 +22,10 @@ export declare class CosFile extends FileBase {
22
22
  * @param userInfo
23
23
  * @returns
24
24
  */
25
- saveFileStream(file: any, fileName: any, saveOption: any, userInfo: any): Promise<FileResult>;
25
+ saveFileStream(file: any, fileName: any, saveOption: FileConfig, userInfo: any): Promise<FileResult>;
26
+ /**
27
+ * 删除指定位置的文件
28
+ * @param filepath
29
+ */
30
+ deleteFile(filepath: string | string[]): void;
26
31
  }
package/dist/cosfile.js CHANGED
@@ -98,5 +98,32 @@ class CosFile extends file_1.FileBase {
98
98
  });
99
99
  });
100
100
  }
101
+ /**
102
+ * 删除指定位置的文件
103
+ * @param filepath
104
+ */
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);
116
+ });
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
+ });
127
+ }
101
128
  }
102
129
  exports.CosFile = CosFile;
package/dist/file.d.ts CHANGED
@@ -37,6 +37,11 @@ export declare abstract class FileBase {
37
37
  * @param userInfo
38
38
  */
39
39
  abstract saveString2File(data: string | Buffer, fileName: string, saveOption: FileConfig, userInfo: any): Promise<FileResult>;
40
+ /**
41
+ * 删除指定位置的文件
42
+ * @param filepath
43
+ */
44
+ abstract deleteFile(filepath: string | string[]): void;
40
45
  /**
41
46
  * 创建多级目录
42
47
  * @param dirpath
package/dist/file.js CHANGED
@@ -17,7 +17,7 @@ class FileBase {
17
17
  */
18
18
  getSaveFileName(saveOption, fileName, userInfo = {}) {
19
19
  let saveFolder = this.getSaveFolder(saveOption, userInfo);
20
- console.log('getSaveFileName', fileName);
20
+ // console.log('getSaveFileName', fileName)
21
21
  let _fileName = this.getSaveOnlyFileName(saveOption, fileName, userInfo);
22
22
  return path_1.default.join(saveFolder, _fileName);
23
23
  }
@@ -39,6 +39,11 @@ declare class FileUtility {
39
39
  * @returns
40
40
  */
41
41
  SaveString2File(bufferData: any, fileName: string, saveOption: FileConfig, userInfo?: any): Promise<FileResult>;
42
+ /**
43
+ * 删除指定位置的文件
44
+ * @param filePath
45
+ */
46
+ DeleteFile(filePath: string | string[]): void;
42
47
  /**
43
48
  * 下载文件并且保存
44
49
  * @param saveDestination
@@ -77,6 +77,13 @@ class FileUtility {
77
77
  return yield this.fileHandler.saveString2File(bufferData, fileName, saveOption, userInfo);
78
78
  });
79
79
  }
80
+ /**
81
+ * 删除指定位置的文件
82
+ * @param filePath
83
+ */
84
+ DeleteFile(filePath) {
85
+ this.fileHandler.deleteFile(filePath);
86
+ }
80
87
  /**
81
88
  * 下载文件并且保存
82
89
  * @param saveDestination
@@ -19,4 +19,9 @@ export declare class LocalFile extends FileBase {
19
19
  * @returns
20
20
  */
21
21
  saveFileStream(file: any, fileName: any, saveOption: FileConfig, userInfo: any): Promise<FileResult>;
22
+ /**
23
+ * 删除指定文件
24
+ * @param filepath
25
+ */
26
+ deleteFile(filepath: string | string[]): void;
22
27
  }
package/dist/localfile.js CHANGED
@@ -63,5 +63,25 @@ class LocalFile extends file_1.FileBase {
63
63
  return { successed: true, filePath: fullFileName };
64
64
  });
65
65
  }
66
+ /**
67
+ * 删除指定文件
68
+ * @param filepath
69
+ */
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);
83
+ });
84
+ }
85
+ }
66
86
  }
67
87
  exports.LocalFile = LocalFile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomistorage",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.js",
package/src/cosfile.ts CHANGED
@@ -2,12 +2,11 @@ import { FileConfig, FileResult } from "./declare";
2
2
  import { FileBase } from "./file";
3
3
  import COS from 'cos-nodejs-sdk-v5';
4
4
 
5
- export class CosFile extends FileBase{
6
-
7
- private bucket : string; ///存储桶名称
5
+ export class CosFile extends FileBase {
6
+ private bucket: string; ///存储桶名称
8
7
  private cos: COS; ///腾讯云COS对象
9
- private region : string; ///存储桶所在的地区
10
- constructor(config:any) {
8
+ private region: string; ///存储桶所在的地区
9
+ constructor(config: any) {
11
10
  super();
12
11
  this.cos = new COS({
13
12
  SecretId: config.SecretId,
@@ -23,7 +22,7 @@ export class CosFile extends FileBase{
23
22
  * @param saveOption
24
23
  * @param userInfo
25
24
  */
26
- saveString2File(data: string|Buffer, fileName: string, saveOption: FileConfig, userInfo: any = {}): Promise<FileResult> {
25
+ saveString2File(data: string | Buffer, fileName: string, saveOption: FileConfig, userInfo: any = {}): Promise<FileResult> {
27
26
  if (!data) data = '';
28
27
  let destinationFileName = this.getSaveFileName(saveOption, fileName, userInfo);
29
28
  const streamBuffer = data instanceof Buffer ? data : Buffer.from(data, "utf-8");
@@ -35,8 +34,8 @@ export class CosFile extends FileBase{
35
34
  ContentLength: streamBuffer.byteLength
36
35
  };
37
36
  return new Promise((success) => {
38
- this.cos.putObject(fileParam, (err:any)=> {
39
- return success({ successed: err == null, filePath: destinationFileName});
37
+ this.cos.putObject(fileParam, (err: any) => {
38
+ return success({ successed: err == null, filePath: destinationFileName });
40
39
  });
41
40
  });
42
41
  }
@@ -48,17 +47,17 @@ export class CosFile extends FileBase{
48
47
  * @param userInfo
49
48
  * @returns
50
49
  */
51
- async saveFileStream(file: any, fileName: any, saveOption: any, userInfo: any): Promise<FileResult> {
50
+ async saveFileStream(file: any, fileName: any, saveOption: FileConfig, userInfo: any): Promise<FileResult> {
52
51
  const destinationFileName = this.getSaveFileName(saveOption, fileName, userInfo);
53
52
  return new Promise((resolve) => {
54
53
  if (saveOption.reRead) {
55
- let dataArr:any =[],//存储读取的结果集合
54
+ let dataArr: any = [],//存储读取的结果集合
56
55
  len = 0;
57
- file.on('data', (chunk:any)=> {
56
+ file.on('data', (chunk: any) => {
58
57
  dataArr.push(chunk);
59
58
  len += chunk.length;
60
59
  });
61
- file.on('end', ()=> {
60
+ file.on('end', () => {
62
61
  const fileParam = {
63
62
  Bucket: this.bucket,
64
63
  Region: this.region,
@@ -66,9 +65,9 @@ export class CosFile extends FileBase{
66
65
  Body: Buffer.concat(dataArr, len), //file,
67
66
  ContentLength: len// file.byteCount || saveOption.contentLength
68
67
  };
69
- this.cos.putObject(fileParam, (err)=> {
68
+ this.cos.putObject(fileParam, (err) => {
70
69
  return resolve({ successed: err == null, filePath: destinationFileName })
71
-
70
+
72
71
  })
73
72
  });
74
73
  } else {
@@ -87,5 +86,30 @@ export class CosFile extends FileBase{
87
86
  }
88
87
  });
89
88
  }
89
+ /**
90
+ * 删除指定位置的文件
91
+ * @param filepath
92
+ */
90
93
 
94
+ deleteFile(filepath: string|string[]): void {
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
+ if (err) console.log('delete file error', err)
104
+ })
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
+ }
91
115
  }
package/src/file.ts CHANGED
@@ -12,7 +12,7 @@ export abstract class FileBase {
12
12
  */
13
13
  getSaveFileName(saveOption: FileConfig, fileName:string, userInfo:any = {}):string {
14
14
  let saveFolder = this.getSaveFolder(saveOption,userInfo)
15
- console.log('getSaveFileName', fileName)
15
+ // console.log('getSaveFileName', fileName)
16
16
  let _fileName = this.getSaveOnlyFileName(saveOption, fileName, userInfo);
17
17
  return path.join(saveFolder, _fileName);
18
18
  }
@@ -83,6 +83,11 @@ export abstract class FileBase {
83
83
  */
84
84
  abstract saveString2File(data: string | Buffer, fileName: string, saveOption: FileConfig, userInfo: any): Promise<FileResult>;
85
85
 
86
+ /**
87
+ * 删除指定位置的文件
88
+ * @param filepath
89
+ */
90
+ abstract deleteFile(filepath: string | string[]):void;
86
91
 
87
92
  /**
88
93
  * 创建多级目录
package/src/filehelper.ts CHANGED
@@ -69,7 +69,13 @@ export function FileHelper(provider: FileProviderEnum, apiOption?:any): FileUtil
69
69
  userInfo: any = {}): Promise<FileResult> {
70
70
  return await this.fileHandler.saveString2File(bufferData,fileName, saveOption, userInfo);
71
71
  }
72
-
72
+ /**
73
+ * 删除指定位置的文件
74
+ * @param filePath
75
+ */
76
+ DeleteFile(filePath:string|string[]):void{
77
+ this.fileHandler.deleteFile(filePath);
78
+ }
73
79
  /**
74
80
  * 下载文件并且保存
75
81
  * @param saveDestination
package/src/localfile.ts CHANGED
@@ -2,7 +2,8 @@ import { FileBase } from "./file";
2
2
  import fs from 'fs';
3
3
  import path from 'path';
4
4
  import { FileConfig, FileResult } from "./declare";
5
- export class LocalFile extends FileBase{
5
+ export class LocalFile extends FileBase {
6
+
6
7
  /**
7
8
  * 直接保存字符串
8
9
  * @param data
@@ -10,15 +11,15 @@ export class LocalFile extends FileBase{
10
11
  * @param saveOption
11
12
  * @param userInfo
12
13
  */
13
- async saveString2File(data: string|Buffer, fileName: string, saveOption: FileConfig, userInfo: any = {}): Promise<FileResult> {
14
+ async saveString2File(data: string | Buffer, fileName: string, saveOption: FileConfig, userInfo: any = {}): Promise<FileResult> {
14
15
  let baseFileName = this.getSaveFileName(saveOption, fileName, userInfo);
15
16
  if (!baseFileName) return { successed: false };
16
17
  let fullFileName = path.resolve(saveOption.basefolder || '', baseFileName);
17
18
  let _saveDir = path.dirname(fullFileName);
18
19
  ///创建本地文件夹
19
20
  if (!this.mkdirsSync(_saveDir)) return { successed: false };
20
- return new Promise(resolve=>{
21
- fs.writeFile(fullFileName, data,(error)=>{
21
+ return new Promise(resolve => {
22
+ fs.writeFile(fullFileName, data, (error) => {
22
23
  return resolve({ successed: error == null, filePath: fullFileName })
23
24
  })
24
25
  });
@@ -34,12 +35,29 @@ export class LocalFile extends FileBase{
34
35
  async saveFileStream(file: any, fileName: any, saveOption: FileConfig, userInfo: any): Promise<FileResult> {
35
36
  let baseFileName = this.getSaveFileName(saveOption, fileName, userInfo);
36
37
  if (!baseFileName) return { successed: false };
37
- let fullFileName = path.resolve(saveOption.basefolder||'', baseFileName);
38
+ let fullFileName = path.resolve(saveOption.basefolder || '', baseFileName);
38
39
  let _saveDir = path.dirname(fullFileName);
39
40
  ///创建本地文件夹
40
- if (!this.mkdirsSync(_saveDir)) return { successed: false };
41
+ if (!this.mkdirsSync(_saveDir)) return { successed: false };
41
42
  file.pipe(fs.createWriteStream(fullFileName));
42
43
  return { successed: true, filePath: fullFileName };
43
44
  }
44
-
45
+ /**
46
+ * 删除指定文件
47
+ * @param filepath
48
+ */
49
+ deleteFile(filepath: string | string[]) {
50
+ if (Array.isArray(filepath)) {
51
+ for (const f of filepath) {
52
+ fs.unlink(f, (err: any) => {
53
+ if (err) console.log('delete file error', err)
54
+ })
55
+ }
56
+ } else {
57
+ fs.unlink(filepath, (err: any) => {
58
+ if (err) console.log('delete file error', err)
59
+ })
60
+ }
61
+
62
+ }
45
63
  }