import constants = require ( '../.constants' ) ; const TYPE_METADATA = 'Metadata' ; interface FilePath { path : string ; /**
     * The path extracted from the files array returned by aria2c.
     * It is present even for metadata, unlike 'path'
     */ inputPath : string ; downloadUri : string ; } param { Object [ ] } files returns { string } export function findAriaFilePath ( files ) { var filePath = files [ 0 ] [ 'path' ] ; var uri = files [ 0 ] . uris [ 0 ] ? files [ 0 ] . uris [ 0 ] . uri : null ; if ( filePath . startsWith ( constants . ARIA_DOWNLOAD_LOCATION ) ) { if ( filePath . substring ( filePath . lastIndexOf ( '.' ) + 1 ) !== 'torrent' ) { return { path : filePath , inputPath : filePath , downloadUri : uri } ; } else { return { path : null , inputPath : filePath , downloadUri : uri } ; } } else { return { path : null , inputPath : filePath , downloadUri : uri } ; } } param { string } filePath returns { string } export function getFileNameFromPath ( filePath , inputPath , downloadUri ? ) { if ( ! filePath ) { return getFilenameFromUri ( inputPath , downloadUri ) ; } var baseDirLength = constants . ARIA_DOWNLOAD_LOCATION . length + 38 ; var nameEndIndex = filePath . indexOf ( '/' , baseDirLength ) ; if ( nameEndIndex === - 1 ) { nameEndIndex = filePath . length ; } var fileName = filePath . substring ( baseDirLength , nameEndIndex ) ; if ( ! fileName ) { return getFilenameFromUri ( inputPath , downloadUri ) ; } return fileName ; } param { string } filePath returns { string } export function getActualDownloadPath ( filePath ) { var baseDirLength = constants . ARIA_DOWNLOAD_LOCATION . length + 38 ; var nameEndIndex = filePath . indexOf ( '/' , baseDirLength ) ; if ( nameEndIndex === - 1 ) { nameEndIndex = filePath . length ; } var fileName = filePath . substring ( 0 , nameEndIndex ) ; return fileName ; } param uri function getFilenameFromUri ( path , uri ) { if ( path ) { if ( path . startsWith ( '[METADATA]' ) ) { return path . substring ( 10 ) ; } else { return TYPE_METADATA ; } } else { if ( uri ) { return uri . replace ( /#.*$|\/\?.*$|\?.*$/ , "" ) . replace ( /^.*\// , "" ) ; } else { return TYPE_METADATA ; } } } export function isFilenameAllowed ( filename ) { if ( ! constants . ARIA_FILTERED_FILENAMES ) return 1 ; if ( filename === TYPE_METADATA ) return - 1 ; for ( var i = 0 ; i < constants . ARIA_FILTERED_FILENAMES . length ; i ++ ) { if ( filename . indexOf ( constants . ARIA_FILTERED_FILENAMES [ i ] ) > - 1 ) return 0 ; } return 1 ; }