/// <summary> /// Method to move a ZephyrFile to 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="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(ZephyrFile file, bool overwrite = true, bool createMissingDirectories = true, bool stopOnError = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { try { if (file.Exists && !overwrite) { throw new Exception($"File [{file.FullName}] Already Exists."); } CopyTo(file, overwrite, createMissingDirectories, stopOnError, false); this.Delete(stopOnError: stopOnError, verbose: false); if (verbose) { Logger.Log($"Moved File [{this.FullName}] to [{file.FullName}].", callbackLabel, callback); } } catch (Exception e) { Logger.Log($"ERROR - {e.Message}", callbackLabel, callback); if (stopOnError) { throw; } } }
/// <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> /// 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> /// Gets a ZephyrFile implementation matching the URL type passed in. /// </summary> /// <param name="url">The Fullname or URL of the file.</param> /// <param name="clients">A collection of connection clients.</param> /// <returns>A ZephyrFile implementation.</returns> public static ZephyrFile GetZephyrFile(string url, Clients clients = null) { ZephyrFile file = null; UrlType type = GetUrlType(url); switch (type) { case UrlType.LocalFile: file = new WindowsZephyrFile(url); break; case UrlType.NetworkFile: file = new WindowsZephyrFile(url); break; case UrlType.AwsS3File: file = new AwsS3ZephyrFile(clients?.aws, url); break; } return(file); }
/// <summary> /// Static method to create a ZephyrFile whose implementation if based on the Fullname / URL passed in. /// </summary> /// <param name="fileName">The Fullname or URL of the file.</param> /// <param name="clients">A collection of connection clients.</param> /// <param name="overwrite">Will overwrite the file if it 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 ZephyrFile instance.</returns> public static ZephyrFile CreateFile(string fileName, Clients clients = null, bool overwrite = true, bool verbose = true, String callbackLabel = null, Action <string, string> callback = null) { ZephyrFile file = Utilities.GetZephyrFile(fileName, clients); return(file.Create(overwrite, verbose, callbackLabel, callback)); }