Example #1
0
        public void DecryptFiles(IPgpEncryption pgpEncryption, DecryptFileContext context, FileInfo[] sourceFiles, CancellationToken token)
        {
            foreach (var sourceFile in sourceFiles)
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                var targetFileName = string.Concat(sourceFile.Name, ".txt");
                var targetPath     = Path.Combine(context.DirectoryDecrypted, targetFileName);

                try
                {
                    logger.DebugFormat("Decrypting \"{0}\" to \"{1}\".", sourceFile.FullName, targetPath);
                    pgpEncryption.DecryptFile(sourceFile.FullName, targetPath);

                    logger.InfoFormat("Decrypted file \"{0}\" to \"{1}\".", sourceFile.FullName, targetPath);

                    var archivePath = Path.Combine(context.DirectoryArchive, "Encrypted", sourceFile.Name);
                    sourceFile.MoveTo(archivePath);
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat(ex, "Unknown error occurred while decrypting file \"{0}\".", sourceFile.FullName);

                    var exceptionPath = Path.Combine(context.DirectoryException, sourceFile.Name);
                    sourceFile.MoveTo(exceptionPath);
                }
            }
        }
Example #2
0
        public void EncryptFiles(IPgpEncryption pgpEncryption, EncryptFileContext context, string searchPattern, CancellationToken token)
        {
            var sourceDirectory = new DirectoryInfo(context.DirectoryDecrypted);

            if (!sourceDirectory.Exists)
            {
                logger.ErrorFormat("Decryption Directory \"{0}\" was not found or has been deleted.",
                                   context.DirectoryDecrypted);
                return;
            }

            var destinationDirectory = new DirectoryInfo(context.DirectoryEncrypted);

            if (!destinationDirectory.Exists)
            {
                logger.ErrorFormat("Encryption Directory \"{0}\" was not found or has been deleted.",
                                   context.DirectoryEncrypted);
                return;
            }

            var archiveDirectory = new DirectoryInfo(context.DirectoryArchive);

            if (!archiveDirectory.Exists)
            {
                archiveDirectory.Create();
                logger.TraceFormat("Archive Directory \"{0}\" was created.", archiveDirectory.FullName);
            }

            FileInfo[] sourceFiles;

            try
            {
                sourceFiles = sourceDirectory.GetFiles(searchPattern);
                logger.TraceFormat("Identified {0} file(s) in directory \"{1}\" with search pattern \"{2}\".",
                                   sourceFiles.Length, sourceDirectory.FullName, searchPattern);

                if (sourceFiles.Length == 0)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                logger.ErrorFormat(ex,
                                   "Error occurred while retrieving files from directory \"{1}\" with search pattern \"{2}\".",
                                   sourceDirectory, searchPattern);

                return;
            }

            EncryptFiles(pgpEncryption, context, sourceFiles, token);
        }
Example #3
0
        public void DecryptFiles(IPgpEncryption pgpEncryption, DecryptFileContext context, string searchPattern, CancellationToken token)
        {
            var sourceDirectory = new DirectoryInfo(context.DirectoryEncrypted);

            if (!sourceDirectory.Exists)
            {
                logger.ErrorFormat("Encryption Directory \"{0}\" was not found or has been deleted.",
                                   context.DirectoryEncrypted);
                return;
            }

            var sourceFiles = sourceDirectory.GetFiles(searchPattern);

            logger.TraceFormat("Identified {0} file(s) in directory \"{1}\" with search pattern \"{2}\".",
                               sourceFiles.Length, sourceDirectory.FullName, searchPattern);

            if (sourceFiles.Length == 0)
            {
                return;
            }

            DecryptFiles(pgpEncryption, context, sourceFiles, token);
        }
Example #4
0
        public void EncryptFiles(IPgpEncryption pgpEncryption, EncryptFileContext context, FileInfo[] sourceFiles, CancellationToken token)
        {
            var archiveDirectory = context.DirectoryArchive;
            var archiveInfo      = new DirectoryInfo(archiveDirectory);

            if (!archiveInfo.Exists)
            {
                archiveInfo.Create();
            }

            foreach (var sourceFile in sourceFiles)
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                var targetFileName = string.Concat(sourceFile.Name, ".pgp");
                var targetPath     = Path.Combine(context.DirectoryEncrypted, targetFileName);

                try
                {
                    var options = new TransactionOptions {
                        IsolationLevel = IsolationLevel.ReadCommitted
                    };
                    using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
                    {
                        logger.DebugFormat("Encrypting \"{0}\" to \"{1}\".", sourceFile.FullName, targetPath);
                        pgpEncryption.EncryptFile(sourceFile.FullName, targetPath);

                        var marketFile = marketDataAccess.LoadOutboundMarketFileByName(sourceFile.Name);
                        marketFile.Status      = MarketFileStatusOptions.Encrypted;
                        marketFile.ProcessDate = DateTime.Now;
                        marketDataAccess.UpdateMarketFile(marketFile);

                        logger.InfoFormat("Encrypted file \"{0}\" to \"{1}\".", sourceFile.FullName, targetPath);

                        var archivePath = Path.Combine(archiveInfo.FullName, sourceFile.Name);
                        MoveFile(sourceFile.FullName, archivePath);
                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat(ex, "Unknown error occurred while encrypting file \"{0}\".", sourceFile.FullName);

                    var options = new TransactionOptions {
                        IsolationLevel = IsolationLevel.ReadCommitted
                    };
                    using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
                    {
                        var marketFile = marketDataAccess.LoadOutboundMarketFileByName(sourceFile.Name);
                        marketFile.Status       = MarketFileStatusOptions.Error;
                        marketFile.ProcessDate  = DateTime.Now;
                        marketFile.ProcessError = "Failed To Encrypt File.";
                        marketDataAccess.UpdateMarketFile(marketFile);

                        var exceptionPath = Path.Combine(context.DirectoryException, sourceFile.Name);
                        MoveFile(sourceFile.FullName, exceptionPath);
                        scope.Complete();
                    }
                }
            }
        }