File

src/app/core/file-upload/file-upload.component.ts

Metadata

selector app-file-upload
styleUrls file-upload.component.scss
templateUrl file-upload.component.html

Inputs

user

Type: any

Constructor

constructor(storage: AngularFireStorage, db: AngularFirestore, authService: any)

Methods

toggleHover
toggleHover(event: boolean)

Toggles if user is hovering over the dropzone

Parameters :
  • event

    Event

Returns: void
startUpload
startUpload(event: FileList)

Starts upload to the server.

Parameters :
  • event

    Forms event, contains the blobs of forms

Returns: void
isActive
isActive(snapshot: any)
Returns: void

Properties

downloadURL
downloadURL: Observable<string>
isHovering
isHovering: boolean
percentage
percentage: Observable<number>
snapshot
snapshot: Observable<any>
task
task: AngularFireUploadTask
import { Component, OnInit, Input } from '@angular/core';
import { AngularFireStorage, AngularFireUploadTask } from '@angular/fire/storage';
import { Observable } from 'rxjs/Observable';
import { AngularFirestore } from '@angular/fire/firestore';
import { tap, finalize } from 'rxjs/operators';
import { User } from 'src/app/_models/user.model';
import { AuthService } from 'src/app/_services/auth.service';

@Component({
  selector: 'app-file-upload',
  templateUrl: './file-upload.component.html',
  styleUrls: ['./file-upload.component.scss']
})
export class FileUploadComponent {
  @Input() user: User;

  // Main task
  task: AngularFireUploadTask;

  // Progress monitoring
  percentage: Observable<number>;

  snapshot: Observable<any>;

  // Download URL
  downloadURL: Observable<string> = new Observable;

  // State for dropzone CSS toggling
  isHovering: boolean;

  constructor(private storage: AngularFireStorage,
    private db: AngularFirestore,
    private authService: AuthService) { }


    /**
     * Toggles if user is hovering over the dropzone
     * @param event Event
     */
  toggleHover(event: boolean) {
    this.isHovering = event;
  }


  /**
   * Starts upload to the server.
   * @param event Forms event, contains the blobs of forms
   */
  startUpload(event: FileList) {
    // The File object
    const file = event.item(0);

    // Client-side validation of image
    if (file.type.split('/')[0] !== 'image') {
      console.error('unsupported file type :( ');
      return;
    }

    // The storage path
    const path = `profile/${new Date().getTime()}_${file.name}`;

    // Totally optional metadata
    const customMetadata = { app: '' };

    // The main task
    this.task = this.storage.upload(path, file, { customMetadata });

    // Progress monitoring
    this.percentage = this.task.percentageChanges();
    this.snapshot   = this.task.snapshotChanges();


    // The file's download URL
    this.snapshot.pipe(finalize(() => {
      this.downloadURL = this.storage.ref(path).getDownloadURL();
      this.downloadURL.subscribe(url => {
        this.user.photoURL = url;
        this.authService.updateUserData(this.user);
      });
    }
    )).subscribe();


    this.snapshot = this.task.snapshotChanges().pipe(
      tap(snap => {
        if (snap.bytesTransferred === snap.totalBytes) {
          // Update firestore on completion

          this.db.collection('profiles').add( { path, size: snap.totalBytes });
        }
      })
    );
  }

  // Determines if the upload task is active
  isActive(snapshot) {
    return snapshot.state === 'running' && snapshot.bytesTransferred < snapshot.totalBytes;
  }

}

results matching ""

    No results matching ""