Skip to content

Commit e7680cf

Browse files
committed
Add new APIs to IWriteableVirtualPathProvider, add impl in InMemoryVPP
1 parent 6085841 commit e7680cf

4 files changed

Lines changed: 70 additions & 13 deletions

File tree

src/ServiceStack/ServiceStack.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@
467467
<Compile Include="VirtualPath\FileSystemVirtualFile.cs" />
468468
<Compile Include="VirtualPath\FileSystemVirtualPathProvider.cs" />
469469
<Compile Include="VirtualPath\InMemoryVirtualPathProvider.cs" />
470+
<Compile Include="VirtualPath\IWriteableVirtualPathProvider.cs" />
470471
<Compile Include="VirtualPath\MultiVirtualPathProvider.cs" />
471472
<Compile Include="VirtualPath\ResourceVirtualDirectory.cs" />
472473
<Compile Include="VirtualPath\ResourceVirtualFile.cs" />

src/ServiceStack/VirtualPath/AbstractVirtualFileBase.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public virtual string ReadAllText()
5959
}
6060
}
6161

62+
public virtual byte[] ReadAllBytes()
63+
{
64+
using (var stream = OpenRead())
65+
{
66+
return stream.ReadFully();
67+
}
68+
}
69+
6270
public abstract Stream OpenRead();
6371

6472
protected virtual String GetVirtualPathToRoot()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.IO;
3+
using ServiceStack.IO;
4+
5+
namespace ServiceStack.VirtualPath
6+
{
7+
public interface IWriteableVirtualPathProvider
8+
{
9+
void AddFile(string filePath, string textContents);
10+
11+
void AddFile(string filePath, Stream stream);
12+
}
13+
14+
public static class WriteableVirtualPathProviderExtensions
15+
{
16+
public static void AddFile(this IVirtualPathProvider pathProvider, string filePath, string textContents)
17+
{
18+
var writable = pathProvider as IWriteableVirtualPathProvider;
19+
if (writable == null)
20+
throw new InvalidOperationException("{0} does not implement IWriteableVirtualPathProvider"
21+
.Fmt(pathProvider.GetType().Name));
22+
23+
writable.AddFile(filePath, textContents);
24+
}
25+
26+
public static void AddFile(this IVirtualPathProvider pathProvider, string filePath, Stream stream)
27+
{
28+
var writable = pathProvider as IWriteableVirtualPathProvider;
29+
if (writable == null)
30+
throw new InvalidOperationException("{0} does not implement IWriteableVirtualPathProvider"
31+
.Fmt(pathProvider.GetType().Name));
32+
33+
writable.AddFile(filePath, stream);
34+
}
35+
}
36+
}

src/ServiceStack/VirtualPath/InMemoryVirtualPathProvider.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88
namespace ServiceStack.VirtualPath
99
{
10-
public interface IWriteableVirtualPathProvider
11-
{
12-
void AddFile(string filePath, string contents);
13-
}
14-
1510
/// <summary>
1611
/// In Memory repository for files. Useful for testing.
1712
/// </summary>
@@ -44,16 +39,21 @@ protected override void Initialize()
4439
{
4540
}
4641

47-
public void AddFile(string filePath, string contents)
48-
{
49-
rootDirectory.AddFile(filePath, contents);
50-
}
51-
5242
public override IVirtualFile GetFile(string virtualPath)
5343
{
5444
return rootDirectory.GetFile(virtualPath)
5545
?? base.GetFile(virtualPath);
5646
}
47+
48+
public void AddFile(string filePath, string textContents)
49+
{
50+
rootDirectory.AddFile(filePath, textContents);
51+
}
52+
53+
public void AddFile(string filePath, Stream stream)
54+
{
55+
rootDirectory.AddFile(filePath, stream);
56+
}
5757
}
5858

5959
public class InMemoryVirtualDirectory : AbstractVirtualDirectoryBase
@@ -134,20 +134,32 @@ protected override IVirtualDirectory GetDirectoryFromBackingDirectoryOrDefault(s
134134
return null;
135135
}
136136

137-
static readonly char[] DirSeps = new[] { '\\', '/' };
138137
public void AddFile(string filePath, string contents)
139138
{
140139
filePath = StripBeginningDirectorySeparator(filePath);
141-
this.files.Add(new InMemoryVirtualFile(VirtualPathProvider, this) {
140+
this.files.Add(new InMemoryVirtualFile(VirtualPathProvider, this)
141+
{
142142
FilePath = filePath,
143143
FileName = filePath.Split(DirSeps).Last(),
144144
TextContents = contents,
145145
});
146146
}
147147

148+
public void AddFile(string filePath, Stream stream)
149+
{
150+
filePath = StripBeginningDirectorySeparator(filePath);
151+
this.files.Add(new InMemoryVirtualFile(VirtualPathProvider, this)
152+
{
153+
FilePath = filePath,
154+
FileName = filePath.Split(DirSeps).Last(),
155+
ByteContents = stream.ReadFully(),
156+
});
157+
}
158+
159+
static readonly char[] DirSeps = new[] { '\\', '/' };
148160
private static string StripBeginningDirectorySeparator(string filePath)
149161
{
150-
if (String.IsNullOrEmpty(filePath))
162+
if (string.IsNullOrEmpty(filePath))
151163
return filePath;
152164

153165
if (DirSeps.Any(d => filePath[0] == d))

0 commit comments

Comments
 (0)