wasSharp – Diff between revs 24 and 25
?pathlinks?
Rev 24 | Rev 25 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - 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.Collections.Generic; |
7 | using System.Collections.Generic; |
|
8 | using System.IO; |
8 | using System.IO; |
|
9 | using System.IO.Compression; |
9 | using System.IO.Compression; |
|
10 | using System.Linq; |
10 | using System.Linq; |
|
11 | using System.Text; |
11 | using System.Text; |
|
12 | using System.Threading.Tasks; |
12 | using System.Threading.Tasks; |
|
13 | |
13 | |
|
14 | namespace wasSharp |
14 | namespace wasSharp |
|
15 | { |
15 | { |
|
16 | public static class IO |
16 | public static class IO |
|
17 | { |
17 | { |
|
18 | /////////////////////////////////////////////////////////////////////////// |
18 | /////////////////////////////////////////////////////////////////////////// |
|
19 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
19 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
20 | /////////////////////////////////////////////////////////////////////////// |
20 | /////////////////////////////////////////////////////////////////////////// |
|
21 | /// <summary> |
21 | /// <summary> |
|
22 | /// Combine multiple paths. |
22 | /// Combine multiple paths. |
|
23 | /// </summary> |
23 | /// </summary> |
|
24 | /// <param name="paths">an array of paths</param> |
24 | /// <param name="paths">an array of paths</param> |
|
25 | /// <returns>a combined path</returns> |
25 | /// <returns>a combined path</returns> |
|
26 | public static string PathCombine(string separator = @"\", params string[] paths) |
26 | public static string PathCombine(string separator = @"\", params string[] paths) |
|
27 | { |
27 | { |
|
28 | return string.Join(separator, paths); |
28 | return string.Join(separator, paths); |
|
29 | } |
29 | } |
|
30 | |
30 | |
|
31 | /// <summary> |
31 | /// <summary> |
|
32 | /// Strip characters that are incompatible with file names. |
32 | /// Strip characters that are incompatible with file names. |
|
33 | /// </summary> |
33 | /// </summary> |
|
34 | /// <param name="fileName">the name of the file</param> |
34 | /// <param name="fileName">the name of the file</param> |
|
35 | /// <returns>a clean string</returns> |
35 | /// <returns>a clean string</returns> |
|
36 | private static string CleanFileName(string fileName) |
36 | private static string CleanFileName(string fileName) |
|
37 | { |
37 | { |
|
38 | return Path.GetInvalidFileNameChars() |
38 | return Path.GetInvalidFileNameChars() |
|
39 | .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty)); |
39 | .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty)); |
|
40 | } |
40 | } |
|
- | 41 | |
||
- | 42 | /////////////////////////////////////////////////////////////////////////// |
||
- | 43 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 44 | /////////////////////////////////////////////////////////////////////////// |
||
- | 45 | /// <summary> |
||
- | 46 | /// Compress a stream using Deflate compression. |
||
- | 47 | /// </summary> |
||
- | 48 | /// <param name="uncompressedSourceStream">the stream to compress</param> |
||
- | 49 | /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param> |
||
- | 50 | /// <param name="leaveOpen">whether to leave the compression stream open</param> |
||
41 | |
51 | /// <returns>an awaitable Task</returns> |
|
42 | public static async Task DeflateCompress(this Stream uncompressedSourceStream, |
52 | public static async Task DeflateCompress(this Stream uncompressedSourceStream, |
|
43 | Stream compressedDestinationStream, bool leaveOpen = false) |
53 | Stream compressedDestinationStream, bool leaveOpen = false) |
|
44 | { |
54 | { |
|
45 | using ( |
55 | using ( |
|
46 | var compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress, |
56 | var compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress, |
|
47 | leaveOpen)) |
57 | leaveOpen)) |
|
48 | { |
58 | { |
|
49 | await uncompressedSourceStream.CopyToAsync(compressionStream); |
59 | await uncompressedSourceStream.CopyToAsync(compressionStream); |
|
50 | } |
60 | } |
|
51 | } |
61 | } |
|
- | 62 | |
||
- | 63 | /////////////////////////////////////////////////////////////////////////// |
||
- | 64 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 65 | /////////////////////////////////////////////////////////////////////////// |
||
- | 66 | /// <summary> |
||
- | 67 | /// Decompress a Deflate-compressed stream. |
||
- | 68 | /// </summary> |
||
- | 69 | /// <param name="compressedSourceStream">the compressed stream to decompress</param> |
||
- | 70 | /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param> |
||
- | 71 | /// <param name="leaveOpen">whether to leave the stream open after decompression</param> |
||
52 | |
72 | /// <returns>an awaitable Task</returns> |
|
53 | public static async Task DeflateDecompress(this Stream compressedSourceStream, |
73 | public static async Task DeflateDecompress(this Stream compressedSourceStream, |
|
54 | Stream uncompressedDestinationStream, bool leaveOpen = false) |
74 | Stream uncompressedDestinationStream, bool leaveOpen = false) |
|
55 | { |
75 | { |
|
56 | using ( |
76 | using ( |
|
57 | var decompressionStream = new DeflateStream(compressedSourceStream, CompressionMode.Decompress, |
77 | var decompressionStream = new DeflateStream(compressedSourceStream, CompressionMode.Decompress, |
|
58 | leaveOpen)) |
78 | leaveOpen)) |
|
59 | { |
79 | { |
|
60 | await decompressionStream.CopyToAsync(uncompressedDestinationStream); |
80 | await decompressionStream.CopyToAsync(uncompressedDestinationStream); |
|
61 | } |
81 | } |
|
62 | } |
82 | } |
|
- | 83 | |
||
- | 84 | /////////////////////////////////////////////////////////////////////////// |
||
- | 85 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 86 | /////////////////////////////////////////////////////////////////////////// |
||
- | 87 | /// <summary> |
||
- | 88 | /// Compress a stream using GZip compression. |
||
- | 89 | /// </summary> |
||
- | 90 | /// <param name="uncompressedSourceStream">the stream to compress</param> |
||
- | 91 | /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param> |
||
- | 92 | /// <param name="leaveOpen">whether to leave the stream open after compressing</param> |
||
63 | |
93 | /// <returns>an awaitable Task</returns> |
|
64 | public static async Task GZipCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream, |
94 | public static async Task GZipCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream, |
|
65 | bool leaveOpen = false) |
95 | bool leaveOpen = false) |
|
66 | { |
96 | { |
|
67 | using ( |
97 | using ( |
|
68 | var compressionStream = new GZipStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen) |
98 | var compressionStream = new GZipStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen) |
|
69 | ) |
99 | ) |
|
70 | { |
100 | { |
|
71 | await uncompressedSourceStream.CopyToAsync(compressionStream); |
101 | await uncompressedSourceStream.CopyToAsync(compressionStream); |
|
72 | } |
102 | } |
|
73 | } |
103 | } |
|
- | 104 | |
||
- | 105 | /////////////////////////////////////////////////////////////////////////// |
||
- | 106 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 107 | /////////////////////////////////////////////////////////////////////////// |
||
- | 108 | /// <summary> |
||
- | 109 | /// Decompress a GZiped stream. |
||
- | 110 | /// </summary> |
||
- | 111 | /// <param name="compressedSourceStream">the stream to decompress</param> |
||
- | 112 | /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param> |
||
- | 113 | /// <param name="leaveOpen">whether the decompression stream should be left open</param> |
||
74 | |
114 | /// <returns>an awaitable Task</returns> |
|
75 | public static async Task GZipDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream, |
115 | public static async Task GZipDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream, |
|
76 | bool leaveOpen = false) |
116 | bool leaveOpen = false) |
|
77 | { |
117 | { |
|
78 | using ( |
118 | using ( |
|
79 | var decompressionStream = new GZipStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen)) |
119 | var decompressionStream = new GZipStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen)) |
|
80 | { |
120 | { |
|
81 | await decompressionStream.CopyToAsync(uncompressedDestinationStream); |
121 | await decompressionStream.CopyToAsync(uncompressedDestinationStream); |
|
82 | } |
122 | } |
|
83 | } |
- | ||
84 | |
- | ||
85 | public static string MimeType(string fileName, char extensionSeparator = '.') |
- | ||
86 | { |
- | ||
87 | switch (fileName.Split('.').Last().ToUpperInvariant()) |
- | ||
88 | { |
- | ||
89 | case "AVI": |
- | ||
90 | return "video/x-msvideo"; |
- | ||
91 | case "CSS": |
- | ||
92 | return "text/css"; |
- | ||
93 | case "DOC": |
- | ||
94 | return "application/msword"; |
- | ||
95 | case "GIF": |
- | ||
96 | return "image/gif"; |
- | ||
97 | case "HTM": |
- | ||
98 | case "HTML": |
- | ||
99 | return "text/html"; |
- | ||
100 | case "JPG": |
- | ||
101 | case "JPEG": |
- | ||
102 | return "image/jpeg"; |
- | ||
103 | case "JS": |
- | ||
104 | return "application/x-javascript"; |
- | ||
105 | case "MP3": |
- | ||
106 | return "audio/mpeg"; |
- | ||
107 | case "PNG": |
- | ||
108 | return "image/png"; |
- | ||
109 | case "PDF": |
- | ||
110 | return "application/pdf"; |
- | ||
111 | case "PPT": |
- | ||
112 | return "application/vnd.ms-powerpoint"; |
- | ||
113 | case "ZIP": |
- | ||
114 | return "application/zip"; |
- | ||
115 | case "TXT": |
- | ||
116 | return "text/plain"; |
- | ||
117 | case "XML": |
- | ||
118 | return "text/xml"; |
- | ||
119 | case "SQLITE": |
- | ||
120 | return "application/x-sqlite3"; |
- | ||
121 | default: |
- | ||
122 | return "application/octet-stream"; |
- | ||
123 | } |
- | ||
124 | } |
123 | } |
|
125 | |
124 | |
|
126 | /////////////////////////////////////////////////////////////////////////// |
125 | /////////////////////////////////////////////////////////////////////////// |
|
127 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
126 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
|
128 | /////////////////////////////////////////////////////////////////////////// |
127 | /////////////////////////////////////////////////////////////////////////// |
|
129 | /// <summary> |
128 | /// <summary> |
|
130 | /// Splits a path using a separator and an escape character. |
129 | /// Splits a path using a separator and an escape character. |
|
131 | /// </summary> |
130 | /// </summary> |
|
132 | /// <param name="path">the path to split</param> |
131 | /// <param name="path">the path to split</param> |
|
133 | /// <param name="separator">the separator character</param> |
132 | /// <param name="separator">the separator character</param> |
|
134 | /// <param name="escape">the escape character</param> |
133 | /// <param name="escape">the escape character</param> |
|
135 | /// <returns>path parts</returns> |
134 | /// <returns>path parts</returns> |
|
136 | public static IEnumerable<string> PathSplit(this string path, char separator, char? escape) |
135 | public static IEnumerable<string> PathSplit(this string path, char separator, char? escape) |
|
137 | { |
136 | { |
|
138 | var s = new Stack<char>(); |
137 | var s = new Stack<char>(); |
|
139 | var p = new StringBuilder(); |
138 | var p = new StringBuilder(); |
|
140 | foreach (var c in path) |
139 | foreach (var c in path) |
|
141 | { |
140 | { |
|
142 | if (c == escape) |
141 | if (c == escape) |
|
143 | { |
142 | { |
|
144 | s.Push(c); |
143 | s.Push(c); |
|
145 | continue; |
144 | continue; |
|
146 | } |
145 | } |
|
147 | if (c == separator) |
146 | if (c == separator) |
|
148 | { |
147 | { |
|
149 | if (s.Count.Equals(0) || !s.Peek().Equals(escape)) |
148 | if (s.Count.Equals(0) || !s.Peek().Equals(escape)) |
|
150 | { |
149 | { |
|
151 | yield return p.ToString(); |
150 | yield return p.ToString(); |
|
152 | p = new StringBuilder(); |
151 | p = new StringBuilder(); |
|
153 | continue; |
152 | continue; |
|
154 | } |
153 | } |
|
155 | s.Pop(); |
154 | s.Pop(); |
|
156 | while (!s.Count.Equals(0)) |
155 | while (!s.Count.Equals(0)) |
|
157 | { |
156 | { |
|
158 | p.Append(s.Pop()); |
157 | p.Append(s.Pop()); |
|
159 | } |
158 | } |
|
160 | p.Append(c); |
159 | p.Append(c); |
|
161 | continue; |
160 | continue; |
|
162 | } |
161 | } |
|
163 | p.Append(c); |
162 | p.Append(c); |
|
164 | } |
163 | } |
|
165 | while (!s.Count.Equals(0)) |
164 | while (!s.Count.Equals(0)) |
|
166 | { |
165 | { |
|
167 | p.Append(s.Pop()); |
166 | p.Append(s.Pop()); |
|
168 | } |
167 | } |
|
169 | yield return p.ToString(); |
168 | yield return p.ToString(); |
|
170 | } |
169 | } |
|
171 | } |
170 | } |
|
172 | } |
171 | } |
|
173 | |
172 | |
|
174 |
|
173 |
|
|
175 | |
174 | |
|
176 | |
175 | |
|
177 | |
176 | |