/// <summary> /// Gets a ZephyrFile implementation matching the URL type passed in. /// </summary> /// <param name="url">The Fullname or URL of the directory.</param> /// <param name="clients">A collection of connection clients.</param> /// <returns>A ZephyrFile implementation.</returns> public static ZephyrDirectory GetZephyrDirectory(string url, Clients clients = null) { ZephyrDirectory dir = null; UrlType type = GetUrlType(url); switch (type) { case UrlType.LocalDirectory: dir = new WindowsZephyrDirectory(url); break; case UrlType.NetworkDirectory: dir = new WindowsZephyrDirectory(url); break; case UrlType.AwsS3Directory: dir = new AwsS3ZephyrDirectory(clients?.aws, url); break; default: throw new Exception($"Url [{url}] Is Not A Known Directory Type."); } return(dir); }
/// <summary> /// Method to copy the contents of the ZephyrDirectory into another ZephyrDirectory. /// It works by using the base "Stream" property and "Create" methods each implementation must create. /// Thus, the ZephyrDirectories do not have to be of the same implementation type. /// </summary> /// <param name="target">The destination ZephyrDirectory.</param> /// <param name="recurse">Copy all sub-directories and all files under this directory.</param> /// <param name="overwrite">Should files copied overwrite existing files of the same name. Directories will be merged.</param> /// <param name="stopOnError">Stop copying when an error is encountered.</param> /// <param name="verbose">Log each file and directory copied.</param> /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param> /// <param name="callback">Optional method that is called for logging purposes.</param> public void CopyTo(ZephyrDirectory target, bool recurse = true, bool overwrite = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { if (this.Exists) { foreach (ZephyrDirectory childDir in GetDirectories()) { try { String targetChildDirName = target.PathCombine(target.FullName, $"{childDir.Name}/"); ZephyrDirectory targetChild = target.CreateDirectory(targetChildDirName); targetChild.Create(verbose: verbose); if (recurse) { childDir.CopyTo(targetChild, recurse, overwrite, verbose, stopOnError, callbackLabel, callback); } } catch (Exception e) { Logger.Log(e.Message, callbackLabel, callback); if (stopOnError) { throw; } } } foreach (ZephyrFile file in GetFiles()) { try { String targetFileName = target.PathCombine(target.FullName, file.Name); ZephyrFile targetFile = target.CreateFile(targetFileName, verbose, callbackLabel, callback); file.CopyTo(targetFile, overwrite, true, stopOnError, verbose, callbackLabel, callback); } catch (Exception e) { Logger.Log(e.Message, callbackLabel, callback); if (stopOnError) { throw; } } } if (verbose) { Logger.Log($"Copied Directory [{this.FullName}] to [{target.FullName}].", callbackLabel, callback); } } else { string message = $"[{this.FullName}] Does Not Exist."; Logger.Log(message, callbackLabel, callback); if (stopOnError) { throw new Exception(message); } } }
/// <summary> /// Moves a ZephyrFile to a ZephyrDirectory with the same name as the original ZephyrFile. /// </summary> /// <param name="dir">The destination directory.</param> /// <param name="overwrite">Should the ZephyrFile overwrite existing ZephyrFile if it exists.</param> /// <param name="createMissingDirectories">Create any missing directories in the path to the file.</param> /// <param name="stopOnError">Throw an exception if the move fails.</param> /// <param name="verbose">Log details of the file being moved.</param> /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param> /// <param name="callback">Optional method that is called for logging purposes.</param> public void MoveTo(ZephyrDirectory dir, bool overwrite = true, bool createMissingDirectories = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { CopyTo(dir, createMissingDirectories, overwrite, stopOnError, false, callbackLabel, callback); this.Delete(stopOnError: stopOnError, verbose: false); if (verbose) { Logger.Log($"Moved File [{this.FullName}] to [{dir.FullName}].", callbackLabel, callback); } }
/// <summary> /// Copies a ZephyrFile into a ZephyrDirectory with the same name as the original ZephyrFile. /// </summary> /// <param name="dir">The destination directory.</param> /// <param name="overwrite">Should the ZephyrFile overwrite existing ZephyrFile if it exists.</param> /// <param name="createMissingDirectories">Create any missing directories in the path to the file.</param> /// <param name="stopOnError">Throw an exception if the copy fails.</param> /// <param name="verbose">Log details of the file being copied.</param> /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param> /// <param name="callback">Optional method that is called for logging purposes.</param> public void CopyTo(ZephyrDirectory dir, bool overwrite = true, bool createMissingDirectories = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { String targetFilePath = dir.PathCombine(dir.FullName, this.Name); ZephyrFile targetFile = dir.CreateFile(targetFilePath); CopyTo(targetFile, overwrite, createMissingDirectories, stopOnError, false, callbackLabel, callback); if (verbose) { Logger.Log($"Copied File [{this.FullName}] to [{dir.FullName}].", callbackLabel, callback); } }
/// <summary> /// Static method to determine if a file or directory exists based on the Fullname/URL passed in. /// </summary> /// <param name="name">The Fullname or URL of the file or directory.</param> /// <param name="clients">A collection of connection clients.</param> /// <returns>Whether or now the file or directory already exists.</returns> public static bool Exists(string name, Clients clients = null) { if (Utilities.IsDirectory(name)) { ZephyrDirectory dir = Utilities.GetZephyrDirectory(name, clients); return(dir.Exists); } else { ZephyrFile file = Utilities.GetZephyrFile(name, clients); return(file.Exists); } }
/// <summary> /// Static method to delete a ZephyrFile or ZephyrDirectory based on the Fullname / URL passed in. /// </summary> /// <param name="name">The Fullname or URL of the file or directory.</param> /// <param name="clients">A collection of connection clients.</param> /// <param name="recurse">Remove all objects in the directory as well. If set to "false", directory must be empty or an exception will be thrown.</param> /// <param name="stopOnError">Stop deleting objects in the directory if an error is encountered.</param> /// <param name="verbose">Log each object that is deleted from the directory.</param> /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param> /// <param name="callback">Optional method that is called for logging purposes.</param> public static void Delete(string name, Clients clients = null, bool recurse = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { if (Utilities.IsDirectory(name)) { ZephyrDirectory dir = Utilities.GetZephyrDirectory(name, clients); dir.Delete(recurse, stopOnError, verbose, callbackLabel, callback); } else { ZephyrFile file = Utilities.GetZephyrFile(name, clients); file.Delete(stopOnError, verbose, callbackLabel, callback); } }
/// <summary> /// Method to copy the contents of the ZephyrFile into another ZephyrFile. /// It works by using the base "Stream" property and "Create"methods each implementation must create. /// Thus, the ZephyrFiles do not have to be of the same implementation type. /// </summary> /// <param name="file">The destination ZephyrFile.</param> /// <param name="overwrite">Should the ZephyrFile overwrite existing ZephyrFile if it exists.</param> /// <param name="stopOnError">Throw an exception if the copy fails.</param> /// <param name="verbose">Log details of the file being copied.</param> /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param> /// <param name="callback">Optional method that is called for logging purposes.</param> public void CopyTo(ZephyrFile file, bool overwrite = true, bool createMissingDirectories = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { try { if (!this.Exists) { throw new Exception($"File [{this.FullName}] Does Not Exist."); } if (file.Exists && !overwrite) { throw new Exception($"File [{file.FullName}] Already Exists."); } String targetDirectory = file.FullName.Substring(0, file.FullName.LastIndexOf(file.Name)); ZephyrDirectory targetDir = file.CreateDirectory(targetDirectory, verbose); if (!targetDir.Exists) { if (createMissingDirectories) { targetDir.Create(verbose: verbose); } else { throw new Exception($"Directory [{targetDir.FullName}] Does Not Exist."); } } Stream source = this.Open(AccessType.Read, verbose); Stream target = file.Open(AccessType.Write, verbose); source.CopyTo(target); this.Close(verbose); file.Close(verbose); if (verbose) { Logger.Log($"Copied File [{this.FullName}] to [{file.FullName}].", callbackLabel, callback); } } catch (Exception e) { Logger.Log($"ERROR - {e.Message}", callbackLabel, callback); if (stopOnError) { throw; } } }
/// <summary> /// Static method to create a ZephyrDirectory whose implementation is based on the Fullname / URL passed in. /// </summary> /// <param name="dirName">The Fullname or URL of the directory.</param> /// <param name="clients">A collection of connection clients.</param> /// <param name="failIfExists">Throws an error if the directory already exists.</param> /// <param name="callbackLabel">Optional "label" to be passed into the callback method.</param> /// <param name="callback">Optional method that is called for logging purposes.</param> /// <returns>A ZephyrDirectory instance.</returns> public static ZephyrDirectory CreateDirectory(string dirName, Clients clients = null, bool failIfExists = false, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { ZephyrDirectory dir = Utilities.GetZephyrDirectory(dirName, clients); return(dir.Create(failIfExists, verbose, callbackLabel, callback)); }