Last active
December 18, 2017 19:24
-
-
Save llucasshenrique/b120fca3d1899cc93fc5544d7254de1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Pipe } from '@angular/core'; | |
import { File } from '@ionic-native/file'; | |
import { Network } from '@ionic-native/network'; | |
import { LocalNotifications } from '@ionic-native/local-notifications'; | |
import { AngularFireAuth } from 'angularfire2/auth'; | |
import { LoadingController } from 'ionic-angular'; | |
import * as firebase from 'firebase'; | |
import { Observable } from "rxjs/Observable"; | |
import { Observer } from "rxjs/Observer"; | |
import { MediaFileData, MediaFile } from "@ionic-native/media-capture"; | |
export class UploadItem { | |
blob: Blob; | |
file: MediaFile; | |
metadata: MediaFileData; | |
} | |
@Pipe({ name: 'fileSize' }) | |
@Injectable() | |
export class FirebaseStorageProvider { | |
private queue: Array<UploadItem> | |
private uploadQueue: Observable<UploadItem> | |
private UID: string | |
constructor( | |
private network: Network, | |
private localNotification: LocalNotifications, | |
private loadingCtrl: LoadingController, | |
private afAuth: AngularFireAuth) { | |
this.UID = afAuth.auth.currentUser.uid | |
this.uploadQueue = Observable.from(this.queue) | |
} | |
addUpload(uploadItem: UploadItem) { | |
this.queue.push(uploadItem) | |
} | |
private allowedNetworks = { | |
'wifi': true, | |
'ethernet': true, | |
'unknown': false, | |
'2g': false, | |
'3g': false, | |
'4g': false, | |
'cellular': false, | |
'none': false | |
} | |
private queueManager() { | |
return new Observable<firebase.storage.UploadTaskSnapshot>((subscriber) => { | |
let uploadRef = firebase.storage().ref(); | |
let uploadTask: firebase.storage.UploadTask | |
if( this.queue.length > 0 ) { | |
let onStage = this.queue[0] | |
let uploadTask = uploadRef.child(`/${this.UID}/${onStage.file.name}`) | |
let upload = uploadTask.put(onStage.blob) | |
this.network.onConnect().subscribe(() => { | |
setTimeout(() => { | |
// Criar objeto para controlar os tipos de rede em que o upload é permitido | |
if( this.allowedNetworks[this.network.type] ) { | |
upload.on(firebase.storage.TaskEvent.STATE_CHANGED, | |
(snapshot: firebase.storage.UploadTaskSnapshot) => { | |
subscriber.next(snapshot) | |
}) | |
upload.then((success) => { | |
subscriber.complete() | |
}).catch((error) => { | |
subscriber.error(error) | |
}) | |
} else { | |
upload.pause() | |
} | |
}, 3000) | |
}) | |
this.network.onDisconnect().subscribe(() => { | |
upload.pause() | |
}) | |
} | |
}) | |
} | |
private notificationManager() { | |
this.localNotification.schedule({ | |
id: 0, | |
title: 'Enviando sua foto', | |
text: '', | |
ongoing: true | |
}) | |
this.queueManager().subscribe((data) => { | |
let totalSize = Math.round(data.totalBytes); | |
let transferred = Math.round(data.bytesTransferred); | |
let percent = (transferred / totalSize) * 100; | |
let uploadStatus = `${this.transform(transferred)} de ${this.transform(totalSize)} ${percent}%` | |
this.localNotification.update({ | |
id: 0, | |
text: uploadStatus | |
}) | |
}, (error) => { | |
this.localNotification.update({ | |
id: 0, | |
text: error | |
}) | |
}, () => { | |
this.localNotification.update({ | |
id: 0, | |
text: 'Envio completo!', | |
ongoing: false | |
}) | |
}) | |
} | |
private transform(bytes: number = 0, precision: number = 2 ): string { | |
const units = [ 'bytes', 'kB', 'MB', 'GB', 'TB', 'PB']; | |
if ( isNaN( parseFloat( String(bytes) )) || ! isFinite( bytes ) ) return '?'; | |
let unit = 0; | |
while ( bytes >= 1024 ) { | |
bytes /= 1024; | |
unit ++; | |
} | |
return bytes.toFixed( + precision ) + ' ' + units[ unit ]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment