/////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // // rights of fair usage, the disclaimer and warranty conditions. // /////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Threading.Tasks; using wasDAVClient; using wasSharp; using wasSharp.Sets; using wasSharp.Web.Utilities; using wasSharpNET.IO.Utilities; using wasStitchNET.Structures; namespace wasStitchNET.Repository { public class Files { /// /// Load all files off a remote repository and return them as a stitchable file. /// /// the wasDAVClient to use /// the web path to retrieve the files from /// the base path of the repository /// the DAV separator character /// a collection of stitch files public static IEnumerable LoadRemoteFiles(Client client, string path, string basePath, char pathSeparator = '/') { Func> getStitchFile = async file => { using (var sha1managed = new SHA1Managed()) { using (var stream = await client.Download(file)) { //fileHash = sha1managed.ComputeHash(stream); return new StitchFile { Path = file.PathSplit(pathSeparator) .Select(WebExtensions.URLUnescapeDataString) .SequenceExcept( basePath.PathSplit(pathSeparator)), SHA1 = sha1managed.ComputeHash(stream).ToHexString(), PathType = StitchPathType.PATH_FILE }; } } }; foreach (var item in client.List(path).Result) switch (item.IsCollection) { case true: var directoryPath = item.Href.TrimEnd(pathSeparator).PathSplit(pathSeparator) .Select(WebExtensions.URLUnescapeDataString) .SequenceExcept( basePath.PathSplit(pathSeparator)); yield return new StitchFile { Path = directoryPath, SHA1 = string.Empty, PathType = StitchPathType.PATH_DIRECTORY }; foreach (var file in LoadRemoteFiles(client, item.Href, basePath)) yield return file; break; default: yield return getStitchFile(item.Href).Result; break; } } /// /// Load all files from a local path and return them as a stitchable file. /// /// the local path to files /// the base path to the folder /// the local filesystem separator to use /// a collection of stitch files public static IEnumerable LoadLocalFiles(string path, string basePath, char pathSeparator = '\\') { foreach (var file in Directory.GetFiles(path)) using (var sha1managed = new SHA1Managed()) { using (var fileStream = IOExtensions.GetWriteStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, STITCH_CONSTANTS.LOCAL_FILE_ACCESS_TIMEOUT)) { yield return new StitchFile { Path = file.PathSplit(pathSeparator) .SequenceExcept( basePath.PathSplit(pathSeparator)), SHA1 = sha1managed.ComputeHash(fileStream).ToHexString(), PathType = StitchPathType.PATH_FILE }; } } foreach (var directory in Directory.GetDirectories(path)) { var directoryPath = directory.PathSplit(pathSeparator) .SequenceExcept( basePath.PathSplit(pathSeparator)); yield return new StitchFile { Path = directoryPath, SHA1 = string.Empty, PathType = StitchPathType.PATH_DIRECTORY }; foreach (var file in LoadLocalFiles(directory, basePath, pathSeparator)) yield return file; } } } }