import { AppStore , DownloadNodesAction , NodeActionTypes , NodeInfo , getAppSelection , getCurrentVersion } from '@alfresco/aca-shared/store' ; import { DownloadZipDialogComponent } from '@alfresco/adf-core' ; import { MinimalNodeEntity , Version } from '@alfresco/js-api' ; import { Injectable } from '@angular/core' ; import { MatDialog } from '@angular/material/dialog' ; import { Actions , Effect , ofType } from '@ngrx/effects' ; import { Store } from '@ngrx/store' ; import { map , take } from 'rxjs/operators' ; import { ContentApiService } from '@alfresco/aca-shared' ; import { ContentUrlService } from '../../services/content-url.service' ; @ Injectable ( ) export class DownloadEffects { constructor ( private store , private actions$ , private contentApi , private dialog , private contentUrlService ) { } @ Effect ( { dispatch : false } ) downloadNode$ = this . actions$ . pipe ( ofType < DownloadNodesAction > ( NodeActionTypes . Download ) , map ( ( action ) => { if ( action . payload && action . payload . length > 0 ) { this . downloadNodes ( action . payload ) ; } else { this . store . select ( getAppSelection ) . pipe ( take ( 1 ) ) . subscribe ( ( selection ) => { if ( selection && ! selection . isEmpty ) { this . store . select ( getCurrentVersion ) . pipe ( take ( 1 ) ) . subscribe ( ( version ) => { if ( version ) { this . downloadFileVersion ( selection . nodes [ 0 ] . entry , version . entry ) ; } else { this . downloadNodes ( selection . nodes ) ; } } ) ; } } ) ; } } ) ) ; private downloadNodes ( toDownload ) { const nodes = toDownload . map ( ( node ) => { const { id , nodeId , name , isFile , isFolder } = node . entry as any ; return { id : this . isSharedLinkPreview ? id : nodeId || id , name , isFile , isFolder } ; } ) ; if ( ! nodes || nodes . length === 0 ) { return ; } if ( nodes . length === 1 ) { this . downloadNode ( nodes [ 0 ] ) ; } else { this . downloadZip ( nodes ) ; } } private downloadNode ( node ) { if ( node ) { if ( node . isFolder ) { this . downloadZip ( [ node ] ) ; } else { this . downloadFile ( node ) ; } } } private downloadFile ( node ) { if ( node && ! this . isSharedLinkPreview ) { this . contentUrlService . getNodeContentUrl ( node . id , true ) . subscribe ( ( contentUrl ) => { this . download ( contentUrl , node . name ) ; } ) ; } if ( node && this . isSharedLinkPreview ) { this . download ( this . contentApi . getSharedLinkContent ( node . id , false ) , node . name ) ; } } private downloadFileVersion ( node , version ) { if ( node && version ) { this . contentUrlService . getVersionContentUrl ( node . id , version . id , true ) . subscribe ( ( contentUrl ) => { this . download ( contentUrl , node . name ) ; } ) ; } } private downloadZip ( nodes ) { if ( nodes && nodes . length > 0 ) { const nodeIds = nodes . map ( ( node ) => node . id ) ; this . dialog . open ( DownloadZipDialogComponent , { width : '600px' , disableClose : true , data : { nodeIds } } ) ; } } private download ( url , fileName ) { if ( url && fileName ) { const link = document . createElement ( 'a' ) ; link . style . display = 'none' ; link . download = fileName ; link . href = url ; document . body . appendChild ( link ) ; link . click ( ) ; document . body . removeChild ( link ) ; } } private get isSharedLinkPreview ( ) { return location . href . includes ( '/preview/s/' ) ; } }