Changes for directories
[pithos-ms-client] / trunk / Pithos.Interfaces / FileInfoExtensions.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics.Contracts;
4 using System.Linq;
5 using System.Text;
6 using System.IO;
7 using System.Text.RegularExpressions;
8 using System.Threading.Tasks;
9
10
11 namespace Pithos.Interfaces
12 {
13     public static class FileInfoExtensions
14     {
15         public static  string AsRelativeTo(this FileSystemInfo fileInfo,string path )
16         {
17             if (String.IsNullOrWhiteSpace(path))
18                 throw new ArgumentNullException("path");            
19             Contract.EndContractBlock();
20             Contract.Assume(Enum.IsDefined(typeof(StringComparison),StringComparison.InvariantCultureIgnoreCase));
21
22             if (!path.EndsWith("\\"))
23                 path=path.ToLower() + "\\";
24             int pathLength = path.Length;            
25             
26             var filePath = fileInfo.GetProperCapitalization();
27             
28             if (!filePath.StartsWith(path,StringComparison.InvariantCultureIgnoreCase))
29                 throw new ArgumentException(String.Format("The path {0} doesn't contain the file {1}",path,filePath));
30             
31             var relativePath = filePath.Substring(pathLength, filePath.Length - pathLength);
32
33             return relativePath;
34         }
35
36         public static string AsRelativeUrlTo(this FileSystemInfo fileInfo,string path )
37         {
38             if (String.IsNullOrWhiteSpace(path))
39                 throw new ArgumentNullException("path");
40             Contract.EndContractBlock();
41
42             var relativePath = fileInfo.AsRelativeTo(path);
43             var replacedSlashes = relativePath.Replace("\\","/");
44             var escaped = Uri.EscapeUriString(replacedSlashes);
45             return escaped;
46         }
47
48         public static string RelativeUriToFilePath(this Uri uri)
49         {
50             var unescaped = Uri.UnescapeDataString(uri.ToString());
51             var path = unescaped.Replace("/", "\\");
52             return path;
53         }
54
55
56         public static string GetProperDirectoryCapitalization(string fileName)
57         {
58             if (String.IsNullOrWhiteSpace(fileName))
59                 throw new ArgumentNullException("fileName");
60             if (!Path.IsPathRooted(fileName))
61                 throw new ArgumentException("fileName must be an absolute path", "fileName");
62             Contract.EndContractBlock();
63
64             var dirInfo = new DirectoryInfo(fileName);
65             return dirInfo.GetProperCapitalization();
66         }
67
68         public static string GetProperCapitalization(this DirectoryInfo dirInfo)
69         {
70             if (dirInfo == null)
71                 throw new ArgumentNullException("dirInfo");
72             Contract.EndContractBlock();
73
74             var parentDirInfo = dirInfo.Parent;
75             if (null == parentDirInfo)
76                 return dirInfo.Name;
77             return Path.Combine(GetProperDirectoryCapitalization(parentDirInfo.FullName),
78                                 parentDirInfo.GetDirectories(dirInfo.Name)[0].Name);
79         }
80
81
82         public static string GetProperFilePathCapitalization(string fileName)
83         {
84             if (String.IsNullOrWhiteSpace(fileName))
85                 throw new ArgumentNullException("fileName");
86             if (!Path.IsPathRooted(fileName))
87                 throw new ArgumentException("fileName must be an absolute path", "fileName");
88             Contract.EndContractBlock();
89
90
91             var fileInfo = new FileInfo(fileName);
92             return fileInfo.GetProperCapitalization();
93         }
94
95         public static string GetProperCapitalization(this FileInfo fileInfo)
96         {
97             if (fileInfo == null)
98                 throw new ArgumentNullException("fileInfo");
99             Contract.EndContractBlock();
100
101
102             var dirInfo = fileInfo.Directory;
103
104             //Directory will not be null for an absolute path
105             Contract.Assume(dirInfo != null);
106
107             return Path.Combine(GetProperDirectoryCapitalization(dirInfo.FullName),
108                                 dirInfo.GetFiles(fileInfo.Name)[0].Name);
109         }
110
111         public static string GetProperCapitalization(this FileSystemInfo info)
112         {
113             if (info is FileInfo)
114                 return (info as FileInfo).GetProperCapitalization();
115             if (info is DirectoryInfo)
116                 return (info as DirectoryInfo).GetProperCapitalization();
117             throw new NotSupportedException("Unexpected parameter type");
118         }
119
120         public static DirectoryInfo WithProperCapitalization(this DirectoryInfo dirInfo)
121         {
122             if (dirInfo==null)
123                 throw new ArgumentNullException("dirInfo");
124             Contract.EndContractBlock();
125
126             var path = dirInfo.GetProperCapitalization();
127             return new DirectoryInfo(path);
128         }
129
130         public static FileInfo WithProperCapitalization(this FileInfo fileInfo)
131         {
132             if (fileInfo==null)
133                 throw new ArgumentNullException("fileInfo");
134             Contract.EndContractBlock();
135
136             var path = fileInfo.GetProperCapitalization();
137             return new FileInfo(path);
138         }
139
140         public static FileSystemInfo WithProperCapitalization(this FileSystemInfo info)
141         {
142             if (info==null)
143                 throw new ArgumentNullException("info");
144             Contract.EndContractBlock();
145
146             if (info is FileInfo)
147                 return (info as FileInfo).WithProperCapitalization();
148             if (info is DirectoryInfo)
149                 return (info as DirectoryInfo).WithProperCapitalization();
150
151             throw new NotSupportedException("Unexpected parameter type");
152         }
153     }
154 }