Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / FileInfoExtensions.cs @ 1caef52e

History | View | Annotate | Download (1.5 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.IO;
6
using System.Text.RegularExpressions;
7

    
8
namespace Pithos.Core.Agents
9
{
10
    static class FileInfoExtensions
11
    {
12
        public static  string AsRelativeTo(this FileInfo fileInfo,string path )
13
        {
14
            if (!path.EndsWith("\\"))
15
                path=path.ToLower() + "\\";
16
            int pathLength = path.Length;            
17
            
18
            var filePath = fileInfo.FullName;
19

    
20
            if (!filePath.StartsWith(path,StringComparison.InvariantCultureIgnoreCase))
21
                throw new ArgumentException(String.Format("The path {0} doesn't contain the file {1}",path,filePath));
22
            
23
            var relativePath = filePath.Substring(pathLength, filePath.Length - pathLength);
24

    
25
            return relativePath;
26
        }
27

    
28
        public static string AsRelativeUrlTo(this FileInfo fileInfo,string path )
29
        {
30

    
31
            var relativePath = fileInfo.AsRelativeTo(path);
32
            var replacedSlashes = relativePath.Replace("\\","/");
33
            var escaped = Uri.EscapeUriString(replacedSlashes);
34
            return escaped;
35
        }
36

    
37
        public static string RelativeUriToFilePath(this Uri uri)
38
        {
39
            return RelativeUrlToFilePath(uri.ToString());
40
        }
41

    
42
        public static string RelativeUrlToFilePath(this string url)
43
        {
44
            var unescaped=Uri.UnescapeDataString(url);
45
            var path = unescaped.Replace("/", "\\");
46
            return path;
47
        }
48

    
49

    
50
    }
51
}