Class representing the model for files which are uploaded
예제 #1
0
        public DataDetail DownloadDocument(DM.File fileDetails)
        {
            Check.IsNotNull<DM.File>(fileDetails, "filetoDownload");
            Check.IsNotNull<ICollection<FileMetadataField>>(fileDetails.FileMetadataFields, "fileMetadata");

            DataDetail dataDetail = new DataDetail();
            dataDetail.FileDetail = fileDetails;

            // Update the metadata to stream, create one more metasheet tab in the file
            dataDetail = this.UpdateMetaDataSheetForExcel(fileDetails);
            dataDetail.MimeTypeToDownLoad = fileDetails.MimeType;
            dataDetail.FileNameToDownLoad = fileDetails.Name;

            return dataDetail;
        }
 public void Initialize()
 {
     this.fileService = new StubIFileService()
     {
         CheckFileExistsStringInt32 = (fileName, userId) =>
         {
             return true;
         },
         UploadFileDataDetail = (dataDetail) =>
         {
             DataDetail objDetail = new DataDetail();
             objDetail.FileDetail.FileId = 1;
             objDetail.FileDetail.CreatedBy = 1;
             return objDetail;
         }
     };
 }
예제 #3
0
        public DataDetail UpdateMetaDataSheetForExcel(DM.File fileDetails)
        {
            Check.IsNotNull(fileDetails, "fileDetails");

            DataDetail dataDetail = new DataDetail();
            dataDetail.FileDetail = fileDetails;
            Stream stream = null;
            try
            {
                stream = GetFileStream(fileDetails.BlobId);

                // do not processor further as the file is not associates with repository.
                if (fileDetails.Status == FileStatus.Uploaded.ToString() || fileDetails.RepositoryId == null || fileDetails.RepositoryId <= 0)
                {
                    dataDetail.DataStream = stream.GetBytes();
                    return dataDetail;
                }

                //FOR CSV, AT THE TIME OF PUBLISH ,ONE MORE CSV SHEET WILL BE CRATED AND SENT AS ZIP
                using (SpreadsheetDocument excelDocument = SpreadsheetDocument.Open(stream, true))
                {
                    Sheet metadataSheet = null;

                    var repositoryMetadata = base.RepositoryService.GetRepositoryById((int)fileDetails.RepositoryId).RepositoryMetadata.FirstOrDefault();
                    ICollection<RepositoryMetadataField> repositoryMetadataFields = null;
                    if (repositoryMetadata != null)
                    {
                        repositoryMetadataFields = repositoryMetadata.RepositoryMetadataFields;
                    }

                    if ((repositoryMetadata != null && repositoryMetadataFields != null) || (fileDetails.FileColumns != null && fileDetails.FileColumns.Any()))
                    {
                        //create the sheet in any case if filecolumns exists from db or it was already existing in the file
                        metadataSheet = excelDocument.InsertMetadataWorksheet(fileDetails.FileMetadataFields.Any(), fileDetails.FileColumns.Any());
                    }

                    if (metadataSheet != null)
                    {
                        var fileColumnUnits = base.FileDataRepository.RetrieveFileColumnUnits();
                        var fileColumnTypes = base.FileDataRepository.RetrieveFileColumnTypes();
                        char column = 'A';

                        int fileMetaDataIndexer = 0;
                        if (repositoryMetadataFields != null && repositoryMetadataFields.Any() && fileDetails.FileMetadataFields != null && fileDetails.FileMetadataFields.Any())
                        {
                            foreach (var repositoryMetaData in repositoryMetadataFields)
                            {
                                // first write the repository metadata
                                excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "A{0}", fileMetaDataIndexer + 2), repositoryMetaData.Name);

                                // write teh metadata value if it exists
                                var fileMetaDataField = fileDetails.FileMetadataFields.Where(fm => fm.RepositoryMetadataFieldId == repositoryMetaData.RepositoryMetadataFieldId).FirstOrDefault();

                                if (fileMetaDataField != null)
                                {
                                    excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "B{0}", fileMetaDataIndexer + 2), fileMetaDataField.MetadataValue);
                                }

                                fileMetaDataIndexer = fileMetaDataIndexer + 1;
                            }

                            column = 'D';
                        }

                        int cellCount = 2;
                        if (fileDetails.FileColumns != null && fileDetails.FileColumns.Any())
                        {
                            char entityName = column, entityDescription = (char)(column + 1), name = (char)(column + 2), description = (char)(column + 3), fileType = (char)(column + 4), fileUnit = (char)(column + 5);

                            foreach (var val in fileDetails.FileColumns)
                            {
                                excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "{0}{1}", entityName, cellCount), val.EntityName);
                                excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "{0}{1}", entityDescription, cellCount), val.EntityDescription);
                                excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "{0}{1}", name, cellCount), val.Name);
                                excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "{0}{1}", description, cellCount), val.Description);
                                if (val.FileColumnTypeId != null && val.FileColumnUnitId != 0 && fileColumnTypes != null)
                                {
                                    excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "{0}{1}", fileType, cellCount), fileColumnTypes.Where(fc => fc.FileColumnTypeId == val.FileColumnTypeId).FirstOrDefault().Name);
                                }
                                else
                                {
                                    excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "{0}{1}", fileType, cellCount), string.Empty);
                                }
                                if (val.FileColumnUnitId != null && val.FileColumnUnitId != 0 && fileColumnUnits != null)
                                {
                                    excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "{0}{1}", fileUnit, cellCount), fileColumnUnits.Where(fu => fu.FileColumnUnitId == val.FileColumnUnitId).FirstOrDefault().Name);
                                }
                                else
                                {
                                    excelDocument.SetCellValue(metadataSheet, string.Format(CultureInfo.InvariantCulture, "{0}{1}", fileUnit, cellCount), string.Empty);
                                }
                                cellCount++;
                            }
                        }

                        WorksheetPart worksheetPart = excelDocument.WorkbookPart.GetPartById(metadataSheet.Id) as WorksheetPart;
                        if (worksheetPart != null)
                        {
                            TableDefinitionPart metadataTable = null;
                            TableDefinitionPart parameterTable = null;

                            // get the tables in Excel
                            var tableParts = worksheetPart.GetPartsOfType<TableDefinitionPart>();
                            if (tableParts != null)
                            {
                                foreach (var item in tableParts)
                                {
                                    if (item.Table.Name == Constants.MetadataRangeName)
                                    {
                                        metadataTable = item;
                                        if (fileDetails.FileMetadataFields != null && fileDetails.FileMetadataFields.Any())
                                        {
                                            metadataTable.Table.Reference = "A1:B" + (fileDetails.FileMetadataFields.Count + 1);
                                        }

                                        metadataTable.Table.Save();
                                    }

                                    if (item.Table.Name == Constants.ParaMetadataRangeName && fileDetails.FileColumns.Any())
                                    {
                                        parameterTable = item;
                                        if (fileDetails.FileColumns != null && fileDetails.FileColumns.Any() && fileDetails.FileMetadataFields.Any())
                                        {
                                            parameterTable.Table.Reference = string.Format(CultureInfo.InvariantCulture, "{0}1:{1}{2}", column, (char)(column + 5), fileDetails.FileColumns.Count + 1);
                                        }

                                        parameterTable.Table.Save();
                                    }
                                }
                            }
                        }

                        excelDocument.WorkbookPart.Workbook.Save();
                    }
                }

                dataDetail.DataStream = stream.GetBytes();
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
            return dataDetail;
        }
        public DataDetail UploadFile(DataDetail dataDetail)
        {
            Check.IsNotNull(dataDetail, "dataDetail");

            bool isUpdate = true;
            var file = new DomainModel.File();
            if (Constants.UploadToAzure)
            {
                file = this.FileDataRepository.GetItem(dataDetail.FileDetail.CreatedBy, dataDetail.FileDetail.Name);
                if (file == null || file.Status.Equals(FileStatus.Posted.ToString(), StringComparison.Ordinal))
                {
                    isUpdate = false;

                    // set the values from the passed object
                    file = dataDetail.FileDetail;
                    file.CreatedOn = DateTime.UtcNow;
                    file.ModifiedOn = DateTime.UtcNow;
                    file.BlobId = Guid.NewGuid().ToString();
                }
                else
                {
                    file.ModifiedBy = dataDetail.FileDetail.CreatedBy;
                    file.ModifiedOn = DateTime.UtcNow;
                    file.Size = dataDetail.FileDetail.Size;
                    dataDetail.FileDetail.BlobId = file.BlobId;
                }
                if (this.BlobDataRepository.UploadFile(dataDetail))
                {
                    if (!isUpdate)
                    {
                        file = this.FileDataRepository.AddFile(file);
                    }
                    else
                    {
                        file = this.FileDataRepository.UpdateFile(file);
                    }
                }

                this.UnitOfWork.Commit();
            }
            dataDetail.FileDetail = file;
            dataDetail.DataStream = null;
            return dataDetail;
        }
        /// <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;
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Upload the blob content to azure.
        /// </summary>
        /// <param name="details">
        /// Details of the blob.
        /// </param>
        /// <param name="containerName">
        /// Name of the container.
        /// </param>
        /// <returns>
        /// True, if the blob is successfully uploaded to azure;otherwise false.
        /// </returns>
        private static bool UploadBlobContent(DataDetail details, string containerName)
        {
            var uploadStatus = false;
            try
            {
                new { details, containerName }.CheckNotNull();
                CloudBlobContainer container = GetContainer(containerName);

                using (Stream filestream = new MemoryStream(details.DataStream))
                {

                    // Seek to start.
                    filestream.Position = 0;

                    // TODO: Check if the input file type and then use either block blob or page blob.
                    // For plate file we need to upload the file as page blob.
                    var blob = container.GetBlockBlobReference(details.FileDetail.BlobId.ToUpperInvariant());
                    blob.Properties.ContentType = details.FileDetail.MimeType;
                    blob.UploadFromStream(filestream, null, new BlobRequestOptions() { ServerTimeout = TimeSpan.FromMinutes(30) });
                }

                uploadStatus = true;
            }
            catch (InvalidOperationException)
            {
                uploadStatus = false;
                throw;
            }
            return uploadStatus;
        }
