import { Either , LeftEither } from '../util/Either' ; import { Backend } from 'polar-shared/src/datastore/Backend' ; import { Logger } from 'polar-shared/src/logger/Logger' ; import { isPresent } from 'polar-shared/src/Preconditions' ; import { IDocMeta } from "polar-shared/src/metadata/IDocMeta" ; import { BackendFileRef } from "polar-shared/src/datastore/BackendFileRef" ; import { DocInfoLike } from "../metadata/DocInfo" ; const log = Logger . create ( ) ; export class BackendFileRefs { /**
     * Get the main BackendFileRef (PHZ or PDF) for this file (either the
     * PHZ or PDF file)
     */ public static toBackendFileRef ( either ) { if ( ! either ) { log . warn ( "No 'either' param specified." ) ; return undefined ; } const docInfo = Either . ofLeft ( either ) . convertLeftToRight ( left => left . docInfo ) ; const filename = docInfo . filename ; if ( filename ) { const backend = docInfo . backend || Backend . STASH ; const backendFileRef = { name : filename , hashcode : docInfo . hashcode , backend } ; return backendFileRef ; } else { } return undefined ; } /**
     * Get all FileRefs for this DocMeta including the main doc but also
     * any image, audio, or video attachments.
     */ public static toBackendFileRefs ( either ) { const result = [ ] ; const fileRef = this . toBackendFileRef ( either ) ; const docInfo = Either . ofLeft ( either ) . convertLeftToRight ( left => left . docInfo ) ; if ( fileRef ) { const backend = docInfo . backend || Backend . STASH ; result . push ( { ... fileRef , backend } ) ; } const attachments = docInfo . attachments || { } ; const attachmentRefs = Object . values ( attachments ) . map ( current => current . fileRef ) . filter ( current => { if ( isPresent ( current ) ) { return true ; } log . warn ( "Doc had missing attachment data: " , docInfo . fingerprint ) ; return false ; } ) ; result . push ( ... attachmentRefs ) ; return result ; } public static equals ( b0 , b1 ) { return b0 . backend === b1 . backend && b0 . name === b1 . name && b0 . hashcode === b1 . hashcode ; } }