Exemplo n.º 1
0
 public TextFileImporter(FileProfile fileToImport, IRepository <T> repository, IDslScriptRunner scriptRunner, ICustomLogicContainer customLogicContainer)
 {
     _fileToImport         = fileToImport;
     _repository           = repository;
     _scriptRunner         = scriptRunner;
     _customLogicContainer = customLogicContainer;
 }
Exemplo n.º 2
0
        public FileApi(
            ITokenProvider tokenProvider,
            string applicationId,
            string applicationName,
            string storagePath)
        {
            this.authDelegate = new AuthDelegateImpl(tokenProvider);

            var applicationInfo = new ApplicationInfo(
                applicationId,
                applicationName);

            var settings = new FileProfile.Settings(storagePath, false, authDelegate, applicationInfo);

            var lateFileProfile = new LateValue <FileProfile>();

            FileProfile.LoadAsync(settings, lateFileProfile);
            this.profile = lateFileProfile.AwaitValue();


            FileEngine.Settings engineSettings = new FileEngine.Settings("1234", "");

            var lateFileEngine = new LateValue <FileEngine>();

            this.profile.AddEngineAsync(engineSettings, lateFileEngine);
            this.engine = lateFileEngine.AwaitValue();
        }
 public ITextFileParser GetSchemaParser(FileProfile fileProfile, Stream stream)
 {
     if (fileProfile.IsFixedWidth)
     {
         var parser = new FixedWidthTextFileParser(stream);
         if (fileProfile.SchemaDetector.ParseStartPosition.HasValue && fileProfile.SchemaDetector.ParseStartPosition.Value > 0 &&
             fileProfile.SchemaDetector.ParseLength.HasValue && fileProfile.SchemaDetector.ParseLength.Value > 0)
         {
             parser.AddFieldWidth(fileProfile.SchemaDetector.ParseStartPosition.Value, fileProfile.SchemaDetector.ParseLength.Value);
             fileProfile.SchemaDetector.SourceColumnNumber = 1;
         }
         return(parser);
     }
     else
     {
         var parser = new DelimitedTextFileParser(stream);
         parser.Delimiters = new[] { fileProfile.Delimiter };
         if (!string.IsNullOrWhiteSpace(fileProfile.TextQualifier))
         {
             parser.HasFieldsEnclosedInQualifier = true;
             parser.SetTextQualifier(fileProfile.TextQualifier);
         }
         else
         {
             parser.HasFieldsEnclosedInQualifier = false;
         }
         return(parser);
     }
 }
Exemplo n.º 4
0
        private string GetFullPath(FileProfile fileProfile)
        {
            var initial = new StringBuilder(fileProfile.LocalPath.Length + 5);

            initial.Append(fileProfile.LocalPath, 1, fileProfile.LocalPath.Length - 1);
            initial.Append('.');
            initial.Append(fileProfile.FileType.GetExtension());
            return(Path.Combine(_hierarchyRoot, initial.ToString()).Replace('\\', '/'));
        }
Exemplo n.º 5
0
        private async Task <StreamContent> MakeStreamContent(FileProfile fileProfile)
        {
            var steam = await fileProfile.GetData().ConfigureAwait(false);

            var content = new StreamContent(steam);

            content.Headers.Add("Extension", fileProfile.FileType.GetExtension());
            return(content);
        }
