Exemplo n.º 1
0
 /// <summary>
 /// Uploads files to the repository.
 /// </summary>
 /// <param name="dataFile">File to be uploaded.</param>
 /// <returns>Uploaded files.</returns>
 public IEnumerable<DataDetail> Upload(DataFile dataFile)
 {
     Check.IsNotNull(dataFile, "dataFile");
     List<DataDetail> collection = new List<DataDetail>();
     DataDetail dataDetail = base.Upload(dataFile);
     collection.Add(dataDetail);
     return collection;
 }
        public void Upload_ShouldReturnUploadedFileWithFileIdGreaterThanZero_WhenAFileIsUploaded()
        {
            IFileService fileService = new StubIFileService()
            {
                UploadFileDataDetail = dd => { dd.FileDetail.FileId = 1; return dd; }
            };

            DefaultFileHandler defaultFileHandler = new DefaultFileHandler(fileService);
            DataFile dataFile = new DataFile() { FileContent = new byte[0] };
            DataDetail dataDetail = defaultFileHandler.Upload(dataFile).First();
            Assert.IsTrue(dataDetail.FileDetail.FileId > 0);
        }
Exemplo n.º 3
0
        public static void SetValuesFrom(this DataDetail dataDetail, DataFile dataFile)
        {
            Check.IsNotNull(dataDetail, "dataDetail");
            Check.IsNotNull(dataFile, "dataFile");

            dataDetail.FileDetail.CreatedBy = dataFile.CreatedBy;
            dataDetail.FileDetail.CreatedOn = DateTime.UtcNow;
            dataDetail.FileDetail.MimeType = dataFile.ContentType;
            dataDetail.FileDetail.Name = dataFile.FileName;
            dataDetail.FileDetail.Size = dataFile.FileContent.Length;
            dataDetail.FileDetail.Title = Path.GetFileNameWithoutExtension(dataFile.FileName);
            dataDetail.FileDetail.Status = FileStatus.Uploaded.ToString();
            dataDetail.FileDetail.CreatedBy = dataFile.CreatedBy;
            dataDetail.FileDetail.isDeleted = false;
        }
Exemplo n.º 4
0
        public DataDetail(DataFile dataFile)
            : this()
        {
            Check.IsNotNull(dataFile, "dataFile");

            this.DataStream = dataFile.FileContent;
            this.FileDetail.CreatedBy = dataFile.CreatedBy;
            this.FileDetail.CreatedOn = DateTime.UtcNow;
            this.FileDetail.MimeType = dataFile.ContentType;
            this.FileDetail.Name = dataFile.FileName;
            this.FileDetail.Size = dataFile.FileContent.Length;
            this.FileDetail.Title = System.IO.Path.GetFileNameWithoutExtension(dataFile.FileName);
            this.FileDetail.Status = FileStatus.Uploaded.ToString();
            this.FileDetail.CreatedBy = dataFile.CreatedBy;
            this.FileDetail.isDeleted = false;
        }
