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

Commit 84061c2

Browse files
committed
chore(code.angularjs): delete old zip files on snapshot
1 parent 9936e57 commit 84061c2

File tree

1 file changed

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

1 file changed

+34
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,38 @@ function sendStoredFile(request, response) {
7272
}
7373
}
7474

75+
function deleteOldSnapshotZip(event) {
76+
const object = event.data;
77+
78+
const bucketId = object.bucket;
79+
const filePath = object.name;
80+
const contentType = object.contentType;
81+
82+
const bucket = gcs.bucket(bucketId);
83+
84+
if (event.eventType === 'providers/cloud.storage/eventTypes/object.change' &&
85+
contentType === 'application/zip' &&
86+
filePath.startsWith('snapshot/')
87+
) {
88+
89+
bucket.getFiles({
90+
prefix: 'snapshot/',
91+
delimiter: '/',
92+
autoPaginate: false
93+
}).then(function(data) {
94+
const files = data[0];
95+
96+
const oldZipFiles = files.filter(file => {
97+
return file.metadata.name !== filePath && file.metadata.contentType === 'application/zip';
98+
});
99+
100+
oldZipFiles.forEach(function(file) {
101+
file.delete();
102+
});
103+
104+
});
105+
}
106+
}
107+
75108
exports.sendStoredFile = functions.https.onRequest(sendStoredFile);
109+
exports.deleteOldSnapshotZip = functions.storage.object().onChange(deleteOldSnapshotZip);

0 commit comments

Comments
 (0)