-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
390 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { FirestoreDrivers } from '@dereekb/firebase'; | ||
import { firestoreClientAccessorDriver } from './driver.accessor'; | ||
import { firestoreClientQueryDriver } from './driver.query'; | ||
import { googleCloudFirestoreAccessorDriver } from './driver.accessor'; | ||
import { googleCloudFirestoreQueryDriver } from './driver.query'; | ||
|
||
export type GoogleCloudFirestoreDrivers = FirestoreDrivers; | ||
|
||
export function googleCloudFirestoreDrivers(): GoogleCloudFirestoreDrivers { | ||
return { | ||
driverIdentifier: '@google-cloud/firestore', | ||
driverType: 'production', | ||
firestoreAccessorDriver: firestoreClientAccessorDriver(), | ||
firestoreQueryDriver: firestoreClientQueryDriver() | ||
firestoreAccessorDriver: googleCloudFirestoreAccessorDriver(), | ||
firestoreQueryDriver: googleCloudFirestoreQueryDriver() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/firebase-server/src/lib/storage/driver.accessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { FirebaseStorage, FirebaseStorageAccessorDriver, FirebaseStorageAccessorDriverGetDownloadUrlFunction, GoogleCloudStorageFilePath, StorageFilePath } from '@dereekb/firebase'; | ||
import { SlashPathFolder } from '@dereekb/util'; | ||
import { Storage as GoogleCloudStorage } from '@google-cloud/storage'; | ||
|
||
export function googleCloudStorageFileForStorageFilePath(storage: GoogleCloudStorage, path: StorageFilePath) { | ||
return storage.bucket(path.bucketId).file(path.pathString); | ||
} | ||
|
||
export function googleCloudStorageFirebaseStorageAccessorDriver(): FirebaseStorageAccessorDriver { | ||
return { | ||
getDownloadUrl: async (storage: FirebaseStorage, path: StorageFilePath) => googleCloudStorageFileForStorageFilePath(storage as GoogleCloudStorage, path).publicUrl() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { FirebaseStorageDrivers, FirestoreDrivers } from '@dereekb/firebase'; | ||
import { googleCloudStorageFirebaseStorageAccessorDriver } from './driver.accessor'; | ||
|
||
export type GoogleCloudFirebaseStorageDrivers = FirebaseStorageDrivers; | ||
|
||
export function googleCloudFirebaseStorageDrivers(): GoogleCloudFirebaseStorageDrivers { | ||
return { | ||
driverIdentifier: '@google-cloud/storage', | ||
driverType: 'production', | ||
storageAccessorDriver: googleCloudStorageFirebaseStorageAccessorDriver() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './driver'; | ||
export * from './driver.accessor'; | ||
export * from './storage.nest'; | ||
export * from './storage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as admin from 'firebase-admin'; | ||
import { InjectionToken, Module, ModuleMetadata, Provider } from '@nestjs/common'; | ||
import { ClassLikeType } from '@dereekb/util'; | ||
import { FIREBASE_APP_TOKEN } from '../firebase/firebase.nest'; | ||
import { googleCloudFirebaseStorageContextFactory } from './storage'; | ||
|
||
// MARK: Tokens | ||
/** | ||
* Token to access the Storage. | ||
*/ | ||
export const FIREBASE_STORAGE_TOKEN: InjectionToken = 'FIREBASE_STORAGE_TOKEN'; | ||
|
||
/** | ||
* Token to access the root StorageContext for a server. | ||
*/ | ||
export const FIREBASE_STORAGE_CONTEXT_TOKEN: InjectionToken = 'FIREBASE_STORAGE_CONTEXT_TOKEN'; | ||
|
||
/** | ||
* Nest provider module for Firebase that provides a firestore, etc. from the firestore token. | ||
*/ | ||
@Module({ | ||
providers: [ | ||
{ | ||
provide: FIREBASE_STORAGE_TOKEN, | ||
useFactory: (app: admin.app.App) => app.storage(), | ||
inject: [FIREBASE_APP_TOKEN] | ||
} | ||
], | ||
exports: [FIREBASE_STORAGE_TOKEN] | ||
}) | ||
export class FirebaseServerStorageModule {} | ||
|
||
/** | ||
* Nest provider module for firebase that includes the FirebaseServerStorageModule and provides a value for STORAGE_CONTEXT_TOKEN using the googleCloudStorageContextFactory. | ||
*/ | ||
@Module({ | ||
imports: [FirebaseServerStorageModule], | ||
providers: [ | ||
{ | ||
provide: FIREBASE_STORAGE_CONTEXT_TOKEN, | ||
useFactory: googleCloudFirebaseStorageContextFactory, | ||
inject: [FIREBASE_STORAGE_TOKEN] | ||
} | ||
], | ||
exports: [FirebaseServerStorageModule, FIREBASE_STORAGE_CONTEXT_TOKEN] | ||
}) | ||
export class FirebaseServerStorageContextModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { FirebaseStorageContextFactory, firebaseStorageContextFactory, FirestoreContextFactory, firestoreContextFactory } from '@dereekb/firebase'; | ||
import { googleCloudFirebaseStorageDrivers } from './driver'; | ||
|
||
/** | ||
* Creates a FirestoreContextFactory that uses the @google-cloud/storage package. | ||
*/ | ||
export const googleCloudFirebaseStorageContextFactory: FirebaseStorageContextFactory = firebaseStorageContextFactory(googleCloudFirebaseStorageDrivers()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './firestore'; | ||
export * from './function'; | ||
export * from './storage'; |
15 changes: 15 additions & 0 deletions
15
packages/firebase/src/lib/client/storage/driver.accessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { FirebaseStorageAccessorDriver, FirebaseStorageAccessorDriverGetDownloadUrlFunction } from '../../common/storage/driver/accessor'; | ||
import { getDownloadURL, FirebaseStorage as ClientFirebaseStorage, ref } from '@firebase/storage'; | ||
import { firebaseStorageFilePathFromStorageFilePath, GoogleCloudStorageFilePath, StorageBucketId, StorageBucketIdRef, StorageFilePath } from '../../common/storage/storage'; | ||
import { SlashPathFolder } from '@dereekb/util'; | ||
import { FirebaseStorage } from '../../common/storage/types'; | ||
|
||
export function firebaseStorageRefForStorageFilePath(storage: ClientFirebaseStorage, path: StorageFilePath) { | ||
return ref(storage, firebaseStorageFilePathFromStorageFilePath(path)); | ||
} | ||
|
||
export function firebaseStorageClientAccessorDriver(): FirebaseStorageAccessorDriver { | ||
return { | ||
getDownloadUrl: (storage: FirebaseStorage, path: StorageFilePath) => getDownloadURL(firebaseStorageRefForStorageFilePath(storage as ClientFirebaseStorage, path)) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { FirebaseStorageDrivers } from '../../common/storage/driver/driver'; | ||
import { firebaseStorageClientAccessorDriver } from './driver.accessor'; | ||
|
||
export type FirebaseStorageClientDrivers = FirebaseStorageDrivers; | ||
|
||
export function firebaseStorageClientDrivers(): FirebaseStorageClientDrivers { | ||
return { | ||
driverIdentifier: '@firebase/storage', | ||
driverType: 'production', | ||
storageAccessorDriver: firebaseStorageClientAccessorDriver() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './driver'; | ||
export * from './driver.accessor'; | ||
export * from './storage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { FirebaseStorageContextFactory, firebaseStorageContextFactory } from '../../common/storage/context'; | ||
import { firebaseStorageClientDrivers } from './driver'; | ||
|
||
/** | ||
* Creates a FirebaseStorageContextFactory that uses the client @firebase/storage package. | ||
*/ | ||
export const clientFirebaseStorageContextFactory: FirebaseStorageContextFactory = firebaseStorageContextFactory(firebaseStorageClientDrivers()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './path.model'; |
37 changes: 37 additions & 0 deletions
37
packages/firebase/src/lib/common/storage/accessor/path.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { SlashPath, slashPathFactory, SlashPathFolder, FactoryWithRequiredInput } from '@dereekb/util'; | ||
import { readFirestoreModelKey, ReadFirestoreModelKeyInput } from '../../firestore/collection/collection'; | ||
|
||
export const BASE_MODEL_STORAGE_FILE_PATH: SlashPathFolder = '/model/'; | ||
|
||
/** | ||
* Shared and configured slashPathFactory configuration for the model storage file path. | ||
*/ | ||
export const MODEL_STORAGE_FILE_SLASH_PATH_FACTORY = slashPathFactory({ startType: 'absolute', basePath: BASE_MODEL_STORAGE_FILE_PATH }); | ||
|
||
export interface ModelStorageSlashPathFactoryConfig { | ||
/** | ||
* Additional base path to provide. | ||
* | ||
* This value is merged with the BASE_MODEL_STORAGE_FILE_PATH (/model/) base path configured for all ModelStorageSlashPathFactory values | ||
*/ | ||
basePath?: string; | ||
} | ||
|
||
/** | ||
* Factory for SlashPath values using input ReadFirestoreModelKeyInput values. | ||
*/ | ||
export type ModelStorageSlashPathFactory<T extends object = object> = FactoryWithRequiredInput<SlashPath, ReadFirestoreModelKeyInput<T>>; | ||
|
||
/** | ||
* Creates a ModelStorageSlashPathFactory. | ||
* | ||
* @param config | ||
* @returns | ||
*/ | ||
export function modelStorageSlashPathFactory<T extends object = object>(config?: ModelStorageSlashPathFactoryConfig): ModelStorageSlashPathFactory<T> { | ||
const { basePath = '' } = config ?? {}; | ||
return (input: ReadFirestoreModelKeyInput) => { | ||
const key = readFirestoreModelKey(input, true); | ||
return MODEL_STORAGE_FILE_SLASH_PATH_FACTORY([basePath, key]); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { FirebaseStorageDrivers } from './driver/driver'; | ||
import { FirebaseStorage } from './types'; | ||
|
||
/** | ||
* A @dereekb/firebase FirebaseStorageContext. Wraps the main FirebaseStorage context and the drivers, as well as utility/convenience functions. | ||
*/ | ||
export interface FirebaseStorageContext<F extends FirebaseStorage = FirebaseStorage> { | ||
readonly storage: F; | ||
readonly drivers: FirebaseStorageDrivers; | ||
} | ||
|
||
/** | ||
* Factory function for generating a FirebaseStorageContext given the input FirebaseStorage. | ||
*/ | ||
export type FirebaseStorageContextFactory<F extends FirebaseStorage = FirebaseStorage> = (firebaseStorage: F) => FirebaseStorageContext; | ||
|
||
/** | ||
* Creates a new FirebaseStorageContextFactory given the input FirebaseStorageDrivers. | ||
* | ||
* @param drivers | ||
* @returns | ||
*/ | ||
export function firebaseStorageContextFactory<F extends FirebaseStorage = FirebaseStorage>(drivers: FirebaseStorageDrivers): FirebaseStorageContextFactory<F> { | ||
return (firebaseStorage: F) => { | ||
const context: FirebaseStorageContext<F> = { | ||
storage: firebaseStorage, | ||
drivers | ||
}; | ||
|
||
return context; | ||
}; | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/firebase/src/lib/common/storage/driver/accessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { GoogleCloudStorageFilePath, StorageFilePath } from '../storage'; | ||
import { FirebaseStorage, StorageDownloadUrl } from '../types'; | ||
|
||
export type FirebaseStorageAccessorDriverGetDownloadUrlFunction = (storage: FirebaseStorage, path: StorageFilePath) => Promise<StorageDownloadUrl>; | ||
|
||
/** | ||
* A driver to use for storage functionality. | ||
*/ | ||
export interface FirebaseStorageAccessorDriver { | ||
readonly getDownloadUrl: FirebaseStorageAccessorDriverGetDownloadUrlFunction; | ||
} | ||
|
||
/** | ||
* Ref to a StorageAccessorDriver. | ||
*/ | ||
export interface FirebaseStorageAccessorDriverRef { | ||
storageAccessorDriver: FirebaseStorageAccessorDriver; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { FirebaseStorageAccessorDriverRef } from './accessor'; | ||
|
||
export type FirebaseStorageDriverIdentifier = string; | ||
export type FirebaseStorageDriverType = 'production' | 'testing'; | ||
|
||
/** | ||
* Implements all FirebaseStorage related driver reference interfaces. | ||
*/ | ||
export interface FirebaseStorageDrivers extends FirebaseStorageAccessorDriverRef { | ||
driverIdentifier?: FirebaseStorageDriverIdentifier; | ||
driverType: FirebaseStorageDriverType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './accessor'; | ||
export * from './driver'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './accessor'; | ||
export * from './driver'; | ||
export * from './context'; | ||
export * from './storage'; | ||
export * from './types'; |
Oops, something went wrong.