Exemplo n.º 5
0
        public IEnumerable<DataDetail> Upload(DataFile dataFile)
        {
            Check.IsNotNull(dataFile, "dataFile");
            List<DataDetail> collection = new List<DataDetail>();
            List<DataFile> dataFiles = null;
            using (MemoryStream memoryStream = new MemoryStream(dataFile.FileContent))
            {
                dataFiles = ZipUtilities.GetListOfFilesFromStream(memoryStream, this.userId);
            }

            foreach (var df in dataFiles)
            {
                var uploadedDataFiles = base.Upload(df);
                collection.Add(uploadedDataFiles);
            }
            return collection;
        }
        public void Upload_ShouldReturnUploadedFileWithFileIdGreaterThanZero_WhenAFileIsUploaded()
        {
            IFileService fileService = new StubIFileService()
            {
                UploadFileDataDetail = dd => { dd.FileDetail.FileId = 1; return dd; }
            };

            DefaultFileHandler defaultFileHandler = new DefaultFileHandler(fileService);
            DataFile dataFile = new DataFile() { FileContent = new byte[0] };
            IEnumerable<DataDetail> dataDetails;
            List<DataFile> dataFiles = new List<DataFile>();
            dataFiles.Add(new DataFile() { FileContent = new byte[0] });
            dataFiles.Add(new DataFile() { FileContent = new byte[0] });

            using (ShimsContext.Create())
            {
                ShimZipUtilities.GetListOfFilesFromStreamStreamInt32 = (zipstream, userId) => dataFiles;
                dataDetails = defaultFileHandler.Upload(dataFile);
            }

            Assert.IsTrue(dataDetails.All(fd => fd.FileDetail.FileId > 0));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Method to download the file from the repository.
        /// </summary>
        /// <param name="downloadInput">Input data required to download the file from the repository.</param>
        /// <returns>Downloaded file data.</returns>
        public DataFile DownloadFile(string downloadUrl, string authorization, string fileName)
        {
            DataFile dataFile = new DataFile();

            AuthToken token = new AuthToken()
            {
                AccessToken = authorization
            };

            dataFile.FileContent = this.DownloadFile(downloadUrl, token);
            string extension = Path.GetExtension(fileName);

            if (extension.ToUpperInvariant().Equals(Constants.XLSX, StringComparison.OrdinalIgnoreCase))
            {
                dataFile.FileExtentsion = extension;
            }
            else
            {
                dataFile.FileExtentsion = ".zip";
            }

            dataFile.FileName = string.Format("{0}{1}", Path.GetFileNameWithoutExtension(fileName), dataFile.FileExtentsion);
            return dataFile;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Method to download the fiel from the specific repository.
        /// </summary>
        /// <param name="downloadInput">Input data required to download the file from the repository.</param>
        /// <returns>Downloaded file data.</returns>
        public DataFile DownloadFile(string downloadUrl, string authorization, string fileName)
        {
            DataFile dataFile = new DataFile();

            Stream outStream = null;
            try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(downloadUrl);
                httpRequest.Method = "GET";

                httpRequest.Headers.Add("Authorization", "Basic " + authorization);
                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                HttpWebResponse ws = (HttpWebResponse)httpRequest.GetResponse();
                outStream = ws.GetResponseStream();

                string zipFileName = Path.GetFileNameWithoutExtension(fileName) + ".zip";

                if (outStream != null)
                {
                    //// writing stream to a temp path

                    string filePath = string.Format(@"{0}{1}\", Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

                    string totalFilePath = string.Format("{0}{1}", filePath, zipFileName);


                    DirectoryInfo dir = Directory.CreateDirectory(filePath);

                    using (Stream zipStream = System.IO.File.OpenWrite(totalFilePath))
                    {
                        outStream.CopyToStream(zipStream);
                    }

                    using (ZipFile tmpZipFile = ZipFile.Read(totalFilePath))
                    {
                        tmpZipFile.ExtractAll(filePath, ExtractExistingFileAction.OverwriteSilently);
                    }

                    // FileInfo[] directories = null;
                    var fileList = dir.GetFiles("*", SearchOption.AllDirectories).ToList();

                    // If the file is xlsx file then download only the actual file

                    if (Path.GetExtension(fileName).Equals(MerritConstants.XLSX, StringComparison.OrdinalIgnoreCase))
                    {
                        var fileDownload = fileList.Find(s => s.Name.Equals(fileName, StringComparison.InvariantCultureIgnoreCase));
                        dataFile.FileContent = File.ReadAllBytes(fileDownload.FullName);
                        dataFile.FileName = fileName;
                        dataFile.ContentType = Helper.GetContentType(Path.GetExtension(fileDownload.FullName));
                    }
                    else
                    {
                        string subDirPath = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
                        string fileNameWithOutExt = Path.GetFileNameWithoutExtension(fileName);

                        var fileDownload = fileList.Where(file => (file.Name.Contains(fileNameWithOutExt) || file.Name.Contains("metadata")) && (Path.GetExtension(file.Name) != ".zip")).ToList();

                        if (fileDownload != null && fileDownload.Count > 0)
                        {
                            dir.CreateSubdirectory(subDirPath);

                            foreach (var fileInf in fileDownload)
                            {
                                fileInf.CopyTo(filePath + subDirPath + "\\" + fileInf.Name);
                            }
                        }
                        dataFile.FileName = fileNameWithOutExt + ".zip";
                        dataFile.ContentType = MerritConstants.ZipMimeType;
                        dataFile.FileContent = ZipFileHelper.ZipFiles(filePath + subDirPath).GetBytes();
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    WebResponse errResp = ex.Response;
                    using (Stream respStream = errResp.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(respStream))
                        {
                            throw new Exception(ex.Message);
                        }
                    }
                }
            }
            return dataFile;
        }
Exemplo n.º 9
0
 /// <summary>
 /// Uploads files to the repository.
 /// </summary>
 /// <param name="dataFile">File to be uploaded.</param>
 /// <returns>Uploaded files.</returns>
 public virtual DataDetail Upload(DataFile dataFile)
 {
     var uploadedDataDetail = this.fileService.UploadFile(new DataDetail(dataFile));
     return uploadedDataDetail;
 }
Exemplo n.º 10
0
        /// <summary>
        /// Removes errors from a file sheet.
        /// </summary>
        /// <param name="userId">User Id</param>
        /// <param name="fileId">File Id</param>
        /// <param name="sheetName">Sheet Name</param>
        /// <param name="errorTypeList">Error type list</param>
        /// <returns>Boolean indicating whether the removal was successful or not.</returns>
        public async Task RemoveErrors(int userId, int fileId, string sheetName, IEnumerable<ErrorType> errorTypeList)
        {
            DM.File file = this.FileDataRepository.GetItem(userId, fileId);
            if (null == file)
            {
                throw new DataFileNotFoundException() { FileId = fileId };
            }
            else if (Path.GetExtension(file.Name) != Constants.XLFileExtension)
            {
                throw new InvalidOperationException(Messages.BestPractisesNotSupportedMessage);
            }

            IFileProcesser fileProcesser = FileFactory.GetFileTypeInstance(Path.GetExtension(file.Name), this.BlobDataRepository, this.FileDataRepository, this.RepositoryService);
            using (Stream stream = GetFileStream(file.BlobId))
            {
                await fileProcesser.RemoveError(stream, sheetName, errorTypeList);

                if (Constants.UploadToAzure)
                {
                    DataFile dataFile = new DataFile()
                    {
                        FileContent = await stream.GetBytesAsync(),
                        ContentType = MimeMapping.GetMimeMapping(file.Name),
                        FileName = file.Name,
                        CreatedBy = userId,
                        FileExtentsion = Path.GetExtension(file.Name)
                    };

                    DataDetail dataDetail = new DataDetail(dataFile);
                    file.ModifiedBy = dataDetail.FileDetail.CreatedBy;
                    file.ModifiedOn = DateTime.UtcNow;
                    file.Size = dataDetail.FileDetail.Size;
                    dataDetail.FileDetail.BlobId = file.BlobId;
                    if (this.BlobDataRepository.UploadFile(dataDetail))
                    {
                        file = this.FileDataRepository.UpdateFile(file);
                    }

                    this.UnitOfWork.Commit();
                    dataDetail.DataStream = null;
                }
            }
        }
Exemplo n.º 11
0
        public DataFile GetDataFile(DM.File file)
        {
            Check.IsNotNull(file, "file");

            IFileProcesser fileProcesser = FileFactory.GetFileTypeInstance(Path.GetExtension(file.Name), this.BlobDataRepository, this.FileDataRepository, this.RepositoryService);
            DataDetail dataDetail = fileProcesser.DownloadDocument(file);

            DataFile dataFile = new DataFile();
            dataFile.ContentType = dataDetail.MimeTypeToDownLoad;
            dataFile.CreatedBy = dataDetail.FileDetail.CreatedBy;
            dataFile.FileInfo = dataDetail.FileDetail;
            dataFile.FileExtentsion = System.IO.Path.GetExtension(dataDetail.FileNameToDownLoad);
            dataFile.FileName = dataDetail.FileNameToDownLoad;
            dataFile.FileContent = dataDetail.DataStream;
            dataFile.IsCompressed = true;

            if (dataFile.FileExtentsion.Equals(Constants.XLFileExtension, StringComparison.OrdinalIgnoreCase))
            {
                dataFile.IsCompressed = false;
            }

            return dataFile;
        }
Exemplo n.º 12
0
        public async Task<HttpResponseMessage> Post(string fileName, string fileExtension, string contentType)
        {
            try
            {
                Check.IsNotEmptyOrWhiteSpace(fileName, "fileName");
                Check.IsNotEmptyOrWhiteSpace(fileExtension, "fileExtension");
                Check.IsNotEmptyOrWhiteSpace(contentType, "contentType");

                byte[] fileContent = default(byte[]);
                if (Request.Content.IsMimeMultipartContent("form-data"))
                {
                    string root = Path.GetTempPath();
                    MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(root);
                    await Request.Content.ReadAsMultipartAsync(streamProvider);
                    MultipartFileData multipartFileData = streamProvider.FileData.First();

                    /*********************************************************
                     * This API under stress can cause IO exception, for File read
                     * or Delete operation. More details @https://aspnetwebstack.codeplex.com/workitem/176 
                     * Following logic retries file operations at most 3 times
                     * inducing the recommended delay in case of failure. 
                     */ 
                    int executionDelay = 50, 
                        readRetryCount = 3,
                        deleteRetryCount = 3;

                    // Retry mechanics for file read operation
                    while (readRetryCount-- > 0) 
                    {
                        try
                        {
                            using (FileStream fileSource = new FileStream(multipartFileData.LocalFileName, FileMode.Open, FileAccess.Read))
                            {
                                fileContent = await fileSource.GetBytesAsync();
                            }
                            break;
                        }
                        catch (IOException ioe)
                        {
                            diagnostics.WriteErrorTrace(TraceEventId.Exception, ioe.ToString());
                        }
                        await Task.Delay(executionDelay);
                    }

                    // Retry mechanics for file delete operation
                    while (deleteRetryCount-- > 0)
                    {
                        try
                        {
                            await Task.Factory.StartNew(() => System.IO.File.Delete(multipartFileData.LocalFileName));
                            break;
                        }
                        catch (IOException ioe)
                        {
                            diagnostics.WriteErrorTrace(TraceEventId.Exception, ioe.ToString());
                        }
                        await Task.Delay(executionDelay);
                    }

                }
                else
                {
                    var bufferlessInputStream = HttpContext.Current.Request.GetBufferlessInputStream();
                    Check.IsNotNull(bufferlessInputStream, "HttpContext.Current.Request.GetBufferlessInputStream()");
                    fileContent = await bufferlessInputStream.GetBytesAsync();
                }

                this.fileService.ValidateForUpload(fileName, this.user.UserId);

                DataFile dataFile = new DataFile
                {
                    FileContent = fileContent,
                    ContentType = contentType,
                    FileName = fileName,
                    CreatedBy = this.user.UserId,
                    FileExtentsion = fileExtension
                };

                IFileHandler fileHandler = this.fileHandlerFactory.GetFileHandler(contentType, this.user.UserId);
                diagnostics.WriteInformationTrace(Utilities.Enums.TraceEventId.Flow, "blob-before file upload");
                var uploadedDataDetails = fileHandler.Upload(dataFile);
                diagnostics.WriteInformationTrace(Utilities.Enums.TraceEventId.Flow, "blob-after file upload");
                if (!uploadedDataDetails.Any() || uploadedDataDetails.Any(dd => dd.FileDetail.FileId == 0))
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, MessageStrings.UploadFailed);
                }

                return Request.CreateResponse<IEnumerable<DataDetail>>(HttpStatusCode.OK, uploadedDataDetails);
            }
            catch (ValidationException validationException)
            {
                HttpError error = validationException.GetHttpError(string.Format(MessageStrings.Upload_Validation_Error_Template, validationException.FileName));
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
            }
            catch (ArgumentException ex)
            {
                message = string.Format(CultureInfo.CurrentCulture, MessageStrings.Argument_Error_Message_Template, ex.ParamName);
                status = HttpStatusCode.BadRequest;
            }

            return Request.CreateErrorResponse(status, message);
        }
