我是Node.js和Google Cloud Functions的新手。我想在图像上传到Firebase存储后生成缩略图并且效果很好。然而,我无法解决一个小问题
const thumbarray = [];
exports.generateThumbnail = functions.storage.object('{pushId}/ProductImages').onFinalize((object, context) => {
// [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 resourceState = object.resourceState; // The resourceState is 'exists' or 'not_exists' (for file/folder deletions).
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
const fileDir = path.dirname(filePath);
const fileName0 = path.basename(filePath);
var fileName = fileName0.split("*")[1];
var thumbname = fileName0.split("*")[2]; // thumbnail 1 or 2 or 3
thumbarray.push(thumbname);
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);
console.log("the file name is ", fileName);
console.log("this is the object ", object);
// [END eventAttributes]
// [START stopConditions]
// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith('image/')) {
console.log('This is not an image.');
return null;
}
// Exit if the image is already a thumbnail.
if (fileName.startsWith(THUMB_PREFIX)) {
console.log('Already a Thumbnail.');
return null;
}
// Download file from bucket.
const bucket = gcs.bucket(fileBucket);
const file = bucket.file(filePath);
const thumbFile = bucket.file(thumbFilePath);
const metadata = {
contentType: contentType,
};
// Create the temp directory where the storage file will be downloaded.
return mkdirp(tempLocalDir).then(() => {
// Download file from bucket.
return file.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], {
capture: ['stdout', 'stderr']
});
}).then(() => {
console.log('Thumbnail created at', tempLocalThumbFile);
// Uploading the Thumbnail.
return bucket.upload(tempLocalThumbFile, {
destination: thumbFilePath,
metadata: metadata
});
}).then(() => {
console.log('Thumbnail uploaded to Storage at', thumbFilePath);
// Once the image has been uploaded delete the local files to free up disk space.
fs.unlinkSync(tempLocalFile);
fs.unlinkSync(tempLocalThumbFile);
// Get the Signed URLs for the thumbnail and original image.
const config = {
action: 'read',
expires: '03-01-2500',
};
return Promise.all([
thumbFile.getSignedUrl(config),
file.getSignedUrl(config),
]);
}).then((results) => {
console.log('Got Signed URLs.');
// var thumbname = fileName0.split("*")[2];
const thumbname = thumbarray[0];
console.log('the thumbname from array is ...', thumbname);
const thumbResult = results[0];
const originalResult = results[1];
const thumbFileUrl = thumbResult[0];
const fileUrl = originalResult[0];
// Add the URLs to the Database
return admin.database().ref('Listings/' + fileName).update({
path: fileUrl,
thumbname: thumbFileUrl // I want thumbname to be thumbname 1 or 2 or 3
});
}).then(() => console.log('Thumbnail URLs saved to database.'));
});
我在代码前面定义了thumbname
var thumbname = fileName0.split("*")[2]; // thumbnail 1 or 2 or 3
我想在稍后的代码末尾调用它
thumbname: thumbFileUrl // I want thumbname to be thumbname 1 or 2 or 3
但它没有得到正确的变量(即缩略图1),而是将thumbname读作字符串而不是变量。如何在此处调用变量?任何帮助都将得到满足