-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
47 lines (37 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const functions = require('firebase-functions');
const path = require('path');
const admin = require('firebase-admin');
admin.initializeApp()
/**
* Limit the backup files up to 50.
* https://googleapis.dev/nodejs/storage/latest/index.html
*/
exports.limitBackup = functions.storage.object().onFinalize(async (object) => {
// backup/uid/test.txt
const filePath = object.name;
// test
const baseFileName = path.basename(filePath, path.extname(filePath));
// backup/uid
const fileDir = path.dirname(filePath);
const bucket = admin.storage().bucket(object.bucket);
const getFilesResult = await bucket.getFiles({ directory: fileDir });
const files = getFilesResult[0];
if (files.length > 50) {
var oldest;
var oldestTime = Number.MAX_VALUE;
for (const file of files) {
const getMetaDataResult = await bucket.file(file.name).getMetadata();
const metaData = getMetaDataResult[0]
const updated = metaData.updated;
const updatedDateMilli = Date.parse(updated);
if (updatedDateMilli < oldestTime) {
oldest = file;
oldestTime = updatedDateMilli;
}
}
if (oldest) {
await oldest.delete()
}
}
return null;
});