wasStitchNET – Rev 13

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  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.Linq;
using wasSharp.Web.Utilities;
using wasSharpNET.IO.Utilities;
using wasStitchNET.Structures;

namespace wasStitchNET.Repository
{
    public class Files
    {
        /// <summary>
        ///     Load all files off a remote repository and return them as a stitchable file.
        /// </summary>
        /// <param name="client">the wasDAVClient to use</param>
        /// <param name="path">the web path to retrieve the files from</param>
        /// <param name="basePath">the base path of the repository</param>
        /// <param name="pathSeparator">the DAV separator character</param>
        /// <returns>a collection of stitch files</returns>
        public static IEnumerable<StitchFile> LoadRemoteFiles(Client client, string path, string basePath,
            char pathSeparator = '/')
        {
            Func<string, Task<StitchFile>> getStitchFile = async file =>
            {
                /*using (var memoryStream = new MemoryStream())
                {
                    using (var stream = await client.Download(file))
                    {
                        await stream.CopyToAsync(memoryStream);
                    }
                    return new StitchFile
                    {
                        Path =
                            file.PathSplit(pathSeparator)
                                .Select(WebExtensions.URLUnescapeDataString)
                                .SequenceExcept(
                                    basePath.PathSplit(pathSeparator)),
                        SHA1 = SHA1.Create().ToHex(memoryStream.ToArray()),
                        PathType = StitchPathType.PATH_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;
                }
        }

        /// <summary>
        ///     Load all files from a local path and return them as a stitchable file.
        /// </summary>
        /// <param name="path">the local path to files</param>
        /// <param name="basePath">the base path to the folder</param>
        /// <param name="pathSeparator">the local filesystem separator to use</param>
        /// <returns>a collection of stitch files</returns>
        public static IEnumerable<StitchFile> 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;
            }
        }
    }
}

Generated by GNU Enscript 1.6.5.90.