exodus-framework 2.0.99996 → 2.0.99998
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/controllers/api/file.d.ts +1 -0
- package/lib/controllers/api/file.d.ts.map +1 -1
- package/lib/controllers/api/file.js +29 -0
- package/lib/middlewares/file.js +1 -1
- package/lib/services/file/FileLibrary.d.ts +6 -1
- package/lib/services/file/FileLibrary.d.ts.map +1 -1
- package/lib/services/file/FileLibrary.js +14 -3
- package/lib/services/file/classes/FileCache.d.ts +3 -0
- package/lib/services/file/classes/FileCache.d.ts.map +1 -1
- package/lib/services/file/classes/FileCache.js +8 -0
- package/package.json +1 -1
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/controllers/api/file.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,sBAAsB,CAAC;
|
1
|
+
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/controllers/api/file.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,sBAAsB,CAAC;AAI9B,cAAM,cAAe,SAAQ,cAAc;IACzC,MAAM,EAAE,cAAc,CAWpB;IAEF,UAAU,EAAE,cAAc,CAgCxB;CACH;AAED,eAAe,cAAc,CAAC"}
|
@@ -7,6 +7,7 @@ exports.default = void 0;
|
|
7
7
|
var _controller = _interopRequireDefault(require("../../app/controller"));
|
8
8
|
require("express-async-errors");
|
9
9
|
var _http = require("../../contracts/http");
|
10
|
+
var _services = require("../../services");
|
10
11
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
11
12
|
// !Tratamento de erros asyncronos
|
12
13
|
|
@@ -20,5 +21,33 @@ class FileController extends _controller.default {
|
|
20
21
|
code: _http.EHttpResponseCode.created
|
21
22
|
}, res);
|
22
23
|
};
|
24
|
+
getByTitle = async (req, res) => {
|
25
|
+
const {
|
26
|
+
title
|
27
|
+
} = req.params;
|
28
|
+
const cached = _services.FileLibraryService.getService().getCachedFileByTitle(title);
|
29
|
+
if (cached) {
|
30
|
+
const stream = cached.getStream();
|
31
|
+
const mimeType = cached.getMimeType() ?? 'application/octet-stream';
|
32
|
+
if (stream) {
|
33
|
+
res.setHeader('Content-Type', mimeType);
|
34
|
+
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(title)}"`);
|
35
|
+
|
36
|
+
// Adiciona um listener para erro no stream
|
37
|
+
stream.on('error', err => {
|
38
|
+
console.error('Erro ao transmitir arquivo:', err);
|
39
|
+
res.status(500).end('Erro ao enviar o arquivo');
|
40
|
+
});
|
41
|
+
stream.pipe(res);
|
42
|
+
return;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
this.send({
|
46
|
+
message: 'Arquivo não encontrado',
|
47
|
+
type: 'error',
|
48
|
+
status: false,
|
49
|
+
code: _http.EHttpResponseCode.informationNotFound
|
50
|
+
}, res);
|
51
|
+
};
|
23
52
|
}
|
24
53
|
var _default = exports.default = FileController;
|
package/lib/middlewares/file.js
CHANGED
@@ -26,7 +26,7 @@ class FileMiddleware extends _controller.default {
|
|
26
26
|
},
|
27
27
|
filename: (req, file, cb) => {
|
28
28
|
// const name = Date.now() + path.extname(file.originalname);
|
29
|
-
const name = Date.now() + '_
|
29
|
+
const name = Date.now() + '_@_' + file.originalname;
|
30
30
|
cb(null, name);
|
31
31
|
}
|
32
32
|
});
|
@@ -1,18 +1,22 @@
|
|
1
|
+
import fs from 'fs-extra';
|
1
2
|
import NodeCache from 'node-cache';
|
2
3
|
import Service from '../../app/classes/service';
|
3
4
|
import MimeType from './classes/Mimetyp';
|
5
|
+
import FileCache from './classes/FileCache';
|
4
6
|
import { IMetaFile } from './contracts/meta';
|
5
7
|
declare class FileLibraryService extends Service {
|
6
8
|
protected cache: NodeCache;
|
7
9
|
protected path: string;
|
8
10
|
protected mime: MimeType;
|
11
|
+
protected mapedTitles: Map<string, string>;
|
9
12
|
onServiceInit(): Promise<void>;
|
10
13
|
onStart(): Promise<boolean>;
|
11
14
|
private loadLibrary;
|
12
15
|
private migratePublicPath;
|
13
16
|
getFilePath(md5: string): string;
|
14
17
|
getUrlPath(md5: string): Promise<string>;
|
15
|
-
|
18
|
+
getFileStream(md5: string): fs.ReadStream;
|
19
|
+
getCachedFileByTitle(title: string): FileCache;
|
16
20
|
deleteFile(md5: string): Promise<boolean>;
|
17
21
|
getFileMeta(md5: string): Promise<IMetaFile>;
|
18
22
|
saveAndRegisterFile(filePath: string, temporary?: boolean): Promise<string>;
|
@@ -20,6 +24,7 @@ declare class FileLibraryService extends Service {
|
|
20
24
|
getMD5FromFile(filePath: string): Promise<string>;
|
21
25
|
getMD5FromBuffer(buffer: Buffer): string;
|
22
26
|
isCached(md5: string): boolean;
|
27
|
+
getMime(): MimeType;
|
23
28
|
}
|
24
29
|
export default FileLibraryService;
|
25
30
|
//# sourceMappingURL=FileLibrary.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"FileLibrary.d.ts","sourceRoot":"","sources":["../../../src/services/file/FileLibrary.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"FileLibrary.d.ts","sourceRoot":"","sources":["../../../src/services/file/FileLibrary.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,SAAS,MAAM,YAAY,CAAC;AAEnC,OAAO,OAAO,MAAM,2BAA2B,CAAC;AAChD,OAAO,QAAQ,MAAM,mBAAmB,CAAC;AAGzC,OAAO,SAAS,MAAM,qBAAqB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,cAAM,kBAAmB,SAAQ,OAAO;IACtC,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC;IAC3B,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC;IAEzB,SAAS,CAAC,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE9B,aAAa;IASb,OAAO;YAMN,WAAW;YA+BX,iBAAiB;IAyBxB,WAAW,CAAC,GAAG,EAAE,MAAM;IAOjB,UAAU,CAAC,GAAG,EAAE,MAAM;IAO5B,aAAa,CAAC,GAAG,EAAE,MAAM;IAOzB,oBAAoB,CAAC,KAAK,EAAE,MAAM;IAO5B,UAAU,CAAC,GAAG,EAAE,MAAM;IAWtB,WAAW,CAAC,GAAG,EAAE,MAAM;IAWvB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,UAAQ;IAyCvD,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IA2B7E,cAAc,CAAC,QAAQ,EAAE,MAAM;IAW/B,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAMxC,QAAQ,CAAC,GAAG,EAAE,MAAM;IAIpB,OAAO;CAGf;AAED,eAAe,kBAAkB,CAAC"}
|
@@ -18,9 +18,11 @@ class FileLibraryService extends _service.default {
|
|
18
18
|
cache;
|
19
19
|
path;
|
20
20
|
mime;
|
21
|
+
mapedTitles;
|
21
22
|
async onServiceInit() {
|
22
23
|
this.cache = new _nodeCache.default();
|
23
24
|
this.mime = new _Mimetyp.default(_mimes.MimeStandardTypes, _mimes.MimeOtherTypes);
|
25
|
+
this.mapedTitles = new Map();
|
24
26
|
this.path = _path.default.resolve(_core.default.getSettings().getHttp().publicPath + '/library');
|
25
27
|
await _fsExtra.default.ensureDir(this.path);
|
26
28
|
}
|
@@ -49,6 +51,7 @@ class FileLibraryService extends _service.default {
|
|
49
51
|
continue;
|
50
52
|
}
|
51
53
|
this.cache.set(fileCache.getMD5(), fileCache);
|
54
|
+
this.mapedTitles.set(fileCache.getTitle(), fileCache.getMD5());
|
52
55
|
}
|
53
56
|
}
|
54
57
|
async migratePublicPath(target) {
|
@@ -80,10 +83,15 @@ class FileLibraryService extends _service.default {
|
|
80
83
|
if (!cachedFile) return null;
|
81
84
|
return cachedFile.getPublicUrl();
|
82
85
|
}
|
83
|
-
|
86
|
+
getFileStream(md5) {
|
84
87
|
const cachedFile = this.cache.get(md5);
|
85
88
|
if (!cachedFile) return null;
|
86
|
-
return cachedFile.
|
89
|
+
return cachedFile.getStream();
|
90
|
+
}
|
91
|
+
getCachedFileByTitle(title) {
|
92
|
+
const md5 = this.mapedTitles.get(title);
|
93
|
+
if (!md5) return null;
|
94
|
+
return this.cache.get(md5);
|
87
95
|
}
|
88
96
|
async deleteFile(md5) {
|
89
97
|
const cachedFile = this.cache.get(md5);
|
@@ -112,7 +120,7 @@ class FileLibraryService extends _service.default {
|
|
112
120
|
const metaFilePath = _path.default.join(this.path, `${md5}.meta`);
|
113
121
|
let originalName = _path.default.basename(filePath);
|
114
122
|
if (temporary) {
|
115
|
-
const split = originalName.split('_
|
123
|
+
const split = originalName.split('_@_');
|
116
124
|
originalName = split[split.length - 1];
|
117
125
|
}
|
118
126
|
await _fsExtra.default.move(filePath, newFilePath, {
|
@@ -176,5 +184,8 @@ class FileLibraryService extends _service.default {
|
|
176
184
|
isCached(md5) {
|
177
185
|
return this.cache.has(md5);
|
178
186
|
}
|
187
|
+
getMime() {
|
188
|
+
return this.mime;
|
189
|
+
}
|
179
190
|
}
|
180
191
|
var _default = exports.default = FileLibraryService;
|
@@ -1,4 +1,5 @@
|
|
1
1
|
import System from '../../../app/classes/system';
|
2
|
+
import fs from 'fs-extra';
|
2
3
|
import { IMetaFile } from '../contracts/meta';
|
3
4
|
declare class FileCache extends System {
|
4
5
|
metaPath: string;
|
@@ -18,6 +19,8 @@ declare class FileCache extends System {
|
|
18
19
|
getMetaFilePath(): string;
|
19
20
|
getMetadata(): IMetaFile;
|
20
21
|
getBuffer(): Promise<Buffer>;
|
22
|
+
getStream(): fs.ReadStream;
|
23
|
+
getMimeType(): string;
|
21
24
|
getCachedFileName(): string;
|
22
25
|
getPublicUrl(): string;
|
23
26
|
isValid(): boolean;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"FileCache.d.ts","sourceRoot":"","sources":["../../../../src/services/file/classes/FileCache.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,6BAA6B,CAAC;
|
1
|
+
{"version":3,"file":"FileCache.d.ts","sourceRoot":"","sources":["../../../../src/services/file/classes/FileCache.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,6BAA6B,CAAC;AACjD,OAAO,EAAE,MAAM,UAAU,CAAC;AAE1B,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAI9C,cAAM,SAAU,SAAQ,MAAM;IAKT,QAAQ,EAAE,MAAM;IAJnC,SAAS,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC9B,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC3B,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC;gBAEN,QAAQ,EAAE,MAAM;IAItB,MAAM;YAIL,QAAQ;YA4BR,YAAY;IAInB,MAAM;IAIN,QAAQ;IAIR,YAAY;IAIZ,OAAO;IAIP,WAAW;IAIX,WAAW;IAIX,eAAe;IAIf,WAAW;IAIX,SAAS;IAIT,SAAS;IAIT,WAAW;IAKX,iBAAiB;IAIjB,YAAY;IAIZ,OAAO;CAGf;AAED,eAAe,SAAS,CAAC"}
|
@@ -8,6 +8,7 @@ var _system = _interopRequireDefault(require("../../../app/classes/system"));
|
|
8
8
|
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
9
9
|
var _path = _interopRequireDefault(require("path"));
|
10
10
|
var _core = _interopRequireDefault(require("../../../app/core"));
|
11
|
+
var _FileLibrary = _interopRequireDefault(require("../FileLibrary"));
|
11
12
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
12
13
|
class FileCache extends _system.default {
|
13
14
|
metadata;
|
@@ -71,6 +72,13 @@ class FileCache extends _system.default {
|
|
71
72
|
getBuffer() {
|
72
73
|
return _fsExtra.default.readFile(this.filePath);
|
73
74
|
}
|
75
|
+
getStream() {
|
76
|
+
return _fsExtra.default.createReadStream(this.filePath);
|
77
|
+
}
|
78
|
+
getMimeType() {
|
79
|
+
const mime = _FileLibrary.default.getService().getMime();
|
80
|
+
return mime.getType(this.getFilePath());
|
81
|
+
}
|
74
82
|
getCachedFileName() {
|
75
83
|
return `${this.getMD5()}.${this.getExtension()}`;
|
76
84
|
}
|