import { Component , OnInit } from "@angular/core" ; import { FileUploader , FileUploaderOptions , Headers , FileItem , ParsedResponseHeaders } from "ng2-file-upload" ; import { RevitConverterService } from "./revit-converter.service" ; import { ConversionJob , UploadAnswer , FormDataStatus } from "./revit-converter.models" ; const baseUrl = "https://developer.api.autodesk.com/oss/v2/buckets/" ; @ Component ( { selector : "app-revit-converter" , templateUrl : "./revit-converter.component.html" , styleUrls : [ "./revit-converter.component.scss" ] , providers : [ RevitConverterService ] } ) export class RevitConverterComponent implements OnInit { public uploader : FileUploader = new FileUploader ( { disableMultipart : true } ) ; public uploaderOptions : FileUploaderOptions ; public hasBaseDropZoneOver : boolean = false ; public hasAnotherDropZoneOver : boolean = false ; public access_token = "" ; constructor ( private _revitConverterService ) { } ngOnInit ( ) { this . _revitConverterService . GetAccessToken ( ) . subscribe ( token => { this . access_token = token . access_token ; this . uploader . onBeforeUploadItem = item => { item . method = "PUT" ; item . url = baseUrl + "revittoifcbucket/objects/" + Date . now ( ) . toString ( ) + "_" + item . file . name ; let headers = [ { name : "Authorization" , value : "Bearer " + this . access_token } , { name : "Content-Type" , value : "application/octet-stream" } ] ; item . headers = headers ; let itemStatus = { intervalId : null , status : "Uploading" , urn : "" , derivativeUrn : "" } ; item . formData = itemStatus ; } ; this . uploader . onAfterAddingFile = ( item ) => { let extension = item . file . name . split ( "." ) . pop ( ) ; if ( extension !== "rvt" ) { item . remove ( ) ; } } ; this . uploader . onCompleteItem = ( item , response , status , headers ) => { console . log ( response ) ; let uploadResponse = JSON . parse ( response ) ; this . _revitConverterService . PostJobRequest ( this . access_token , uploadResponse . objectId ) . subscribe ( answer => { this . _revitConverterService . GetJobStatus ( this . access_token , answer . urn ) . subscribe ( jobStatus1 => { let itemStatus = { intervalId : null , status : "Converting" , urn : answer . urn , derivativeUrn : "" } ; item . formData = itemStatus ; if ( jobStatus1 . derivatives . length === 0 ) { let interval = setInterval ( this . UpdateJobStatus , 2000 , this . access_token , answer . urn , this . _revitConverterService , item ) ; item . formData . intervalId = interval ; } else { if ( jobStatus1 . status === "failed" ) { itemStatus . status = "Failed" ; } else { itemStatus . status = "Complete" ; let derivativeUrn = jobStatus1 . derivatives . filter ( derivative => derivative . outputType === "ifc" ) [ 0 ] . children [ 0 ] . urn ; itemStatus . derivativeUrn = derivativeUrn ; } } } , error => ( this . _revitConverterService . errorMessage = < any > error ) ) ; } , error => ( this . _revitConverterService . errorMessage = < any > error ) ) ; } ; } , error => ( this . _revitConverterService . errorMessage = < any > error ) ) ; } /**
     * UpdateJobStatus
     */ public UpdateJobStatus ( access_token , urn , service , item ) { service . GetJobStatus ( access_token , urn ) . subscribe ( jobCurrentStatus => { let itemStatus = item . formData ; if ( jobCurrentStatus . derivatives . length === 0 ) { let conversionProgress = jobCurrentStatus . progress . match ( new RegExp ( "\\d*%" ) ) [ 0 ] ; itemStatus . status = "Converting " + conversionProgress ; } else { if ( jobCurrentStatus . status === "failed" ) { itemStatus . status = "Failed" ; } else { itemStatus . status = "Complete" ; let derivativeUrn = jobCurrentStatus . derivatives . filter ( derivative => derivative . outputType === "ifc" ) [ 0 ] . children [ 0 ] . urn ; itemStatus . derivativeUrn = derivativeUrn ; } } } , error => ( this . _revitConverterService . errorMessage = < any > error ) ) ; } /**
     * Download file
     */ public DownloadFile ( urn , derivative ) { let fileName = derivative . replace ( /^.*[\\\/]/ , "" ) ; fileName = fileName . replace ( new RegExp ( "\\d*_" ) , "" ) ; this . _revitConverterService . GetDerivative ( this . access_token , urn , derivative ) . subscribe ( data => this . _revitConverterService . downloadFile ( data , fileName ) ) ; } /**
     * DeleteBuckets
     */ public DeleteBuckets ( ) { let bucketsNames = [ "bucketname" ] ; bucketsNames . forEach ( bucketsName => { this . _revitConverterService . DeleteBucket ( this . access_token , bucketsName ) . subscribe ( response => { console . log ( bucketsName + response ) ; } , error => ( this . _revitConverterService . errorMessage = < any > error ) ) ; } ) ; } public fileOverBase ( e ) { this . hasBaseDropZoneOver = e ; } public fileOverAnother ( e ) { this . hasAnotherDropZoneOver = e ; } }