import * as path from 'path' ; import { combineLatest , Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { ITrackInfo } from 'src/ts/download/resource/resource-info' ; import { ITrackDownloadInfo } from 'src/ts/download/track-download-info' ; import { ITrackDownloadMethodInfo } from 'src/ts/download/track-download-method' ; import { TrackDownloadMethodService } from 'src/ts/download/track-download-method-service' ; import { IOptions } from 'src/ts/options/option' ; import { OptionsObservables } from 'src/ts/options/options-observables' ; import { FilenameService } from 'src/ts/util/filename-service' ; import DownloadOptions = chrome . downloads . DownloadOptions ; export const TrackDownloadInfoFactory = { create$ ( trackInfo , downloadLocation ) { return combineLatest ( TrackDownloadMethodService . getDownloadMethodInfo$ ( trackInfo ) , OptionsObservables . getOptions$ ( ) ) . pipe ( map ( ( [ downloadMethodInfo , options ] ) => { trackInfo = cleanTrackTitleIfEnabled ( trackInfo , options ) ; return toDownloadInfo ( trackInfo , downloadMethodInfo , downloadLocation , options ) ; } ) ) ; } , } ; function toDownloadInfo ( trackInfo , downloadMethodInfo , downloadLocation , options ) { const filePath = getFilePath ( downloadLocation , trackInfo . title , downloadMethodInfo . format ) ; return { downloadMethod : downloadMethodInfo . downloadMethod , downloadOptions : getDownloadOptions ( filePath , downloadMethodInfo . url , options ) , originalUrl : downloadMethodInfo . url , trackInfo , } ; } function cleanTrackTitleIfEnabled ( trackInfo , options ) { if ( ! options . cleanTrackTitle . enabled ) { return trackInfo ; } const regex = new RegExp ( options . cleanTrackTitle . pattern , 'i' ) ; const cleanedTitle = trackInfo . title . replace ( regex , '' ) . trim ( ) ; return { ... trackInfo , title : cleanedTitle === '' ? trackInfo . title : cleanedTitle , } ; } function getFilePath ( downloadLocation , trackTitle , fileExtension ) { const location = `${ FilenameService . removeSpecialCharacters ( downloadLocation ) }` ; const filename = `${ FilenameService . removeSpecialCharacters ( trackTitle ) }.${ fileExtension }` ; return path . join ( location , filename ) ; } function getDownloadOptions ( filePath , url , options ) { return { conflictAction : options . overwriteExistingFiles ? 'overwrite' : 'uniquify' , filename : filePath , saveAs : false , url , } ; }