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