73 lines
2.4 KiB
JavaScript
73 lines
2.4 KiB
JavaScript
/**
|
|
* Import function triggers from their respective submodules:
|
|
*
|
|
* const {onCall} = require("firebase-functions/v2/https");
|
|
* const {onDocumentWritten} = require("firebase-functions/v2/firestore");
|
|
*
|
|
* See a full list of supported triggers at https://firebase.google.com/docs/functions
|
|
*/
|
|
|
|
const {onRequest} = require("firebase-functions/v2/https");
|
|
const logger = require("firebase-functions/logger");
|
|
|
|
// Create and deploy your first functions
|
|
// https://firebase.google.com/docs/functions/get-started
|
|
|
|
// exports.helloWorld = onRequest((request, response) => {
|
|
// logger.info("Hello logs!", {structuredData: true});
|
|
// response.send("Hello from Firebase!");
|
|
// });
|
|
|
|
const functions = require('firebase-functions');
|
|
const admin = require('firebase-admin');
|
|
const { Storage } = require('@google-cloud/storage');
|
|
admin.initializeApp();
|
|
const storage = new Storage();
|
|
|
|
// Nouvelle version HTTP sécurisée
|
|
exports.moveEventFileV2 = functions.https.onRequest(async (req, res) => {
|
|
// Ajout des headers CORS
|
|
res.set('Access-Control-Allow-Origin', '*');
|
|
res.set('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
if (req.method === 'OPTIONS') {
|
|
res.set('Access-Control-Allow-Methods', 'POST, OPTIONS');
|
|
res.status(204).send('');
|
|
return;
|
|
}
|
|
// Vérification du token Firebase dans l'en-tête Authorization
|
|
let uid = null;
|
|
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
|
|
const idToken = req.headers.authorization.split('Bearer ')[1];
|
|
try {
|
|
const decodedToken = await admin.auth().verifyIdToken(idToken);
|
|
uid = decodedToken.uid;
|
|
} catch (e) {
|
|
res.status(401).json({ error: 'Invalid token' });
|
|
return;
|
|
}
|
|
} else {
|
|
res.status(401).json({ error: 'No token provided' });
|
|
return;
|
|
}
|
|
|
|
const { sourcePath, destinationPath } = req.body.data || {};
|
|
if (!sourcePath || !destinationPath) {
|
|
res.status(400).json({ error: 'Source and destination paths are required.' });
|
|
return;
|
|
}
|
|
|
|
const bucketName = admin.storage().bucket().name;
|
|
const bucket = storage.bucket(bucketName);
|
|
|
|
try {
|
|
await bucket.file(sourcePath).copy(bucket.file(destinationPath));
|
|
await bucket.file(sourcePath).delete();
|
|
const [url] = await bucket.file(destinationPath).getSignedUrl({
|
|
action: 'read',
|
|
expires: '03-01-2500',
|
|
});
|
|
res.status(200).json({ url });
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
}); |