'use strict' ; import { Uri } from 'vscode' ; import { RequestHelper } from '../util/requestHelper' ; import { ISPRequest , IAuthOptions } from 'sp-request' ; import { SPPull , ISPPullContext , ISPPullOptions } from 'sppull' ; import { WorkspaceHelper } from '../util/workspaceHelper' ; import { spsave , ICoreOptions , FileOptions } from 'spsave' ; import { ISPFileInformation , IConfig , IFileGateway } from '../spgo' ; export class SPFileGateway implements IFileGateway { _config : IConfig ; constructor ( config ) { this . _config = config ; } public checkOutFile ( fileUri , spr ) { let sharePointSiteUrl = WorkspaceHelper . getSiteUriForActiveWorkspace ( fileUri . toString ( ) , this . _config ) ; return spr . requestDigest ( sharePointSiteUrl . toString ( ) ) . then ( digest => { return spr . post ( sharePointSiteUrl + "/_api/web/GetFileByServerRelativeUrl('" + encodeURI ( fileUri . path ) + "')/CheckOut()" , { body : { } , headers : RequestHelper . createAuthHeaders ( this . _config , digest ) } ) ; } ) ; } public deleteFile ( fileUri , spr ) { let sharePointSiteUrl = WorkspaceHelper . getSiteUriForActiveWorkspace ( this . _config . sharePointSiteUrl + fileUri , this . _config ) ; return spr . requestDigest ( sharePointSiteUrl . toString ( ) ) . then ( digest => { return spr . post ( sharePointSiteUrl + "/_api/web/GetFileByServerRelativeUrl('" + encodeURI ( fileUri . path ) + "')" , { body : { } , headers : RequestHelper . createAuthHeaders ( this . _config , digest , { 'X-HTTP-Method' : 'DELETE' , 'accept' : 'application/json; odata=verbose' , 'content-type' : 'application/json; odata=verbose' } ) } ) ; } ) ; } public downloadFiles ( context , fileOptions ) { return SPPull . download ( context , fileOptions ) ; } public getFileInformation ( fileUri , spr ) { let sharePointSiteUrl = WorkspaceHelper . getSiteUriForActiveWorkspace ( this . _config . sharePointSiteUrl + fileUri , this . _config ) ; return spr . requestDigest ( sharePointSiteUrl . toString ( ) ) . then ( digest => { return spr . get ( sharePointSiteUrl + "/_api/web/GetFileByServerRelativeUrl('" + encodeURI ( fileUri . path ) + "')/?$select=Name,ServerRelativeUrl,CheckOutType,TimeLastModified,CheckedOutByUser" , { body : { } , headers : RequestHelper . createAuthHeaders ( this . _config , digest ) } ) . then ( response => { let fileInfo = { checkOutType : response . body . d . CheckOutType , name : response . body . d . Name , timeLastModified : response . body . d . TimeLastModified } ; if ( fileInfo . checkOutType == 0 || fileInfo . checkOutType == 1 ) { return spr . get ( sharePointSiteUrl + "/_api/web/GetFileByServerRelativeUrl('" + encodeURI ( fileUri . path ) + "')/CheckedOutByUser?$select=Title,Email" , { body : { } , headers : RequestHelper . createAuthHeaders ( this . _config , digest ) } ) . then ( userInfo => { fileInfo . checkOutBy = userInfo . body . d . Title ; return fileInfo ; } ) ; } else { return fileInfo ; } } ) ; } ) ; } public undoCheckOutFile ( fileUri , spr ) { let sharePointSiteUrl = WorkspaceHelper . getSiteUriForActiveWorkspace ( this . _config . sharePointSiteUrl + fileUri , this . _config ) ; return spr . requestDigest ( sharePointSiteUrl . toString ( ) ) . then ( digest => { return spr . post ( sharePointSiteUrl + "/_api/web/GetFileByServerRelativeUrl('" + encodeURI ( fileUri . path ) + "')/undocheckout()" , { body : { } , headers : RequestHelper . createAuthHeaders ( this . _config , digest ) } ) ; } ) ; } public uploadFiles ( coreOptions , credentials , fileOptions ) { RequestHelper . setNtlmHeader ( this . _config ) ; return spsave ( coreOptions , credentials , fileOptions ) ; } }