예제 #7
0
        /// <summary>
        /// Gets the content of the blob from specified container.
        /// </summary>
        /// <param name="blobName">
        /// Name of the blob.
        /// </param>
        /// <param name="containerName">
        /// name of the container.
        /// </param>
        /// <returns>
        /// The blob details.
        /// </returns>
        private static DataDetail GetBlob(string blobName, string containerName)
        {
            new { blobName, containerName }.CheckNotNull();

            using (Stream outputStream = new MemoryStream())
            {
                CloudBlobContainer container = GetContainer(containerName);
                GetContent(blobName, outputStream, container);

                DataDetail dataDetail = new DataDetail()
                {
                    DataStream = outputStream.GetBytes()
                };

                return dataDetail;
            }
        }
예제 #8
0
        /// <summary>
        /// Checks if the blob content is present in azure or not.
        /// </summary>
        /// <param name="details">
        /// Details of the blob.
        /// </param>
        /// <param name="containerName">
        /// Name of the container.
        /// </param>
        /// <returns>
        /// True, if the blob is successfully found to azure;otherwise false.
        /// </returns>
        private static bool ExistsBlobContent(DataDetail details, string containerName)
        {
            var returnStatus = false;
            new { details, containerName }.CheckNotNull();

            CloudBlobContainer container = GetContainer(containerName);

            // TODO: Check if the input file type and then use either block blob or page blob.
            // For plate file we need to upload the file as page blob.
            var blob = container.GetBlobReferenceFromServer(details.FileDetail.BlobId.ToUpperInvariant());
            blob.FetchAttributes();
            returnStatus = true;

            return returnStatus;
        }
