Repository navigation
Expand file tree
/
Copy pathFileExtensions.cs
More file actions
83 lines (71 loc) · 2.66 KB
/
Copy pathFileExtensions.cs
File metadata and controls
83 lines (71 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.IO;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack
{
public static class FileExtensions
{
public static void SaveTo(this IHttpFile httpFile, string filePath)
{
using var sw = new StreamWriter(File.Create(filePath));
httpFile.InputStream.WriteTo(sw.BaseStream);
}
public static void SaveTo(this IHttpFile httpFile, IVirtualFiles vfs, string filePath)
{
vfs.WriteFile(filePath, httpFile.InputStream);
}
public static void WriteTo(this IHttpFile httpFile, Stream stream)
{
httpFile.InputStream.WriteTo(stream);
}
public static async Task SaveToAsync(this IHttpFile httpFile, string filePath)
{
#if NET6_0_OR_GREATER
await
#endif
using var sw = new StreamWriter(File.Create(filePath));
await httpFile.InputStream.WriteToAsync(sw.BaseStream).ConfigAwait();
}
public static async Task SaveToAsync(this IHttpFile httpFile, IVirtualFiles vfs, string filePath, CancellationToken token=default)
{
await vfs.WriteFileAsync(filePath, httpFile.InputStream, token).ConfigAwait();
}
public static async Task WriteToAsync(this IHttpFile httpFile, Stream stream)
{
await httpFile.InputStream.WriteToAsync(stream).ConfigAwait();
}
public static string MapServerPath(this string relativePath)
{
return HostContext.IsAspNetHost
? relativePath.MapHostAbsolutePath()
: relativePath.MapAbsolutePath();
}
public static bool IsRelativePath(this string relativeOrAbsolutePath)
{
return !relativeOrAbsolutePath.Contains(":")
&& !relativeOrAbsolutePath.StartsWith("/")
&& !relativeOrAbsolutePath.StartsWith("\\");
}
public static byte[] ReadFully(this FileInfo file)
{
using var fs = file.OpenRead();
return fs.ReadFully();
}
public static Task<byte[]> ReadFullyAsync(this FileInfo file, CancellationToken token=new())
{
using var fs = file.OpenRead();
return fs.ReadFullyAsync(token);
}
public static string ReadAllText(this FileInfo file)
{
return file.ReadFully().FromUtf8Bytes();
}
public static async Task<string> ReadAllTextAsync(this FileInfo file, CancellationToken token=new())
{
return (await file.ReadFullyAsync(token)).FromUtf8Bytes();
}
}
}