'use strict' ; import * as vscode from 'vscode' ; import { Uri } from 'vscode' ; import { Logger } from '../util/logger' ; import { ISPPullOptions } from 'sppull' ; import { Constants } from '../constants' ; import { UrlHelper } from '../util/urlHelper' ; import { FileHelper } from '../util/fileHelper' ; import { ICoreOptions , FileOptions } from 'spsave' ; import { ISPRequest , IAuthOptions } from 'sp-request' ; import { RequestHelper } from '../util/requestHelper' ; import { SPFileGateway } from '../gateway/spFileGateway' ; import { WorkspaceHelper } from '../util/workspaceHelper' ; import { IPublishingAction , IConfig , IFileGateway } from '../spgo' ; import { DownloadFileOptionsFactory } from '../factory/downloadFileOptionsFactory' ; export class SPFileService { _config : IConfig ; _fileGateway : IFileGateway ; constructor ( config , gateway ? ) { this . _config = config ; this . _fileGateway = gateway || new SPFileGateway ( config ) ; } public checkOutFile ( textDocument ) { let spr = RequestHelper . createRequest ( vscode . window . spgo , this . _config ) ; let fileUri = UrlHelper . getServerRelativeFileUri ( textDocument . fsPath , this . _config ) ; return this . _fileGateway . checkOutFile ( fileUri , spr ) ; } public deleteFileFromServer ( fileUri ) { let spr = RequestHelper . createRequest ( vscode . window . spgo , this . _config ) ; let remoteFileUri = UrlHelper . getServerRelativeFileUri ( fileUri . fsPath , this . _config ) ; return this . _fileGateway . deleteFile ( remoteFileUri , spr ) ; } public downloadFiles ( siteUrl , remoteFolder ) { remoteFolder = UrlHelper . ensureLeadingWebSlash ( remoteFolder ) ; let factory = new DownloadFileOptionsFactory ( remoteFolder ) ; let context = { siteUrl : siteUrl . toString ( ) , creds : RequestHelper . createCredentials ( vscode . window . spgo , this . _config ) } ; let options = factory . createFileOptions ( siteUrl , this . _config ) ; return this . _fileGateway . downloadFiles ( context , options ) ; } public downloadFileMajorVersion ( filePath , downloadFilePath ? ) { let remoteFolder = FileHelper . getFolderFromPath ( filePath , this . _config ) ; let remoteFileUri = UrlHelper . getServerRelativeFileUri ( filePath . fsPath , this . _config ) ; let sharePointSiteUrl = WorkspaceHelper . getSiteUriForActiveWorkspace ( remoteFileUri . path , this . _config ) ; let context = { siteUrl : sharePointSiteUrl . toString ( ) , creds : RequestHelper . createCredentials ( vscode . window . spgo , this . _config ) } ; let options = { spBaseFolder : sharePointSiteUrl . path === '' ? '/' : sharePointSiteUrl . path , spRootFolder : UrlHelper . normalizeSlashes ( remoteFolder ) , strictObjects : [ remoteFileUri . path ] , dlRootFolder : downloadFilePath } ; return this . _fileGateway . downloadFiles ( context , options ) ; } public getFileInformation ( textDocument ) { let fileUri = UrlHelper . getServerRelativeFileUri ( textDocument . fsPath , this . _config ) ; let spr = RequestHelper . createRequest ( vscode . window . spgo , this . _config ) ; Logger . outputMessage ( `Getting file information for:  ${ textDocument . fsPath }` , vscode . window . spgo . outputChannel ) ; return this . _fileGateway . getFileInformation ( fileUri , spr ) ; } public checkoutFile ( filePath ) { let fileUri = UrlHelper . getServerRelativeFileUri ( filePath . fsPath , this . _config ) ; let spr = RequestHelper . createRequest ( vscode . window . spgo , this . _config ) ; Logger . outputMessage ( `Checking out File:  ${ fileUri . fsPath }` , vscode . window . spgo . outputChannel ) ; return this . _fileGateway . checkOutFile ( fileUri , spr ) ; } public publishWorkspace ( publishingInfo ) { let credentials = RequestHelper . createCredentials ( vscode . window . spgo , this . _config ) ; let remoteFileUri = Uri . parse ( `${ this . _config . sharePointSiteUrl }${ this . _config . publishWorkspaceOptions . destinationFolder }` ) ; let coreOptions = this . buildCoreUploadOptions ( remoteFileUri , publishingInfo ) ; let fileOptions = { glob : this . _config . publishWorkspaceOptions . globPattern , folder : this . _config . publishWorkspaceOptions . destinationFolder , base : this . _config . publishWorkspaceOptions . localRoot } ; Logger . outputMessage ( `publishing files:  ${ this . _config . publishWorkspaceOptions . globPattern }` , vscode . window . spgo . outputChannel ) ; return this . _fileGateway . uploadFiles ( coreOptions , credentials , fileOptions ) ; } public undoFileCheckout ( filePath ) { let fileUri = UrlHelper . getServerRelativeFileUri ( filePath . fsPath , this . _config ) ; let spr = RequestHelper . createRequest ( vscode . window . spgo , this . _config ) ; Logger . outputMessage ( `Discarding Check out for File:  ${ fileUri . fsPath }` , vscode . window . spgo . outputChannel ) ; return this . _fileGateway . undoCheckOutFile ( fileUri , spr ) ; } public uploadFilesToServer ( publishingInfo ) { let credentials = RequestHelper . createCredentials ( vscode . window . spgo , this . _config ) ; let remoteFileUri = UrlHelper . getServerRelativeFileUri ( publishingInfo . contentUri , this . _config ) ; let coreOptions = this . buildCoreUploadOptions ( remoteFileUri , publishingInfo ) ; let localFilePath = this . calculateBaseFolder ( coreOptions ) ; let fileOptions = { glob : publishingInfo . contentUri , base : localFilePath , folder : '/' } ; Logger . outputMessage ( `Uploading file:  ${ publishingInfo . contentUri }` , vscode . window . spgo . outputChannel ) ; return this . _fileGateway . uploadFiles ( coreOptions , credentials , fileOptions ) ; } private buildCoreUploadOptions ( remoteFileUri , publishingInfo ) { var coreOptions = { siteUrl : WorkspaceHelper . getSiteUriForActiveWorkspace ( remoteFileUri . toString ( ) , this . _config ) . toString ( ) , checkinMessage : encodeURI ( publishingInfo . message ) , checkin : false } ; if ( publishingInfo . scope === Constants . PUBLISHING_MAJOR ) { coreOptions . checkin = true ; coreOptions [ 'checkinType' ] = 1 ; } else if ( publishingInfo . scope === Constants . PUBLISHING_MINOR ) { coreOptions . checkin = true ; coreOptions [ 'checkinType' ] = 0 ; } return coreOptions ; } private calculateBaseFolder ( coreOptions ) { let subSiteUrl = coreOptions . siteUrl . split ( this . _config . sharePointSiteUrl ) [ 1 ] ; return this . _config . sourceRoot + FileHelper . convertToForwardSlash ( subSiteUrl ) ; } }