Skip to content

Commit 90b3251

Browse files
committed
Deprecate IWriteableVirtualPathProvider and rename to IVirtualFileSystem and move to SS.Interfaces
1 parent c646209 commit 90b3251

8 files changed

Lines changed: 71 additions & 28 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace ServiceStack.IO
6+
{
7+
public interface IVirtualFileSystem : IVirtualPathProvider
8+
{
9+
void WriteFile(string filePath, string textContents);
10+
11+
void WriteFile(string filePath, Stream stream);
12+
13+
void WriteFiles(IEnumerable<IVirtualFile> files, Func<IVirtualFile, string> toPath = null);
14+
15+
void DeleteFile(string filePath);
16+
17+
void DeleteFiles(IEnumerable<string> filePaths);
18+
19+
void DeleteFolder(string dirPath);
20+
}
21+
}

src/ServiceStack.Interfaces/ServiceStack.Interfaces.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
</Compile>
139139
<Compile Include="Data\OptimisticConcurrencyException.cs" />
140140
<Compile Include="ICompressor.cs" />
141+
<Compile Include="IO\IVirtualFileSystem.cs" />
141142
<Compile Include="IO\IEndpoint.cs" />
142143
<Compile Include="IQuery.cs" />
143144
<Compile Include="IReceiver.cs" />

src/ServiceStack.Razor/RazorFormat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ public RazorPage CreatePage(string razorContents)
249249
if (this.VirtualPathProvider == null)
250250
throw new ArgumentNullException("VirtualPathProvider");
251251

252-
var writableFileProvider = this.VirtualPathProvider as IWriteableVirtualPathProvider;
252+
var writableFileProvider = this.VirtualPathProvider as IVirtualFileSystem;
253253
if (writableFileProvider == null)
254-
throw new InvalidOperationException("VirtualPathProvider is not IWriteableVirtualPathProvider");
254+
throw new InvalidOperationException("VirtualPathProvider is not IVirtualFileSystem");
255255

256256
var tmpPath = "/__tmp/{0}.cshtml".Fmt(Guid.NewGuid().ToString("N"));
257257
writableFileProvider.WriteFile(tmpPath, razorContents);

src/ServiceStack/ServiceStack.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@
467467
<Compile Include="VirtualPath\FileSystemVirtualFile.cs" />
468468
<Compile Include="VirtualPath\FileSystemVirtualPathProvider.cs" />
469469
<Compile Include="VirtualPath\InMemoryVirtualPathProvider.cs" />
470-
<Compile Include="VirtualPath\IWriteableVirtualPathProvider.cs" />
471470
<Compile Include="VirtualPath\MultiVirtualPathProvider.cs" />
472471
<Compile Include="VirtualPath\ResourceVirtualDirectory.cs" />
473472
<Compile Include="VirtualPath\ResourceVirtualFile.cs" />
@@ -585,6 +584,7 @@
585584
<Compile Include="Metadata\IndexOperationsControl.cs" />
586585
<Compile Include="Metadata\XsdTypeNames.cs" />
587586
<Compile Include="Metadata\XsdTypes.cs" />
587+
<Compile Include="VirtualPath\VirtualPathProviderExtensions.cs" />
588588
<Compile Include="WebSudoFeature.cs" />
589589
<Compile Include="WebSudoRequiredAttribute.cs" />
590590
<Compile Include="XsdUtils.cs" />

src/ServiceStack/ServiceStackHost.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,19 @@ public virtual ServiceStackHost Init()
154154

155155
if (VirtualPathProvider == null)
156156
{
157-
var pathProviders = GetVirtualPathProviders();
157+
var pathProviders = GetVirtualPathProviders().Where(x => x != null).ToList();
158158

159159
VirtualPathProvider = pathProviders.Count > 1
160160
? new MultiVirtualPathProvider(this, pathProviders.ToArray())
161161
: pathProviders.First();
162162
}
163163

164+
if (VirtualFileSystem == null)
165+
{
166+
var fs = GetVirtualPathProviders().FirstOrDefault(x => x is FileSystemVirtualPathProvider);
167+
VirtualFileSystem = fs as IVirtualFileSystem;
168+
}
169+
164170
OnAfterInit();
165171