Exemplo n.º 6
0
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            var DC       = new DataClassesDataContext();
            var FileName = "";
            if (FileProfile.HasFile)
            {
                string   strPic  = FileProfile.FileName;
                var      path    = Server.MapPath("ClientUpload/");
                DateTime date    = DateTime.Now;
                string   strdate = date.ToString();

                var charsToRemove = new string[] { "%", "-", ":", " ", "\\", "/" };
                foreach (var c in charsToRemove)
                {
                    strdate = strdate.Replace(c, string.Empty);
                    strPic  = strPic.Replace(c, string.Empty);
                }
                //filename = strdate + strPic;
                FileName = strPic;
                FileProfile.SaveAs(path + "/" + FileName);
            }
            else
            {
                FileName = hdnImage.Value;
            }


            tblClient ClientUpdate = DC.tblClients.Single(ob => ob.ClientID == Convert.ToInt32(Session["ClientID"]));
            ClientUpdate.ClientName  = txtName.Text;
            ClientUpdate.EmailID     = txtEmail.Text;
            ClientUpdate.ContactNo   = txtCNO.Text;
            ClientUpdate.CompanyName = txtComName.Text;
            ClientUpdate.WebsiteURL  = txtURl.Text;
            if (FileProfile.FileName != "")
            {
                ClientUpdate.ImageName = FileProfile.FileName;
            }
            else
            {
                ClientUpdate.ImageName = FileName;
            }
            DC.SubmitChanges();
            BindClient();
            Response.Redirect("TrackProject.aspx");
        }
        catch (Exception ex)
        {
            int    session    = Convert.ToInt32(Session["ClientID"].ToString());
            string PageName   = System.IO.Path.GetFileName(Request.Url.AbsolutePath);
            string MACAddress = GetMacAddress();
            AddErrorLog(ref ex, PageName, "Client", session, 0, MACAddress);
            ClientScript.RegisterStartupScript(GetType(), "abc", "alert('Something went wrong! Try again');", true);
        }
    }
 public TextFileParser(FileProfile fileToProcess, IFileData fileData, IDslScriptRunner scriptRunner, string[] headerFields, int startAtLineNumber = -1)
 {
     _table                 = new Table();
     _scriptRunner          = scriptRunner;
     _fileToStreamConverter = new FileToStreamConverter();
     _fileToProcess         = fileToProcess;
     _fileData              = fileData;
     _headerFields          = headerFields;
     _headerLines           = startAtLineNumber;
 }
Exemplo n.º 8
0
 public CompositeTextFileParser(FileProfile fileToProcess, IFileData fileData, IDslScriptRunner scriptRunner, ICustomLogicContainer customLogicContainer, bool isEdiFileFormat = false, int startAtLineNumber = -1)
 {
     _result                = new FileParsingResult();
     _fileToProcess         = fileToProcess;
     _scriptRunner          = scriptRunner;
     _fileToStreamConverter = new FileToStreamConverter();
     _writers               = new Dictionary <object, StreamWriter>();
     _customLogicContainer  = customLogicContainer;
     _fileData              = fileData;
     _isEdiFileFormat       = isEdiFileFormat;
     _startAtLineNumber     = startAtLineNumber;
     SplitSchemas();
 }
Exemplo n.º 9
0
        public async Task <Stream> GetData(FileProfile fileProfile)
        {
            var filePathContent          = new StringContent(fileProfile.LocalPath, Encoding.UTF8, "text/plain");
            var fileTypeContent          = new StringContent(fileProfile.FileType.ToString(), Encoding.UTF8, "text/plain");
            var multipartFormDataContent = new MultipartFormDataContent
            {
                { filePathContent, "filePath" },
                { fileTypeContent, "fileType" },
            };

            using var httpRequestMessage = await CreateHttpRequestMessageWithAuthHeader(HttpMethod.Get, Endpoints.GetFileEndpoint, multipartFormDataContent).ConfigureAwait(false);

            var responseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

            responseMessage.EnsureSuccessStatusCode();
            var dataStream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);

            return(dataStream);
        }
