doomistorage 1.0.0 → 1.0.1
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 +5 -0
- package/dist/cosfile.js +27 -0
- package/dist/file.d.ts +5 -0
- package/dist/filehelper.d.ts +5 -0
- package/dist/filehelper.js +7 -0
- package/dist/localfile.d.ts +5 -0
- package/dist/localfile.js +20 -0
- package/package.json +1 -1
- package/src/cosfile.ts +39 -13
- package/src/file.ts +5 -0
- package/src/filehelper.ts +7 -1
- package/src/localfile.ts +25 -7
package/dist/cosfile.d.ts
CHANGED
|
@@ -23,4 +23,9 @@ export declare class CosFile extends FileBase {
|
|
|
23
23
|
* @returns
|
|
24
24
|
*/
|
|
25
25
|
saveFileStream(file: any, fileName: any, saveOption: any, 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/filehelper.d.ts
CHANGED
|
@@ -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
|
package/dist/filehelper.js
CHANGED
|
@@ -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
|
package/dist/localfile.d.ts
CHANGED
|
@@ -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
package/src/cosfile.ts
CHANGED
|
@@ -2,12 +2,13 @@ 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
|
-
|
|
5
|
+
export class CosFile extends FileBase {
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
private bucket: string; ///存储桶名称
|
|
8
9
|
private cos: COS; ///腾讯云COS对象
|
|
9
|
-
private region
|
|
10
|
-
constructor(config:any) {
|
|
10
|
+
private region: string; ///存储桶所在的地区
|
|
11
|
+
constructor(config: any) {
|
|
11
12
|
super();
|
|
12
13
|
this.cos = new COS({
|
|
13
14
|
SecretId: config.SecretId,
|
|
@@ -23,7 +24,7 @@ export class CosFile extends FileBase{
|
|
|
23
24
|
* @param saveOption
|
|
24
25
|
* @param userInfo
|
|
25
26
|
*/
|
|
26
|
-
saveString2File(data: string|Buffer, fileName: string, saveOption: FileConfig, userInfo: any = {}): Promise<FileResult> {
|
|
27
|
+
saveString2File(data: string | Buffer, fileName: string, saveOption: FileConfig, userInfo: any = {}): Promise<FileResult> {
|
|
27
28
|
if (!data) data = '';
|
|
28
29
|
let destinationFileName = this.getSaveFileName(saveOption, fileName, userInfo);
|
|
29
30
|
const streamBuffer = data instanceof Buffer ? data : Buffer.from(data, "utf-8");
|
|
@@ -35,8 +36,8 @@ export class CosFile extends FileBase{
|
|
|
35
36
|
ContentLength: streamBuffer.byteLength
|
|
36
37
|
};
|
|
37
38
|
return new Promise((success) => {
|
|
38
|
-
this.cos.putObject(fileParam, (err:any)=> {
|
|
39
|
-
return success({ successed: err == null, filePath: destinationFileName});
|
|
39
|
+
this.cos.putObject(fileParam, (err: any) => {
|
|
40
|
+
return success({ successed: err == null, filePath: destinationFileName });
|
|
40
41
|
});
|
|
41
42
|
});
|
|
42
43
|
}
|
|
@@ -52,13 +53,13 @@ export class CosFile extends FileBase{
|
|
|
52
53
|
const destinationFileName = this.getSaveFileName(saveOption, fileName, userInfo);
|
|
53
54
|
return new Promise((resolve) => {
|
|
54
55
|
if (saveOption.reRead) {
|
|
55
|
-
let dataArr:any =[],//存储读取的结果集合
|
|
56
|
+
let dataArr: any = [],//存储读取的结果集合
|
|
56
57
|
len = 0;
|
|
57
|
-
file.on('data',
|
|
58
|
+
file.on('data', (chunk: any) => {
|
|
58
59
|
dataArr.push(chunk);
|
|
59
60
|
len += chunk.length;
|
|
60
61
|
});
|
|
61
|
-
file.on('end',
|
|
62
|
+
file.on('end', () => {
|
|
62
63
|
const fileParam = {
|
|
63
64
|
Bucket: this.bucket,
|
|
64
65
|
Region: this.region,
|
|
@@ -66,9 +67,9 @@ export class CosFile extends FileBase{
|
|
|
66
67
|
Body: Buffer.concat(dataArr, len), //file,
|
|
67
68
|
ContentLength: len// file.byteCount || saveOption.contentLength
|
|
68
69
|
};
|
|
69
|
-
this.cos.putObject(fileParam,
|
|
70
|
+
this.cos.putObject(fileParam, (err) => {
|
|
70
71
|
return resolve({ successed: err == null, filePath: destinationFileName })
|
|
71
|
-
|
|
72
|
+
|
|
72
73
|
})
|
|
73
74
|
});
|
|
74
75
|
} else {
|
|
@@ -87,5 +88,30 @@ export class CosFile extends FileBase{
|
|
|
87
88
|
}
|
|
88
89
|
});
|
|
89
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* 删除指定位置的文件
|
|
93
|
+
* @param filepath
|
|
94
|
+
*/
|
|
90
95
|
|
|
96
|
+
deleteFile(filepath: string|string[]): void {
|
|
97
|
+
///如果是数组,则一次性删除多个文件
|
|
98
|
+
if (Array.isArray(filepath)){
|
|
99
|
+
const params = {
|
|
100
|
+
Bucket: this.bucket,
|
|
101
|
+
Region: this.region,
|
|
102
|
+
Objects: filepath.map(item => { return { Key: item }}),
|
|
103
|
+
};
|
|
104
|
+
return this.cos.deleteMultipleObject(params,(err:any)=>{
|
|
105
|
+
if (err) console.log('delete file error', err)
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
const params = {
|
|
109
|
+
Bucket: this.bucket,
|
|
110
|
+
Region: this.region,
|
|
111
|
+
Key: filepath,
|
|
112
|
+
};
|
|
113
|
+
this.cos.deleteObject(params, (err: any) => {
|
|
114
|
+
if (err) console.log('delete file error', err)
|
|
115
|
+
})
|
|
116
|
+
}
|
|
91
117
|
}
|
package/src/file.ts
CHANGED
|
@@ -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))
|
|
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
|
}
|