166172
LogInitComplete();
@@ -311,8 +317,16 @@ public Dictionary<Type, Func<IRequest, object>> RequestBinders
311317

312318
public List<IPlugin> Plugins { get; set; }
313319

320+
/// <summary>
321+
/// Cascading number of file sources, inc. Embedded Resources, File System, In Memory, S3
322+
/// </summary>
314323
public IVirtualPathProvider VirtualPathProvider { get; set; }
315324

325+
/// <summary>
326+
/// Read/Write Virtual FileSystem. Defaults to FileSystemVirtualPathProvider
327+
/// </summary>
328+
public IVirtualFileSystem VirtualFileSystem { get; set; }
329+
316330
/// <summary>
317331
/// Executed immediately before a Service is executed. Use return to change the request DTO used, must be of the same type.
318332
/// </summary>

src/ServiceStack/VirtualPath/FileSystemVirtualPathProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace ServiceStack.VirtualPath
77
{
8-
public class FileSystemVirtualPathProvider : AbstractVirtualPathProviderBase, IWriteableVirtualPathProvider
8+
public class FileSystemVirtualPathProvider : AbstractVirtualPathProviderBase, IVirtualFileSystem, IWriteableVirtualPathProvider
99
{
1010
protected DirectoryInfo RootDirInfo;
1111
protected FileSystemVirtualDirectory RootDir;

src/ServiceStack/VirtualPath/InMemoryVirtualPathProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
namespace ServiceStack.VirtualPath
99
{
10+
[Obsolete("Renamed to IVirtualFileSystem")]
11+
public interface IWriteableVirtualPathProvider : IVirtualFileSystem {}
12+
1013
/// <summary>
1114
/// In Memory Virtual Path Provider.
1215
/// </summary>
13-
public class InMemoryVirtualPathProvider : AbstractVirtualPathProviderBase, IWriteableVirtualPathProvider
16+
public class InMemoryVirtualPathProvider : AbstractVirtualPathProviderBase, IVirtualFileSystem, IWriteableVirtualPathProvider
1417
{
1518
public InMemoryVirtualPathProvider(IAppHost appHost)
1619
: base(appHost)

src/ServiceStack/VirtualPath/IWriteableVirtualPathProvider.cs renamed to src/ServiceStack/VirtualPath/VirtualPathProviderExtensions.cs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,19 @@
66

77
namespace ServiceStack.VirtualPath
88
{
9-
public interface IWriteableVirtualPathProvider : IVirtualPathProvider
9+
public static class VirtualPathProviderExtensions
1010
{
11-
void WriteFile(string filePath, string textContents);
11+
private const string ErrorNotWritable = "{0} does not implement IVirtualFileSystem";
1212

13-
void WriteFile(string filePath, Stream stream);
14-
15-
void WriteFiles(IEnumerable<IVirtualFile> files, Func<IVirtualFile, string> toPath = null);
16-
17-
void DeleteFile(string filePath);
18-
19-
void DeleteFiles(IEnumerable<string> filePaths);
20-
21-
void DeleteFolder(string dirPath);
22-
}
13+
public static bool IsFile(this IVirtualPathProvider pathProvider, string filePath)
14+
{
15+
return pathProvider.GetFile(filePath) != null;
16+
}
2317

24-
public static class WriteableVirtualPathProviderExtensions
25-
{
26-
private const string ErrorNotWritable = "{0} does not implement IWriteableVirtualPathProvider";
18+
public static bool IsDirectory(this IVirtualPathProvider pathProvider, string filePath)
19+
{
20+
return pathProvider.GetDirectory(filePath) != null;
21+
}
2722

2823
[Obsolete("Renamed to WriteFile")]
2924
public static void AddFile(this IVirtualPathProvider pathProvider, string filePath, string textContents)
@@ -33,7 +28,7 @@ public static void AddFile(this IVirtualPathProvider pathProvider, string filePa
3328

3429
public static void WriteFile(this IVirtualPathProvider pathProvider, string filePath, string textContents)
3530
{
36-
var writableFs = pathProvider as IWriteableVirtualPathProvider;
31+
var writableFs = pathProvider as IVirtualFileSystem;
3732
if (writableFs == null)
3833
throw new InvalidOperationException(ErrorNotWritable.Fmt(pathProvider.GetType().Name));
3934

@@ -42,7 +37,7 @@ public static void WriteFile(this IVirtualPathProvider pathProvider, string file
4237

4338
public static void WriteFile(this IVirtualPathProvider pathProvider, string filePath, Stream stream)
4439
{
45-
var writableFs = pathProvider as IWriteableVirtualPathProvider;
40+
var writableFs = pathProvider as IVirtualFileSystem;
4641
if (writableFs == null)
4742
throw new InvalidOperationException(ErrorNotWritable.Fmt(pathProvider.GetType().Name));
4843

@@ -51,7 +46,7 @@ public static void WriteFile(this IVirtualPathProvider pathProvider, string file
5146

5247
public static void WriteFile(this IVirtualPathProvider pathProvider, string filePath, byte[] bytes)
5348
{
54-
var writableFs = pathProvider as IWriteableVirtualPathProvider;
49+
var writableFs = pathProvider as IVirtualFileSystem;
5550
if (writableFs == null)
5651
throw new InvalidOperationException(ErrorNotWritable.Fmt(pathProvider.GetType().Name));
5752

@@ -63,7 +58,7 @@ public static void WriteFile(this IVirtualPathProvider pathProvider, string file
6358

6459
public static void DeleteFile(this IVirtualPathProvider pathProvider, string filePath)
6560
{
66-
var writableFs = pathProvider as IWriteableVirtualPathProvider;
61+
var writableFs = pathProvider as IVirtualFileSystem;
6762
if (writableFs == null)
6863
throw new InvalidOperationException(ErrorNotWritable.Fmt(pathProvider.GetType().Name));
6964

@@ -72,16 +67,25 @@ public static void DeleteFile(this IVirtualPathProvider pathProvider, string fil
7267

7368
public static void DeleteFiles(this IVirtualPathProvider pathProvider, IEnumerable<string> filePaths)
7469
{
75-
var writableFs = pathProvider as IWriteableVirtualPathProvider;
70+
var writableFs = pathProvider as IVirtualFileSystem;
7671
if (writableFs == null)
7772
throw new InvalidOperationException(ErrorNotWritable.Fmt(pathProvider.GetType().Name));
7873

7974
writableFs.DeleteFiles(filePaths);
8075
}
8176

77+
public static void DeleteFiles(this IVirtualPathProvider pathProvider, IEnumerable<IVirtualFile> files)
78+
{
79+
var writableFs = pathProvider as IVirtualFileSystem;
80+
if (writableFs == null)
81+
throw new InvalidOperationException(ErrorNotWritable.Fmt(pathProvider.GetType().Name));
82+
83+
writableFs.DeleteFiles(files.Map(x => x.VirtualPath));
84+
}
85+
8286
public static void DeleteFolder(this IVirtualPathProvider pathProvider, string dirPath)
8387
{
84-
var writableFs = pathProvider as IWriteableVirtualPathProvider;
88+
var writableFs = pathProvider as IVirtualFileSystem;
8589
if (writableFs == null)
8690
throw new InvalidOperationException(ErrorNotWritable.Fmt(pathProvider.GetType().Name));
8791

@@ -90,7 +94,7 @@ public static void DeleteFolder(this IVirtualPathProvider pathProvider, string d
9094

9195
public static void WriteFiles(this IVirtualPathProvider pathProvider, IEnumerable<IVirtualFile> srcFiles, Func<IVirtualFile, string> toPath = null)
9296
{
93-
var writableFs = pathProvider as IWriteableVirtualPathProvider;
97+
var writableFs = pathProvider as IVirtualFileSystem;
9498
if (writableFs == null)
9599
throw new InvalidOperationException(ErrorNotWritable.Fmt(pathProvider.GetType().Name));
96100

0 commit comments

Comments
 (0)