wasStitchNET – Blame information for rev 13
?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; |
||
15 | using wasSharp.Linq; |
||
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 memoryStream = new MemoryStream()) |
||
38 | { |
||
39 | using (var stream = await client.Download(file)) |
||
40 | { |
||
41 | await stream.CopyToAsync(memoryStream); |
||
42 | } |
||
43 | return new StitchFile |
||
44 | { |
||
45 | Path = |
||
46 | file.PathSplit(pathSeparator) |
||
47 | .Select(WebExtensions.URLUnescapeDataString) |
||
48 | .SequenceExcept( |
||
49 | basePath.PathSplit(pathSeparator)), |
||
50 | SHA1 = SHA1.Create().ToHex(memoryStream.ToArray()), |
||
51 | PathType = StitchPathType.PATH_FILE |
||
52 | }; |
||
53 | }*/ |
||
54 | using (var sha1managed = new SHA1Managed()) |
||
55 | { |
||
56 | using (var stream = await client.Download(file)) |
||
57 | { |
||
58 | //fileHash = sha1managed.ComputeHash(stream); |
||
59 | return new StitchFile |
||
60 | { |
||
61 | Path = |
||
62 | file.PathSplit(pathSeparator) |
||
63 | .Select(WebExtensions.URLUnescapeDataString) |
||
64 | .SequenceExcept( |
||
65 | basePath.PathSplit(pathSeparator)), |
||
66 | SHA1 = sha1managed.ComputeHash(stream).ToHexString(), |
||
67 | PathType = StitchPathType.PATH_FILE |
||
68 | }; |
||
69 | } |
||
70 | } |
||
71 | }; |
||
72 | |||
73 | foreach (var item in client.List(path).Result) |
||
74 | switch (item.IsCollection) |
||
75 | { |
||
76 | case true: |
||
77 | var directoryPath = item.Href.TrimEnd(pathSeparator).PathSplit(pathSeparator) |
||
78 | .Select(WebExtensions.URLUnescapeDataString) |
||
79 | .SequenceExcept( |
||
80 | basePath.PathSplit(pathSeparator)); |
||
81 | yield return new StitchFile |
||
82 | { |
||
83 | Path = directoryPath, |
||
84 | SHA1 = string.Empty, |
||
85 | PathType = StitchPathType.PATH_DIRECTORY |
||
86 | }; |
||
87 | foreach (var file in LoadRemoteFiles(client, item.Href, basePath)) |
||
88 | yield return file; |
||
89 | break; |
||
90 | |||
91 | default: |
||
92 | yield return getStitchFile(item.Href).Result; |
||
93 | break; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /// <summary> |
||
98 | /// Load all files from a local path and return them as a stitchable file. |
||
99 | /// </summary> |
||
100 | /// <param name="path">the local path to files</param> |
||
101 | /// <param name="basePath">the base path to the folder</param> |
||
102 | /// <param name="pathSeparator">the local filesystem separator to use</param> |
||
103 | /// <returns>a collection of stitch files</returns> |
||
104 | public static IEnumerable<StitchFile> LoadLocalFiles(string path, string basePath, char pathSeparator = '\\') |
||
105 | { |
||
106 | foreach (var file in Directory.GetFiles(path)) |
||
107 | using (var sha1managed = new SHA1Managed()) |
||
108 | { |
||
109 | using (var fileStream = IOExtensions.GetWriteStream(file, FileMode.Open, |
||
110 | FileAccess.Read, FileShare.Read, STITCH_CONSTANTS.LOCAL_FILE_ACCESS_TIMEOUT)) |
||
111 | { |
||
112 | yield return new StitchFile |
||
113 | { |
||
114 | Path = |
||
115 | file.PathSplit(pathSeparator) |
||
116 | .SequenceExcept( |
||
117 | basePath.PathSplit(pathSeparator)), |
||
118 | SHA1 = sha1managed.ComputeHash(fileStream).ToHexString(), |
||
119 | PathType = StitchPathType.PATH_FILE |
||
120 | }; |
||
121 | } |
||
122 | } |
||
123 | foreach (var directory in Directory.GetDirectories(path)) |
||
124 | { |
||
125 | var directoryPath = directory.PathSplit(pathSeparator) |
||
126 | .SequenceExcept( |
||
127 | basePath.PathSplit(pathSeparator)); |
||
128 | yield return new StitchFile |
||
129 | { |
||
130 | Path = directoryPath, |
||
131 | SHA1 = string.Empty, |
||
132 | PathType = StitchPathType.PATH_DIRECTORY |
||
133 | }; |
||
134 | foreach (var file in LoadLocalFiles(directory, basePath, pathSeparator)) |
||
135 | yield return file; |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | } |