如何使用云功能通过签名的下载URL删除存储图像?

时间:2018-01-08 17:30:38

标签: firebase firebase-storage google-cloud-functions google-cloud-firestore

我正在使用firebase云功能生成缩略图并将已签名的图片网址存储在firestore中:

'use strict';

const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')({keyFilename: 'service-account-credentials.json'});
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');

const THUMB_MAX_HEIGHT = 200;
const THUMB_MAX_WIDTH = 200;
const THUMB_PREFIX = 'thumb_';

exports.onUploadImage = functions.storage.object().onChange(async event => {
    const filePath = event.data.name;
    const contentType = event.data.contentType;
    const fileDir = path.dirname(filePath);
    const fileName = path.basename(filePath);
    const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX}${fileName}`));
    const tempLocalFile = path.join(os.tmpdir(), filePath);
    const tempLocalDir = path.dirname(tempLocalFile);
    const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

    if (!contentType.startsWith('image/')) {
        return null;
    }
    if (fileName.startsWith(THUMB_PREFIX)) {
        return null;
    }
    if (event.data.resourceState === 'not_exists') {
        return null;
    }

    const tankId = event.data.metadata.tankId;
    const userId = event.data.metadata.userId;
    const imageType = event.data.metadata.type;

    const bucket = gcs.bucket(event.data.bucket);
    const file = bucket.file(filePath);
    const thumbFile = bucket.file(thumbFilePath);
    
    const metadata = {
        contentType: contentType,
        customMetadata: {
            'type': imageType
        }
    };

    try {
        await mkdirp(tempLocalDir); 
        await file.download({destination: tempLocalFile}); 
        await spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile], {capture: ['stdout', 'stderr']});
        await bucket.upload(tempLocalThumbFile, { destination: thumbFilePath, metadata: metadata });
        await fs.unlinkSync(tempLocalFile);
        await fs.unlinkSync(tempLocalThumbFile);

        const config = {
            action: 'read',
            expires: '03-01-2500'
        };

        const results = await Promise.all([
            thumbFile.getSignedUrl(config),
            file.getSignedUrl(config)
        ]);

        const thumbResult = results[0];
        const originalResult = results[1];
        const thumbFileUrl = thumbResult[0];
        const fileUrl = originalResult[0];

        const tankRef = admin.firestore().collection('tanks').doc(tankId);

        switch(imageType) {
            case 'gallery':
                await tankRef
                    .collection('gallery')
                    .add({
                        url: fileUrl,
                        thumbnail: thumbFileUrl,
                        createdAt: new Date()
                    });

                const tankSnapshot = await tankRef.get();
                const tankData = await tankSnapshot.data();

                let galleryCount = tankData.galleryCount || 0;
                galleryCount += 1;

                if (galleryCount < 0) galleryCount = 0;

                return await tankRef.update({ galleryCount }, { merge: true });
            case 'tankImage':
                await tankRef.set({ image: fileUrl, image_thumb: thumbFileUrl }, { merge: true });
                return null;
            case 'profileImage':
                await admin.auth().updateUser(userId, { photoURL: thumbFileUrl });
                await admin.firestore()
                    .collection('users')
                    .doc(userId)
                    .set({image: fileUrl});
                return null;
            default:
                return null
        }
    }
    catch(err) {
        console.log(err);
    }
});

现在我正在尝试编写另一个云函数,在删除firestore db条目时删除存储文件:

exports.onGalleryImageDelete = functions.firestore
    .document('/tanks/{tankId}/gallery/{docId}')
    .onDelete(async event => {
        const deletedDoc = event.data.previous.data();
        const bucket = admin.storage().bucket();

        await bucket.file(deletedDoc.url).delete();  // this is wrong... no idea how to solve this
        await bucket.file(deletedDoc.thumbnail).delete();

        return await updateTankDocumentCounter(event, 'galleryCount', 'onDelete');
    });

此代码实际上不起作用并返回一些API错误。我如何从给定的签名下载网址中删除这些图片?

2 个答案:

答案 0 :(得分:0)

根据Doug Stevenson的评论,我将路径存储到db并最终得到了这个云函数:

&#13;
&#13;
exports.onGalleryImageDelete = functions.firestore
    .document('/tanks/{tankId}/gallery/{docId}')
    .onDelete(async event => {
        const deletedDoc = event.data.previous.data();
        const filePath = deletedDoc.path;
        const fileDir = path.dirname(filePath);
        const fileName = path.basename(filePath);
        const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX}${fileName}`));

        const bucket = admin.storage().bucket();

        return await Promise.all([
            await bucket.file(filePath).delete(),
            await bucket.file(thumbFilePath).delete(),
            updateTankDocumentCounter(event, 'galleryCount', 'onDelete')
        ]);
    });
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我也遇到了这个问题。结果返回一个存储所有隐藏信息的文档。 如果对Firebase有好处,其他人则非常糟糕。