forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualPathUtils.cs
More file actions
103 lines (83 loc) · 3.23 KB
/
VirtualPathUtils.cs
File metadata and controls
103 lines (83 loc) · 3.23 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) ServiceStack, Inc. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.IO;
namespace ServiceStack
{
public static class VirtualPathUtils
{
public static Stack<string> TokenizeVirtualPath(this string str, IVirtualPathProvider pathProvider)
{
if (pathProvider == null)
throw new ArgumentNullException(nameof(pathProvider));
return TokenizeVirtualPath(str, pathProvider.VirtualPathSeparator);
}
public static Stack<string> TokenizeVirtualPath(this string str, string virtualPathSeparator)
{
if (string.IsNullOrEmpty(str))
return new Stack<string>();
var tokens = str.Split(new[] { virtualPathSeparator }, StringSplitOptions.RemoveEmptyEntries);
return new Stack<string>(tokens.Reverse());
}
public static Stack<string> TokenizeResourcePath(this string str, char pathSeparator = '.')
{
if (string.IsNullOrEmpty(str))
return new Stack<string>();
var n = str.Count(c => c == pathSeparator);
var tokens = str.Split(new[] { pathSeparator }, n);
return new Stack<string>(tokens.Reverse());
}
public static IEnumerable<IGrouping<string, string[]>> GroupByFirstToken(this IEnumerable<string> resourceNames, char pathSeparator = '.')
{
return resourceNames.Select(n => n.Split(new[] { pathSeparator }, 2))
.GroupBy(t => t[0]);
}
public static byte[] ReadAllBytes(this IVirtualFile file)
{
using (var stream = file.OpenRead())
{
var bytes = stream.ReadFully();
return bytes;
}
}
public static bool Exists(this IVirtualNode node)
{
return node != null;
}
public static bool IsFile(this IVirtualNode node)
{
return node is IVirtualFile;
}
public static bool IsDirectory(this IVirtualNode node)
{
return node is IVirtualDirectory;
}
public static IVirtualNode GetVirtualNode(this IVirtualPathProvider pathProvider, string virtualPath)
{
return (IVirtualNode)pathProvider.GetFile(virtualPath)
?? pathProvider.GetDirectory(virtualPath);
}
public static IVirtualFile GetDefaultDocument(this IVirtualDirectory dir, List<string> defaultDocuments)
{
foreach (var defaultDoc in defaultDocuments)
{
var defaultFile = dir.GetFile(defaultDoc);
if (defaultFile == null) continue;
return defaultFile;
}
return null;
}
public static TimeSpan MaxRetryOnExceptionTimeout { get; } = TimeSpan.FromSeconds(10);
internal static void SleepBackOffMultiplier(this int i)
{
var nextTryMs = (2 ^ i) * 50;
#if NETSTANDARD2_0
System.Threading.Tasks.Task.Delay(nextTryMs).Wait();
#elif NET45
System.Threading.Thread.Sleep(nextTryMs);
#endif
}
}
}