Example #1
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();
                    }
                }
            }
        }