public PublisherAction(PublisherTypes.ActionType type, PublisherActionResult result, string source, string target) { Type = type; Result = result; SourcePath = source; TargetPath = target; }
/// <summary> /// Creates a directory and returns a PublisherActionResult with the result /// </summary> /// <param name="directorypath">Path of the directory to be created</param> /// <param name="currentusersecurity">CurrentUserSecurity object used to check access rights</param> /// <returns>PublisherActionResult containing specific information in both Failed and Succesful cases</returns> public static PublisherActionResult CreateDirectory(string directorypath, CurrentUserSecurity currentusersecurity) { try { string existingparent = GetLastExistingAncestorDirectory(directorypath); PublisherActionResult precheckresult = CreateDirectoryPreCheck(directorypath, existingparent, currentusersecurity); if (precheckresult.ResultType != PublisherTypes.ActionResultType.Successful) { return(precheckresult); } else { Directory.CreateDirectory(directorypath); return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"Directory ({directorypath}) is succesfully created!" )); } } catch (Exception) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"There was an unexpected error while creating the directory ({directorypath})!" )); } }
/// <summary> /// Copies the file to a given path /// </summary> /// <param name="sourcepath">Path of the source file</param> /// <param name="targetpath">Path of the target file</param> /// <param name="currentusersecurity">CurrentUserSecurity object used to check access rights</param> /// <param name="overwrite">Whether to overwrite the existing file or not</param> /// <returns>PublisherActionResult containing specific information in both Failed and Succesful cases</returns> public static PublisherActionResult CopyFileToFile(string sourcepath, string targetpath, CurrentUserSecurity currentusersecurity, bool overwrite) { PublisherActionResult precheckresult = CopyFileToFilePreCheck(sourcepath, targetpath, currentusersecurity); if (precheckresult.ResultType != PublisherTypes.ActionResultType.Successful) { return(precheckresult); } else if ((File.Exists(targetpath) && overwrite)) { try { File.Copy(sourcepath, targetpath, true); } catch (Exception) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"There was an unexpected error while overwriting the file ({targetpath})!" )); } return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"File succesfully copied ({sourcepath} => {targetpath})" )); } else { try { File.Copy(sourcepath, targetpath, false); } catch (Exception) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"There was an unexpected error while copying the file({sourcepath} => {targetpath})!" )); } return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"File succesfully copied ({sourcepath} => {targetpath})" )); } }
/// <summary> /// Archives a directory and its content /// </summary> /// <param name="directorypath">Path of the directory to archive</param> /// <param name="currentusersecurity">CurrentUserSecurity object used to check access rights</param> /// <param name="zippath">Path of the archive file</param> /// <param name="overwritearchive">Whether to overwrite the existing archive or not</param> /// <returns>PublisherActionResult containing specific information in both Failed and Succesful cases</returns> public static PublisherActionResult ArchiveDirectory(string directorypath, CurrentUserSecurity currentusersecurity, string zippath = null, bool overwritearchive = false) { string archivepath = zippath; try { if (!Directory.Exists(directorypath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.WithWarnings, $"Directory ({directorypath}) does not exist!" )); } if (archivepath.IsNullOrWhiteSpace()) { archivepath = GetArchiveDirectoryPath(directorypath); } PublisherActionResult precheckresult = ArchivedirectoryPreCheck(directorypath, currentusersecurity, archivepath, overwritearchive); if (precheckresult.ResultType != PublisherTypes.ActionResultType.Successful) { return(precheckresult); } else { try { if (File.Exists(archivepath) && overwritearchive) { File.Delete(archivepath); } } catch (Exception) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"There was an unexpected error while deleting the already existing archive ({archivepath})!" )); } try { ZipFile.CreateFromDirectory(directorypath, archivepath, CompressionLevel.Fastest, true); } catch (Exception) { throw; } } } catch (Exception) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"There was an unexpected error while archiving the directory ({directorypath})!" )); } return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"Directory succesfully archived ({directorypath} => ({zippath}))!" )); }
/// <summary> /// Archives a fileto a given path /// </summary> /// <param name="filepath">Path of the file to be archived</param> /// <param name="currentusersecurity">CurrentUserSecurity object used to check access rights</param> /// <param name="zippath">Path of the archive</param> /// <param name="overwritearchvie">Whether to overwrite the archive or not</param> /// <returns>PublisherActionResult containing specific information in both Failed and Succesful cases</returns> public static PublisherActionResult ArchiveFile(string filepath, CurrentUserSecurity currentusersecurity, string zippath = null, bool overwritearchvie = false) { string archivepath = zippath; try { if (archivepath == null || String.IsNullOrEmpty(archivepath)) { archivepath = GetArchiveFilePath(filepath); } PublisherActionResult precheckresult = ArchiveFilePreCheck(filepath, archivepath, currentusersecurity, overwritearchvie); if (precheckresult.ResultType != PublisherTypes.ActionResultType.Successful) { return(precheckresult); } else { try { if (File.Exists(archivepath) && overwritearchvie) { File.Delete(archivepath); } } catch (Exception) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"There was an unexpected error while deleting the already existing archive file ({archivepath})!" )); } try { using (var zip = ZipFile.Open(archivepath, ZipArchiveMode.Create)) { zip.CreateEntryFromFile(filepath, Path.GetFileName(filepath)); } } catch (Exception) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"There was an unexpected error while archiving the file ({filepath} => {archivepath})!" )); } return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"File succesfully archived ({filepath} => {archivepath})!" )); } } catch (Exception) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"There was an unexpected error while archiving the file ({filepath})!" )); } }