예제 #1
0
        public async Task <int> InsertFileAsync(string fileName, byte[] fileContent)
        {
            _connection.Open();

            var complressedFileContent = _compressionService.Compress(fileContent);

            var updateResult = await _connection.ExecuteAsync(
                "INSERT INTO FileInfo (Name, Content) " +
                "VALUES (@Name, @Content)",
                param : new { Name = fileName, Content = complressedFileContent });

            _connection.Close();

            return(updateResult);
        }
예제 #2
0
        static void Main(string[] args)
        {
            CompressFileApp app      = new CompressFileApp();
            Logger          logger   = app.Logger;
            ExitCode        exitCode = ExitCode.None;

            try
            {
                logger.Info("Application started.");

                CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

                Options options = new Options();

                Parser.Default.ParseArgumentsStrict(args, options, CommandLineOptions.ArgumentParsingFailed);

                using (ICompressionService compressionService = new CompressionService())
                {
                    compressionService.Compress(options.InputPath, options.OutputFile);
                    exitCode = ExitCode.Success;
                }
            }
            catch (Exception ex)
            {
                exitCode = new ExceptionHandlingService(ex).GetExitCode();
            }
            finally
            {
                logger.Info(CultureInfo.InvariantCulture, "Application exited with code: {0}", (int)exitCode);
                Environment.Exit((int)exitCode);
            }
        }
예제 #3
0
        public void CompressAndDecompressToSameResultTest(byte[] data)
        {
            var compressionService = new CompressionService();
            var compressedData     = compressionService.Compress(data);
            var decompressedData   = compressionService.Decompress(compressedData);

            Assert.True(data.SequenceEqual(decompressedData));
        }
예제 #4
0
        /// <summary>
        /// Build a final compressed, encrypted, and encoded packet.
        /// </summary>
        /// <returns>The packet data.</returns>
        public byte[] BuildPacket()
        {
            // Get the packet data
            byte[] data = Data.ToArray();

            // Compression
            bool compressed = false;

            if (DataLength > 100)
            {
                byte[] compressedData = CompressionService.Compress(data);
                if (compressedData.Length < data.Length)
                {
                    data       = compressedData;
                    compressed = true;
                }
            }
            ushort dataLength = (ushort)data.Length;

            // Create the packet header
            ClientPacketHeader header = new ClientPacketHeader
            {
                OpCode     = OpCode,
                Compressed = compressed,
                Length     = dataLength,
                Timestamp  = TimeUtil.GetUnixTimestamp()
            };

            LastHeader = header;

            // Append the header and CRC to the compressed data
            ToffeePacket packet = new ToffeePacket(Sender);

            packet.Write(header);
            packet.Write(data, true);
            packet.Write(CRC.CalculateCRC32(data));
            data = packet.GetBytes();

            // Encryption
            bool encrypted = ((Encrypted && Sender.UseEncryption) || (Sender.ForceEncryption));

            if ((encrypted) && (!Sender.UseEncryption))
            {
                Sender.Disconnect();
                return(new byte[0]);
            }
            if (encrypted)
            {
                data = Sender.Encryption.Encrypt(data);
            }

            // Create the final packet
            packet = new ToffeePacket(Sender);
            packet.Write(encrypted);
            packet.Write(data, true);
            return(packet.GetBytes());
        }
예제 #5
0
        public void CompressedDataSmallerThenOriginalTest(byte[] data)
        {
            var compressionService = new CompressionService();
            var compressedData     = compressionService.Compress(data);

            var originalDataSize   = data.Length * sizeof(byte);
            var compressedDataSize = compressedData.Length * sizeof(byte);

            Assert.LessOrEqual(compressedDataSize, compressedDataSize,
                               "Compressed data size ({0}) is greater then original ({1}).",
                               compressedDataSize, originalDataSize);
        }
