/// <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})!" )); } }
public Publisher() { _CurrentUserSecurity = new CurrentUserSecurity(); Publications = new List <PublisherInfo>(); Actions = new List <PublisherAction>(); }
/// <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> /// Returns error specific PublisherTypes.ActionResultType.Failed or PublisherTypes.ActionResultType.WithWarnings /// type PublisherActionResults in case parameters are incorrect, or ActionResultType.Successful if they are correct /// </summary> /// <param name="directorypath">Path of the directory to be archived</param> /// <param name="currentusersecurity">CurrentUserSecurity object used to check access rights</param> /// <param name="archivepath">path of the archive file</param> /// <param name="overwritearchive">Whether to overwrite an already existing archive</param> /// <returns>PublisherActionResult with specific ActionResultType and message</returns> public static PublisherActionResult ArchivedirectoryPreCheck(string directorypath, CurrentUserSecurity currentusersecurity, string archivepath, bool overwritearchive) { if (String.IsNullOrEmpty(archivepath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Zip archive path ({archivepath}) is not valid!" )); } else if (File.Exists(archivepath) && !overwritearchive) { return(new PublisherActionResult( PublisherTypes.ActionResultType.WithWarnings, $"Zip archive ({archivepath}) already exists!" )); } else if (!currentusersecurity.HasAccess(new DirectoryInfo(directorypath), FileSystemRights.Read)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"No read access to the source directory ({directorypath})!" )); } else if (!currentusersecurity.HasAccess(new DirectoryInfo(Path.GetDirectoryName(archivepath)), FileSystemRights.Write)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"No write access to the archive target path ({archivepath})!" )); } else { return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"Pre Check Succesful!" )); } }
/// <summary> /// Returns error specific PublisherTypes.ActionResultType.Failed or PublisherTypes.ActionResultType.WithWarnings /// type PublisherActionResults in case parameters are incorrect, or ActionResultType.Successful if they are correct /// </summary> /// <param name="filepath">Path of the file to archive</param> /// <param name="archivepath">Path of the archive file</param> /// <param name="currentusersecurity">CurrentUserSecurity object used to check access rights</param> /// <param name="overwritearchvie">Whether to overwrite an already existing archive</param> /// <returns>PublisherActionResult with specific ActionResultType and message</returns> public static PublisherActionResult ArchiveFilePreCheck(string filepath, string archivepath, CurrentUserSecurity currentusersecurity, bool overwritearchvie) { if (archivepath == null || String.IsNullOrEmpty(archivepath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Zip path ({archivepath}) is invalid!" )); } else if (!File.Exists(filepath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.WithWarnings, $"File ({filepath}) does not exist (nothing has been archived)!" )); } else if (!currentusersecurity.HasAccess(new FileInfo(filepath), FileSystemRights.Read)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"No read access to the source file ({filepath})!" )); } else if (File.Exists(archivepath) && !overwritearchvie) { return(new PublisherActionResult( PublisherTypes.ActionResultType.WithWarnings, $"Zip archive ({archivepath}) already exists!" )); } else if (!currentusersecurity.HasAccess(new DirectoryInfo(Path.GetDirectoryName(archivepath)), FileSystemRights.Write)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"No write access to target path ({archivepath})!" )); } else { return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"Pre Check Succesful!" )); } }
/// <summary> /// Returns error specific PublisherTypes.ActionResultType.Failed or PublisherTypes.ActionResultType.WithWarnings /// type PublisherActionResults in case parameters are incorrect, or ActionResultType.Successful if they are correct /// </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> /// <returns>PublisherActionResult with specific ActionResultType and message</returns> public static PublisherActionResult CopyFileToFilePreCheck(string sourcepath, string targetpath, CurrentUserSecurity currentusersecurity) { if (String.IsNullOrEmpty(sourcepath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Source path ({sourcepath}) is not valid!" )); } else if (!File.Exists(sourcepath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Source file ({sourcepath}) does not exist!" )); } else if (!currentusersecurity.HasAccess(new FileInfo(sourcepath), FileSystemRights.Read)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"No read access to source file ({sourcepath})!" )); } else if (String.IsNullOrEmpty(targetpath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Target path ({targetpath}) is not valid!" )); } else if (!Directory.Exists(Path.GetDirectoryName(targetpath))) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Target directory ({Path.GetDirectoryName(targetpath)}) does not exist!" )); } else if (!currentusersecurity.HasAccess(new DirectoryInfo(Path.GetDirectoryName(targetpath)), FileSystemRights.Write)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"No write access to target path ({targetpath})!" )); } else { return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"Pre Check Succesful!" )); } }
/// <summary> /// Returns error specific PublisherTypes.ActionResultType.Failed or PublisherTypes.ActionResultType.WithWarnings /// type PublisherActionResults in case parameters are incorrect, or ActionResultType.Successful if they are correct /// </summary> /// <param name="directorypath">Path of the directory to create</param> /// <param name="existingancestor">Path to last existing ancestor</param> /// <param name="currentusersecurity">CurrentUserSecurity object used to check access rights</param> /// <returns>PublisherActionResult with specific ActionResultType and message</returns> private static PublisherActionResult CreateDirectoryPreCheck(string directorypath, string existingancestor, CurrentUserSecurity currentusersecurity) { if (existingancestor == null) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Could not determine root directory for ({directorypath})!" )); } else if (String.IsNullOrEmpty(directorypath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Target directory path ({directorypath}) was invalid!" )); } else if (!currentusersecurity.HasAccess(new DirectoryInfo(existingancestor), FileSystemRights.Write)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"No write access to the target directory ({existingancestor})!" )); } else if (Directory.Exists(directorypath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.WithWarnings, $"Directory ({directorypath}) already exists!" )); } else { return(new PublisherActionResult( PublisherTypes.ActionResultType.Successful, $"Pre Check Succesful!" )); } }
/// <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})!" )); } }
/// <summary> /// Copies the file to a given directory /// </summary> /// <param name="sourcepath">Path of the source file</param> /// <param name="targetpath">Path of the target directory</param> /// <param name="currentusersecurity">CurrentUserSecurity object used to check access rights</param> /// <param name="overwrite">Whether to overwrite the already existing file or not</param> /// <returns>PublisherActionResult containing specific information in both Failed and Succesful cases</returns> public static PublisherActionResult CopyFileToDirectory(string sourcepath, string targetpath, CurrentUserSecurity currentusersecurity, bool overwrite) { if (String.IsNullOrEmpty(sourcepath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Source path ({sourcepath}) is not valid!" )); } else if (String.IsNullOrEmpty(targetpath)) { return(new PublisherActionResult( PublisherTypes.ActionResultType.Failed, $"Target path ({targetpath}) is not valid!" )); } else { return(CopyFileToFile(sourcepath, Path.Combine(targetpath, Path.GetFileName(sourcepath)), currentusersecurity, overwrite)); } }