Ich versuche, die Profilbildfunktion in meiner Android-App zu implementieren. Also habe ich Generate Thumbnail Probe von Firebase verwendet. Wenn ich also ein Bild in voller Größe hochlade, wird es als Miniaturbild für mich generiert. Aber ich möchte die URL des Thumbnails in meiner Echtzeitdatenbank aktualisieren, sobald das Thumbnail erstellt wurde.Aktualisieren der Firebase-Datenbank zu Speicheränderungen mithilfe von Firebase-Funktionen
const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const spawn = require('child-process-promise').spawn;
const LOCAL_TMP_FOLDER = '/tmp/';
// Max height and width of the thumbnail in pixels.
const THUMB_MAX_HEIGHT = 100;
const THUMB_MAX_WIDTH = 100;
// Thumbnail prefix added to file names.
const THUMB_PREFIX = 'thumb_';
/**
* When an image is uploaded in the Storage bucket We generate a thumbnail automatically using
* ImageMagick.
*/
exports.generateThumbnail = functions.storage.object().onChange(event => {
const filePath = event.data.name;
const filePathSplit = filePath.split('/');
const fileName = filePathSplit.pop();
const fileDir = filePathSplit.join('/') + (filePathSplit.length > 0 ? '/' : '');
const thumbFilePath = `${fileDir}${THUMB_PREFIX}${fileName}`;
const tempLocalDir = `${LOCAL_TMP_FOLDER}${fileDir}`;
const tempLocalFile = `${tempLocalDir}${fileName}`;
const tempLocalThumbFile = `${LOCAL_TMP_FOLDER}${thumbFilePath}`;
// Exit if this is triggered on a file that is not an image.
if (!event.data.contentType.startsWith('image/')) {
console.log('This is not an image.');
return;
}
// Exit if the image is already a thumbnail.
if (fileName.startsWith(THUMB_PREFIX)) {
console.log('Already a Thumbnail.');
return;
}
// Exit if this is a move or deletion event.
if (event.data.resourceState === 'not_exists') {
console.log('This is a deletion event.');
return;
}
// Create the temp directory where the storage file will be downloaded.
return mkdirp(tempLocalDir).then(() => {
// Download file from bucket.
const bucket = gcs.bucket(event.data.bucket);
return bucket.file(filePath).download({
destination: tempLocalFile
}).then(() => {
console.log('The file has been downloaded to', tempLocalFile);
// Generate a thumbnail using ImageMagick.
return spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile]).then(() => {
console.log('Thumbnail created at', tempLocalThumbFile);
// Uploading the Thumbnail.
return bucket.upload(tempLocalThumbFile, {
destination: thumbFilePath
}).then(() => {
console.log('Thumbnail uploaded to Storage at', thumbFilePath);
// Don't know what to write here.
});
});
});
});
});
Sobald die endgültige Zusage dieses Codes abgeschlossen ist, möchte ich aus dem generierten Profil pic die Download-URL nehmen bei /users/{userId}/profilePic
Danke für Ihre Antwort. Aber können Sie mir helfen, die herunterladbare URL anstelle von thumbFilePath zu bekommen. Da ich in Db speichern und es zum Speichern in meiner Benutzeroberfläche abrufen möchte, brauche ich herunterladbare URl. –
Es gibt eine Funktion speziell für die Rückgabe des Downloadref. 'databaseRef.child (thumbFilePath) .getDownloadURL(). Dann (function (url) {' Ich habe meine Antwort bearbeitet zeigt, was das wie –
getDownloadURL aussehen könnte() sollte von databaseRef oder storageRef genannt werden? –