doomistorage 1.0.10 → 2.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.
@@ -1,91 +0,0 @@
1
- import { FileConfig, FileResult } from "./declare";
2
- import { FileBase } from "./file";
3
- /**
4
- * 文件处理器类型
5
- */
6
- export declare const FileProviderEnum: {
7
- readonly LOCAL: "local";
8
- readonly TENCENTCOS: "tencentcos";
9
- };
10
- export type FileProviderEnum = typeof FileProviderEnum[keyof typeof FileProviderEnum];
11
- /**
12
- * 获取到文件处理的对象
13
- * @param destination
14
- * @returns
15
- */
16
- export declare function FileHelper(provider?: FileProviderEnum, apiOption?: any): FileUtility;
17
- /**
18
- * 文件帮助工具
19
- */
20
- declare class FileUtility {
21
- private fileHandler;
22
- constructor(handler: FileBase);
23
- /**
24
- * 读取文件并保存至目标路径
25
- * @param file 文件对象
26
- * @param provider : 文件处理的实例名
27
- * @param fileName :最终需要保存的文件名称
28
- * @param saveOption :
29
- * @param userInfo
30
- * @returns
31
- */
32
- SaveFileStream(fileName: string, file: any, saveOption: FileConfig, userInfo?: any): Promise<FileResult>;
33
- /**
34
- * 保存字符流到对应的存储设备
35
- * @param bufferData
36
- * @param fileName
37
- * @param saveOption
38
- * @param userInfo
39
- * @returns
40
- */
41
- SaveString2File(bufferData: any, fileName: string, saveOption: FileConfig, userInfo?: any): Promise<FileResult>;
42
- /**
43
- * 删除指定位置的文件
44
- * @param filePath
45
- */
46
- DeleteFile(filePath: string | string[]): Promise<{
47
- successed: boolean;
48
- }>;
49
- /**
50
- * 批量下载指定的文件并上传到存储中
51
- * 替代原来file中的对应方法save2localForRemoteImage
52
- * 用于百度UEditor的保存文件
53
- * @param urlArray
54
- * @param saveOption
55
- * @param userInfo
56
- * @returns
57
- */
58
- BatchDownloadImage(urlArray: string[], saveOption: FileConfig, userInfo?: any): Promise<FileResult[]>;
59
- /**
60
- * 下载文件并且保存
61
- * @param saveDestination
62
- * @param fileUrl
63
- * @param savesetting
64
- * @param userinfo
65
- * @param allowWebPFormat
66
- * @returns
67
- */
68
- DownloadFile(fileUrl: string, savefileName: string | null, savesetting: FileConfig, userInfo?: any, allowWebPFormat?: boolean): Promise<FileResult>;
69
- /**
70
- * 预测生成文件保存后的文件路径
71
- * @param saveOption
72
- * @param fileName
73
- * @param userInfo
74
- */
75
- getSaveFileName(saveOption: FileConfig, fileName: string, userInfo?: any): string;
76
- /**
77
- * 预测生成文件保存后的文件路径
78
- * @param saveOption
79
- * @param fileName
80
- * @param userInfo
81
- */
82
- getSaveFolder(saveOption: FileConfig, userInfo?: any): string;
83
- /**
84
- * 预测生成文件保存后的文件名称
85
- * @param saveOption
86
- * @param fileName
87
- * @param userInfo
88
- */
89
- getSaveOnlyFileName(saveOption: FileConfig, fileName: string, userInfo?: any): string;
90
- }
91
- export {};
@@ -1,185 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.FileHelper = exports.FileProviderEnum = void 0;
16
- const cosfile_1 = require("./cosfile");
17
- const localfile_1 = require("./localfile");
18
- const axios_1 = __importDefault(require("axios"));
19
- const path_1 = __importDefault(require("path"));
20
- const fs_1 = __importDefault(require("fs"));
21
- let config;
22
- /**
23
- * 读取系统配置文件的腾讯云设置
24
- * @returns
25
- */
26
- function getConfigurationSetting(settingName, isSection = true) {
27
- var _a;
28
- if (!config) {
29
- let configfilename = process.env["CONFIGFILE"] || 'configuration.json';
30
- let configfile = path_1.default.join(process.cwd(), configfilename);
31
- if (!fs_1.default.existsSync(configfile))
32
- return null;
33
- config = require(configfile);
34
- }
35
- if (!config)
36
- return null;
37
- //const config = require(configfile)[settingName || 'tencentCOS'];
38
- return isSection ? config[settingName || 'tencentCOS'] : ((_a = config.appsetting[settingName || 'destination']) !== null && _a !== void 0 ? _a : 'tencentcos');
39
- }
40
- /**
41
- * 文件处理器类型
42
- */
43
- exports.FileProviderEnum = {
44
- LOCAL: 'local',
45
- TENCENTCOS: 'tencentcos',
46
- };
47
- /**
48
- * 获取到文件处理的对象
49
- * @param destination
50
- * @returns
51
- */
52
- function FileHelper(provider, apiOption) {
53
- if (!provider)
54
- provider = getConfigurationSetting('destination', false);
55
- let filehandler;
56
- switch (provider) {
57
- case exports.FileProviderEnum.LOCAL:
58
- filehandler = new localfile_1.LocalFile();
59
- break;
60
- case exports.FileProviderEnum.TENCENTCOS:
61
- if (!apiOption || typeof (apiOption) == 'string') {
62
- apiOption = getConfigurationSetting(apiOption);
63
- }
64
- filehandler = new cosfile_1.CosFile(apiOption);
65
- break;
66
- default: filehandler = new localfile_1.LocalFile();
67
- }
68
- return new FileUtility(filehandler);
69
- }
70
- exports.FileHelper = FileHelper;
71
- /**
72
- * 文件帮助工具
73
- */
74
- class FileUtility {
75
- constructor(handler) {
76
- this.fileHandler = handler;
77
- }
78
- /**
79
- * 读取文件并保存至目标路径
80
- * @param file 文件对象
81
- * @param provider : 文件处理的实例名
82
- * @param fileName :最终需要保存的文件名称
83
- * @param saveOption :
84
- * @param userInfo
85
- * @returns
86
- */
87
- SaveFileStream(fileName, file, saveOption, userInfo = {}) {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- return yield this.fileHandler.saveFileStream(file, fileName, saveOption, userInfo);
90
- });
91
- }
92
- /**
93
- * 保存字符流到对应的存储设备
94
- * @param bufferData
95
- * @param fileName
96
- * @param saveOption
97
- * @param userInfo
98
- * @returns
99
- */
100
- SaveString2File(bufferData, fileName, saveOption, userInfo = {}) {
101
- return __awaiter(this, void 0, void 0, function* () {
102
- return yield this.fileHandler.saveString2File(bufferData, fileName, saveOption, userInfo);
103
- });
104
- }
105
- /**
106
- * 删除指定位置的文件
107
- * @param filePath
108
- */
109
- DeleteFile(filePath) {
110
- return __awaiter(this, void 0, void 0, function* () {
111
- return yield this.fileHandler.deleteFile(filePath);
112
- });
113
- }
114
- /**
115
- * 批量下载指定的文件并上传到存储中
116
- * 替代原来file中的对应方法save2localForRemoteImage
117
- * 用于百度UEditor的保存文件
118
- * @param urlArray
119
- * @param saveOption
120
- * @param userInfo
121
- * @returns
122
- */
123
- BatchDownloadImage(urlArray, saveOption, userInfo = {}) {
124
- return __awaiter(this, void 0, void 0, function* () {
125
- let promiseArr = [];
126
- for (const url of urlArray) {
127
- promiseArr.push(this.DownloadFile(url, null, saveOption, userInfo));
128
- }
129
- return yield Promise.all(promiseArr);
130
- });
131
- }
132
- /**
133
- * 下载文件并且保存
134
- * @param saveDestination
135
- * @param fileUrl
136
- * @param savesetting
137
- * @param userinfo
138
- * @param allowWebPFormat
139
- * @returns
140
- */
141
- DownloadFile(fileUrl, savefileName, savesetting, userInfo = {}, allowWebPFormat = false) {
142
- return __awaiter(this, void 0, void 0, function* () {
143
- if (!allowWebPFormat && fileUrl.indexOf('&tp=webp') >= 0)
144
- fileUrl = fileUrl.replace('&tp=webp', '');
145
- let fileName = savefileName ? savefileName : path_1.default.basename(fileUrl);
146
- try {
147
- const downloadResult = yield axios_1.default.get(fileUrl, { responseType: 'arraybuffer' });
148
- if (downloadResult.data) {
149
- return this.SaveString2File(downloadResult.data, fileName, savesetting, userInfo);
150
- }
151
- return { successed: false, error: '下载文件中没有任何内容' };
152
- }
153
- catch (error) {
154
- return { successed: false, error: error };
155
- }
156
- });
157
- }
158
- /**
159
- * 预测生成文件保存后的文件路径
160
- * @param saveOption
161
- * @param fileName
162
- * @param userInfo
163
- */
164
- getSaveFileName(saveOption, fileName, userInfo = {}) {
165
- return this.fileHandler.getSaveFileName(saveOption, fileName, userInfo);
166
- }
167
- /**
168
- * 预测生成文件保存后的文件路径
169
- * @param saveOption
170
- * @param fileName
171
- * @param userInfo
172
- */
173
- getSaveFolder(saveOption, userInfo = {}) {
174
- return this.fileHandler.getSaveFolder(saveOption, userInfo);
175
- }
176
- /**
177
- * 预测生成文件保存后的文件名称
178
- * @param saveOption
179
- * @param fileName
180
- * @param userInfo
181
- */
182
- getSaveOnlyFileName(saveOption, fileName, userInfo = {}) {
183
- return this.fileHandler.getSaveOnlyFileName(saveOption, fileName, userInfo);
184
- }
185
- }
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './filehelper';
2
- export * from './uploader';
package/dist/index.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./filehelper"), exports);
18
- __exportStar(require("./uploader"), exports);
@@ -1,30 +0,0 @@
1
- /// <reference types="node" />
2
- import { FileBase } from "./file";
3
- import { FileConfig, FileResult } from "./declare";
4
- export declare class LocalFile extends FileBase {
5
- /**
6
- * 直接保存字符串
7
- * @param data
8
- * @param fileName
9
- * @param saveOption
10
- * @param userInfo
11
- */
12
- saveString2File(data: string | Buffer, fileName: string, saveOption: FileConfig, userInfo?: any): Promise<FileResult>;
13
- /**
14
- * 读取文件对象并保存至本地存储
15
- * @param file
16
- * @param fileName
17
- * @param saveOption
18
- * @param userInfo
19
- * @returns
20
- */
21
- saveFileStream(file: any, fileName: any, saveOption: FileConfig, userInfo: any): Promise<FileResult>;
22
- /**
23
- * 删除指定文件
24
- * @param filepath
25
- */
26
- deleteFile(filepath: string | string[]): Promise<{
27
- successed: boolean;
28
- error?: any;
29
- }>;
30
- }
package/dist/localfile.js DELETED
@@ -1,92 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.LocalFile = void 0;
16
- const file_1 = require("./file");
17
- const fs_1 = __importDefault(require("fs"));
18
- const path_1 = __importDefault(require("path"));
19
- class LocalFile extends file_1.FileBase {
20
- /**
21
- * 直接保存字符串
22
- * @param data
23
- * @param fileName
24
- * @param saveOption
25
- * @param userInfo
26
- */
27
- saveString2File(data, fileName, saveOption, userInfo = {}) {
28
- return __awaiter(this, void 0, void 0, function* () {
29
- let baseFileName = this.getSaveFileName(saveOption, fileName, userInfo);
30
- if (!baseFileName)
31
- return { successed: false };
32
- let fullFileName = path_1.default.resolve(saveOption.basefolder || '', baseFileName);
33
- let _saveDir = path_1.default.dirname(fullFileName);
34
- ///创建本地文件夹
35
- if (!this.mkdirsSync(_saveDir))
36
- return { successed: false };
37
- return new Promise(resolve => {
38
- fs_1.default.writeFile(fullFileName, data, (error) => {
39
- return resolve({ successed: error == null, filePath: fullFileName });
40
- });
41
- });
42
- });
43
- }
44
- /**
45
- * 读取文件对象并保存至本地存储
46
- * @param file
47
- * @param fileName
48
- * @param saveOption
49
- * @param userInfo
50
- * @returns
51
- */
52
- saveFileStream(file, fileName, saveOption, userInfo) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- let baseFileName = this.getSaveFileName(saveOption, fileName, userInfo);
55
- if (!baseFileName)
56
- return { successed: false };
57
- let fullFileName = path_1.default.resolve(saveOption.basefolder || '', baseFileName);
58
- let _saveDir = path_1.default.dirname(fullFileName);
59
- ///创建本地文件夹
60
- if (!this.mkdirsSync(_saveDir))
61
- return { successed: false };
62
- file.pipe(fs_1.default.createWriteStream(fullFileName));
63
- return { successed: true, filePath: fullFileName };
64
- });
65
- }
66
- /**
67
- * 删除指定文件
68
- * @param filepath
69
- */
70
- deleteFile(filepath) {
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 });
88
- });
89
- });
90
- }
91
- }
92
- exports.LocalFile = LocalFile;
@@ -1,2 +0,0 @@
1
- import multer from 'multer';
2
- export declare function getFileUploader(uploadConfig: any): multer.Multer;
package/dist/uploader.js DELETED
@@ -1,65 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getFileUploader = void 0;
7
- const multer_1 = __importDefault(require("multer"));
8
- const path_1 = __importDefault(require("path"));
9
- const fs_1 = __importDefault(require("fs"));
10
- const localfile_1 = require("./localfile");
11
- class LocalUploader {
12
- constructor(config) {
13
- this.uploadconfig = config !== null && config !== void 0 ? config : {};
14
- this.fileUtilily = new localfile_1.LocalFile();
15
- }
16
- get diskStorage() {
17
- return multer_1.default.diskStorage({
18
- //设置上传后文件路径,uploads文件夹会自动创建。
19
- destination: (req, _file, cb) => {
20
- ///获取上传的文件的类型,以便将文件保存到指定的目录
21
- let filetype = req.query.filetype;
22
- ////不允许没有配置的文件
23
- if (!filetype) {
24
- req.fileerror = { successed: false, errmsg: "missing filetype for upload" };
25
- return cb("missing filetype for upload", null);
26
- }
27
- let saveOption = this.uploadconfig.mapping[filetype];
28
- if (!saveOption) {
29
- req.fileerror = { successed: false, errmsg: "filetype not configurated in upload setting" };
30
- return cb("filetype not configurated in upload setting", null);
31
- }
32
- req.fileconfig = saveOption;
33
- /**
34
- * 将url中的参数拼装成对象,以匹配文件存储的设置
35
- */
36
- let fileparam = Object.assign({}, req.user, req.query);
37
- let destinationFolder = this.fileUtilily.getSaveFolder(saveOption, fileparam);
38
- ///文件上传后的短路径,相对路径,
39
- req.shortpath = destinationFolder;
40
- ////如果目录不存在,则创建目录
41
- destinationFolder = path_1.default.join(this.uploadconfig.dest, destinationFolder);
42
- if (!fs_1.default.existsSync(destinationFolder))
43
- this.fileUtilily.mkdirsSync(path_1.default.resolve(destinationFolder));
44
- cb(null, destinationFolder);
45
- },
46
- //给上传文件重命名,获取添加后缀名
47
- filename: (req, file, cb) => {
48
- let saveOption = req.fileconfig; //this.config.mapping[filetype];
49
- if (!saveOption) {
50
- req.fileerror = { successed: false, errmsg: "filetype not configurated in upload setting" };
51
- cb("filetype not configurated in upload setting", null);
52
- }
53
- let finalFile = this.fileUtilily.getSaveOnlyFileName(saveOption, file.originalname);
54
- req.shortpath = path_1.default.join(req.shortpath, finalFile);
55
- cb(null, finalFile);
56
- //cb(null, file.fieldname + '-' + Date.now() + "." + fileFormat[fileFormat.length - 1]);
57
- }
58
- });
59
- }
60
- }
61
- function getFileUploader(uploadConfig) {
62
- let handler = new LocalUploader(uploadConfig);
63
- return (0, multer_1.default)({ storage: handler.diskStorage });
64
- }
65
- exports.getFileUploader = getFileUploader;