Skip to content

Commit 3de3dbe

Browse files
thatfiredevlaurenzlong
authored andcommitted
[storage-resize-images] delete original image from bucket after resize (#6)
1 parent 8dbe121 commit 3de3dbe

File tree

7 files changed

+59
-2
lines changed

7 files changed

+59
-2
lines changed

storage-resize-images/extension.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ params:
115115
Invalid sizes, must be a comma-separated list of WIDTHxHEIGHT values.
116116
required: true
117117

118+
- param: DELETE_ORIGINAL_FILE
119+
type: select
120+
label: Deletion of original file
121+
description: >-
122+
Do you want to automatically delete the original file from the Cloud Storage
123+
bucket? Note that these deletions cannot be undone.
124+
options:
125+
- label: Yes
126+
value: true
127+
- label: No
128+
value: false
129+
default: false
130+
required: true
131+
118132
- param: RESIZED_IMAGES_PATH
119133
label: Cloud Storage path for resized images
120134
description: >

storage-resize-images/functions/lib/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ exports.default = {
2020
cacheControlHeader: process.env.CACHE_CONTROL_HEADER,
2121
imageSizes: process.env.IMG_SIZES.split(","),
2222
resizedImagesPath: process.env.RESIZED_IMAGES_PATH,
23+
deleteOriginalFile: process.env.DELETE_ORIGINAL_FILE,
2324
};

storage-resize-images/functions/lib/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ exports.generateResizedImage = functions.storage.object().onFinalize((object) =>
5959
const fileExtension = path.extname(filePath);
6060
const fileNameWithoutExtension = fileName.slice(0, -fileExtension.length);
6161
let originalFile;
62+
let remoteFile;
6263
try {
6364
originalFile = path.join(os.tmpdir(), filePath);
6465
const tempLocalDir = path.dirname(originalFile);
@@ -67,7 +68,7 @@ exports.generateResizedImage = functions.storage.object().onFinalize((object) =>
6768
yield mkdirp(tempLocalDir);
6869
logs.tempDirectoryCreated(tempLocalDir);
6970
// Download file from bucket.
70-
const remoteFile = bucket.file(filePath);
71+
remoteFile = bucket.file(filePath);
7172
logs.imageDownloading(filePath);
7273
yield remoteFile.download({ destination: originalFile });
7374
logs.imageDownloaded(filePath, originalFile);
@@ -102,6 +103,19 @@ exports.generateResizedImage = functions.storage.object().onFinalize((object) =>
102103
fs.unlinkSync(originalFile);
103104
logs.tempOriginalFileDeleted(filePath);
104105
}
106+
if (config_1.default.deleteOriginalFile) {
107+
// Delete the original file
108+
if (remoteFile) {
109+
try {
110+
logs.remoteFileDeleting(filePath);
111+
yield remoteFile.delete();
112+
logs.remoteFileDeleted(filePath);
113+
}
114+
catch (err) {
115+
logs.errorDeleting(err);
116+
}
117+
}
118+
}
105119
}
106120
}));
107121
const resizeImage = ({ bucket, originalFile, fileDir, fileNameWithoutExtension, fileExtension, contentType, size, }) => __awaiter(this, void 0, void 0, function* () {

storage-resize-images/functions/lib/logs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,9 @@ exports.tempResizedFileDeleted = (path) => {
7676
exports.tempResizedFileDeleting = (path) => {
7777
console.log(`Deleting temporary resized file: '${path}'`);
7878
};
79+
exports.remoteFileDeleted = (path) => {
80+
console.log(`Deleted original file from storage bucket: '${path}'`);
81+
};
82+
exports.remoteFileDeleting = (path) => {
83+
console.log(`Deleting original file from storage bucket: '${path}'`);
84+
};

storage-resize-images/functions/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ export default {
1919
cacheControlHeader: process.env.CACHE_CONTROL_HEADER,
2020
imageSizes: process.env.IMG_SIZES.split(","),
2121
resizedImagesPath: process.env.RESIZED_IMAGES_PATH,
22+
deleteOriginalFile: process.env.DELETE_ORIGINAL_FILE === "true",
2223
};

storage-resize-images/functions/src/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const generateResizedImage = functions.storage.object().onFinalize(
6565
const fileNameWithoutExtension = fileName.slice(0, -fileExtension.length);
6666

6767
let originalFile;
68+
let remoteFile;
6869
try {
6970
originalFile = path.join(os.tmpdir(), filePath);
7071
const tempLocalDir = path.dirname(originalFile);
@@ -75,7 +76,7 @@ export const generateResizedImage = functions.storage.object().onFinalize(
7576
logs.tempDirectoryCreated(tempLocalDir);
7677

7778
// Download file from bucket.
78-
const remoteFile = bucket.file(filePath);
79+
remoteFile = bucket.file(filePath);
7980
logs.imageDownloading(filePath);
8081
await remoteFile.download({ destination: originalFile });
8182
logs.imageDownloaded(filePath, originalFile);
@@ -113,6 +114,18 @@ export const generateResizedImage = functions.storage.object().onFinalize(
113114
fs.unlinkSync(originalFile);
114115
logs.tempOriginalFileDeleted(filePath);
115116
}
117+
if (config.deleteOriginalFile) {
118+
// Delete the original file
119+
if (remoteFile) {
120+
try {
121+
logs.remoteFileDeleting(filePath);
122+
await remoteFile.delete();
123+
logs.remoteFileDeleted(filePath);
124+
} catch (err) {
125+
logs.errorDeleting(err);
126+
}
127+
}
128+
}
116129
}
117130
}
118131
);

storage-resize-images/functions/src/logs.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,11 @@ export const tempResizedFileDeleted = (path: string) => {
9797
export const tempResizedFileDeleting = (path: string) => {
9898
console.log(`Deleting temporary resized file: '${path}'`);
9999
};
100+
101+
export const remoteFileDeleted = (path: string) => {
102+
console.log(`Deleted original file from storage bucket: '${path}'`);
103+
};
104+
105+
export const remoteFileDeleting = (path: string) => {
106+
console.log(`Deleting original file from storage bucket: '${path}'`);
107+
};

0 commit comments

Comments
 (0)