예제 #9
0
 /// <summary>
 /// Uploads a file to azure as a blob.
 /// </summary>
 /// <param name="details">
 /// Details of the file which has to be uploaded to azure.
 /// </param>
 /// <returns>
 /// True if the file is uploaded successfully; otherwise false.
 /// </returns>
 public bool UploadFile(DataDetail details)
 {
     details.CheckNotNull();
     return UploadBlobContent(details, Constants.ContainerName);
 }
예제 #10
0
 /// <summary>
 /// Checks a file in azure.
 /// </summary>
 /// <param name="details">
 /// Details of the file which has to be checked.
 /// </param>
 /// <returns>
 /// True if the file is found successfully; otherwise false.
 /// </returns>
 public bool CheckIfExists(DataDetail details)
 {
     details.CheckNotNull();
     return ExistsBlobContent(details, Constants.ContainerName);
 }
예제 #11
0
        protected Models.DataDetail DownloadFileWithMetadataAsZip(DomainModel.File fileDetails)
        {
            // Validate parameters
            Check.IsNotNull<DomainModel.File>(fileDetails, "filetoDownload");
            Check.IsNotNull<ICollection<FileMetadataField>>(fileDetails.FileMetadataFields, "fileMetadata");
            string fileName = default(string);
            try
            {
                // initialize data detail 
                DataDetail dataDetail = new DataDetail();
                dataDetail.FileDetail = fileDetails;
                dataDetail.MimeTypeToDownLoad = MediaTypeNames.Application.Zip;
                dataDetail.FileNameToDownLoad = string.Join(string.Empty, Path.GetFileNameWithoutExtension(fileDetails.Name), Constants.ZipFileExtension);

                // create work directory
                fileName = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
                DirectoryInfo dir = Directory.CreateDirectory(fileName);

                // download data file
                using (Stream dataStream = BlobDataRepository.GetBlob(fileDetails.BlobId))
                {
                    string dataFileName = Path.Combine(fileName, fileDetails.Name);
                    using (Stream dataFile = System.IO.File.OpenWrite(dataFileName))
                    {
                        dataStream.Seek(0, SeekOrigin.Begin);
                        dataStream.CopyToStream(dataFile);
                        dataFile.Flush();
                    }
                }

                // download metadata
                string metadata = GetMetadata(fileDetails);
                if (!string.IsNullOrEmpty(metadata))
                {
                    string metadataFileName = Path.Combine(fileName, string.Concat("File-metadata", Constants.CSVFileExtension));
                    using (StreamWriter metadataStream = new StreamWriter(metadataFileName))
                    {
                        metadataStream.WriteLine(metadata);
                    }
                }

                // archive
                dataDetail.DataStream = ZipFileHelper.ZipFiles(dir.FullName).GetBytes();

                return dataDetail;
            }
            finally
            {
                // delete the directory
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    Directory.Delete(fileName, true);
                }
            }

        }