import { Backend } from 'polar-shared/src/datastore/Backend' ; import { FilePaths } from 'polar-shared/src/util/FilePaths' ; import { Hashcodes } from 'polar-shared/src/util/Hashcodes' ; import { StoragePath } from './FirebaseDatastore' ; import { StorageSettings } from './FirebaseDatastore' ; import { FirebaseDocMetaID } from './FirebaseDatastore' ; import { Optional } from 'polar-shared/src/util/ts/Optional' ; import { CloudFunctions } from './firebase/CloudFunctions' ; import { Firebase , UserID } from '../firebase/Firebase' ; import firebase from 'firebase/app' ; import { Preconditions } from 'polar-shared/src/Preconditions' ; import { FileRef } from "polar-shared/src/datastore/FileRef" ; import { Logger } from "polar-shared/src/logger/Logger" ; import { UserIDStr } from "polar-firebase/src/firebase/om/Profiles" ; export class FirebaseDatastores { public static computeDatastoreGetFileURL ( request ) { const endpoint = CloudFunctions . createEndpoint ( ) ; return `${ endpoint }/datastoreGetFile/?data=` + encodeURIComponent ( JSON . stringify ( request ) ) ; } public static computeStoragePath ( backend , fileRef , uid ) { const ext = FilePaths . toExtension ( fileRef . name ) ; const suffix = ext . map ( value => { if ( ! value . startsWith ( '.' ) ) { value = '.' + value ; } return value ; } ) . getOrElse ( '' ) ; const settings = this . computeStorageSettings ( ext ) . getOrUndefined ( ) ; let key ; if ( fileRef . hashcode ) { key = { uid , backend , alg : fileRef . hashcode . alg , enc : fileRef . hashcode . enc , data : fileRef . hashcode . data , suffix } ; } else { key = { uid , filename : fileRef . name } ; } const id = Hashcodes . createID ( key , 40 ) ; const path = `${ backend }/${ id }${ suffix }` ; return { path , settings } ; } private static computeStorageSettings ( optionalExt ) { const PUBLIC_MAX_AGE_1WEEK = 'public,max-age=604800' ; const ext = optionalExt . getOrElse ( '' ) . toLowerCase ( ) ; if ( ext === 'jpg' || ext === 'jpeg' ) { return Optional . of ( { cacheControl : PUBLIC_MAX_AGE_1WEEK , contentType : 'image/jpeg' } ) ; } if ( ext === 'pdf' ) { return Optional . of ( { cacheControl : PUBLIC_MAX_AGE_1WEEK , contentType : 'application/pdf' } ) ; } if ( ext === 'png' ) { return Optional . of ( { cacheControl : PUBLIC_MAX_AGE_1WEEK , contentType : 'image/png' } ) ; } if ( ext === 'svg' ) { return Optional . of ( { cacheControl : PUBLIC_MAX_AGE_1WEEK , contentType : 'image/svg' } ) ; } return Optional . of ( { cacheControl : PUBLIC_MAX_AGE_1WEEK , contentType : 'application/octet-stream' } ) ; } public static computeDocMetaID ( fingerprint , uid ) { return Hashcodes . createID ( uid + ':' + fingerprint , 32 ) ; } } interface DatastoreGetFileRequest { readonly idToken : string ; readonly docID : string ; readonly backend : Backend ; readonly fileRef : FileRef ; }