Added RedirectUsingNamedRoute
[castle.git] / Core / Castle.Core / Resource / UncResource.cs
blobfc8b254cb3d9ae8f47e20ff8fcc826e1322a55d2
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Core.Resource
17 using System;
18 using System.IO;
20 /// <summary>
21 /// Enable access to files on network shares
22 /// </summary>
23 public class UncResource : AbstractStreamResource
25 private String basePath;
26 private string filePath;
28 public UncResource(CustomUri resource)
30 CreateStream = delegate
32 return CreateStreamFromUri(resource, DefaultBasePath);
36 public UncResource(CustomUri resource, String basePath)
38 CreateStream = delegate
40 return CreateStreamFromUri(resource, basePath);
44 public UncResource(String resourceName) : this(new CustomUri(resourceName))
48 public UncResource(String resourceName, String basePath) : this(new CustomUri(resourceName), basePath)
52 public override String FileBasePath
54 get { return basePath; }
57 public override IResource CreateRelative(String resourceName)
59 return new UncResource(Path.Combine(basePath, resourceName));
62 public override string ToString()
64 return String.Format("UncResource: [{0}] [{1}]", filePath, basePath);
67 private Stream CreateStreamFromUri(CustomUri resource, String basePath)
69 if (resource == null)
70 throw new ArgumentNullException("resource");
71 if (!resource.IsUnc)
72 throw new ArgumentException("Resource must be an Unc", "resource");
73 if (!resource.IsFile)
74 throw new ArgumentException("The specified resource is not a file", "resource");
76 String filePath = resource.Path;
78 if (!File.Exists(filePath) && basePath != null)
80 filePath = Path.Combine(basePath, filePath);
83 this.filePath = Path.GetFileName(filePath);
84 this.basePath = Path.GetDirectoryName(filePath);
86 CheckFileExists(filePath);
88 return File.OpenRead(filePath);
91 private void CheckFileExists(String path)
93 if (!File.Exists(path))
95 String message = String.Format("File {0} could not be found", path);
96 throw new ResourceException(message);