wasStitchNET – Blame information for rev 20
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
13 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | using System.Collections.Generic; |
||
9 | using System.IO; |
||
10 | using System.Linq; |
||
11 | using System.Security.Cryptography; |
||
12 | using System.Threading.Tasks; |
||
13 | using wasDAVClient; |
||
14 | using wasSharp; |
||
20 | office | 15 | using wasSharp.Sets; |
13 | office | 16 | using wasSharp.Web.Utilities; |
17 | using wasSharpNET.IO.Utilities; |
||
18 | using wasStitchNET.Structures; |
||
19 | |||
20 | namespace wasStitchNET.Repository |
||
21 | { |
||
22 | public class Files |
||
23 | { |
||
24 | /// <summary> |
||
25 | /// Load all files off a remote repository and return them as a stitchable file. |
||
26 | /// </summary> |
||
27 | /// <param name="client">the wasDAVClient to use</param> |
||
28 | /// <param name="path">the web path to retrieve the files from</param> |
||
29 | /// <param name="basePath">the base path of the repository</param> |
||
30 | /// <param name="pathSeparator">the DAV separator character</param> |
||
31 | /// <returns>a collection of stitch files</returns> |
||
32 | public static IEnumerable<StitchFile> LoadRemoteFiles(Client client, string path, string basePath, |
||
33 | char pathSeparator = '/') |
||
34 | { |
||
35 | Func<string, Task<StitchFile>> getStitchFile = async file => |
||
36 | { |
||
37 | using (var sha1managed = new SHA1Managed()) |
||
38 | { |
||
39 | using (var stream = await client.Download(file)) |
||
40 | { |
||
41 | //fileHash = sha1managed.ComputeHash(stream); |
||
42 | return new StitchFile |
||
43 | { |
||
44 | Path = |
||
45 | file.PathSplit(pathSeparator) |
||
46 | .Select(WebExtensions.URLUnescapeDataString) |
||
47 | .SequenceExcept( |
||
48 | basePath.PathSplit(pathSeparator)), |
||
49 | SHA1 = sha1managed.ComputeHash(stream).ToHexString(), |
||
50 | PathType = StitchPathType.PATH_FILE |
||
51 | }; |
||
52 | } |
||
53 | } |
||
54 | }; |
||
55 | |||
56 | foreach (var item in client.List(path).Result) |
||
57 | switch (item.IsCollection) |
||
58 | { |
||
59 | case true: |
||
60 | var directoryPath = item.Href.TrimEnd(pathSeparator).PathSplit(pathSeparator) |
||
61 | .Select(WebExtensions.URLUnescapeDataString) |
||
62 | .SequenceExcept( |
||
63 | basePath.PathSplit(pathSeparator)); |
||
64 | yield return new StitchFile |
||
65 | { |
||
66 | Path = directoryPath, |
||
67 | SHA1 = string.Empty, |
||
68 | PathType = StitchPathType.PATH_DIRECTORY |
||
69 | }; |
||
70 | foreach (var file in LoadRemoteFiles(client, item.Href, basePath)) |
||
71 | yield return file; |
||
72 | break; |
||
73 | |||
74 | default: |
||
75 | yield return getStitchFile(item.Href).Result; |
||
76 | break; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /// <summary> |
||
81 | /// Load all files from a local path and return them as a stitchable file. |
||
82 | /// </summary> |
||
83 | /// <param name="path">the local path to files</param> |
||
84 | /// <param name="basePath">the base path to the folder</param> |
||
85 | /// <param name="pathSeparator">the local filesystem separator to use</param> |
||
86 | /// <returns>a collection of stitch files</returns> |
||
87 | public static IEnumerable<StitchFile> LoadLocalFiles(string path, string basePath, char pathSeparator = '\\') |
||
88 | { |
||
89 | foreach (var file in Directory.GetFiles(path)) |
||
90 | using (var sha1managed = new SHA1Managed()) |
||
91 | { |
||
92 | using (var fileStream = IOExtensions.GetWriteStream(file, FileMode.Open, |
||
93 | FileAccess.Read, FileShare.Read, STITCH_CONSTANTS.LOCAL_FILE_ACCESS_TIMEOUT)) |
||
94 | { |
||
95 | yield return new StitchFile |
||
96 | { |
||
97 | Path = |
||
98 | file.PathSplit(pathSeparator) |
||
99 | .SequenceExcept( |
||
100 | basePath.PathSplit(pathSeparator)), |
||
101 | SHA1 = sha1managed.ComputeHash(fileStream).ToHexString(), |
||
102 | PathType = StitchPathType.PATH_FILE |
||
103 | }; |
||
104 | } |
||
105 | } |
||
106 | foreach (var directory in Directory.GetDirectories(path)) |
||
107 | { |
||
108 | var directoryPath = directory.PathSplit(pathSeparator) |
||
109 | .SequenceExcept( |
||
110 | basePath.PathSplit(pathSeparator)); |
||
111 | yield return new StitchFile |
||
112 | { |
||
113 | Path = directoryPath, |
||
114 | SHA1 = string.Empty, |
||
115 | PathType = StitchPathType.PATH_DIRECTORY |
||
116 | }; |
||
117 | foreach (var file in LoadLocalFiles(directory, basePath, pathSeparator)) |
||
118 | yield return file; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | } |