import { Component , NgZone } from '@angular/core' ; import { PluginService } from '../../services/PluginService' ; import { Insomnia } from '@ionic-native/insomnia' ; import { NotificationService } from '../../services/NotificationService' ; import { Transfer , TransferObject , FileUploadResult } from '@ionic-native/transfer' ; import { AppConfiguration } from '../../application/AppConfiguration' ; import { ImageResizer , ImageResizerOptions } from '@ionic-native/image-resizer' ; import { UploadImage } from '../../models/UploadImage' ; import { Helper } from "../../application/Helper" ; import { DialogService } from '../../services/DialogService' ; import { LoadingController } from 'ionic-angular' ; @ Component ( { selector : 'upload-example' , templateUrl : 'upload-example.html' } ) export class UploadExamplePage { /**
     * The selected images
     */ public images : Array < UploadImage > = [ ] ; /**
     * A flag that indicates whether the app is uploading files or not
     */ public is_uploading : boolean = false ; /**
     * The index of the current image being uploaded
     */ public current : number = 0 ; /**
     * Total images to upload
     */ public total : number ; /**
     * Amount of progress made during an image upload
     */ public progress : number = 0 ; /**
     * The transfer object
     */ private file_transfer : TransferObject = this . transfer . create ( ) ; /**
     * A collection of upload responses
     */ public upload_responses : any = [ ] ; constructor ( private plugin_service , private ng_zone , private insomnia , private notification_service , private transfer , private image_resizer , private dialog_service ) { } /**
     * Opens the albums and displays the selected images if available
     */ public async open_albums ( ) { let originals = await this . plugin_service . open_albums ( ) ; let requests = await originals . map ( async ( x ) => new UploadImage ( { path : x , guid : Helper . guid ( ) , thumb : await this . resize ( x ) } ) ) ; let images = await Promise . all ( requests ) ; this . images = this . images . concat ( images ) ; } /**
     * Opens the camera and displays the picture if available
     */ public async open_camera ( ) { let orignal = await this . plugin_service . open_camera ( ) ; this . images . push ( new UploadImage ( { path : orignal , guid : Helper . guid ( ) , thumb : await this . resize ( orignal ) } ) ) ; } param original private async resize ( original ) { let options = { uri : original , folderName : 'Protonet' , quality : 90 , width : 1280 , height : 1280 } as ImageResizerOptions ; return this . image_resizer . resize ( options ) ; } param image private delete ( image ) { this . dialog_service . confirm_delete ( 'Are you sure you want to delete this image?' , confirmed => { if ( confirmed ) this . images = this . images . filter ( x => x . guid != image . guid ) ; } ) ; } /**
     * Starts the upload of the
     */ public async start_upload ( ) { this . is_uploading = true ; this . total = this . images . length ; this . insomnia . keepAwake ( ) ; this . upload ( ) ; } param progressEvent public on_progress = ( progressEvent ) : void => { this . ng_zone . run ( ( ) => { if ( progressEvent . lengthComputable ) { let progress = Math . round ( ( progressEvent . loaded / progressEvent . total ) * 100 ) ; if ( progress > 100 ) progress = 100 ; if ( progress < 0 ) progress = 0 ; this . progress = progress ; } } ) ; } ; /**
     * Actually uploads an image
     */ public async upload ( ) { this . file_transfer . onProgress ( this . on_progress ) ; let options = { fileKey : 'file' , fileName : this . images [ this . current ] . path . split ( '/' ) . pop ( ) , mimeType : 'image/jpeg' , chunkedMode : false , headers : { 'Content-Type' : undefined } , params : { } } ; try { let result = await this . file_transfer . upload ( encodeURI ( this . images [ this . current ] . path ) , encodeURI ( AppConfiguration . base_url + 'media/' ) , options , false ) ; this . on_success ( result ) ; } catch ( e ) { this . on_failed ( e ) ; } } param result public on_success = ( result ) : void => { this . images [ this . current ] . uploaded = true ; if ( this . current + 1 < this . images . length ) { this . current ++ ; this . progress = 0 ; this . upload ( ) ; } else { this . insomnia . allowSleepAgain ( ) ; this . is_uploading = false ; this . images = [ ] ; this . current = 0 ; this . notification_service . notify_success ( 'Succesfully uploaded your images' ) ; } } ; param error public on_failed = ( error ) : void => { this . is_uploading = false ; this . insomnia . allowSleepAgain ( ) ; if ( error . code == 3 || error . code == 4 ) return ; this . notification_service . notify_error ( error ) ; } ; }