Exemplo n.º 13
0
        public async Task<HttpResponseMessage> Post(string nameIdentifier)
        {
            try
            {
                Check.IsNotEmptyOrWhiteSpace(nameIdentifier, "nameIdentifier");
                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

                string root = Path.GetTempPath();
                MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(root);
                await Request.Content.ReadAsMultipartAsync(streamProvider);
                MultipartFileData multipartFileData = streamProvider.FileData.First();
                byte[] fileContent;
                using (FileStream fileSource = new FileStream(multipartFileData.LocalFileName, FileMode.Open, FileAccess.Read))
                {
                    fileContent = await fileSource.GetBytesAsync();
                }

                string fileName = multipartFileData.Headers.ContentDisposition.FileName.Trim(new char[] { '\"' });
                string fileExtension = Path.GetExtension(fileName);
                string contentType = MimeMapping.GetMimeMapping(fileName);
                Task.Factory.StartNew(() => System.IO.File.Delete(multipartFileData.LocalFileName));
                User impersonatedUser = IdentityHelper.GetUser(this.userService, nameIdentifier);

                this.fileService.ValidateForUpload(fileName, impersonatedUser.UserId);

                DataFile dataFile = new DataFile
                {
                    FileContent = fileContent,
                    ContentType = contentType,
                    FileName = fileName,
                    CreatedBy = impersonatedUser.UserId,
                    FileExtentsion = fileExtension
                };

                IFileHandler fileHandler = this.fileHandlerFactory.GetFileHandler(contentType, impersonatedUser.UserId);
                var uploadedDataDetails = fileHandler.Upload(dataFile);
                if (!uploadedDataDetails.Any() || uploadedDataDetails.Any(dd => dd.FileDetail.FileId == 0))
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, MessageStrings.UploadFailed);
                }

                return Request.CreateResponse<IEnumerable<DataDetail>>(HttpStatusCode.OK, uploadedDataDetails);
            }
            catch (ArgumentException ex)
            {
                message = string.Format(CultureInfo.CurrentCulture, MessageStrings.Argument_Error_Message_Template, ex.ParamName);
                status = HttpStatusCode.BadRequest;
            }

            return Request.CreateErrorResponse(status, message);
        }