-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.env.companion
executable file
·48 lines (39 loc) · 2.16 KB
/
.env.companion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env node
/**
* Custom key generator for Uppy Companion
* This file implements the /CLIENT/PROJECT/filename structure for S3 uploads
*
* File Structure:
* - CLIENT - The client's name or code (spaces replaced with underscores)
* - PROJECT - The project name (spaces replaced with underscores)
* - filename - The original filename with UUIDs stripped
*
* Example: "Client_Name/Project_Name/original_filename.mp4"
*/
// This function will be used by Companion to generate clean keys for S3 uploads
// It strips out UUIDs and organizes files by client and project folders
// Reference: https://uppy.io/docs/companion/#s3-storage
function stripUuid(req, filename) {
// Extract client and project info from request metadata
const clientName = req.body && req.body.metadata ? req.body.metadata.client : 'default';
const projectName = req.body && req.body.metadata ? req.body.metadata.project : 'default';
// Normalize the client code and project name (replace spaces with underscores)
// Exactly matching the backend s3Service.generateKey function logic
const normalizedClientCode = clientName ? clientName.replace(/\s+/g, '_') : 'default';
const normalizedProjectName = projectName ? projectName.replace(/\s+/g, '_') : 'default';
// Strip out the UUID pattern from the filename
// This regex matches UUIDs in standard formats: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx-
const filenameWithoutUuid = filename.replace(/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-)/gi, '');
// Ensure the filename is valid for S3 but preserve the original name as much as possible
// Only replace characters that are invalid for S3 keys
const safeName = filenameWithoutUuid.replace(/[\/\\:*?"<>|]/g, '_');
// Log the transformation for debugging
console.log(`Original filename: ${filename}`);
console.log(`Clean filename without UUID: ${safeName}`);
// Create the key using the /CLIENT/PROJECT/filename structure
// This ensures files are organized consistently in S3 storage
const key = `${normalizedClientCode}/${normalizedProjectName}/${safeName}`;
console.log(`Generated S3 key: ${key}`);
return key;
}
module.exports = { stripUuid };