@scefira/dfw 0.1.15 → 0.2.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/lib/DFWInstance.d.ts +20 -20
- package/lib/DFWInstance.js +55 -55
- package/lib/DFWUtils.d.ts +27 -27
- package/lib/DFWUtils.js +63 -63
- package/lib/DFWUtils.js.map +1 -1
- package/lib/index.d.ts +10 -10
- package/lib/index.js +25 -25
- package/lib/manager/APIManager.d.ts +47 -47
- package/lib/manager/APIManager.d.ts.map +1 -1
- package/lib/manager/APIManager.js +207 -233
- package/lib/manager/APIManager.js.map +1 -1
- package/lib/manager/DFWModule.d.ts +13 -13
- package/lib/manager/DFWModule.js +19 -19
- package/lib/manager/DatabaseManager.d.ts +8 -8
- package/lib/manager/DatabaseManager.js +24 -24
- package/lib/manager/FileManager.d.ts +130 -130
- package/lib/manager/FileManager.js +254 -254
- package/lib/manager/SecurityManager.d.ts +50 -50
- package/lib/manager/SecurityManager.d.ts.map +1 -1
- package/lib/manager/SecurityManager.js +187 -191
- package/lib/manager/SecurityManager.js.map +1 -1
- package/lib/manager/SessionManager.d.ts +40 -40
- package/lib/manager/SessionManager.d.ts.map +1 -1
- package/lib/manager/SessionManager.js +195 -173
- package/lib/manager/SessionManager.js.map +1 -1
- package/lib/manager/UserManager.d.ts +42 -42
- package/lib/manager/UserManager.js +62 -62
- package/lib/test.d.ts +1 -1
- package/lib/test.js +70 -70
- package/lib/test.js.map +1 -1
- package/lib/types/APIListenerConfig.d.ts +52 -52
- package/lib/types/APIListenerConfig.js +2 -2
- package/lib/types/DFWBoot.d.ts +18 -18
- package/lib/types/DFWBoot.d.ts.map +1 -1
- package/lib/types/DFWBoot.js +2 -2
- package/lib/types/DFWConfig.d.ts +54 -54
- package/lib/types/DFWConfig.js +2 -2
- package/lib/types/DFWRequestScheme.d.ts +33 -37
- package/lib/types/DFWRequestScheme.d.ts.map +1 -1
- package/lib/types/DFWRequestScheme.js +2 -2
- package/package.json +4 -3
- package/prisma/schema.prisma +5 -27
|
@@ -1,255 +1,255 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
-
};
|
|
37
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
const util_1 = require("util");
|
|
39
|
-
const DFWModule_1 = __importDefault(require("./DFWModule"));
|
|
40
|
-
const md5_file_1 = __importDefault(require("md5-file"));
|
|
41
|
-
const express_1 = __importDefault(require("express"));
|
|
42
|
-
const fs = __importStar(require("fs"));
|
|
43
|
-
const path = __importStar(require("path"));
|
|
44
|
-
const DFWUtils_1 = __importDefault(require("../DFWUtils"));
|
|
45
|
-
const luxon_1 = require("luxon");
|
|
46
|
-
const fileExistsAsync = (0, util_1.promisify)(fs.exists);
|
|
47
|
-
const fileRenameAsync = (0, util_1.promisify)(fs.rename);
|
|
48
|
-
const fileMakeDir = (0, util_1.promisify)(fs.mkdir);
|
|
49
|
-
const fileMakeTempDir = (0, util_1.promisify)(fs.mkdtemp);
|
|
50
|
-
const fileStat = (0, util_1.promisify)(fs.stat);
|
|
51
|
-
const fileUnlink = (0, util_1.promisify)(fs.unlink);
|
|
52
|
-
const fileCopy = (0, util_1.promisify)(fs.copyFile);
|
|
53
|
-
const DEFAULT_PUBLIC_STATIC_FILES_PATH = "/static/upload";
|
|
54
|
-
class FileManager extends DFWModule_1.default {
|
|
55
|
-
get PUBLIC_STATIC_FILES_PATH() {
|
|
56
|
-
return this.publicStaticFilesPath;
|
|
57
|
-
}
|
|
58
|
-
constructor(DFW) {
|
|
59
|
-
super(DFW);
|
|
60
|
-
this.LOCAL_PUBLIC_UPLOAD_DIR = ".dfw/upload/public";
|
|
61
|
-
this.LOCAL_PROTECTED_UPLOAD_DIR = ".dfw/upload/protected";
|
|
62
|
-
this.publicStaticFilesPath = DEFAULT_PUBLIC_STATIC_FILES_PATH;
|
|
63
|
-
this.middleware = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
req.dfw.FileManager = this;
|
|
65
|
-
});
|
|
66
|
-
if (DFW.config.upload && DFW.config.upload.tempDir) {
|
|
67
|
-
this.tmpDir = DFW.config.upload.tempDir;
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
this.tmpDir = ".dfw/temp";
|
|
71
|
-
}
|
|
72
|
-
if (fs.existsSync(this.tmpDir)) {
|
|
73
|
-
fs.rmSync(this.tmpDir, { recursive: true });
|
|
74
|
-
fs.mkdirSync(this.tmpDir);
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
fs.mkdirSync(this.tmpDir);
|
|
78
|
-
}
|
|
79
|
-
this.tmpDir = path.normalize(fs.mkdtempSync(`${this.tmpDir}${path.sep}`));
|
|
80
|
-
// Public static upload path
|
|
81
|
-
DFW.APIManager.server.use(this.publicStaticFilesPath, express_1.default.static(this.LOCAL_PUBLIC_UPLOAD_DIR, { maxAge: 2592000 }));
|
|
82
|
-
// Clear expired files each 6 hours
|
|
83
|
-
/*
|
|
84
|
-
setInterval(() => {
|
|
85
|
-
this.sweepExpiredFilesAsync();
|
|
86
|
-
}, 21600000);
|
|
87
|
-
*/
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Takes a local tmp file and move to the files on DFW folder and save db record
|
|
91
|
-
*/
|
|
92
|
-
assignLocalFileAsync(filePath, cfg, moveLocalFile = false) {
|
|
93
|
-
var _a;
|
|
94
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
-
if ((yield fileExistsAsync(filePath)) == false) {
|
|
96
|
-
throw `Process uploaded file async error, unable to find file ${filePath}`;
|
|
97
|
-
}
|
|
98
|
-
let checksum = yield (0, md5_file_1.default)(filePath);
|
|
99
|
-
let stats = yield fileStat(filePath);
|
|
100
|
-
let filename = `${DFWUtils_1.default.uuid()}.${(_a = cfg.ext) !== null && _a !== void 0 ? _a : DFWUtils_1.default.getFilenameExtension(filePath)}`;
|
|
101
|
-
let mimetype = DFWUtils_1.default.getFileMimetype(filename);
|
|
102
|
-
let partialPath = `${cfg.protected ? this.LOCAL_PROTECTED_UPLOAD_DIR : this.LOCAL_PUBLIC_UPLOAD_DIR}/${luxon_1.DateTime.now().toFormat("y/MM")}`;
|
|
103
|
-
let expire = cfg.expiration ? cfg.expiration : null;
|
|
104
|
-
let finalFilePath = `${partialPath}/${filename}`;
|
|
105
|
-
let description = cfg.description;
|
|
106
|
-
let variant = cfg.variant;
|
|
107
|
-
let idParent = typeof cfg.parent == "object" ? cfg.parent.id : cfg.parent;
|
|
108
|
-
let idUser = typeof cfg.user === "object" ? cfg.user.id : cfg.user;
|
|
109
|
-
if ((yield fileExistsAsync(partialPath)) == false) {
|
|
110
|
-
yield fileMakeDir(partialPath, { recursive: true });
|
|
111
|
-
}
|
|
112
|
-
if (moveLocalFile) {
|
|
113
|
-
yield fileRenameAsync(filePath, finalFilePath).catch((e) => {
|
|
114
|
-
throw new Error("Process uploaded file async error, unable to move file uploaded: " + e);
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
yield fileCopy(filePath, finalFilePath).catch((e) => {
|
|
119
|
-
throw new Error("Process uploaded file async error, unable to copy file uploaded: " + e);
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
// Removing same variant if has variant and parent
|
|
123
|
-
if (cfg.parent && cfg.variant) {
|
|
124
|
-
let variantFiles = yield this.db.dfw_file.findMany({
|
|
125
|
-
where: {
|
|
126
|
-
idParent,
|
|
127
|
-
variant,
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
for (let vFile of variantFiles) {
|
|
131
|
-
yield this.removeFileAsync(vFile);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return this.db.dfw_file.create({
|
|
135
|
-
data: {
|
|
136
|
-
idParent,
|
|
137
|
-
mimetype,
|
|
138
|
-
checksum,
|
|
139
|
-
expire,
|
|
140
|
-
variant,
|
|
141
|
-
description,
|
|
142
|
-
idUser,
|
|
143
|
-
path: finalFilePath,
|
|
144
|
-
slug: cfg.slug,
|
|
145
|
-
size: stats.size,
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
*
|
|
152
|
-
* @param bodyFileName
|
|
153
|
-
* @param cfg
|
|
154
|
-
*/
|
|
155
|
-
flushUpload(req, file, cfg = {}) {
|
|
156
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
-
if (!req.files)
|
|
158
|
-
throw `DFW_ERROR_UPLOAD_ENPOINT_MUST_BE_ENABLED`;
|
|
159
|
-
if (!req.files[file])
|
|
160
|
-
throw `DFW_ERROR_UNABLE_TO_FOUND_FILE_UPLOAD_NAME`;
|
|
161
|
-
if (Array.isArray(!req.files[file])) {
|
|
162
|
-
return req.files[file].map((fileData, index) => __awaiter(this, void 0, void 0, function* () {
|
|
163
|
-
return yield this.assignLocalFileAsync(fileData.tempFilePath, Object.assign({
|
|
164
|
-
user: req.dfw.session.user,
|
|
165
|
-
ext: DFWUtils_1.default.getFilenameExtension(fileData.name)
|
|
166
|
-
}, cfg), true);
|
|
167
|
-
}));
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
return yield this.assignLocalFileAsync(req.files[file].tempFilePath, Object.assign({
|
|
171
|
-
user: req.dfw.session.user,
|
|
172
|
-
ext: DFWUtils_1.default.getFilenameExtension(req.files[file].name)
|
|
173
|
-
}, cfg), true);
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
*
|
|
179
|
-
* @param posfix
|
|
180
|
-
* @returns
|
|
181
|
-
*/
|
|
182
|
-
generateTempFileName(posfix) {
|
|
183
|
-
return path.join(this.tmpDir, `${DFWUtils_1.default.uuid()}${posfix ? `.${posfix}` : ""}`);
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Get an array of all childrens of the file in the tree (including 2 levels of nested files)
|
|
187
|
-
* @param file
|
|
188
|
-
*/
|
|
189
|
-
getChildrenFileIdsAsync(file) {
|
|
190
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
191
|
-
let queryRes = yield this.db.dfw_file.findMany({
|
|
192
|
-
select: {
|
|
193
|
-
id: true,
|
|
194
|
-
idParent: true,
|
|
195
|
-
children: true,
|
|
196
|
-
},
|
|
197
|
-
where: {
|
|
198
|
-
idParent: typeof file == "number" ? file : file.id
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
let res = [];
|
|
202
|
-
for (let f of queryRes) {
|
|
203
|
-
res.push(f.id);
|
|
204
|
-
for (let fc of f.children) {
|
|
205
|
-
res.push(fc.id);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
return res.flat();
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
*
|
|
213
|
-
* @param file
|
|
214
|
-
*/
|
|
215
|
-
removeFileAsync(file) {
|
|
216
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
217
|
-
let ids = [typeof file == "object" ? file.id : file, ...yield this.getChildrenFileIdsAsync(file)];
|
|
218
|
-
let deletableFiles = yield this.db.dfw_file.findMany({
|
|
219
|
-
where: {
|
|
220
|
-
id: {
|
|
221
|
-
in: ids
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
yield this.db.dfw_file.deleteMany({
|
|
226
|
-
where: {
|
|
227
|
-
id: {
|
|
228
|
-
in: ids
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}).catch((e) => { throw new Error(`Unable to delete file records in DB ` + e); });
|
|
232
|
-
for (let file of deletableFiles) {
|
|
233
|
-
if (fs.existsSync(file.path)) {
|
|
234
|
-
yield fileUnlink(file.path).then(() => {
|
|
235
|
-
this.db.dfw_file.delete({
|
|
236
|
-
where: {
|
|
237
|
-
id: file.id
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
}).catch(() => { });
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
return deletableFiles;
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
*
|
|
248
|
-
*/
|
|
249
|
-
sweepExpiredFilesAsync() {
|
|
250
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
exports.default = FileManager;
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
const util_1 = require("util");
|
|
39
|
+
const DFWModule_1 = __importDefault(require("./DFWModule"));
|
|
40
|
+
const md5_file_1 = __importDefault(require("md5-file"));
|
|
41
|
+
const express_1 = __importDefault(require("express"));
|
|
42
|
+
const fs = __importStar(require("fs"));
|
|
43
|
+
const path = __importStar(require("path"));
|
|
44
|
+
const DFWUtils_1 = __importDefault(require("../DFWUtils"));
|
|
45
|
+
const luxon_1 = require("luxon");
|
|
46
|
+
const fileExistsAsync = (0, util_1.promisify)(fs.exists);
|
|
47
|
+
const fileRenameAsync = (0, util_1.promisify)(fs.rename);
|
|
48
|
+
const fileMakeDir = (0, util_1.promisify)(fs.mkdir);
|
|
49
|
+
const fileMakeTempDir = (0, util_1.promisify)(fs.mkdtemp);
|
|
50
|
+
const fileStat = (0, util_1.promisify)(fs.stat);
|
|
51
|
+
const fileUnlink = (0, util_1.promisify)(fs.unlink);
|
|
52
|
+
const fileCopy = (0, util_1.promisify)(fs.copyFile);
|
|
53
|
+
const DEFAULT_PUBLIC_STATIC_FILES_PATH = "/static/upload";
|
|
54
|
+
class FileManager extends DFWModule_1.default {
|
|
55
|
+
get PUBLIC_STATIC_FILES_PATH() {
|
|
56
|
+
return this.publicStaticFilesPath;
|
|
57
|
+
}
|
|
58
|
+
constructor(DFW) {
|
|
59
|
+
super(DFW);
|
|
60
|
+
this.LOCAL_PUBLIC_UPLOAD_DIR = ".dfw/upload/public";
|
|
61
|
+
this.LOCAL_PROTECTED_UPLOAD_DIR = ".dfw/upload/protected";
|
|
62
|
+
this.publicStaticFilesPath = DEFAULT_PUBLIC_STATIC_FILES_PATH;
|
|
63
|
+
this.middleware = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
req.dfw.FileManager = this;
|
|
65
|
+
});
|
|
66
|
+
if (DFW.config.upload && DFW.config.upload.tempDir) {
|
|
67
|
+
this.tmpDir = DFW.config.upload.tempDir;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
this.tmpDir = ".dfw/temp";
|
|
71
|
+
}
|
|
72
|
+
if (fs.existsSync(this.tmpDir)) {
|
|
73
|
+
fs.rmSync(this.tmpDir, { recursive: true });
|
|
74
|
+
fs.mkdirSync(this.tmpDir);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
fs.mkdirSync(this.tmpDir);
|
|
78
|
+
}
|
|
79
|
+
this.tmpDir = path.normalize(fs.mkdtempSync(`${this.tmpDir}${path.sep}`));
|
|
80
|
+
// Public static upload path
|
|
81
|
+
DFW.APIManager.server.use(this.publicStaticFilesPath, express_1.default.static(this.LOCAL_PUBLIC_UPLOAD_DIR, { maxAge: 2592000 }));
|
|
82
|
+
// Clear expired files each 6 hours
|
|
83
|
+
/*
|
|
84
|
+
setInterval(() => {
|
|
85
|
+
this.sweepExpiredFilesAsync();
|
|
86
|
+
}, 21600000);
|
|
87
|
+
*/
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Takes a local tmp file and move to the files on DFW folder and save db record
|
|
91
|
+
*/
|
|
92
|
+
assignLocalFileAsync(filePath, cfg, moveLocalFile = false) {
|
|
93
|
+
var _a;
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
if ((yield fileExistsAsync(filePath)) == false) {
|
|
96
|
+
throw `Process uploaded file async error, unable to find file ${filePath}`;
|
|
97
|
+
}
|
|
98
|
+
let checksum = yield (0, md5_file_1.default)(filePath);
|
|
99
|
+
let stats = yield fileStat(filePath);
|
|
100
|
+
let filename = `${DFWUtils_1.default.uuid()}.${(_a = cfg.ext) !== null && _a !== void 0 ? _a : DFWUtils_1.default.getFilenameExtension(filePath)}`;
|
|
101
|
+
let mimetype = DFWUtils_1.default.getFileMimetype(filename);
|
|
102
|
+
let partialPath = `${cfg.protected ? this.LOCAL_PROTECTED_UPLOAD_DIR : this.LOCAL_PUBLIC_UPLOAD_DIR}/${luxon_1.DateTime.now().toFormat("y/MM")}`;
|
|
103
|
+
let expire = cfg.expiration ? cfg.expiration : null;
|
|
104
|
+
let finalFilePath = `${partialPath}/${filename}`;
|
|
105
|
+
let description = cfg.description;
|
|
106
|
+
let variant = cfg.variant;
|
|
107
|
+
let idParent = typeof cfg.parent == "object" ? cfg.parent.id : cfg.parent;
|
|
108
|
+
let idUser = typeof cfg.user === "object" ? cfg.user.id : cfg.user;
|
|
109
|
+
if ((yield fileExistsAsync(partialPath)) == false) {
|
|
110
|
+
yield fileMakeDir(partialPath, { recursive: true });
|
|
111
|
+
}
|
|
112
|
+
if (moveLocalFile) {
|
|
113
|
+
yield fileRenameAsync(filePath, finalFilePath).catch((e) => {
|
|
114
|
+
throw new Error("Process uploaded file async error, unable to move file uploaded: " + e);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
yield fileCopy(filePath, finalFilePath).catch((e) => {
|
|
119
|
+
throw new Error("Process uploaded file async error, unable to copy file uploaded: " + e);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
// Removing same variant if has variant and parent
|
|
123
|
+
if (cfg.parent && cfg.variant) {
|
|
124
|
+
let variantFiles = yield this.db.dfw_file.findMany({
|
|
125
|
+
where: {
|
|
126
|
+
idParent,
|
|
127
|
+
variant,
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
for (let vFile of variantFiles) {
|
|
131
|
+
yield this.removeFileAsync(vFile);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return this.db.dfw_file.create({
|
|
135
|
+
data: {
|
|
136
|
+
idParent,
|
|
137
|
+
mimetype,
|
|
138
|
+
checksum,
|
|
139
|
+
expire,
|
|
140
|
+
variant,
|
|
141
|
+
description,
|
|
142
|
+
idUser,
|
|
143
|
+
path: finalFilePath,
|
|
144
|
+
slug: cfg.slug,
|
|
145
|
+
size: stats.size,
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
*
|
|
152
|
+
* @param bodyFileName
|
|
153
|
+
* @param cfg
|
|
154
|
+
*/
|
|
155
|
+
flushUpload(req, file, cfg = {}) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
if (!req.files)
|
|
158
|
+
throw `DFW_ERROR_UPLOAD_ENPOINT_MUST_BE_ENABLED`;
|
|
159
|
+
if (!req.files[file])
|
|
160
|
+
throw `DFW_ERROR_UNABLE_TO_FOUND_FILE_UPLOAD_NAME`;
|
|
161
|
+
if (Array.isArray(!req.files[file])) {
|
|
162
|
+
return req.files[file].map((fileData, index) => __awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
return yield this.assignLocalFileAsync(fileData.tempFilePath, Object.assign({
|
|
164
|
+
user: req.dfw.session.user,
|
|
165
|
+
ext: DFWUtils_1.default.getFilenameExtension(fileData.name)
|
|
166
|
+
}, cfg), true);
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
return yield this.assignLocalFileAsync(req.files[file].tempFilePath, Object.assign({
|
|
171
|
+
user: req.dfw.session.user,
|
|
172
|
+
ext: DFWUtils_1.default.getFilenameExtension(req.files[file].name)
|
|
173
|
+
}, cfg), true);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
*
|
|
179
|
+
* @param posfix
|
|
180
|
+
* @returns
|
|
181
|
+
*/
|
|
182
|
+
generateTempFileName(posfix) {
|
|
183
|
+
return path.join(this.tmpDir, `${DFWUtils_1.default.uuid()}${posfix ? `.${posfix}` : ""}`);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Get an array of all childrens of the file in the tree (including 2 levels of nested files)
|
|
187
|
+
* @param file
|
|
188
|
+
*/
|
|
189
|
+
getChildrenFileIdsAsync(file) {
|
|
190
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
191
|
+
let queryRes = yield this.db.dfw_file.findMany({
|
|
192
|
+
select: {
|
|
193
|
+
id: true,
|
|
194
|
+
idParent: true,
|
|
195
|
+
children: true,
|
|
196
|
+
},
|
|
197
|
+
where: {
|
|
198
|
+
idParent: typeof file == "number" ? file : file.id
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
let res = [];
|
|
202
|
+
for (let f of queryRes) {
|
|
203
|
+
res.push(f.id);
|
|
204
|
+
for (let fc of f.children) {
|
|
205
|
+
res.push(fc.id);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return res.flat();
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
*
|
|
213
|
+
* @param file
|
|
214
|
+
*/
|
|
215
|
+
removeFileAsync(file) {
|
|
216
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
217
|
+
let ids = [typeof file == "object" ? file.id : file, ...yield this.getChildrenFileIdsAsync(file)];
|
|
218
|
+
let deletableFiles = yield this.db.dfw_file.findMany({
|
|
219
|
+
where: {
|
|
220
|
+
id: {
|
|
221
|
+
in: ids
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
yield this.db.dfw_file.deleteMany({
|
|
226
|
+
where: {
|
|
227
|
+
id: {
|
|
228
|
+
in: ids
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}).catch((e) => { throw new Error(`Unable to delete file records in DB ` + e); });
|
|
232
|
+
for (let file of deletableFiles) {
|
|
233
|
+
if (fs.existsSync(file.path)) {
|
|
234
|
+
yield fileUnlink(file.path).then(() => {
|
|
235
|
+
this.db.dfw_file.delete({
|
|
236
|
+
where: {
|
|
237
|
+
id: file.id
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}).catch(() => { });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return deletableFiles;
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
*
|
|
248
|
+
*/
|
|
249
|
+
sweepExpiredFilesAsync() {
|
|
250
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
exports.default = FileManager;
|
|
255
255
|
//# sourceMappingURL=FileManager.js.map
|
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import { Request, Response } from "express";
|
|
2
|
-
import { DFWRequest } from "../types/DFWRequestScheme";
|
|
3
|
-
import { ListenerSecurityConfig } from "../types/APIListenerConfig";
|
|
4
|
-
import DFWModule from "./DFWModule";
|
|
5
|
-
import { dfw_access, dfw_credential, dfw_user } from "@prisma/client";
|
|
6
|
-
export type SecurityScheme = {
|
|
7
|
-
hasAccessAsync: (access: string | string[] | number | number[] | dfw_access | dfw_access[]) => Promise<boolean>;
|
|
8
|
-
hasCredentialsAsync: (credentials: string | string[] | number | number[] | dfw_credential | dfw_credential[]) => Promise<boolean>;
|
|
9
|
-
};
|
|
10
|
-
export default class SecurityManager extends DFWModule {
|
|
11
|
-
static readonly RULE_LOGGED_SESSION = 0;
|
|
12
|
-
static readonly RULE_ACCESS = 1;
|
|
13
|
-
static readonly RULE_CREDENTIAL = 2;
|
|
14
|
-
static readonly RULE_BODY_PARAMS_SETTED = 3;
|
|
15
|
-
static readonly RULE_QUERY_PARAMS_SETTED = 4;
|
|
16
|
-
static readonly RULE_LABELS: {
|
|
17
|
-
0: string;
|
|
18
|
-
1: string;
|
|
19
|
-
2: string;
|
|
20
|
-
3: string;
|
|
21
|
-
4: string;
|
|
22
|
-
};
|
|
23
|
-
middleware: (req: DFWRequest, res: Response) => Promise<void>;
|
|
24
|
-
/**
|
|
25
|
-
* Genera un array de security bindings a partir de un obejto ListenerSecurityConfig
|
|
26
|
-
* @param securityObject
|
|
27
|
-
*/
|
|
28
|
-
static jsonToBindings(securityObject: ListenerSecurityConfig): [number, any][];
|
|
29
|
-
static verifyPassword(encoded: string, test: string): boolean;
|
|
30
|
-
static encryptPassword(password: string): string;
|
|
31
|
-
/**
|
|
32
|
-
* Check all security bindings from a request
|
|
33
|
-
* @param req
|
|
34
|
-
* @param bindings
|
|
35
|
-
*/
|
|
36
|
-
checkBindingArrayAsync(req: DFWRequest, bindings: [number, any][]): Promise<boolean>;
|
|
37
|
-
/**
|
|
38
|
-
*
|
|
39
|
-
* @param req
|
|
40
|
-
* @param type
|
|
41
|
-
* @param value
|
|
42
|
-
*/
|
|
43
|
-
checkBindingAsync(req: DFWRequest, type: number, value: any | any[]): Promise<boolean>;
|
|
44
|
-
createCredentialAsync(name: string): Promise<dfw_credential>;
|
|
45
|
-
checkUserCredentialsAsync(user: dfw_user | null | undefined, credential: dfw_credential | dfw_credential[] | string | string[]): Promise<boolean>;
|
|
46
|
-
checkUserAccessAsync(user: dfw_user | null | undefined, access: dfw_access | dfw_access[] | string | string[]): Promise<boolean>;
|
|
47
|
-
addCredentialToAsync(user: number | dfw_user, credential: dfw_credential | number | string | any[]): any;
|
|
48
|
-
checkBodyParams(req: Request, params: string[]): boolean;
|
|
49
|
-
checkQueryParams(req: Request, params: string[]): boolean;
|
|
50
|
-
}
|
|
1
|
+
import { Request, Response } from "express";
|
|
2
|
+
import { DFWRequest } from "../types/DFWRequestScheme";
|
|
3
|
+
import { ListenerSecurityConfig } from "../types/APIListenerConfig";
|
|
4
|
+
import DFWModule from "./DFWModule";
|
|
5
|
+
import { dfw_access, dfw_credential, dfw_user } from "@prisma/client";
|
|
6
|
+
export type SecurityScheme = {
|
|
7
|
+
hasAccessAsync: (access: string | string[] | number | number[] | dfw_access | dfw_access[]) => Promise<boolean>;
|
|
8
|
+
hasCredentialsAsync: (credentials: string | string[] | number | number[] | dfw_credential | dfw_credential[]) => Promise<boolean>;
|
|
9
|
+
};
|
|
10
|
+
export default class SecurityManager extends DFWModule {
|
|
11
|
+
static readonly RULE_LOGGED_SESSION = 0;
|
|
12
|
+
static readonly RULE_ACCESS = 1;
|
|
13
|
+
static readonly RULE_CREDENTIAL = 2;
|
|
14
|
+
static readonly RULE_BODY_PARAMS_SETTED = 3;
|
|
15
|
+
static readonly RULE_QUERY_PARAMS_SETTED = 4;
|
|
16
|
+
static readonly RULE_LABELS: {
|
|
17
|
+
0: string;
|
|
18
|
+
1: string;
|
|
19
|
+
2: string;
|
|
20
|
+
3: string;
|
|
21
|
+
4: string;
|
|
22
|
+
};
|
|
23
|
+
middleware: (req: DFWRequest, res: Response) => Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Genera un array de security bindings a partir de un obejto ListenerSecurityConfig
|
|
26
|
+
* @param securityObject
|
|
27
|
+
*/
|
|
28
|
+
static jsonToBindings(securityObject: ListenerSecurityConfig): [number, any][];
|
|
29
|
+
static verifyPassword(encoded: string, test: string): boolean;
|
|
30
|
+
static encryptPassword(password: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Check all security bindings from a request
|
|
33
|
+
* @param req
|
|
34
|
+
* @param bindings
|
|
35
|
+
*/
|
|
36
|
+
checkBindingArrayAsync(req: DFWRequest, bindings: [number, any][]): Promise<boolean>;
|
|
37
|
+
/**
|
|
38
|
+
*
|
|
39
|
+
* @param req
|
|
40
|
+
* @param type
|
|
41
|
+
* @param value
|
|
42
|
+
*/
|
|
43
|
+
checkBindingAsync(req: DFWRequest, type: number, value: any | any[]): Promise<boolean>;
|
|
44
|
+
createCredentialAsync(name: string): Promise<dfw_credential>;
|
|
45
|
+
checkUserCredentialsAsync(user: dfw_user | null | undefined, credential: dfw_credential | dfw_credential[] | string | string[]): Promise<boolean>;
|
|
46
|
+
checkUserAccessAsync(user: dfw_user | null | undefined, access: dfw_access | dfw_access[] | string | string[]): Promise<boolean>;
|
|
47
|
+
addCredentialToAsync(user: number | dfw_user, credential: dfw_credential | number | string | any[]): any;
|
|
48
|
+
checkBodyParams(req: Request, params: string[]): boolean;
|
|
49
|
+
checkQueryParams(req: Request, params: string[]): boolean;
|
|
50
|
+
}
|
|
51
51
|
//# sourceMappingURL=SecurityManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SecurityManager.d.ts","sourceRoot":"","sources":["../../src/manager/SecurityManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,SAAS,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAEtE,MAAM,MAAM,cAAc,GAAG;IACzB,cAAc,EAAE,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,UAAU,GAAG,UAAU,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAC/G,mBAAmB,EAAE,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,cAAc,GAAG,cAAc,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CACpI,CAAA;AAED,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,SAAS;IAElD,MAAM,CAAC,QAAQ,CAAC,mBAAmB,KAAK;IACxC,MAAM,CAAC,QAAQ,CAAC,WAAW,KAAK;IAChC,MAAM,CAAC,QAAQ,CAAC,eAAe,KAAK;IACpC,MAAM,CAAC,QAAQ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