internal Extractor(string targetDirectory, byte[] aesKey, byte[] aesIV) { this._targetDirectory = targetDirectory; try { _encryptorOptions = Watcher.SystemConfiguration.GetConfigurationClass(new EncryptorOptions()); _aesKey = _encryptorOptions.GetOption <string>("aesKey").Select(c => (byte)c).ToArray(); } catch { this._aesKey = aesKey; } try { _aesIV = _encryptorOptions.GetOption <string>("aesIV").Select(c => (byte)c).ToArray(); } catch { this._aesIV = aesIV; } this._encryptor = new Encryptor(); this._archivator = new Archivator(); try { _archivatorOptions = Watcher.SystemConfiguration.GetConfigurationClass(new ArchivatorOptions()); } catch { Logger.RecordStatusAsync("Warning, archivator options was not found in the config"); } try { _archivator.IsNeedToLogArchivator = _archivatorOptions.GetOption <bool>("NeedToLog"); } catch { Logger.RecordStatusAsync("Warning, archivator need to log option was not found in the config"); _archivator.IsNeedToLogArchivator = true; } try { _compressorOptions = Watcher.SystemConfiguration.GetConfigurationClass(new CompressorOptions()); _archivator.CompressionLevel = _compressorOptions.GetOption <int>("CompressingLevel").ToString(); } catch { try { _archivator.CompressionLevel = _compressorOptions.GetOption <string>("CompressingLevel"); } catch { _archivator.CompressionLevel = "Optimal"; } } if (_archivatorOptions.GetOption <bool>("NeedToArchieve")) { ArchiveOldFiles(_targetDirectory); } }
private void OnCreated(object sender, FileSystemEventArgs e) { string path = e.FullPath; try { FileInfo fileInfo = new FileInfo(path); DateTime dateTime = fileInfo.LastWriteTime; string year = dateTime.ToString("yyyy"); string month = dateTime.ToString("MM"); string day = dateTime.ToString("dd"); byte[] key, iv; (key, iv) = Encryptor.CreateKeyAndIV(); string name = Path.GetFileNameWithoutExtension(path); string extension = Path.GetExtension(path); string fileName = Path.GetFileName(path); string gzPath = Path.Combine(targetDir, name + ".gz"); string newPath = Path.Combine(targetDir, name + extension); //encrypting byte [] content = File.ReadAllBytes(path); content = Encryptor.Encrypt(content, key, iv); File.WriteAllBytes(path, content); //compressing and copying to targetDir Archive.Compress(path, gzPath); if (File.Exists(path)) { File.Delete(path); } if (enableArchiveDirectory) { //copying to additional archive char sep = Path.DirectorySeparatorChar; string archiveDir = Path.Combine(targetDir, $"archive{sep}year {year}{sep}month {month}{sep}day {day}"); string archiveName = name + '_' + dateTime.ToString("yyyy_MM_dd_HH_mm_ss") + ".gz"; string archivePath = Path.Combine(archiveDir, archiveName); Directory.CreateDirectory(archiveDir); File.Copy(gzPath, archivePath); } //decompressing Archive.Decompress(gzPath, newPath); if (File.Exists(gzPath)) { File.Delete(gzPath); } //decrypting content = File.ReadAllBytes(newPath); string decrypted = Encryptor.Decrypt(content, key, iv); File.WriteAllText(newPath, decrypted); report += $"File \"{name}\" is moved from \"{sourceDir}\" to \"{targetDir}\" successfully!"; dbLogger.Log(report); } catch (Exception exception) { report += $"A following problem occured: {exception}"; dbLogger.Log(report); } }