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, expand, filter, map, mergeMap, switchMap } from 'rxjs/operators';\r\nimport { AppContext } from './app-context';\r\nimport { Net } from './net';\r\n\r\n/**\r\n * Sharing file mode.\r\n */\r\nexport enum DeploymentShareMode {\r\n    /**\r\n     * Admin share.\r\n     */\r\n    Admin = 'adminshare',\r\n\r\n    /**\r\n     * User share.\r\n     */\r\n    User = 'usershare'\r\n}\r\n\r\n/**\r\n * Download states.\r\n */\r\nexport enum DownloadState {\r\n    /**\r\n     * Download not started.\r\n     */\r\n    NotStarted = 'NotStarted',\r\n\r\n    /**\r\n     * Download is running.\r\n     */\r\n    Running = 'Running',\r\n\r\n    /**\r\n     * Download is cancelled.\r\n     */\r\n    Cancelled = 'Cancelled',\r\n\r\n    /**\r\n     * Download failed.\r\n     */\r\n    Failed = 'Failed',\r\n\r\n    /**\r\n     * Download completed.\r\n     */\r\n    Completed = 'Completed'\r\n}\r\n\r\n/**\r\n * Result data of created deployment share.\r\n */\r\nexport interface DeploymentShareResult {\r\n    /**\r\n     * deployment share mode.\r\n     */\r\n    mode: DeploymentShareMode;\r\n\r\n    /**\r\n     * Share name.\r\n     */\r\n    name: string;\r\n\r\n    /**\r\n     * Created time (ISO time).\r\n     */\r\n    createdTime: string;\r\n}\r\n\r\n/**\r\n * Request data of download.\r\n */\r\nexport interface DownloadRequest {\r\n    /**\r\n     * URL to download.\r\n     */\r\n    url: string;\r\n\r\n    /**\r\n     * Path to file to store.\r\n     */\r\n    path: string;\r\n\r\n    /**\r\n     * Flag indicating if the file URL is trusted and signature validation can be skipped.\r\n     */\r\n    trustedUrlOmitValidation?: boolean;\r\n}\r\n\r\n/**\r\n * Result data of download.\r\n */\r\nexport interface DownloadResult {\r\n    /**\r\n     * Download identity.\r\n     */\r\n    id: string;\r\n\r\n    /**\r\n     * Requested number of items.\r\n     */\r\n    requested: number;\r\n\r\n    /**\r\n     * Remaining number of items.\r\n     */\r\n    remaining: number;\r\n\r\n    /**\r\n     * Started time (ISO).\r\n     */\r\n    startedTime: string;\r\n}\r\n\r\n/**\r\n * Status data of download.\r\n */\r\nexport interface DownloadStatus {\r\n    /**\r\n     * Download identity.\r\n     */\r\n    id: string;\r\n\r\n    /**\r\n     * Requested number of items.\r\n     */\r\n    requested: number;\r\n\r\n    /**\r\n     * Remaining number of items.\r\n     */\r\n    remaining: number;\r\n\r\n    /**\r\n     * Error number of items.\r\n     */\r\n    errors: number;\r\n\r\n    /**\r\n     * Started time (ISO).\r\n     */\r\n    startedTime: string;\r\n\r\n    /**\r\n     * Completed time (ISO).\r\n     */\r\n    completedTime: string;\r\n\r\n    /**\r\n     * Last error message.\r\n     */\r\n    lastError?: string;\r\n\r\n    /**\r\n     * Sharing mode.\r\n     */\r\n    mode: DeploymentShareMode;\r\n\r\n    /**\r\n     * Name of network share.\r\n     */\r\n    name: string;\r\n\r\n    /**\r\n     * State of the download.\r\n     */\r\n    downloadState: DownloadState;\r\n}\r\n\r\n/**\r\n * Status of file on the shared folder.\r\n */\r\nexport interface FileStatus {\r\n    /**\r\n     * Url of download source.\r\n     */\r\n    url: string;\r\n\r\n    /**\r\n     * Timestamp when download completed (ISO).\r\n     */\r\n    timestamp: string;\r\n\r\n    /**\r\n     * File path within the share folder.\r\n     */\r\n    path: string;\r\n}\r\n\r\n/**\r\n * Secure deployment share class.\r\n */\r\nexport class DeploymentShare {\r\n    private static deploymentShareUrl = 'deployment/{0}';\r\n    private static deploymentShareNameUrl = 'deployment/{0}/{1}';\r\n    private static deploymentShareDownloadUrl = 'deployment/{0}/{1}/download';\r\n    private static deploymentShareDownloadIdUrl = 'deployment/{0}/{1}/download/{2}';\r\n    private static deploymentShareDownloadCancelUrl = 'deployment/{0}/{1}/download/{2}/cancel';\r\n    private static deploymentShareStatusUrl = 'deployment/status';\r\n    private static deploymentShareFileUrl = 'deployment/{0}/{1}/file';\r\n    private static deploymentShareFilePathUrl = 'deployment/{0}/{1}/file/{2}';\r\n    private static deploymentCopyUrl = 'deployment/{0}/{1}/copy';\r\n\r\n    /**\r\n     * Initializes a new instance of the DeploymentShare class.\r\n     *\r\n     * @param appContext the application context.\r\n     */\r\n    constructor(private appContext: AppContext) { }\r\n\r\n    /**\r\n     * Create a new deployment network share. (elevate the desktop gateway if required.)\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     * @param shareName the name of share.\r\n     */\r\n    public createShare(shareMode: DeploymentShareMode, shareName: string): Observable<DeploymentShareResult> {\r\n        const url = DeploymentShare.deploymentShareNameUrl.format(shareMode, shareName);\r\n        return this.appContext.gateway.put(url, '{}')\r\n            .pipe(\r\n                catchError((error, caught) => {\r\n                    // catch elevation error and retry after elevated.\r\n                    if (this.appContext.gateway.isElevationRequired(error)) {\r\n                        return this.appContext.gateway.pollingGatewayElevated()\r\n                            .pipe(switchMap(elevated => elevated ? caught : throwError(() => error)));\r\n                    }\r\n\r\n                    return throwError(() => error);\r\n                }),\r\n                map((response: DeploymentShareResult) => {\r\n                    if (!response) {\r\n                        throw new Error('DeploymentShare createShare() call is failed with null response result.');\r\n                    }\r\n\r\n                    return response;\r\n                })\r\n            );\r\n    }\r\n\r\n    /**\r\n     * Delete the deployment network share. (elevate the desktop gateway if required.)\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     * @param shareName the name of share.\r\n     */\r\n    public deleteShare(shareMode: DeploymentShareMode, shareName: string): Observable<void> {\r\n        const url = DeploymentShare.deploymentShareNameUrl.format(shareMode, shareName);\r\n        return this.appContext.gateway.delete(url)\r\n            .pipe(\r\n                catchError((error, caught) => {\r\n                    // catch elevation error and retry after elevated.\r\n                    if (this.appContext.gateway.isElevationRequired(error)) {\r\n                        return this.appContext.gateway.pollingGatewayElevated()\r\n                            .pipe(switchMap(elevated => elevated ? caught : throwError(() => error)));\r\n                    }\r\n\r\n                    return throwError(() => error);\r\n                }),\r\n                map(() => null)\r\n            );\r\n    }\r\n\r\n    /**\r\n     * List existing deployment network shares.\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     */\r\n    public queryShares(shareMode: DeploymentShareMode): Observable<DeploymentShareResult[]> {\r\n        const url = DeploymentShare.deploymentShareUrl.format(shareMode);\r\n        return this.appContext.gateway.get(url)\r\n            .pipe(map((response: any) => {\r\n                const value: DeploymentShareResult[] = response && response.value;\r\n                if (!value) {\r\n                    throw new Error('DeploymentShare queryShares() call is failed with null response result.');\r\n                }\r\n\r\n                return value;\r\n            }));\r\n    }\r\n\r\n    /**\r\n     * Download files from specified urls and store to the deployment network share folder.\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     * @param shareName the name of share.\r\n     * @param request the request object.\r\n     * @param timeout the timeout for the download in milliseconds.\r\n     * @param cancelReady (Optional) the cancel ready callback. Retain the supplied ID to call cancelDownload().\r\n     * @param polling (Optional) the polling timer milliseconds (default is 2000, and minimum is 1000).\r\n     */\r\n    public downloadFiles(\r\n        shareMode: DeploymentShareMode,\r\n        shareName: string,\r\n        request: DownloadRequest[],\r\n        timeout?: number,\r\n        cancelReady?: (id: string) => void,\r\n        polling?: number): Observable<DownloadStatus> {\r\n\r\n        // default polling every 2 seconds, and minimum every 1 second.\r\n        polling = polling == null ? 2000 : Math.max(polling, 1000);\r\n        const url = DeploymentShare.deploymentShareDownloadUrl.format(shareMode, shareName);\r\n        const body = JSON.stringify({ value: request, timeout: timeout });\r\n        return this.appContext.gateway.post(url, body)\r\n            .pipe(\r\n                mergeMap((response: DownloadResult) => {\r\n                    if (!response) {\r\n                        return throwError(() => new Error('DeploymentShare downloadFiles() call is failed with null response or empty Location header.'));\r\n                    }\r\n\r\n                    if (cancelReady) {\r\n                        cancelReady(response.id);\r\n                    }\r\n\r\n                    const statusUrl = DeploymentShare.deploymentShareDownloadIdUrl.format(shareMode, shareName, response.id);\r\n                    return this.queryStatus(statusUrl)\r\n                        .pipe(\r\n                            expand((status) => {\r\n                                if (status.downloadState === DownloadState.Running) {\r\n                                    return of(null)\r\n                                        .pipe(\r\n                                            delay(polling),\r\n                                            mergeMap(() => this.queryStatus(statusUrl))\r\n                                        );\r\n                                }\r\n\r\n                                return EMPTY;\r\n                            }),\r\n                            filter(status =>\r\n                                status.downloadState !== DownloadState.Running &&\r\n                                status.downloadState !== DownloadState.NotStarted\r\n                            ));\r\n                })\r\n            );\r\n    }\r\n\r\n    /**\r\n     * Cancel the download by ID.\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     * @param shareName the name of share.\r\n     * @param id the identification of download call responded by cancelReady() call back.\r\n     */\r\n    public cancelDownload(shareMode: DeploymentShareMode, shareName: string, id: string): Observable<void> {\r\n        const url = DeploymentShare.deploymentShareDownloadCancelUrl.format(shareMode, shareName, id);\r\n        return this.appContext.gateway.post(url, '{}')\r\n            .pipe(map(() => null));\r\n    }\r\n\r\n    /**\r\n     * Query active downloads with filtering conditions.\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     * @param shareName the name of share.\r\n     */\r\n    public queryDownloads(shareMode?: DeploymentShareMode, shareName?: string): Observable<DownloadStatus[]> {\r\n        const url = DeploymentShare.deploymentShareStatusUrl;\r\n        return this.appContext.gateway.get(url)\r\n            .pipe(\r\n                map((response: DownloadStatus[]) => {\r\n                    if (!response) {\r\n                        throw new Error('DeploymentShare queryDownloads() call is failed with null response.');\r\n                    }\r\n\r\n                    return response.filter(item => (!shareMode || item.mode === shareMode) && (!shareName || item.name === shareName));\r\n                })\r\n            );\r\n    }\r\n\r\n    /**\r\n     * Query files on the share.\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     * @param shareName the name of share.\r\n     */\r\n    public queryFiles(shareMode: DeploymentShareMode, shareName: string): Observable<FileStatus[]> {\r\n        const url = DeploymentShare.deploymentShareFileUrl.format(shareMode, shareName);\r\n        return this.appContext.gateway.get(url)\r\n            .pipe(\r\n                map((response: any) => {\r\n                    const files: FileStatus[] = response && response.files;\r\n                    if (!files) {\r\n                        throw new Error('DeploymentShare queryFiles() call is failed with null response.');\r\n                    }\r\n\r\n                    return files;\r\n                })\r\n            );\r\n    }\r\n\r\n    /**\r\n     * Delete a file or a directory on the share.\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     * @param shareName the name of share.\r\n     * @param path the path to the file or the directory on the share.\r\n     */\r\n    public deleteItem(shareMode: DeploymentShareMode, shareName: string, path: string): Observable<void> {\r\n        const path64 = Net.base64urlEncode(path);\r\n        const url = DeploymentShare.deploymentShareFilePathUrl.format(shareMode, shareName, path64);\r\n        return this.appContext.gateway.delete(url)\r\n            .pipe(map(() => null));\r\n    }\r\n\r\n    /**\r\n     * Create a text/XML file in network share.\r\n     *\r\n     * @param shareMode the sharing mode.\r\n     * @param shareName the name of share.\r\n     * @param path the path to the file or the directory on the share.\r\n     * @param content the linese of content for the file to create.\r\n     */\r\n    public createTextFile(shareMode: DeploymentShareMode, shareName: string, path: string, content: string[]): Observable<void> {\r\n        const path64 = Net.base64urlEncode(path);\r\n        const url = DeploymentShare.deploymentShareFilePathUrl.format(shareMode, shareName, path64);\r\n\r\n        const body = JSON.stringify({ content: content });\r\n\r\n        return this.appContext.gateway.post(url, body).pipe(map(() => null));\r\n    }\r\n\r\n    /**\r\n     * Copy file or folder in network share.\r\n     * @param shareMode The share mode.\r\n     * @param shareName the name of the share.\r\n     * @param source The source path of the item to copy relative to the network share.\r\n     * @param destination The destination path of the item to copy relative to the network share.\r\n     */\r\n    public copyItem(shareMode: DeploymentShareMode, shareName: string, source: string, destination: string): Observable<void> {\r\n        const url = DeploymentShare.deploymentCopyUrl.format(shareMode, shareName);\r\n\r\n        const body = JSON.stringify({ source: source, destination: destination });\r\n\r\n        return this.appContext.gateway.post(url, body).pipe(map(() => null));\r\n    }\r\n\r\n    private queryStatus(statusUrl: string): Observable<DownloadStatus> {\r\n        return this.appContext.gateway.get(statusUrl)\r\n            .pipe(map((response: DownloadStatus) => {\r\n                if (!response) {\r\n                    throw new Error('DeploymentShare download() call is failed with null response for status.');\r\n                }\r\n\r\n                return response;\r\n            }));\r\n    }\r\n}\r\n"]}