import { Injectable } from '@angular/core'; import { HttpClient, HttpRequest, HttpEventType, HttpResponse } from '@angular/common/http'; import { Subject } from 'rxjs'; import { Observable } from 'rxjs'; import { IcsToastService } from '@varmasagi/ics-toast'; import { DriveRepresentation } from './dtos/DriveRepresentation'; import { environment } from './environments/environment'; const apiUrl = environment.drive.URL; const Access = environment.access.URL; const sampleUrl = 'https://jsonplaceholder.typicode.com/posts'; /** * Service providing methods to carry out Ics Drive Operations. * To be included in the providers array in Application Modules, * and injected into the component constructor. */ @Injectable() export class IcsDriveService { fileDest: Observable<{}>; public imageDesc: Array = []; driveRepresentation: DriveRepresentation; public respStatus: number; constructor( private toastService: IcsToastService, private http: HttpClient ) {} /** * Used to upload file(s) to Ics Drive. * @param files - Accepts a Set of type File */ public upload(files: Set,folderId:number): any[] { const status: any[] = []; files.forEach(file => { const formData: FormData = new FormData(); formData.append('file', file, file.name); const req = new HttpRequest('POST', apiUrl + 'files/new?folderId='+folderId, formData, { reportProgress: true }); const progress = new Subject(); this.http.request(req).subscribe( event => { if (event.type === HttpEventType.UploadProgress) { const percentDone = Math.round((100 * event.loaded) / event.total); progress.next(percentDone); } else if (event instanceof HttpResponse) { this.driveRepresentation = new DriveRepresentation(); const resultData: any = event.body; this.driveRepresentation.id = resultData.id; this.driveRepresentation.name = resultData.name; this.imageDesc.push(this.driveRepresentation); if (event.status === 201) { this.respStatus = event.status; } else { this.respStatus = null; } progress.complete(); } status['resp'] = this.imageDesc; }, err => { this.toastService.error('Not Uploaded !'); } ); status['filename'] = { progress: progress.asObservable() }; }); return status; } /** * Returns details about a file including its URL * @param args - Id of the file whos details are to be fetched. */ getFileList(args): Observable<{}> { const fileLocation = apiUrl + 'files/' + args + '/location'; return this.http.get(fileLocation); } /** * Used to delete a file from Ics Drive * @param args - Id of the file to delete */ delete(args): Observable<{}> { const fileDelete = apiUrl + 'files/' + args + '/delete'; return this.http.delete(fileDelete); } /** * Used to get the directory structure of a folder, returns * subfolders and files within the folder * @param args - Id of the folder whos contents are needed. */ getDirectoryList(args): Observable<{}> { const dirLocation = apiUrl + 'folders/' + args + '/contents'; return this.http.get(dirLocation); } /** * Used to connect to Ics Drive. If no account exists for the user, * it will be created. */ getDrive(): Observable { return this.http.post(apiUrl + 'connect', '{}'); } /** * Returns a list of files with their URLs * @param args - Accepts a string, either comma seperated ids, * or "recent". */ getFilesLists(args): Observable<{}> { const listUrl = apiUrl + 'files/list?files=' + args; return this.http.get(listUrl); } getSampleData() { return this.http.get(sampleUrl); } /** * Used to upload the image of a file. Internal implementation, * not intended to be used manually */ sharingFileUpload(args): Observable { return this.http.post(apiUrl + 'files/newBase64', args); } /** * Internal implementation, not intended to be used manually. */ getHtmlUrl(data): Observable { return this.http.get(Access + 'share/' + data); } }