data-primals-engine 1.7.2 → 1.7.3
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/README.md +160 -160
- package/client/package-lock.json +8430 -8430
- package/client/src/APIInfo.jsx +1 -1
- package/client/src/App.jsx +2 -1
- package/client/src/App.scss +1635 -1626
- package/client/src/AssistantChat.jsx +1 -0
- package/client/src/CalendarView.jsx +1 -0
- package/client/src/ContentView.jsx +3 -3
- package/client/src/Dashboard.jsx +5 -2
- package/client/src/DashboardChart.jsx +1 -0
- package/client/src/DashboardFlexViewItem.jsx +1 -0
- package/client/src/DashboardHtmlViewItem.jsx +1 -0
- package/client/src/DashboardView.jsx +2 -0
- package/client/src/DataEditor.jsx +0 -1
- package/client/src/DataImporter.jsx +489 -468
- package/client/src/DataLayout.jsx +6 -3
- package/client/src/DataTable.jsx +4 -3
- package/client/src/Dialog.jsx +92 -90
- package/client/src/Dialog.scss +122 -116
- package/client/src/DisplayFlexNodeRenderer.jsx +1 -0
- package/client/src/FlexBuilderControls.jsx +1 -0
- package/client/src/FlexBuilderPreview.jsx +1 -1
- package/client/src/FlexNode.jsx +1 -1
- package/client/src/HistoryDialog.jsx +3 -2
- package/client/src/KPIWidget.jsx +1 -1
- package/client/src/KanbanView.jsx +1 -0
- package/client/src/ModelCreator.jsx +4 -0
- package/client/src/ModelList.jsx +1 -0
- package/client/src/PackGallery.jsx +5 -4
- package/client/src/RTETrans.jsx +1 -1
- package/client/src/RelationField.jsx +2 -0
- package/client/src/RelationSelectorWidget.jsx +2 -0
- package/client/src/RelationValue.jsx +1 -0
- package/client/src/RestoreDialog.jsx +1 -0
- package/client/src/WorkflowEditor.jsx +3 -0
- package/client/src/contexts/CommandContext.jsx +2 -1
- package/client/src/contexts/ModelContext.jsx +3 -3
- package/client/src/hooks/data.js +1 -0
- package/client/src/hooks/useValidation.js +1 -0
- package/client/src/translations.js +24 -24
- package/doc/AI-assistance.md +87 -63
- package/doc/Concepts.md +122 -0
- package/doc/Custom-Endpoints.md +31 -0
- package/doc/Event-system.md +13 -14
- package/doc/Home.md +33 -0
- package/doc/Modules.md +83 -0
- package/doc/Packs-gallery.md +8 -23
- package/doc/Workflows.md +32 -0
- package/doc/automation-workflows.md +141 -102
- package/doc/dashboards-kpis-charts.md +47 -49
- package/doc/data-management.md +126 -120
- package/doc/data-models.md +68 -75
- package/doc/roles-permissions.md +144 -43
- package/doc/sharding-replication.md +158 -0
- package/doc/users.md +54 -30
- package/package.json +1 -1
- package/server.js +37 -37
- package/src/constants.js +7 -8
- package/src/data.js +521 -520
- package/src/email.js +157 -154
- package/src/engine.js +117 -7
- package/src/gameObject.js +6 -0
- package/src/i18n.js +0 -1
- package/src/modules/auth-google/index.js +53 -50
- package/src/modules/bucket.js +346 -339
- package/src/modules/data/data.backup.js +400 -376
- package/src/modules/data/data.cluster.js +410 -42
- package/src/modules/data/data.operations.js +3666 -3635
- package/src/modules/data/data.replication.js +106 -64
- package/src/modules/data/data.routes.js +87 -64
- package/src/modules/data/data.scheduling.js +2 -1
- package/src/modules/file.js +248 -247
- package/src/modules/user.js +11 -1
- package/src/sso.js +91 -25
- package/test/cluster.test.js +221 -0
- package/test/core.test.js +0 -2
- package/test/replication.test.js +163 -0
- package/doc/core-concepts.md +0 -33
package/src/modules/file.js
CHANGED
|
@@ -1,248 +1,249 @@
|
|
|
1
|
-
|
|
2
|
-
// Nouvelle fonction pour ajouter un fichier privé
|
|
3
|
-
import {maxPrivateFileSize, megabytes} from "../constants.js";
|
|
4
|
-
import {isLocalUser} from "../data.js";
|
|
5
|
-
import i18n from "../../src/i18n.js";
|
|
6
|
-
import {getCollection} from "./mongodb.js";
|
|
7
|
-
import {getFileExtension, isGUID, uuidv4} from "../core.js";
|
|
8
|
-
import path from "node:path";
|
|
9
|
-
import process from "node:process";
|
|
10
|
-
import fs from "node:fs";
|
|
11
|
-
import { checkServerCapacity} from "./data/index.js";
|
|
12
|
-
import crypto from "node:crypto";
|
|
13
|
-
import * as tar from "tar";
|
|
14
|
-
import {promisify} from "node:util";
|
|
15
|
-
import {calculateTotalUserStorageUsage, hasPermission} from "./user.js";
|
|
16
|
-
import {Logger} from "../gameObject.js";
|
|
17
|
-
import {deleteFromS3, getUserS3Config, uploadToS3} from "./bucket.js";
|
|
18
|
-
import {Config} from "../config.js";
|
|
19
|
-
|
|
20
|
-
const pbkdf2Async = promisify(crypto.pbkdf2);
|
|
21
|
-
|
|
22
|
-
let engine, logger;
|
|
23
|
-
const fsPromises = fs.promises;
|
|
24
|
-
|
|
25
|
-
// Encryption settings
|
|
26
|
-
const algorithm = 'aes-256-cbc'; // Algorithm to use
|
|
27
|
-
const iterations = 100000; // Number of iterations for PBKDF2
|
|
28
|
-
const keyLength = 32; // Key length for AES-256
|
|
29
|
-
const ivLength = 16; // IV length for AES
|
|
30
|
-
|
|
31
|
-
// Function to derive a key and IV from a passphrase and salt
|
|
32
|
-
async function deriveKeyAndIV(password, salt) {
|
|
33
|
-
const key = await pbkdf2Async(password, salt, iterations, keyLength, 'sha256');
|
|
34
|
-
// L'IV n'est plus dérivé ici.
|
|
35
|
-
return { key };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
export const unzip = async (file) => {
|
|
40
|
-
await tar.extract({ file: file, gzip: true, sync: true });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const zip = async (filename) => {
|
|
44
|
-
await tar.create({ gzip: true, sync: true, file: filename+'.gz' }, [filename])
|
|
45
|
-
}
|
|
46
|
-
export const addFile = async (file, user) => {
|
|
47
|
-
if (!file) throw new Error("Le fichier est requis");
|
|
48
|
-
|
|
49
|
-
// Rendre compatible avec formidable v2 (path, name, type) et v3 (filepath, originalFilename, mimetype)
|
|
50
|
-
const filePath = file.path || file.filepath;
|
|
51
|
-
const originalName = file.name || file.originalFilename;
|
|
52
|
-
const mimeType = file.type || file.mimetype;
|
|
53
|
-
|
|
54
|
-
if (!filePath || !originalName) {
|
|
55
|
-
throw new Error("Les informations du fichier (nom, chemin) sont incomplètes.");
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const m = Config.Get('maxPrivateFileSize', maxPrivateFileSize);
|
|
59
|
-
if (file.size > m) {
|
|
60
|
-
throw new Error(`La taille du fichier dépasse la limite autorisée (${m / megabytes} Mo).`);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (user.username !== 'demo' && isLocalUser(user) && !await hasPermission(["API_ADMIN", "API_UPLOAD_FILE"], user)) {
|
|
64
|
-
throw new Error(i18n.t("api.permission.uploadFile"));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const incomingDataSize = file.size;
|
|
68
|
-
|
|
69
|
-
const userStorageLimit = await engine.userProvider.getUserStorageLimit(user);
|
|
70
|
-
const currentStorageUsage = await calculateTotalUserStorageUsage(user);
|
|
71
|
-
|
|
72
|
-
if (currentStorageUsage + incomingDataSize > userStorageLimit) {
|
|
73
|
-
throw new Error(i18n.t("api.data.storageLimitExceeded", { limit: Math.round(userStorageLimit / megabytes) }));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const serverCapacity = await checkServerCapacity(incomingDataSize);
|
|
77
|
-
if (!serverCapacity.isSufficient) {
|
|
78
|
-
throw new Error(i18n.t("api.data.serverStorageFull", "Le serveur a atteint sa capacité de stockage maximale. Veuillez réessayer plus tard."));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Générer un GUID pour le fichier
|
|
82
|
-
const guid = uuidv4();
|
|
83
|
-
const s3Config = await getUserS3Config(user);
|
|
84
|
-
const extension = getFileExtension(originalName);
|
|
85
|
-
const newFilename = `${guid}.${extension}`;
|
|
86
|
-
|
|
87
|
-
const fileData = {
|
|
88
|
-
guid: guid,
|
|
89
|
-
name: originalName, // Nom original du fichier
|
|
90
|
-
filename: newFilename, // Nom du fichier stocké (GUID + extension)
|
|
91
|
-
size: file.size,
|
|
92
|
-
mimeType: mimeType,
|
|
93
|
-
createdAt: new Date(),
|
|
94
|
-
user: user.username,
|
|
95
|
-
mainUser: user._user
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
if (s3Config && s3Config.bucketName && s3Config.accessKeyId && s3Config.secretAccessKey) {
|
|
99
|
-
try {
|
|
100
|
-
await uploadToS3(s3Config, filePath, newFilename);
|
|
101
|
-
|
|
102
|
-
fileData.storage = 's3';
|
|
103
|
-
logger.info(`Fichier ${newFilename} téléversé sur le bucket S3 ${s3Config.bucketName}.`);
|
|
104
|
-
} catch (error) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
*
|
|
144
|
-
* @
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (!
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
1
|
+
|
|
2
|
+
// Nouvelle fonction pour ajouter un fichier privé
|
|
3
|
+
import {maxPrivateFileSize, megabytes} from "../constants.js";
|
|
4
|
+
import {isLocalUser} from "../data.js";
|
|
5
|
+
import i18n from "../../src/i18n.js";
|
|
6
|
+
import {getCollection} from "./mongodb.js";
|
|
7
|
+
import {getFileExtension, isGUID, uuidv4} from "../core.js";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import process from "node:process";
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import { checkServerCapacity} from "./data/index.js";
|
|
12
|
+
import crypto from "node:crypto";
|
|
13
|
+
import * as tar from "tar";
|
|
14
|
+
import {promisify} from "node:util";
|
|
15
|
+
import {calculateTotalUserStorageUsage, hasPermission} from "./user.js";
|
|
16
|
+
import {Logger} from "../gameObject.js";
|
|
17
|
+
import {deleteFromS3, getUserS3Config, uploadToS3} from "./bucket.js";
|
|
18
|
+
import {Config} from "../config.js";
|
|
19
|
+
|
|
20
|
+
const pbkdf2Async = promisify(crypto.pbkdf2);
|
|
21
|
+
|
|
22
|
+
let engine, logger;
|
|
23
|
+
const fsPromises = fs.promises;
|
|
24
|
+
|
|
25
|
+
// Encryption settings
|
|
26
|
+
const algorithm = 'aes-256-cbc'; // Algorithm to use
|
|
27
|
+
const iterations = 100000; // Number of iterations for PBKDF2
|
|
28
|
+
const keyLength = 32; // Key length for AES-256
|
|
29
|
+
const ivLength = 16; // IV length for AES
|
|
30
|
+
|
|
31
|
+
// Function to derive a key and IV from a passphrase and salt
|
|
32
|
+
async function deriveKeyAndIV(password, salt) {
|
|
33
|
+
const key = await pbkdf2Async(password, salt, iterations, keyLength, 'sha256');
|
|
34
|
+
// L'IV n'est plus dérivé ici.
|
|
35
|
+
return { key };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
export const unzip = async (file) => {
|
|
40
|
+
await tar.extract({ file: file, gzip: true, sync: true });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const zip = async (filename) => {
|
|
44
|
+
await tar.create({ gzip: true, sync: true, file: filename+'.gz' }, [filename])
|
|
45
|
+
}
|
|
46
|
+
export const addFile = async (file, user) => {
|
|
47
|
+
if (!file) throw new Error("Le fichier est requis");
|
|
48
|
+
|
|
49
|
+
// Rendre compatible avec formidable v2 (path, name, type) et v3 (filepath, originalFilename, mimetype)
|
|
50
|
+
const filePath = file.path || file.filepath;
|
|
51
|
+
const originalName = file.name || file.originalFilename;
|
|
52
|
+
const mimeType = file.type || file.mimetype;
|
|
53
|
+
|
|
54
|
+
if (!filePath || !originalName) {
|
|
55
|
+
throw new Error("Les informations du fichier (nom, chemin) sont incomplètes.");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const m = Config.Get('maxPrivateFileSize', maxPrivateFileSize);
|
|
59
|
+
if (file.size > m) {
|
|
60
|
+
throw new Error(`La taille du fichier dépasse la limite autorisée (${m / megabytes} Mo).`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (user.username !== 'demo' && isLocalUser(user) && !await hasPermission(["API_ADMIN", "API_UPLOAD_FILE"], user)) {
|
|
64
|
+
throw new Error(i18n.t("api.permission.uploadFile"));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const incomingDataSize = file.size;
|
|
68
|
+
|
|
69
|
+
const userStorageLimit = await engine.userProvider.getUserStorageLimit(user);
|
|
70
|
+
const currentStorageUsage = await calculateTotalUserStorageUsage(user);
|
|
71
|
+
|
|
72
|
+
if (currentStorageUsage + incomingDataSize > userStorageLimit) {
|
|
73
|
+
throw new Error(i18n.t("api.data.storageLimitExceeded", { limit: Math.round(userStorageLimit / megabytes) }));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const serverCapacity = await checkServerCapacity(incomingDataSize);
|
|
77
|
+
if (!serverCapacity.isSufficient) {
|
|
78
|
+
throw new Error(i18n.t("api.data.serverStorageFull", "Le serveur a atteint sa capacité de stockage maximale. Veuillez réessayer plus tard."));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Générer un GUID pour le fichier
|
|
82
|
+
const guid = uuidv4();
|
|
83
|
+
const s3Config = await getUserS3Config(user);
|
|
84
|
+
const extension = getFileExtension(originalName);
|
|
85
|
+
const newFilename = `${guid}.${extension}`;
|
|
86
|
+
|
|
87
|
+
const fileData = {
|
|
88
|
+
guid: guid,
|
|
89
|
+
name: originalName, // Nom original du fichier
|
|
90
|
+
filename: newFilename, // Nom du fichier stocké (GUID + extension)
|
|
91
|
+
size: file.size,
|
|
92
|
+
mimeType: mimeType,
|
|
93
|
+
createdAt: new Date(),
|
|
94
|
+
user: user.username,
|
|
95
|
+
mainUser: user._user
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
if (s3Config && s3Config.bucketName && s3Config.accessKeyId && s3Config.secretAccessKey) {
|
|
99
|
+
try {
|
|
100
|
+
await uploadToS3(s3Config, filePath, newFilename);
|
|
101
|
+
|
|
102
|
+
fileData.storage = 's3';
|
|
103
|
+
logger.info(`Fichier ${newFilename} téléversé sur le bucket S3 ${s3Config.bucketName}.`);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
// Si le téléversement S3 échoue, on se rabat sur le stockage local
|
|
106
|
+
logger.error(`Le téléversement S3 a échoué pour ${originalName}: ${error.message}. Tentative de sauvegarde locale.`, error);
|
|
107
|
+
await saveLocally(filePath, newFilename, fileData, originalName);
|
|
108
|
+
} finally {
|
|
109
|
+
// Nettoyer le fichier temporaire uploadé par express-formidable
|
|
110
|
+
await fsPromises.unlink(filePath).catch(e => logger.warn(`Échec de la suppression du fichier temporaire ${filePath}: ${e.message}`));
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
// Si la configuration S3 est absente ou désactivée, on utilise le stockage local par défaut.
|
|
114
|
+
logger.info("Configuration S3 non trouvée ou désactivée. Utilisation du stockage local.");
|
|
115
|
+
await saveLocally(filePath, newFilename, fileData, originalName);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const filesCollection = await getCollection("files");
|
|
119
|
+
await filesCollection.insertOne(fileData);
|
|
120
|
+
|
|
121
|
+
return guid;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
async function saveLocally(sourcePath, newFilename, fileData, originalName) {
|
|
125
|
+
const uploadDir = path.join(process.cwd(), "uploads", "private");
|
|
126
|
+
if (!fs.existsSync(uploadDir)) {
|
|
127
|
+
fs.mkdirSync(uploadDir, { recursive: true });
|
|
128
|
+
}
|
|
129
|
+
const newPath = path.join(uploadDir, newFilename);
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
await fsPromises.copyFile(sourcePath, newPath);
|
|
133
|
+
fileData.storage = 'local';
|
|
134
|
+
fileData.path = newPath; // Le chemin complet pour les fichiers locaux
|
|
135
|
+
logger.info(`Fichier ${newFilename} sauvegardé localement dans ${newPath}.`);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
logger.error(`Le stockage du fichier local a échoué pour ${originalName}: ${error.message}`, error);
|
|
138
|
+
throw new Error("Le stockage du fichier local a échoué.");
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Récupère les métadonnées d'un fichier depuis la base de données.
|
|
144
|
+
* @param {string} guid - Le GUID du fichier.
|
|
145
|
+
* @returns {Promise<object|null>} L'objet de métadonnées du fichier ou null si non trouvé.
|
|
146
|
+
*/
|
|
147
|
+
export const getFile = async (guid) => {
|
|
148
|
+
const filesCollection = await getCollection("files");
|
|
149
|
+
return await filesCollection.findOne({ guid });
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const removeFile = async (guid, user) => {
|
|
153
|
+
if (!guid) return false;
|
|
154
|
+
if (!isGUID(guid)) throw new Error("Le GUID du fichier n'est pas valide.");
|
|
155
|
+
|
|
156
|
+
const fileData = await getFile(guid);
|
|
157
|
+
if (!fileData) {
|
|
158
|
+
logger.info(`Tentative de suppression d'un fichier inexistant avec le GUID : ${guid}`, 'warn');
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (fileData.storage === 's3') {
|
|
163
|
+
const s3Config = await getUserS3Config(user);
|
|
164
|
+
if (s3Config && s3Config.bucketName) { // Correction: bucketName au lieu de bucket
|
|
165
|
+
try {
|
|
166
|
+
await deleteFromS3(s3Config, fileData.filename);
|
|
167
|
+
logger.info(`Fichier ${fileData.filename} supprimé du bucket S3 ${s3Config.bucketName}.`);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
logger.info(`La suppression S3 a échoué pour ${fileData.filename}: ${error.message}`, 'error');
|
|
170
|
+
throw new Error("La suppression S3 a échoué.");
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
logger.info(`Configuration S3 non trouvée pour l'utilisateur, impossible de supprimer le fichier ${fileData.filename} de S3.`, 'error');
|
|
174
|
+
throw new Error("Configuration S3 non trouvée, impossible de supprimer le fichier.");
|
|
175
|
+
}
|
|
176
|
+
} else if (fileData.storage === 'local') {
|
|
177
|
+
try {
|
|
178
|
+
if (fileData.path && fs.existsSync(fileData.path)) {
|
|
179
|
+
await fsPromises.unlink(fileData.path);
|
|
180
|
+
logger.info(`Fichier local ${fileData.path} supprimé.`);
|
|
181
|
+
} else {
|
|
182
|
+
logger.info(`Fichier local non trouvé au chemin ${fileData.path}, mais suppression de l'enregistrement en BDD.`, 'warn');
|
|
183
|
+
}
|
|
184
|
+
} catch (error) {
|
|
185
|
+
logger.info(`La suppression du fichier local a échoué pour ${fileData.path}: ${error.message}`, 'error');
|
|
186
|
+
throw new Error("La suppression du fichier local a échoué.");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const filesCollection = await getCollection("files");
|
|
191
|
+
await filesCollection.deleteOne({ guid });
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
// Function to encrypt the file content
|
|
198
|
+
export async function encryptFile(filePath, password) {
|
|
199
|
+
try {
|
|
200
|
+
const salt = crypto.randomBytes(16);
|
|
201
|
+
const iv = crypto.randomBytes(ivLength);
|
|
202
|
+
|
|
203
|
+
// On ne dérive que la clé
|
|
204
|
+
const { key } = await deriveKeyAndIV(password, salt);
|
|
205
|
+
|
|
206
|
+
const fileData = await fs.promises.readFile(filePath);
|
|
207
|
+
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
|
208
|
+
const encryptedData = Buffer.concat([cipher.update(fileData), cipher.final()]);
|
|
209
|
+
|
|
210
|
+
// On it : [salt][iv][données chiffrées]
|
|
211
|
+
await fs.promises.writeFile(filePath, Buffer.concat([salt, iv, encryptedData]));
|
|
212
|
+
|
|
213
|
+
console.log('File encrypted successfully.', filePath);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
console.error('Error during encryption:', error.message);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// Function to decrypt the file content
|
|
219
|
+
export async function decryptFile(filePath, password) {
|
|
220
|
+
try {
|
|
221
|
+
const fileData = await fs.promises.readFile(filePath);
|
|
222
|
+
|
|
223
|
+
// Extraire le sel, l'IV et les données
|
|
224
|
+
const salt = fileData.slice(0, 16);
|
|
225
|
+
// NOUVEAU: Extraire l'IV qui suit le sel
|
|
226
|
+
const iv = fileData.slice(16, 16 + ivLength);
|
|
227
|
+
const encryptedData = fileData.slice(16 + ivLength);
|
|
228
|
+
|
|
229
|
+
// On dérive la même clé en utilisant le sel extrait
|
|
230
|
+
const { key } = await deriveKeyAndIV(password, salt);
|
|
231
|
+
|
|
232
|
+
// On utilise l'IV extrait pour le déchiffrement
|
|
233
|
+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|
234
|
+
const decryptedData = Buffer.concat([decipher.update(encryptedData), decipher.final()]);
|
|
235
|
+
|
|
236
|
+
await fs.promises.writeFile(filePath, decryptedData);
|
|
237
|
+
|
|
238
|
+
console.log('File decrypted successfully.');
|
|
239
|
+
} catch (error) {
|
|
240
|
+
console.error('Error during decryption:', error.message);
|
|
241
|
+
// Relancer l'erreur peut être utile pour que l'appelant sache que ça a échoué
|
|
242
|
+
throw new Error(`Decryption failed: ${error.message}`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export async function onInit(defaultEngine) {
|
|
247
|
+
engine = defaultEngine;
|
|
248
|
+
logger = engine.getComponent(Logger);
|
|
248
249
|
}
|
package/src/modules/user.js
CHANGED
|
@@ -68,7 +68,8 @@ export const middlewareAuthenticator = async (req, res, next) => {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
try {
|
|
71
|
-
|
|
71
|
+
const logger = engine.getComponent(Logger);
|
|
72
|
+
// 1. On demande au provider d'identifier l'utilisateur
|
|
72
73
|
await engine.userProvider.initiateUser(req);
|
|
73
74
|
|
|
74
75
|
// 2. On vérifie simplement si le provider a attaché un utilisateur
|
|
@@ -344,6 +345,15 @@ export async function getEnv(user){
|
|
|
344
345
|
return envObject;
|
|
345
346
|
}
|
|
346
347
|
|
|
348
|
+
export function getInternalSmtpConfig(){
|
|
349
|
+
const cfg = Config.Get('emailDefaultConfig', emailDefaultConfig);
|
|
350
|
+
cfg.host = process.env.SMTP_HOST || cfg.host;
|
|
351
|
+
cfg.port = process.env.SMTP_PORT || cfg.port;
|
|
352
|
+
cfg.user = process.env.SMTP_USER || cfg.user;
|
|
353
|
+
cfg.pass = process.env.SMTP_PASS || cfg.pass;
|
|
354
|
+
return cfg;
|
|
355
|
+
}
|
|
356
|
+
|
|
347
357
|
export async function getSmtpConfig(user) {
|
|
348
358
|
|
|
349
359
|
const cfg = Config.Get('emailDefaultConfig', emailDefaultConfig);
|