Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7566c7d

Browse files
committed
chore(code.angularjs): delete old zip files on snapshot
1 parent 02a19c0 commit 7566c7d

File tree

1 file changed

+36
-0
lines changed
  • scripts/code.angularjs.org-firebase/functions

1 file changed

+36
-0
lines changed

scripts/code.angularjs.org-firebase/functions/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,40 @@ function sendStoredFile(request, response) {
169169
}
170170
}
171171

172+
function deleteOldSnapshotZip(event) {
173+
const object = event.data;
174+
175+
const bucketId = object.bucket;
176+
const filePath = object.name;
177+
const contentType = object.contentType;
178+
const resourceState = object.resourceState;
179+
180+
const bucket = gcs.bucket(bucketId);
181+
182+
if (contentType !== 'application/zip' ||
183+
!filePath.startsWith('snapshot/') ||
184+
resourceState === 'not_exists' // Deletion event
185+
) {
186+
return;
187+
}
188+
189+
bucket.getFiles({
190+
prefix: 'snapshot/',
191+
delimiter: '/',
192+
autoPaginate: false
193+
}).then(function(data) {
194+
const files = data[0];
195+
196+
const oldZipFiles = files.filter(file => {
197+
return file.metadata.name !== filePath && file.metadata.contentType === 'application/zip';
198+
});
199+
200+
oldZipFiles.forEach(function(file) {
201+
file.delete();
202+
});
203+
204+
});
205+
}
206+
172207
exports.sendStoredFile = functions.https.onRequest(sendStoredFile);
208+
exports.deleteOldSnapshotZip = functions.storage.object().onChange(deleteOldSnapshotZip);

0 commit comments

Comments
 (0)