@programisto/edrm-storage 1.0.6 → 1.0.9
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,4 +1,5 @@
|
|
|
1
1
|
import { EnduranceRouter } from '@programisto/endurance';
|
|
2
|
+
import { defaultConfig } from '../config/edrm-storage.config.js';
|
|
2
3
|
import { EdrmStorageService } from '../services/edrm-storage.service.js';
|
|
3
4
|
class EdrmStorageRouter extends EnduranceRouter {
|
|
4
5
|
storageService;
|
|
@@ -47,6 +48,9 @@ class EdrmStorageRouter extends EnduranceRouter {
|
|
|
47
48
|
* entityId:
|
|
48
49
|
* type: string
|
|
49
50
|
* description: Identifiant de l’entité métier associée
|
|
51
|
+
* public:
|
|
52
|
+
* type: boolean
|
|
53
|
+
* description: Si true, image publique accessible depuis n'importe quelle entité (portalEntityId non défini)
|
|
50
54
|
* provider:
|
|
51
55
|
* type: string
|
|
52
56
|
* enum: [S3]
|
|
@@ -70,14 +74,23 @@ class EdrmStorageRouter extends EnduranceRouter {
|
|
|
70
74
|
*/
|
|
71
75
|
this.post('/files/init', userOptions, async (req, res) => {
|
|
72
76
|
try {
|
|
73
|
-
const { originalName, mimeType, size, tenantId, entityName, entityId, provider = 'S3', metadata, tags } = req.body;
|
|
74
|
-
|
|
77
|
+
const { originalName, mimeType, size, tenantId, entityName, entityId, public: isPublic = false, provider = 'S3', metadata, tags } = req.body;
|
|
78
|
+
const isPublicFile = isPublic === true;
|
|
79
|
+
const effectiveEntityName = isPublicFile ? 'portal' : entityName;
|
|
80
|
+
const effectiveEntityId = isPublicFile ? 'all' : entityId;
|
|
81
|
+
if (!originalName || !mimeType || !size || !tenantId) {
|
|
75
82
|
return res.status(400).json({
|
|
76
83
|
success: false,
|
|
77
|
-
message: 'Paramètres manquants: originalName, mimeType, size, tenantId
|
|
84
|
+
message: 'Paramètres manquants: originalName, mimeType, size, tenantId'
|
|
78
85
|
});
|
|
79
86
|
}
|
|
80
|
-
|
|
87
|
+
if (!isPublicFile && (!entityName || !entityId)) {
|
|
88
|
+
return res.status(400).json({
|
|
89
|
+
success: false,
|
|
90
|
+
message: 'Paramètres manquants: entityName et entityId (ou public: true)'
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
const result = await this.storageService.initUpload(originalName, mimeType, size, tenantId, effectiveEntityName, effectiveEntityId, provider, metadata, tags, isPublicFile ? undefined : req.entity?._id?.toString());
|
|
81
94
|
return res.json({
|
|
82
95
|
success: true,
|
|
83
96
|
data: result
|
|
@@ -162,8 +175,14 @@ class EdrmStorageRouter extends EnduranceRouter {
|
|
|
162
175
|
this.get('/files/:fileId/download', userOptions, async (req, res) => {
|
|
163
176
|
try {
|
|
164
177
|
const { fileId } = req.params;
|
|
165
|
-
const { filename
|
|
166
|
-
const
|
|
178
|
+
const { filename } = req.query;
|
|
179
|
+
const defaultExp = defaultConfig.general.downloadExpiresIn;
|
|
180
|
+
const rawExp = req.query.expiresIn;
|
|
181
|
+
const parsedExp = rawExp != null && String(rawExp).trim() !== ''
|
|
182
|
+
? parseInt(String(rawExp), 10)
|
|
183
|
+
: defaultExp;
|
|
184
|
+
const expiresInSec = Number.isFinite(parsedExp) && parsedExp > 0 ? parsedExp : defaultExp;
|
|
185
|
+
const result = await this.storageService.getDownloadUrl(fileId, filename, expiresInSec, req.entity?._id?.toString());
|
|
167
186
|
return res.json({
|
|
168
187
|
success: true,
|
|
169
188
|
data: result
|
|
@@ -127,7 +127,8 @@ export class EdrmStorageService {
|
|
|
127
127
|
url: downloadResponse.url,
|
|
128
128
|
expiresAt: downloadResponse.expiresAt,
|
|
129
129
|
filename: downloadResponse.filename,
|
|
130
|
-
contentType: downloadResponse.contentType
|
|
130
|
+
contentType: fileRecord.mimeType || downloadResponse.contentType,
|
|
131
|
+
size: fileRecord.size
|
|
131
132
|
};
|
|
132
133
|
}
|
|
133
134
|
async deleteFile(fileId, portalEntityId) {
|