예제 #6
0
        public void CompressionService_Compression_CompressValidSourceDirectory()
        {
            // arrange
            DateTime currentTime     = DateTime.Now;
            string   sourceDirectory = @"C:\_LogFiles\TF1";
            string   targetDirectory = @"C:\_ArchiveFiles\" + currentTime.ToString("ddMMyy");

            // Create the directory if it doesn't exist
            if (!Directory.Exists(sourceDirectory))
            {
                Directory.CreateDirectory(sourceDirectory);
            }
            string sevenZipPath = @"C:\Program Files\7-Zip\7z.exe";

            // act
            bool result = CompressionService.Compress(sevenZipPath, sourceDirectory, targetDirectory);

            // assert
            Assert.IsTrue(result);
        }
예제 #7
0
        public void CompressionService_Compress_CompressInvalidSourceDirectory()
        {
            // arrange
            DateTime currentTime     = DateTime.Now;
            string   sourceDirectory = @"C:\_LogFiles\adfadfasdf";
            string   targetDirectory = @"C:\_ArchiveFiles\" + currentTime.ToString("ddMMyy");
            string   sevenZipPath    = @"C:\Program Files\7-Zip\7z.exe";
            bool     result;

            // act
            try
            {
                result = CompressionService.Compress(sevenZipPath, sourceDirectory, targetDirectory);
            }
            catch
            {
                result = false;
            }


            // assert
            Assert.IsFalse(result);
        }
예제 #8
0
        /// <summary>
        /// Build a final compressed, encrypted, and encoded packet.
        /// </summary>
        /// <returns>The packet data.</returns>
        public byte[] BuildPacket()
        {
            // Get the packet data
            byte[] data = Data.ToArray();

            // Compression
            bool compressed = false;

            if (DataLength > 100)
            {
                byte[] compressedData = CompressionService.Compress(data);
                if (compressedData.Length < data.Length)
                {
                    data       = compressedData;
                    compressed = true;
                }
            }
            ushort dataLength = (ushort)data.Length;

            // Create the packet header
            InternalPacketHeader header = new InternalPacketHeader
            {
                OpCode     = OpCode,
                Compressed = compressed,
                Length     = dataLength
            };

            LastHeader = header;

            // Append the header and CRC to the compressed data
            ToffeePacket packet = new ToffeePacket(Sender);

            packet.Write(header);
            packet.Write(data, true);
            packet.Write(CRC.CalculateCRC32(data));
            return(packet.GetBytes());
        }
예제 #9
0
        public static async Task Main(string[] args)
        {
            // Make sure only days, source Directory, and targetDirectory are entered into program
            if (args.Length != 4)
            {
                await SystemUtilityService.NotifySystemAdminAsync(Constants.InvalidArgs, Constants.SystemAdminEmailAddress).ConfigureAwait(false);

                return;
            }

            DateTime currentTime = DateTime.Now;
            int      days;
            string   targetDirectory = "";

            try
            {
                days = Convert.ToInt32(args[1]);
            }
            catch
            {
                await SystemUtilityService.NotifySystemAdminAsync(Constants.FirstArgumentNotInt, Constants.SystemAdminEmailAddress).ConfigureAwait(false);

                return;
            }

            string sourceDir;

            if (Directory.Exists(args[2]))
            {
                sourceDir = args[2];
            }
            else
            {
                await SystemUtilityService.NotifySystemAdminAsync(Constants.SourceDirectoryDNE, Constants.SystemAdminEmailAddress).ConfigureAwait(false);

                return;
            }

            if (Directory.Exists(args[3]))
            {
                targetDirectory = args[3];
            }

            targetDirectory += currentTime.ToString(Constants.NamingFormatString);
            bool result = false;

            // FetchLogs will return true if files of the proper age are found in the source directory.
            if (FileFetchingService.FetchLogs(sourceDir, targetDirectory, days))
            {
                if (CompressionService.Compress(Constants.SevenZipPath, sourceDir, targetDirectory))
                {
                    result = await FTPService.SendAsync(Constants.FTPUrl, "", targetDirectory, Constants.FTPUsername, Constants.FTPpassword).ConfigureAwait(false);
                }
            }

            // Notify system admin if Archiving fails to run successfully.
            if (result == false)
            {
                await SystemUtilityService.NotifySystemAdminAsync("Archiving failed on" + currentTime, Constants.SystemAdminEmailAddress).ConfigureAwait(false);
            }
        }