Example #1
0
        public OperationStatus PostFile(PublishFileModel publishFileModel)
        {
            PublishSkyDriveFileModel publishSkyDriveFileModel = (PublishSkyDriveFileModel)publishFileModel;
            Check.IsNotNull(publishSkyDriveFileModel.File, "File");
            Check.IsNotEmptyOrWhiteSpace(publishSkyDriveFileModel.File.FileName, "File name");
            Check.IsNotNull(publishSkyDriveFileModel.File.FileContent, "File content");
            if (publishSkyDriveFileModel.File.FileContent.Length == 0)
            {
                throw new ArgumentException("File content is of zero length.");
            }

            Check.IsNotNull(publishSkyDriveFileModel.AuthToken, "AuthToken");

            string fileId = this.UploadFile(publishSkyDriveFileModel);
            OperationStatus status = OperationStatus.CreateSuccessStatus();
            status.CustomReturnValues = fileId;
            return status;
        }
        /// <summary>
        /// method to post the file to repository
        /// </summary>
        /// <param name="request">request object</param>
        /// <param name="repositoryModel">repository model</param>
        /// <param name="file">file</param>
        /// <returns>returns File Identifier.</returns>
        public OperationStatus PostFile(PublishFileModel publishFileModel)
        {
            PublishDataVerseFileModel publishDataVerseFileModel = (PublishDataVerseFileModel)publishFileModel;
            Check.IsNotNull(publishDataVerseFileModel.File, "File");
            Check.IsNotEmptyOrWhiteSpace(publishDataVerseFileModel.File.FileName, "File name");
            Check.IsNotNull(publishDataVerseFileModel.File.FileContent, "File content");
            if (publishDataVerseFileModel.File.FileContent.Length == 0)
            {
                throw new ArgumentException("File content is of zero length.");
            }

            this.UploadFile(publishDataVerseFileModel);
            OperationStatus status = OperationStatus.CreateSuccessStatus();
            return status;
        }
Example #3
0
        public OperationStatus PostFile(PublishFileModel publishFileModel)
        {
            OperationStatus status;

            Encoding encoding = Encoding.UTF8;
            string fileName = string.Empty;
            HttpWebResponse httpResponse = null;
            Stream dataStream = null;
            StreamReader reader = null;
            string restResponse;
            PublishMerritFileModel publishMerritFileModel = (PublishMerritFileModel)publishFileModel;
            DataFile file = publishFileModel.File;
            MerritQueryData request = publishMerritFileModel.MerritQueryData;
            RepositoryModel repositoryModel = publishMerritFileModel.RepositoryModel;

            if (file != null && !string.IsNullOrEmpty(file.FileName))
            {
                fileName = file.FileName.Trim();
            }
       
            ////Ensure there's no directory path in the file name
            fileName = Path.GetFileName(fileName);

            currentFileName = Path.GetFileNameWithoutExtension(fileName);

            ////create temporary directory to unzip the files and zip the files again
            string tempFolder = MerritConstants.TempDownloadPath;

            Directory.CreateDirectory(tempFolder);

            DownloadFile(fileName, file.FileContent, file.FileExtentsion, file.IsCompressed, tempFolder);

            try
            {
                //// Create erc, eml and data-mainfest files
                CreateFile(MerritConstants.FileCreationType.ManiFest, Path.Combine(tempFolder, MerritConstants.MrtManifestFile), request, encoding, file.FileInfo, repositoryModel.SelectedRepository);
                CreateFile(MerritConstants.FileCreationType.ERC, Path.Combine(tempFolder, MerritConstants.MrtErcFile), request, encoding, file.FileInfo, repositoryModel.SelectedRepository);
                CreateFile(MerritConstants.FileCreationType.EML, Path.Combine(tempFolder, MerritConstants.MrtEmlFile), request, encoding, file.FileInfo, repositoryModel.SelectedRepository, publishFileModel.FileColumnTypes, publishFileModel.FileColumnUnits);
            }
            catch
            {
                ////supressed this error
            }

            ////actual zipping functionality will start here
            MemoryStream memoryStream = ZipUtilities.ZipFiles(tempFolder);
            memoryStream.Seek(0, SeekOrigin.Begin);

            byte[] byteArray = memoryStream.ToArray();

            string profile = request["Profile"].Value;
            string arkIdentity = request["ARK"].Value;
            string email = request["Creator: Email"] != null ? request["Creator: Email"].Value : MerritConstants.AdminEmail;

            Stream formDataStream = new System.IO.MemoryStream();
            try
            {
                string boundary = "--" + MerritConstants.Boundary;

                StringBuilder body = new StringBuilder();
                body.AppendLine(boundary);
                body.AppendLine("Content-disposition: form-data; name=\"file\"; filename=\"" + Path.GetFileNameWithoutExtension(fileName) + ".zip\"");
                body.AppendLine("Content-type: application/zip");
                body.AppendLine();

                formDataStream.Write(encoding.GetBytes(body.ToString()), 0, encoding.GetByteCount(body.ToString()));
                formDataStream.Write(byteArray, 0, byteArray.Length);
                formDataStream.Write(encoding.GetBytes(MerritConstants.CLRF), 0, encoding.GetByteCount(MerritConstants.CLRF));

                body = new StringBuilder();
                body.AppendLine(boundary);
                body.AppendLine("Content-disposition: form-data; name=\"type\"");
                body.AppendLine();
                body.AppendLine("container");
                body.AppendLine(boundary);
                body.AppendLine("Content-disposition: form-data; name=\"profile\"");
                body.AppendLine();
                body.AppendLine(profile);

                body.AppendLine(boundary);
                body.AppendLine("Content-disposition: form-data; name=\"primary-identifier\"");
                body.AppendLine();
                body.AppendLine(arkIdentity);
                body.AppendLine(boundary);
                body.AppendLine("Content-disposition: form-data; name=\"notification\"");
                body.AppendLine();
                if (string.IsNullOrWhiteSpace(email))
                {
                    body.AppendLine(MerritConstants.AdminEmail);
                }
                else
                {
                    body.AppendLine(email);
                }
                body.AppendLine(boundary);

                body.AppendLine("Content-disposition: form-data; name=\"digestType\"");
                body.AppendLine();
                body.AppendLine("md5");
                body.AppendLine(boundary);

                body.AppendLine("Content-disposition: form-data; name=\"digestValue\"");
                body.AppendLine();
                body.AppendLine(GenerateCheckSum(byteArray));
                body.AppendLine(boundary);

                body.AppendLine("Content-disposition: form-data; name=\"responseForm\"");
                body.AppendLine();

                ////Not sure is this right value or not. stress test required
                body.AppendLine("anvl");
                body.AppendLine(boundary + "--");

                formDataStream.Write(encoding.GetBytes(body.ToString()), 0, encoding.GetByteCount(body.ToString()));
                formDataStream.Position = 0;

                HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(repositoryModel.RepositoryLink);

                httpRequest.ContentType = "multipart/form-data; boundary=" + MerritConstants.Boundary;
                httpRequest.Method = "POST";
                httpRequest.Timeout = Constants.PostFileTimeOutMinutes * 60 * 1000;
                byte[] buffer = new byte[formDataStream.Length];
                formDataStream.Read(buffer, 0, buffer.Length);

                httpRequest.ContentLength = buffer.Length;
                httpRequest.Headers.Add("Authorization", "Basic " + repositoryModel.Authorization);

                dataStream = httpRequest.GetRequestStream();
                dataStream.Write(buffer, 0, buffer.Length);

                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                dataStream = httpResponse.GetResponseStream();
                reader = new StreamReader(dataStream);
                restResponse = reader.ReadToEnd();

                if (!restResponse.ToUpper().Contains("BATCHID"))
                {
                    status = OperationStatus.CreateFailureStatus("Issue with the repository configuration, Contact administrator.");
                }
                else
                {
                    status = OperationStatus.CreateSuccessStatus();
                }

            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (formDataStream != null)
                {
                    formDataStream.Dispose();
                }
                if (httpResponse != null)
                {
                    httpResponse.Close();
                }
            }

            return status;
        }