Exemplo n.º 10
0
        private static async Task SyncFile(Config config, CancellationToken token, FileProfile path, ILogger logger, string configSavePath)
        {
            var fileInfo = new FileInfo(path.Path);

            //needs to be synced
            if (fileInfo.LastWriteTime > path.LastSynced)
            {
                logger.LogDebug($"Syncing {path.Path}");

                try
                {
                    await using var sourceStream      = File.OpenRead(path.Path);
                    await using var destinationStream = File.Create(Path.Combine(configSavePath, fileInfo.Name));

                    //nested try catch is needed for scoping
                    try
                    {
                        await sourceStream.CopyToAsync(destinationStream, 81920, token);

                        token.ThrowIfCancellationRequested();
                    }
                    catch (OperationCanceledException)//Delete file if cancelled
                    {
                        logger.LogDebug($"deleteing file {Path.Combine(configSavePath, fileInfo.Name)}");
                        await destinationStream.DisposeAsync();//free up file handles

                        await sourceStream.DisposeAsync();

                        File.Delete(Path.Combine(configSavePath, fileInfo.Name));
                        throw;
                    }
                }
                catch (IOException e)//file cant be opened
                {
                    logger.LogCritical($"Failed to open file {fileInfo}");
                    logger.LogDebug(e.Message);
                }
            }
            else
            {
                logger.LogDebug($"Skipping {path.Path}");
            }
        }
        /// <summary>
        /// Gets the file.
        /// </summary>
        /// <param name="fileId">The file identifier.</param>
        /// <param name="estateId">The estate identifier.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task <FileDetails> GetFile(Guid fileId,
                                                Guid estateId,
                                                CancellationToken cancellationToken)
        {
            FileAggregate fileAggregate =
                await this.FileAggregateRepository.GetLatestVersion(fileId, cancellationToken);

            if (fileAggregate.IsCreated == false)
            {
                throw new NotFoundException($"File with Id [{fileId}] not found");
            }

            FileDetails fileDetails = fileAggregate.GetFile();

            EstateReportingGenericContext context = await this.DbContextFactory.GetContext(estateId, cancellationToken);

            Merchant merchant = await context.Merchants.AsAsyncEnumerable()
                                .SingleOrDefaultAsync(m => m.MerchantId == fileDetails.MerchantId, cancellationToken);

            if (merchant != null)
            {
                fileDetails.MerchantName = merchant.Name;
            }

            EstateSecurityUser userDetails = await context.EstateSecurityUsers.AsAsyncEnumerable()
                                             .SingleOrDefaultAsync(u => u.SecurityUserId == fileDetails.UserId);

            if (userDetails != null)
            {
                fileDetails.UserEmailAddress = userDetails.EmailAddress;
            }

            FileProfile fileProfile = await this.GetFileProfile(fileDetails.FileProfileId, cancellationToken);

            if (fileProfile != null)
            {
                fileDetails.FileProfileName = fileProfile.Name;
            }

            return(fileDetails);
        }
        /// <summary>
        /// Creates the process file request.
        /// </summary>
        /// <param name="fileProfile">The file profile.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        private IRequest CreateProcessFileRequest(FileProfile fileProfile,
                                                  String fileName)
        {
            IRequest request = null;

            // Extract the file Id
            var fields = fileName.Split('-');

            switch (fileProfile.RequestType)
            {
            case "SafaricomTopupRequest":
                request = new SafaricomTopupRequest(Guid.Parse(fields[1]), fileName, fileProfile.FileProfileId);
                break;

            case "VoucherRequest":
                request = new VoucherRequest(Guid.Parse(fields[1]), fileName, fileProfile.FileProfileId);
                break;
            }

            return(request);
        }
 public ITextFileParser GetParser(FileProfile fileProfile, Stream stream)
 {
     if (fileProfile.IsFixedWidth)
     {
         var parser       = new FixedWidthTextFileParser(stream);
         int sourceColNum = 1;
         foreach (RowDefinition rowDefinition in fileProfile.FileTypes[0].RowDefinitions)
         {
             if (rowDefinition.ParseStartPosition.HasValue && rowDefinition.ParseStartPosition.Value > 0 &&
                 rowDefinition.ParseLength.HasValue && rowDefinition.ParseLength.Value > 0)
             {
                 parser.AddFieldWidth(
                     rowDefinition.ParseStartPosition.Value,
                     rowDefinition.ParseLength.Value);
                 rowDefinition.SourceColumnNumber = sourceColNum;
                 sourceColNum++;
             }
             else
             {
                 rowDefinition.SourceColumnNumber = null;
             }
         }
         return(parser);
     }
     else
     {
         var parser = new DelimitedTextFileParser(stream);
         parser.Delimiters = new[] { fileProfile.Delimiter };
         if (!string.IsNullOrWhiteSpace(fileProfile.TextQualifier))
         {
             parser.HasFieldsEnclosedInQualifier = true;
             parser.SetTextQualifier(fileProfile.TextQualifier);
         }
         else
         {
             parser.HasFieldsEnclosedInQualifier = false;
         }
         return(parser);
     }
 }
Exemplo n.º 14
0
        private async Task <bool> SaveFile(FileProfile fileProfile, FileWriteMode writeMode)
        {
            using var fileInputStream = await fileProfile.GetData().ConfigureAwait(false);

            var pathToSave = GetFullPath(fileProfile);

            if (writeMode == FileWriteMode.CreateNew && File.Exists(pathToSave))
            {
                return(false);
            }
            if (writeMode == FileWriteMode.Override && !File.Exists(pathToSave))
            {
                return(false);
            }
            var fileMode = writeMode switch
            {
                FileWriteMode.Override => FileMode.Create,
                FileWriteMode.CreateNew => FileMode.CreateNew,
                _ => throw new ArgumentOutOfRangeException(nameof(writeMode), writeMode, null)
            };

            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(pathToSave) !);
                using (var outputStream = new FileStream(pathToSave, fileMode))
                {
                    await fileInputStream.CopyToAsync(outputStream).ConfigureAwait(false);
                }

                File.SetCreationTimeUtc(pathToSave, fileProfile.LastTimeUtc);
                File.SetLastWriteTimeUtc(pathToSave, fileProfile.LastTimeUtc);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
 public void Initialize(FileProfile fileProfile)
 {
     _initialized = true;
     if (string.IsNullOrWhiteSpace(fileProfile.CustomLogicDllPath) || string.IsNullOrWhiteSpace(fileProfile.CustomLogicClassName))
     {
         return;
     }
     string[] customLogicAssemblyAndType = fileProfile.CustomLogicClassName.Split(new[] { ',' });
     if (customLogicAssemblyAndType.Length != 2)
     {
         return;
     }
     try
     {
         AppDomainSetup clads = new AppDomainSetup
         {
             ApplicationBase      = AppDomain.CurrentDomain.BaseDirectory,
             PrivateBinPath       = AppDomain.CurrentDomain.RelativeSearchPath,
             ShadowCopyFiles      = "true",
             DisallowCodeDownload = true
         };
         //TODO: configuration file?
         _customLogicAppDomain = AppDomain.CreateDomain("Secondary Scripting Domain", null, clads);
         string customLogicDllFile = customLogicAssemblyAndType[0].Trim();
         if (!customLogicDllFile.ToLower().EndsWith(".dll"))
         {
             customLogicDllFile += ".dll";
         }
         customLogicDllFile   = Path.Combine(fileProfile.CustomLogicDllPath, customLogicDllFile);
         _customLogicInstance = (ICustomParseLogic)_customLogicAppDomain.CreateInstanceFromAndUnwrap(customLogicDllFile, customLogicAssemblyAndType[1].Trim());
     }
     catch (Exception e)
     {
         _customLogicInstance = null;
         throw new TypeLoadException("The custom parse logic type could not be loaded. [" + customLogicAssemblyAndType + "]", e);
     }
 }
Exemplo n.º 16
0
 public Task <Stream> GetData(FileProfile fileProfile)
 {
     return(Task.FromResult(_dataDictionary[fileProfile.FileType]));
 }
Exemplo n.º 17
0
        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">File Id [{request.FileId}] has no lines added</exception>
        /// <exception cref="NotFoundException">
        /// File Line Number {request.LineNumber} not found in File Id {request.FileId}
        /// or
        /// No file profile found with Id {fileDetails.FileProfileId}
        /// or
        /// Merchant not found with Id {fileDetails.MerchantId} on estate Id {fileDetails.EstateId}
        /// or
        /// No contracts found for Merchant Id {fileDetails.MerchantId} on estate Id {fileDetails.EstateId}
        /// or
        /// No merchant contract for operator Id {fileProfile.OperatorName} found for Merchant Id {merchant.MerchantId}
        /// or
        /// No variable value product found on the merchant contract for operator Id {fileProfile.OperatorName} and Merchant Id {merchant.MerchantId}
        /// </exception>
        public async Task <Unit> Handle(ProcessTransactionForFileLineRequest request,
                                        CancellationToken cancellationToken)
        {
            // Get the file aggregate, this tells us the file profile information
            FileAggregate fileAggregate = await this.FileAggregateRepository.GetLatestVersion(request.FileId, cancellationToken);

            FileDetails fileDetails = fileAggregate.GetFile();

            if (fileDetails.FileLines.Any() == false)
            {
                throw new NotSupportedException($"File Id [{request.FileId}] has no lines added");
            }

            FileLine fileLine = fileDetails.FileLines.SingleOrDefault(f => f.LineNumber == request.LineNumber);

            if (fileLine == null)
            {
                throw new NotFoundException($"File Line Number {request.LineNumber} not found in File Id {request.FileId}");
            }

            if (fileLine.ProcessingResult != ProcessingResult.NotProcessed)
            {
                // Line already processed
                return(new Unit());
            }

            FileProfile fileProfile = await this.FileProcessorManager.GetFileProfile(fileDetails.FileProfileId, cancellationToken);

            if (fileProfile == null)
            {
                throw new NotFoundException($"No file profile found with Id {fileDetails.FileProfileId}");
            }

            // Determine if we need to actually process this file line
            if (this.FileLineCanBeIgnored(fileLine.LineData, fileProfile.FileFormatHandler))
            {
                // Write something to aggregate to say line was explicity ignored
                fileAggregate.RecordFileLineAsIgnored(fileLine.LineNumber);
                await this.FileAggregateRepository.SaveChanges(fileAggregate, cancellationToken);

                return(new Unit());
            }

            // need to now parse the line (based on the file format), this builds the metadata
            Dictionary <String, String> transactionMetadata = this.ParseFileLine(fileLine.LineData, fileProfile.FileFormatHandler);

            if (transactionMetadata == null)
            {
                // Line failed to parse so record this
                fileAggregate.RecordFileLineAsRejected(fileLine.LineNumber, "Invalid Format");
                await this.FileAggregateRepository.SaveChanges(fileAggregate, cancellationToken);

                return(new Unit());
            }

            // Add the file data to the request metadata
            transactionMetadata.Add("FileId", request.FileId.ToString());
            transactionMetadata.Add("FileLineNumber", fileLine.LineNumber.ToString());

            String operatorName = fileProfile.OperatorName;

            if (transactionMetadata.ContainsKey("OperatorName"))
            {
                // extract the value
                operatorName        = transactionMetadata["OperatorName"];
                transactionMetadata = transactionMetadata.Where(x => x.Key != "OperatorName").ToDictionary(x => x.Key, x => x.Value);
            }

            this.TokenResponse = await this.GetToken(cancellationToken);

            Interlocked.Increment(ref FileRequestHandler.TransactionNumber);

            // Get the merchant details
            MerchantResponse merchant = await this.EstateClient.GetMerchant(this.TokenResponse.AccessToken, fileDetails.EstateId, fileDetails.MerchantId, cancellationToken);

            if (merchant == null)
            {
                throw new NotFoundException($"Merchant not found with Id {fileDetails.MerchantId} on estate Id {fileDetails.EstateId}");
            }
            List <ContractResponse> contracts = await this.EstateClient.GetMerchantContracts(this.TokenResponse.AccessToken, fileDetails.EstateId, fileDetails.MerchantId, cancellationToken);

            if (contracts.Any() == false)
            {
                throw new NotFoundException($"No contracts found for Merchant Id {fileDetails.MerchantId} on estate Id {fileDetails.EstateId}");
            }

            ContractResponse?contract = null;

            if (fileProfile.OperatorName == "Voucher")
            {
                contract = contracts.SingleOrDefault(c => c.Description.Contains(operatorName));
            }
            else
            {
                contract = contracts.SingleOrDefault(c => c.OperatorName == operatorName);
            }


            if (contract == null)
            {
                throw new NotFoundException($"No merchant contract for operator Id {operatorName} found for Merchant Id {merchant.MerchantId}");
            }

            ContractProduct?product = contract.Products.SingleOrDefault(p => p.Value == null);  // TODO: Is this enough or should the name be used and stored in file profile??

            if (product == null)
            {
                throw new NotFoundException($"No variable value product found on the merchant contract for operator Id {fileProfile.OperatorName} and Merchant Id {merchant.MerchantId}");
            }

            // Build a transaction request message
            SaleTransactionRequest saleTransactionRequest = new SaleTransactionRequest
            {
                EstateId                      = fileDetails.EstateId,
                MerchantId                    = fileDetails.MerchantId,
                TransactionDateTime           = fileDetails.FileReceivedDateTime,
                TransactionNumber             = FileRequestHandler.TransactionNumber.ToString(),
                TransactionType               = "Sale",
                ContractId                    = contract.ContractId,
                DeviceIdentifier              = merchant.Devices.First().Value,
                OperatorIdentifier            = contract.OperatorName,
                ProductId                     = product.ProductId,
                AdditionalTransactionMetadata = transactionMetadata,
            };

            SerialisedMessage serialisedRequestMessage = new SerialisedMessage
            {
                Metadata = new Dictionary <String, String>
                {
                    { "estate_id", fileDetails.EstateId.ToString() },
                    { "merchant_id", fileDetails.MerchantId.ToString() }
                },
                SerialisedData = JsonConvert.SerializeObject(saleTransactionRequest, new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.All
                })
            };

            Logger.LogInformation(serialisedRequestMessage.SerialisedData);

            // Send request to transaction processor
            SerialisedMessage serialisedResponseMessage = await this.TransactionProcessorClient.PerformTransaction(this.TokenResponse.AccessToken, serialisedRequestMessage, cancellationToken);

            // Get the sale transaction response
            SaleTransactionResponse saleTransactionResponse = JsonConvert.DeserializeObject <SaleTransactionResponse>(serialisedResponseMessage.SerialisedData);

            if (saleTransactionResponse.ResponseCode == "0000")
            {
                // record response against file line in file aggregate
                fileAggregate.RecordFileLineAsSuccessful(request.LineNumber, saleTransactionResponse.TransactionId);
            }
            else
            {
                fileAggregate.RecordFileLineAsFailed(request.LineNumber, saleTransactionResponse.TransactionId, saleTransactionResponse.ResponseCode, saleTransactionResponse.ResponseMessage);
            }

            // Save changes to file aggregate
            // TODO: Add retry round this save (maybe 3 retries)
            await this.FileAggregateRepository.SaveChanges(fileAggregate, cancellationToken);

            return(new Unit());
        }
Exemplo n.º 18
0
        private async Task <Unit> ProcessFile(Guid fileId,
                                              Guid fileProfileId,
                                              String fileName,
                                              CancellationToken cancellationToken)
        {
            IFileInfo   inProgressFile = null;
            FileProfile fileProfile    = null;

            try
            {
                fileProfile = await this.FileProcessorManager.GetFileProfile(fileProfileId, cancellationToken);

                if (fileProfile == null)
                {
                    throw new NotFoundException($"No file profile found with Id {fileProfileId}");
                }

                // Check the processed/failed directories exist
                if (this.FileSystem.Directory.Exists(fileProfile.ProcessedDirectory) == false)
                {
                    Logger.LogWarning($"Creating Directory {fileProfile.ProcessedDirectory} as not found");
                    this.FileSystem.Directory.CreateDirectory(fileProfile.ProcessedDirectory);
                }

                if (this.FileSystem.Directory.Exists(fileProfile.FailedDirectory) == false)
                {
                    Logger.LogWarning($"Creating Directory {fileProfile.FailedDirectory} as not found");
                    this.FileSystem.Directory.CreateDirectory(fileProfile.FailedDirectory);
                }

                inProgressFile = this.FileSystem.FileInfo.FromFileName(fileName);

                if (inProgressFile.Exists == false)
                {
                    throw new FileNotFoundException($"File {inProgressFile.FullName} not found");
                }

                FileAggregate fileAggregate =
                    await this.FileAggregateRepository.GetLatestVersion(fileId, cancellationToken);

                if (fileAggregate.IsCreated == false)
                {
                    throw new InvalidOperationException($"File with Id {fileId} not created");
                }

                String fileContent = null;
                //Open file for Read\Write
                using (Stream fs = inProgressFile.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
                {
                    //Create object of StreamReader by passing FileStream object on which it needs to operates on
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        //Use ReadToEnd method to read all the content from file
                        fileContent = await sr.ReadToEndAsync();
                    }
                }

                if (String.IsNullOrEmpty(fileContent) == false)
                {
                    String[] fileLines = fileContent.Split(fileProfile.LineTerminator);

                    foreach (String fileLine in fileLines)
                    {
                        fileAggregate.AddFileLine(fileLine);
                    }

                    await this.FileAggregateRepository.SaveChanges(fileAggregate, cancellationToken);
                }

                Logger.LogInformation(
                    $"About to move file {inProgressFile.Name} to [{fileProfile.ProcessedDirectory}]");

                // TODO: Move file now
                inProgressFile.MoveTo($"{fileProfile.ProcessedDirectory}/{inProgressFile.Name}");

                return(new Unit());
            }
            catch (Exception e)
            {
                if (inProgressFile != null && fileProfile != null)
                {
                    inProgressFile.MoveTo($"{fileProfile.FailedDirectory}/{inProgressFile.Name}");
                }

                Logger.LogError(e);
                throw;
            }
        }
Exemplo n.º 19
0
        // TODO: Create a domain service

        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        /// <exception cref="NotFoundException">No file profile found with Id {request.FileProfileId}</exception>
        /// <exception cref="System.IO.FileNotFoundException">File {file.FullName} not found</exception>
        /// <exception cref="System.IO.DirectoryNotFoundException">Directory {fileProfile.ListeningDirectory} not found</exception>
        public async Task <Guid> Handle(UploadFileRequest request,
                                        CancellationToken cancellationToken)
        {
            DateTime importLogDateTime = request.FileUploadedDateTime;

            // This will now create the import log and add an event for the file being uploaded
            Guid importLogId = Helpers.CalculateFileImportLogAggregateId(importLogDateTime.Date, request.EstateId);

            // Get the import log
            FileImportLogAggregate fileImportLogAggregate = await this.FileImportLogAggregateRepository.GetLatestVersion(importLogId, cancellationToken);

            if (fileImportLogAggregate.IsCreated == false)
            {
                // First file of the day so create
                fileImportLogAggregate.CreateImportLog(request.EstateId, importLogDateTime);
            }

            // Move the file
            FileProfile fileProfile = await this.FileProcessorManager.GetFileProfile(request.FileProfileId, cancellationToken);

            if (fileProfile == null)
            {
                throw new NotFoundException($"No file profile found with Id {request.FileProfileId}");
            }

            // Copy file from the temp location to file processing listening directory
            IFileInfo file = this.FileSystem.FileInfo.FromFileName(request.FilePath);

            if (file.Exists == false)
            {
                throw new FileNotFoundException($"File {file.FullName} not found");
            }
            String originalName = file.Name;

            if (this.FileSystem.Directory.Exists(fileProfile.ListeningDirectory) == false)
            {
                throw new DirectoryNotFoundException($"Directory {fileProfile.ListeningDirectory} not found");
            }

            // Read the file data
            String fileContent = null;

            //Open file for Read\Write
            using (Stream fs = file.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
            {
                //Create object of StreamReader by passing FileStream object on which it needs to operates on
                using (StreamReader sr = new StreamReader(fs))
                {
                    //Use ReadToEnd method to read all the content from file
                    fileContent = await sr.ReadToEndAsync();
                }
            }

            Guid fileId = this.CreateGuidFromFileData(fileContent);

            String fileDestination = $"{fileProfile.ListeningDirectory}//{request.EstateId:N}-{fileId:N}";

            file.MoveTo(fileDestination, overwrite: true);

            // Update Import log aggregate
            fileImportLogAggregate.AddImportedFile(fileId, request.MerchantId, request.UserId, request.FileProfileId, originalName, fileDestination, request.FileUploadedDateTime);

            // Save changes
            await this.FileImportLogAggregateRepository.SaveChanges(fileImportLogAggregate, cancellationToken);

            return(fileId);
        }
Exemplo n.º 20
0
 public TextFileGenerator(FileProfile fileToGenerate)
 {
     _fileToGenerate = fileToGenerate;
 }
Exemplo n.º 21
0
 public Task <Stream> GetData(FileProfile fileProfile)
 {
     return(_filesGroup.RelatedFiles.First(profile => profile.FileType == fileProfile.FileType).GetData());
 }
 public T GetSchemaParser <T>(FileProfile fileProfile, Stream stream)
     where T : class, ITextFileParser
 {
     return(GetSchemaParser(fileProfile, stream) as T);
 }
Exemplo n.º 23
0
 /// <summary>
 /// Crawls the passed directory, accumulating the paths of all files in subdirectories
 /// and the initial directory.
 /// </summary>
 /// <returns>A dictionary of directory:files[] where files are the files in that directory</returns>
 public static Dictionary <string, IEnumerable <string> > CrawlDirectory(FileProfile path)
 {
     var resultDict = new Dictionary <string, IEnumerable <string> >();
Exemplo n.º 24
0
 public string GetSdkVersion()
 {
     return(FileProfile.GetSdkVersion());
 }
Exemplo n.º 25
0
        public Task <Stream> GetData(FileProfile fileProfile)
        {
            var stream = new FileStream(GetFullPath(fileProfile), FileMode.Open, FileAccess.Read);

            return(Task.FromResult <Stream>(stream));
        }
Exemplo n.º 26
0
        public async Task <List <FileProfile> > GetProfileFileTypeByTypeAsync(string profileType, int profileId = 0, string webRootPath = null, string mcId = null)
        {
            if (string.IsNullOrWhiteSpace(profileType))
            {
                AddError("Dữ liệu không hợp lệ");
                return(null);
            }
            profileType = profileType.ToLower();
            int type = 0;

            switch (profileType)
            {
            case "common":
                type = (int)ProfileType.Common;
                break;

            case "courier":
                type = (int)ProfileType.Courier;
                break;

            case "mcredit":
                type = (int)ProfileType.MCredit;
                break;

            case "company":
                type = (int)ProfileType.Company;
                break;

            case "revoke":
                type = (int)ProfileType.RevokeDebt;
                break;
            }
            if (profileType == "mcredit")
            {
                return(await GetFileUploadMcreditAsync(profileId, mcId, webRootPath));
            }
            bool isReadOnly = false;

            if (type == (int)ProfileType.Common)
            {
                var profileResult = await _rpProfile.GetByIdAsync(profileId);

                if (!profileResult.success || profileResult.data == null)
                {
                    isReadOnly = false;
                }
                if ((_process.User.isSale || _process.User.isRsmAsmSS) && profileResult.data.Ma_Trang_Thai != (int)ProfileStatus.Draft)
                {
                    isReadOnly = true;
                }
            }

            var fileTypes = await _rpFile.GetByType(type);

            if (fileTypes == null || !fileTypes.Any())
            {
                return(null);
            }
            var files = await _rpFile.GetFilesByProfileIdAsync(type, profileId);

            var result = new List <FileProfile>();

            foreach (var kind in fileTypes)
            {
                var filesByType = files.Where(p => p.Key == kind.FileKey);
                //foreach (var f in filesByType)
                //{
                //    f.FileUrl = Path.Combine(webRootPath, f.FileUrl);
                //}
                var item = new FileProfile
                {
                    Id            = kind.Id,
                    RootPath      = webRootPath,
                    FileKey       = kind.FileKey,
                    IsRequire     = kind.IsRequire,
                    Name          = kind.Name,
                    ProfileTypeId = kind.ProfileTypeId,
                    IsReadOnly    = isReadOnly,
                    ProfileFiles  = filesByType != null?filesByType.ToList() : new List <FileUploadModel>()
                };
                result.Add(item);
            }
            return(result);
        }
Exemplo n.º 27
0
 private string GetFullPath(FileProfile fileProfile)
 {
     return(GetFullPath($"{fileProfile.LocalPath}.{fileProfile.FileType.GetExtension()}"));
 }