Пример #1
0
        public FolderState IsValidFolder(string folderPath, string username, string password)
        {
            try
            {
                var uri = PanoramaUtil.GetContainersUri(ServerUri, folderPath, false);

                using (var webClient = new WebClientWithCredentials(ServerUri, username, password))
                {
                    JToken response = webClient.Get(uri);

                    // User needs write permissions to publish to the folder
                    if (!PanoramaUtil.CheckFolderPermissions(response))
                    {
                        return(FolderState.nopermission);
                    }

                    // User can only upload to a TargetedMS folder type.
                    if (!PanoramaUtil.CheckFolderType(response))
                    {
                        return(FolderState.notpanorama);
                    }
                }
            }
            catch (WebException ex)
            {
                var response = ex.Response as HttpWebResponse;
                if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(FolderState.notfound);
                }
                else
                {
                    throw;
                }
            }
            return(FolderState.valid);
        }
Пример #2
0
        public override Uri  SendZipFile(Server server, string folderPath, string zipFilePath, IProgressMonitor progressMonitor)
        {
            _progressMonitor = progressMonitor;
            _progressStatus  = new ProgressStatus(String.Empty);
            var zipFileName = Path.GetFileName(zipFilePath) ?? string.Empty;

            // Upload zip file to pipeline folder.
            using (_webClient = new NonStreamBufferingWebClient(server.URI, server.Username, server.Password))
            {
                _webClient.UploadProgressChanged += webClient_UploadProgressChanged;
                _webClient.UploadFileCompleted   += webClient_UploadFileCompleted;

                var     webDav         = PanoramaUtil.Call(server.URI, @"pipeline", folderPath, @"getPipelineContainer", true);
                JObject jsonWebDavInfo = _webClient.Get(webDav);

                string webDavUrl = (string)jsonWebDavInfo[@"webDavURL"];

                // Upload Url minus the name of the zip file.
                var baseUploadUri = new Uri(server.URI, webDavUrl); // webDavUrl will start with the context path (e.g. /labkey/...). We will get the correct URL even if serverUri has a context path.
                // Use Uri.EscapeDataString instead of Uri.EscapleUriString.
                // The latter will not escape characters such as '+' or '#'
                var escapedZipFileName = Uri.EscapeDataString(zipFileName);
                var tmpUploadUri       = new Uri(baseUploadUri, escapedZipFileName + @".part");
                var uploadUri          = new Uri(baseUploadUri, escapedZipFileName);

                lock (this)
                {
                    // Write to a temp file first. This will be renamed after a successful upload or deleted if the upload is canceled.
                    // Add a "Temporary" header so that LabKey marks this as a temporary file.
                    // https://www.labkey.org/issues/home/Developer/issues/details.view?issueId=19220
                    _webClient.Headers.Add(@"Temporary", @"T");
                    _webClient.UploadFileAsync(tmpUploadUri, @"PUT", zipFilePath);

                    // Wait for the upload to complete
                    Monitor.Wait(this);
                }

                if (progressMonitor.IsCanceled)
                {
                    // Delete the temporary file on the server
                    progressMonitor.UpdateProgress(
                        _progressStatus =
                            _progressStatus.ChangeMessage(
                                Resources.WebPanoramaPublishClient_SendZipFile_Deleting_temporary_file_on_server));
                    DeleteTempZipFile(tmpUploadUri, server.AuthHeader);
                    return(null);
                }
                // Remove the "Temporary" header added while uploading the file
                _webClient.Headers.Remove(@"Temporary");

                // Make sure the temporary file was uploaded to the server
                ConfirmFileOnServer(tmpUploadUri, server.AuthHeader);

                // Rename the temporary file
                _progressStatus = _progressStatus.ChangeMessage(
                    Resources.WebPanoramaPublishClient_SendZipFile_Renaming_temporary_file_on_server);
                progressMonitor.UpdateProgress(_progressStatus);

                RenameTempZipFile(tmpUploadUri, uploadUri, server.AuthHeader);

                _progressStatus = _progressStatus.ChangeMessage(Resources.WebPanoramaPublishClient_SendZipFile_Waiting_for_data_import_completion___);
                progressMonitor.UpdateProgress(_progressStatus = _progressStatus.ChangePercentComplete(-1));

                // Data must be completely uploaded before we can import.
                Uri importUrl = PanoramaUtil.Call(server.URI, @"targetedms", folderPath, @"skylineDocUploadApi");

                // Need to tell server which uploaded file to import.
                var dataImportInformation = new NameValueCollection
                {
                    // For now, we only have one root that user can upload to
                    { @"path", @"./" },
                    { @"file", zipFileName }
                };

                JToken importResponse = _webClient.Post(importUrl, dataImportInformation);

                // ID to check import status.
                var details   = importResponse[@"UploadedJobDetails"];
                int rowId     = (int)details[0][@"RowId"];
                Uri statusUri = PanoramaUtil.Call(server.URI, @"query", folderPath, @"selectRows",
                                                  @"query.queryName=job&schemaName=pipeline&query.rowId~eq=" + rowId);
                bool complete = false;
                // Wait for import to finish before returning.
                Uri result = null;
                while (!complete)
                {
                    if (progressMonitor.IsCanceled)
                    {
                        return(null);
                    }

                    JToken jStatusResponse = _webClient.Get(statusUri);
                    JToken rows            = jStatusResponse[@"rows"];
                    var    row             = rows.FirstOrDefault(r => (int)r[@"RowId"] == rowId);
                    if (row == null)
                    {
                        continue;
                    }

                    string status = (string)row[@"Status"];
                    result = new Uri(server.URI, (string)row[@"_labkeyurl_Description"]);
                    if (string.Equals(status, @"ERROR"))
                    {
                        var jobUrl = new Uri(server.URI, (string)row[@"_labkeyurl_RowId"]);
                        var e      = new PanoramaImportErrorException(server.URI, jobUrl);
                        progressMonitor.UpdateProgress(
                            _progressStatus = _progressStatus.ChangeErrorException(e));
                        throw e;
                    }

                    complete = string.Equals(status, @"COMPLETE");
                }

                progressMonitor.UpdateProgress(_progressStatus.Complete());
                return(result);
            }
        }