Я создаю миниатюру из каждого изображения, которое загружаю в хранилище Firebase. Как я могу получить downloadURL вновь созданного эскиза и сохранить его в Firestore?
Спасибо за вашу помощь.
Путь в Firestore:
groups --> groupId --> smallImage
Пример кода, который я использую для создания миниатюр:
const functions = require("firebase-functions");
const { Storage } = require('@google-cloud/storage');
const projectId = 'xx'
let gcs = new Storage ({
projectId
});
const spawn = require("child-process-promise").spawn;
const path = require("path");
const os = require("os");
const fs = require('fs');
// [END import]
// [START generateThumbnail]
// [START generateThumbnailTrigger]
exports.generateThumbnail = functions.storage.object().onFinalize((object) => {
// [END generateThumbnailTrigger]
// [START eventAttributes]
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
const fileName = path.basename(filePath);
if (fileName.endsWith('_small')) {
console.log('Already a Thumbnail.');
return null;
}
// [START thumbnailGeneration]
// Download file from bucket.
const bucket = gcs.bucket(fileBucket);
const tempFilePath = path.join(os.tmpdir(), fileName);
const metadata = {
contentType: contentType,
};
return bucket.file(filePath).download({
destination: tempFilePath,
}).then(() => {
console.log('Image downloaded locally to', tempFilePath);
// Generate a thumbnail using ImageMagick.
return spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath]);
}).then(() => {
console.log('Thumbnail created at', tempFilePath);
const thumbFileName = path.basename(filePath) + '_small';
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
// Uploading the thumbnail.
return bucket.upload(tempFilePath, {
destination: thumbFilePath,
metadata: metadata,
});
// Once the thumbnail has been uploaded delete the local file to free up disk space.
}).then(() => fs.unlinkSync(tempFilePath));
// [END thumbnailGeneration]
});