/// <summary> /// ## Eat /// /// Deletes the specified files. /// </summary> /// <param name="files"></param> /// <returns></returns> public PakuResult Eat(DirectoryInfo dir, IList <VirtualFileInfo> files, string parameters) { // attempt to delete the files, tracking which ones we could delete PakuResult result = new PakuResult(); foreach (VirtualFileInfo vfi in files) { FileInfo fi = vfi.ToFileInfo(); if (fi.Exists) { try { fi.Delete(); result.RemovedFiles.Add(vfi); } catch (Exception ex) { result.Error = ex; break; } } else { result.Error = new ArgumentException($"File {vfi.FullName} does not exist."); break; } } return(result); }
/// <summary> /// ## Execute /// /// Executes the pipeline on a directory with the specified parameters. /// </summary> /// <param name="directory"></param> /// <param name="selectParams"></param> /// <param name="filterParams"></param> /// <returns></returns> public PakuResult Execute(string directory, string selectParams, string filterParams, string pakuParams, bool logToFile = false) { PakuResult result = new PakuResult(); // check if directory exists first DirectoryInfo di = new DirectoryInfo(directory); if (!di.Exists) { // if not, throw an argument exception // we do not want to create a logger in this case, as it will create the directory structure for the log file result.Error = new ArgumentException($"Directory does not exist: {directory}"); } else { // setup the logger BootstrapLogger(logToFile, directory); try { Logger.Info($"Cleaning directory: {directory}"); IList <VirtualFileInfo> files = SelectionStrategy.Select(di, selectParams); Logger.Info($"Files selected: {files.Count}"); files = FilterStrategy.Filter(files, filterParams); Logger.Info($"Files filtered: {files.Count}"); result = PakuStrategy.Eat(di, files, pakuParams); Logger.Info($"Files removed: {result.RemovedFiles.Count}"); foreach (VirtualFileInfo fi in result.RemovedFiles) { Logger.Debug($"Removed: {fi.Name}"); } } catch (Exception ex) { result.Error = ex; } if (!result.Success) { Logger.Error(result.Error); } } // write any errors to the console if (!result.Success) { Console.WriteLine(result.Error.Message); } return(result); }
/// <summary> /// ## Eat /// /// Eats the specified list of files by adding them to a zip archive and then deleting them. /// </summary> /// <param name="files"></param> /// <returns></returns> public PakuResult Eat(DirectoryInfo dir, IList <VirtualFileInfo> files, string parameters) { if (!String.IsNullOrEmpty(parameters)) { ZipFilePrefix = parameters; } // attempt to delete the files, tracking which ones we could delete PakuResult result = new PakuResult(); string zipName = Path.Combine(dir.FullName, $"{ZipFilePrefix}_{DateTime.Now.ToString("yyyyMMdd_HH-mm-ss-fff")}.zip"); using (var stream = new FileStream(zipName, FileMode.Create)) { using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true)) { // add files foreach (VirtualFileInfo vfi in files) { FileInfo fi = vfi.ToFileInfo(); if (fi.Exists) { try { // add to archive and remove from disk archive.CreateEntryFromFile(fi.FullName, fi.Name, CompressionLevel.Optimal); fi.Delete(); result.RemovedFiles.Add(vfi); } catch (Exception ex) { // stop execution, but do not throw exception yet // because we do not want to corrupt the zip file result.Error = ex; break; } } else { result.Error = new ArgumentException($"File {vfi.FullName} does not exist."); break; } } } stream.Close(); } // add zip file to result result.CreatedFiles.Add(new VirtualFileInfo(new FileInfo(zipName))); return(result); }
/// <summary> /// ## Eat /// /// Eats files by uploading them to Azure blob storage and then deleting them locally. /// </summary> /// <param name="dir"></param> /// <param name="files"></param> /// <param name="parameters"></param> /// <returns></returns> public PakuResult Eat(DirectoryInfo dir, IList <VirtualFileInfo> files, string parameters) { PakuResult result = new PakuResult(); try { AzurePakuConfig config; try { string json = File.ReadAllText(parameters); config = JsonConvert.DeserializeObject <AzurePakuConfig>(json); } catch (Exception ex) { throw new ArgumentException("parameters should be a valid path to an Azure configuration JSON file. JSON keys: ConnectionString, Container.", ex); } // upload the files Upload(config.ConnectionString, config.Container, files); // remove all uploaded files foreach (VirtualFileInfo vfi in files) { FileInfo fi = vfi.ToFileInfo(); if (fi.Exists) { fi.Delete(); result.RemovedFiles.Add(vfi); } } } catch (Exception ex) { result.Error = ex; } return(result); }
public PakuResult Eat(DirectoryInfo dir, IList <VirtualFileInfo> files, string parameters) { PakuResult result = new PakuResult(); try { PgpPakuStrategyParams parms = new PgpPakuStrategyParams(parameters); // zip all filtered files before encryption ZipPakuStrategy zipper = new ZipPakuStrategy(); PakuResult zipResult = zipper.Eat(dir, files, parms.OutputFilePrefix); if (zipResult.Success) { VirtualFileInfo zipFile = zipResult.CreatedFiles[0]; // then encrypt VirtualFileInfo pgpFile = Encrypt(zipFile, zipFile.FullName + ".pgp", parms.PgpPublicKeyPath); // and remove the temporary zip file generated File.Delete(zipFile.FullName); result.CreatedFiles = new List <VirtualFileInfo>() { pgpFile }; result.RemovedFiles = zipResult.RemovedFiles; } else { result.Error = zipResult.Error; } } catch (Exception ex) { result.Error = ex; } return(result); }