Skip to content

Commit a770a24

Browse files
committed
Add create storage endpoint
1 parent 31ac581 commit a770a24

File tree

5 files changed

+11247
-23192
lines changed

5 files changed

+11247
-23192
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Node.js
22
node_modules
3-
yarn-error.log
43

54
/lib
65
/secrets
@@ -12,4 +11,7 @@ yarn-error.log
1211

1312
# Test projects
1413
/test/*/build
15-
/test/*/.firebaserc
14+
/test/*/.firebaserc
15+
16+
# Firebase
17+
firebase-debug.log

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@backupfire/firebase",
3-
"version": "1.3.0-beta.1",
3+
"version": "1.3.0-beta.6",
44
"description": "Backup Fire Firebase agent",
55
"keywords": [
66
"backup Firebase database",

src/storage/index.ts

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncMiddleware from '../_lib/asyncMiddleware'
22
import { Storage as CloudStorage, Bucket } from '@google-cloud/storage'
33

4-
export type StorageOptions = {
4+
export interface StorageOptions {
55
bucketsAllowlist?: string[]
66
}
77

@@ -14,34 +14,72 @@ export function storageListMiddleware({ bucketsAllowlist }: StorageOptions) {
1414
})
1515
}
1616

17+
interface StorageRetentionData {
18+
removeOldBackups: boolean
19+
keepBackupsValue: number
20+
keepBackupsUnit: KeepBackupsUnit
21+
}
22+
23+
type KeepBackupsUnit = 'years' | 'months' | 'days'
24+
25+
export interface CreateStorageRequestBody
26+
extends Partial<StorageRetentionData> {
27+
storageId: string
28+
location: string
29+
storageClass?: 'standard' | 'nearline' | 'coldline' | 'archive'
30+
}
31+
1732
export function createStorageMiddleware({ bucketsAllowlist }: StorageOptions) {
33+
const storage = new CloudStorage()
34+
1835
return asyncMiddleware(async (request, response) => {
19-
// TODO: Validate options
36+
const body = request.body as CreateStorageRequestBody
37+
38+
const [bucket] = await storage.createBucket(body.storageId, {
39+
[body.storageClass || 'nearline']: true,
40+
location: body.location
41+
})
42+
43+
if (
44+
body.removeOldBackups &&
45+
body.keepBackupsValue &&
46+
body.keepBackupsUnit
47+
) {
48+
const deleteAge = keepBackupInDays(
49+
body.keepBackupsValue,
50+
body.keepBackupsUnit
51+
)
2052

21-
// await backupUsers(options)
53+
if (body.removeOldBackups) {
54+
await bucket.addLifecycleRule(
55+
{
56+
action: { type: 'Delete' },
57+
condition: { age: deleteAge }
58+
},
59+
{ append: false }
60+
)
61+
} else {
62+
await bucket.setMetadata({ lifecycle: null })
63+
}
64+
}
2265

23-
response.send([])
66+
const [bucketData] = await bucket.get()
67+
response.send(bucketAsStorage(bucketData))
2468
})
2569
}
2670

27-
export type UpdateStorageOptions = {
28-
bucketsAllowlist?: string[]
71+
export interface PasswordedStorageOptions extends StorageOptions {
2972
adminPassword: string
3073
}
3174

32-
type UpdateStorageRequestBody = {
75+
interface UpdateStorageRequestBody extends StorageRetentionData {
3376
password: string
34-
removeOldBackups: boolean
35-
keepBackupsValue: number
36-
keepBackupsUnit: KeepBackupsUnit
3777
}
3878

39-
type KeepBackupsUnit = 'years' | 'months' | 'days'
40-
4179
export function updateStorageMiddleware({
4280
bucketsAllowlist,
4381
adminPassword
44-
}: UpdateStorageOptions) {
82+
}: PasswordedStorageOptions) {
4583
return asyncMiddleware(async (request, response) => {
4684
// TODO: Validate options
4785
const storageId = request.params.storageId as string
@@ -73,15 +111,10 @@ export function updateStorageMiddleware({
73111
}
74112

75113
const [bucketData] = await bucket.get()
76-
77114
response.send(bucketAsStorage(bucketData))
78115
})
79116
}
80117

81-
export interface ListFilesOptions {
82-
bucketsAllowlist?: string[]
83-
}
84-
85118
export interface ListFilesRequestQuery {
86119
path: string
87120
maxResults: string | undefined
@@ -90,12 +123,12 @@ export interface ListFilesRequestQuery {
90123
prefix: string | undefined
91124
}
92125

93-
export function listFilesMiddleware({ bucketsAllowlist }: ListFilesOptions) {
126+
export function listFilesMiddleware({ bucketsAllowlist }: StorageOptions) {
94127
const storage = new CloudStorage()
95128

96129
return asyncMiddleware(async (request, response) => {
97130
const storageId = request.params.storageId as string
98-
const query = request.body as ListFilesRequestQuery
131+
const query = (request.query as unknown) as ListFilesRequestQuery
99132

100133
const bucket = storage.bucket(storageId)
101134
const [files] = await bucket.getFiles({
@@ -163,7 +196,7 @@ export function keepBackupInDays(value: number, unit: KeepBackupsUnit) {
163196
}
164197
}
165198

166-
export type Storage = {
199+
export interface Storage {
167200
name: string
168201
id: string
169202
location: StorageLocation
@@ -175,7 +208,7 @@ export type Storage = {
175208
lifecycle: LifecycleRule[]
176209
}
177210

178-
type LifecycleRule = {
211+
interface LifecycleRule {
179212
action: { type: string; storageClass?: string } | string
180213
condition: { [key: string]: boolean | Date | number | string }
181214
storageClass?: string

0 commit comments

Comments
 (0)