Files
2024-09-22 21:45:37 +02:00

65 lines
1.9 KiBLFS
JavaScript

const admin = require("firebase-admin");
const fs = require("fs");
const path = require("path");
// Firebase service account configuration
const serviceAccount = require("./monde-en-carton-firebase-adminsdk-obu1h-67330da48c.json"); // Replace with your Firebase service account file path
// Initialize Firebase Admin SDK
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "gs://monde-en-carton.appspot.com", // Replace with your Firebase project ID
});
const bucket = admin.storage().bucket();
const root = "/Users/gabrielvidal/PROJECTS/monde-usage-unity/Assets/";
// Function to recursively get all files in a directory
const getFiles = (dir, fileList = []) => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = getFiles(path.join(dir, file), fileList);
} else {
// Exclude files that are not .mp4
if (!file.endsWith(".mp4")) return;
fileList.push(path.join(dir, file));
}
});
return fileList;
};
// Upload files to Firebase keeping folder structure
const uploadFile = async (filePath) => {
const destination = filePath.replace(root, ""); // Remove the root path to keep the folder structure
await bucket.upload(filePath, {
destination: destination,
metadata: {
cacheControl: "public, max-age=31536000",
},
});
console.log(`Uploaded ${filePath} to ${destination}`);
};
// Path to your local folder containing files
const localFolderPath = path.join(__dirname, "../../../Videos"); // Points to the 'Assets/Videos' folder
const files = getFiles(localFolderPath);
console.log(`Uploading ${files.length} files...`);
const uploadFiles = async () => {
for (let i = 0; i < files.length; i++) {
await uploadFile(files[i])
.then(() => console.log(`Successfully uploaded ${files[i]}`))
.catch((err) => console.error(`Failed to upload ${files[i]}:`, err));
}
};
uploadFiles();