-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuploadImages.js
More file actions
92 lines (77 loc) · 2.99 KB
/
uploadImages.js
File metadata and controls
92 lines (77 loc) · 2.99 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require('dotenv').config();
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
const fs = require('fs');
const path = require('path');
const mime = require('mime-types');
const s3Client = new S3Client({
region: 'auto',
endpoint: process.env.CLOUDFLARE_ENDPOINT,
credentials: {
accessKeyId: process.env.CLOUDFLARE_ACCESS_KEY,
secretAccessKey: process.env.CLOUDFLARE_SECRET_KEY
}
});
function moveFileToUploaded(filePath, relativePath) {
const uploadedFolder = path.join(__dirname, 'media', 'uploaded', 'public');
const destinationDir = path.join(uploadedFolder, relativePath);
const destinationPath = path.join(destinationDir, path.basename(filePath));
fs.mkdirSync(destinationDir, { recursive: true });
fs.renameSync(filePath, destinationPath);
};
async function uploadFile(filePath, fileKey, relativePath) {
const fileStream = fs.createReadStream(filePath);
const contentType = mime.lookup(filePath) || 'image/webp';
const uploadParams = {
Bucket: process.env.CLOUDFLARE_BUCKET_NAME,
Key: `public/${fileKey.replace(/\\/g, '/')}`,
Body: fileStream,
ContentType: contentType
};
try {
const command = new PutObjectCommand(uploadParams);
await s3Client.send(command);
moveFileToUploaded(filePath, relativePath);
console.log(`✅ Uploaded: ${fileKey}`);
} catch (error) {
console.error(`❌ Error uploading ${fileKey}:`, error);
throw error;
}
};
async function uploadDirectory(dirPath, s3Prefix, relativePath) {
const files = fs.readdirSync(dirPath);
const uploadPromises = [];
for (const file of files) {
const filePath = path.join(dirPath, file);
const s3Key = path.join(s3Prefix, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
uploadPromises.push(uploadDirectory(filePath, s3Key, path.join(relativePath, file)));
} else {
uploadPromises.push(uploadFile(filePath, s3Key, relativePath));
}
}
await Promise.all(uploadPromises);
};
async function uploadAll() {
try {
const rootFolder = path.join(__dirname, 'media', 'missing');
const foldersToUpload = ['cardimages', 'cardsquares', 'crops'];
const uploadPromises = [];
for (const folder of foldersToUpload) {
const folderPath = path.join(rootFolder, folder);
const s3Prefix = `${folder}`;
if (fs.existsSync(folderPath)) {
console.log(`📂 Uploading files from ${folder}...`);
uploadPromises.push(uploadDirectory(folderPath, s3Prefix, folder));
} else {
console.log(`⚠️ Folder ${folder} is not found.`);
}
}
await Promise.all(uploadPromises);
console.log('✅ All the images has been successfully uploaded');
} catch (error) {
console.error('❌ Error while uploading: ', error);
process.exit(1);
}
};
uploadAll();