File

projects/web-mev/src/app/features/file-manager/services/file-manager.service.ts

Description

File service

Used for operations with file in the File Manager

Index

Properties
Methods
Accessors

Constructor

constructor(httpClient: HttpClient, adapter: FileAdapter, analysesService: AnalysesService)
Parameters :
Name Type Optional
httpClient HttpClient No
adapter FileAdapter No
analysesService AnalysesService No

Methods

addDropboxFile
addDropboxFile(files)

Add file, post method for Dropbox upload

Parameters :
Name Optional
files No
Returns : Observable<any>
addFile
addFile(files: any[])

Add file, post method

Execute the uploads sequentially, because uploading multiple files could potentially tie-up the server since a bunch of threads would be busy ingesting the data for a long time

Parameters :
Name Type Optional
files any[] No
Returns : void
deleteFile
deleteFile(id: number | string)

Delete file

Parameters :
Name Type Optional
id number | string No
Returns : void
Public getAllFiles
getAllFiles()

Get file list

Returns : void
getDialogData
getDialogData()
Returns : any
getFilePreview
getFilePreview(fileId: number | string)

Preview file content

Display only the first 5 rows of file

Parameters :
Name Type Optional
fileId number | string No
Returns : Observable<any>
getFileTypes
getFileTypes()

Get file resource list

Returns : Observable<FileType[]>
updateFile
updateFile(file: File)

Update file properties, put method

Parameters :
Name Type Optional
file File No
Returns : void

Properties

Private Readonly API_URL
Default value : environment.apiUrl
dataChange
Type : BehaviorSubject<File[]>
Default value : new BehaviorSubject<File[]>([])
dialogData
Type : any
Private Readonly FILE_VALIDATION_PROGRESS_STATUSES
Type : []
Default value : [ 'Validating...', 'Processing...' ]
Public fileUploadsProgress
Type : BehaviorSubject<Map<string, object>>
Default value : new BehaviorSubject<Map<string, object>>(new Map<string, object>())
Private maxTime
Type : number
Default value : 20000

Accessors

data
getdata()
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, timer, forkJoin } from 'rxjs';
import { HttpClient, HttpEventType, HttpParams } from '@angular/common/http';
import {
  map,
  switchMap,
  takeWhile,
  concatMap,
  takeUntil
} from 'rxjs/operators';
import { File, FileAdapter } from '@app/shared/models/file';
import { environment } from '@environments/environment';
import { FileType } from '@app/shared/models/file-type';
import { AnalysesService } from '@app/features/analysis/services/analysis.service';

/**
 * File service
 *
 * Used for operations with file in the File Manager
 */
@Injectable({
  providedIn: 'root'
})
export class FileService {
  private readonly API_URL = environment.apiUrl;
  private readonly FILE_VALIDATION_PROGRESS_STATUSES = [
    'Validating...',
    'Processing...'
  ];
  private maxTime = 20000; //  check the file validation status only for the first 20 secs

  dataChange: BehaviorSubject<File[]> = new BehaviorSubject<File[]>([]);

  public fileUploadsProgress: BehaviorSubject<
    Map<string, object>
  > = new BehaviorSubject<Map<string, object>>(new Map<string, object>());

  // Temporarily stores data from dialogs
  dialogData: any;

  constructor(
    private httpClient: HttpClient,
    private adapter: FileAdapter,
    private analysesService: AnalysesService
  ) {}

  get data(): File[] {
    return this.dataChange.getValue();
  }

  getDialogData() {
    return this.dialogData;
  }

  /**
   * Get file resource list
   *
   */
  getFileTypes(): Observable<FileType[]> {
    return this.httpClient.get<FileType[]>(`${this.API_URL}/resource-types/`);
  }

  /**
   * Get file list
   *
   */
  public getAllFiles(): void {
    // refresh the status of the resource validation process every 2 seconds
    const maxTimer$ = timer(this.maxTime);
    timer(0, 2000)
      .pipe(
        concatMap(() => this.httpClient.get(`${this.API_URL}/resources/`)),
        map((files: File[]) => files.map(file => this.adapter.adapt(file))),
        takeWhile(
          files =>
            files.some(file =>
              this.FILE_VALIDATION_PROGRESS_STATUSES.includes(file.status)
            ),
          true
        ),
        takeUntil(maxTimer$)
      )
      .subscribe(data => {
        this.dataChange.next(data);
      });
  }

  /**
   * Add file, post method
   *
   * Execute the uploads sequentially, because uploading multiple files could potentially tie-up
   * the server since a bunch of threads would be busy ingesting the data for a long time
   */
  addFile(files: any[]): void {
    const fileUploadsProgressMap = new Map<string, object>();

    for (let i = 0; i < files.length; i++) {
      fileUploadsProgressMap[files[i].name] = { percent: 0, isUploaded: false };
    }
    this.fileUploadsProgress.next(fileUploadsProgressMap);

    for (let i = 0; i < files.length; i++) {
      const formData = new FormData();
      formData.set('upload_file', files[i], files[i].name);

      this.httpClient
        .post(`${this.API_URL}/resources/upload/`, formData, {
          reportProgress: true,
          observe: 'events'
        })
        .subscribe(event => {
          switch (event.type) {
            case HttpEventType.UploadProgress:
              const percentDone = Math.round(
                (event.loaded * 100) / event.total
              );
              fileUploadsProgressMap[files[i].name] = {
                percent: percentDone,
                isUploaded: false
              };
              this.fileUploadsProgress.next(fileUploadsProgressMap);
              break;
            case HttpEventType.Response:
              fileUploadsProgressMap[files[i].name] = {
                percent: 100,
                isUploaded: true
              };
              this.fileUploadsProgress.next(fileUploadsProgressMap);
              this.dialogData = formData;
          }
        });
    }
  }

  /**
   * Add file, post method for Dropbox upload
   *
   */
  addDropboxFile(files): Observable<any> {
    return this.httpClient
      .post(`${this.API_URL}/resources/dropbox-upload/`, files)
      .pipe(
        switchMap(response => {
          return forkJoin(
            response['upload_ids'].map(execOperationId =>
              this.analysesService.getExecutedOperationResult(execOperationId)
            )
          );
        })
      );
  }

  /**
   * Update file properties, put method
   *
   */
  updateFile(file: File): void {
    this.httpClient
      .put(`${this.API_URL}/resources/${file.id}/`, file)
      .subscribe(data => {
        this.dialogData = file;
      });
  }

  /**
   * Delete file
   *
   */
  deleteFile(id: number | string): void {
    this.httpClient.delete(`${this.API_URL}/resources/${id}/`).subscribe();
  }

  /**
   * Preview file content
   *
   * Display only the first 5 rows of file
   */
  getFilePreview(fileId: number | string): Observable<any> {
    const params = {
      params: new HttpParams().set('page', '1').set('page_size', '5')
    };
    return <Observable<any>>(
      this.httpClient.get(
        `${this.API_URL}/resources/${fileId}/contents/`,
        params
      )
    );
  }
}

result-matching ""

    No results matching ""