import { FilePaths } from 'polar-shared/src/util/FilePaths' ; import { AddFileRequest } from './AddFileRequest' ; import { Optional } from 'polar-shared/src/util/ts/Optional' ; import { isPresent } from 'polar-shared/src/Preconditions' ; import { Reducers } from 'polar-shared/src/util/Reducers' ; export namespace AddFileRequests { export function fromURL ( url ) { const toBasename = ( input ) : string => { input = input . replace ( /[?#].*$/ , '' ) ; return FilePaths . basename ( input ) ; } ; const parsedURL = new URL ( url ) ; const basenames = [ ] ; if ( parsedURL . searchParams . get ( 'url' ) ) { basenames . push ( toBasename ( parsedURL . searchParams . get ( 'url' ) ! ) ) ; } basenames . push ( toBasename ( url ) ) ; const basename = basenames . filter ( current => isPresent ( current ) ) . reduce ( Reducers . FIRST ) ; return { docPath : url , basename } ; } export function fromPath ( path ) { return { docPath : path , basename : FilePaths . basename ( path ) } ; } export function computeDirectly ( event ) { if ( event . dataTransfer && event . dataTransfer . files ) { return computeFromFileList ( Array . from ( event . dataTransfer . files ) ) ; } else { return [ ] ; } } function isFileSupported ( name ) { return FilePaths . hasExtension ( name , 'pdf' ) || FilePaths . hasExtension ( name , 'epub' ) ; } export function computeFromFileList ( files ) { function toAddFileRequest ( file ) { if ( file . path ) { return { docPath : file . path , basename : FilePaths . basename ( file . path ) } ; } else { return { docPath : URL . createObjectURL ( file ) , basename : file . name , } ; } } return Array . from ( files ) . filter ( file => isFileSupported ( file . name ) ) . map ( toAddFileRequest ) ; } export async function computeRecursively ( event ) { return Optional . empty ( ) ; } }