Convert ActiveRecord update code to direct ADO calls to reduce locks
[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
78             try
79             {
80
81
82                 if (dirInfo.Exists)
83                     return Path.Combine(GetProperDirectoryCapitalization(parentDirInfo.FullName),
84                                         parentDirInfo.GetDirectories(dirInfo.Name)[0].Name);
85                 else
86                 {
87                     return dirInfo.FullName;
88                 }
89             }
90             catch (DirectoryNotFoundException)
91             {
92                 //An exception can occur if a directory is deleted right after the Exists call
93                 return dirInfo.FullName;
94             }
95         }
96
97
98         public static string GetProperFilePathCapitalization(string fileName)
99         {
100             if (String.IsNullOrWhiteSpace(fileName))
101                 throw new ArgumentNullException("fileName");
102             if (!Path.IsPathRooted(fileName))
103                 throw new ArgumentException("fileName must be an absolute path", "fileName");
104             Contract.EndContractBlock();
105
106
107             var fileInfo = new FileInfo(fileName);
108             return fileInfo.GetProperCapitalization();
109         }
110
111         public static string GetProperCapitalization(this FileInfo fileInfo)
112         {
113             if (fileInfo == null)
114                 throw new ArgumentNullException("fileInfo");
115             Contract.EndContractBlock();
116
117
118             var dirInfo = fileInfo.Directory;
119
120             //Directory will not be null for an absolute path
121             Contract.Assume(dirInfo != null);
122
123             try
124             {
125
126                 if (fileInfo.Exists)
127                     return Path.Combine(GetProperDirectoryCapitalization(dirInfo.FullName),
128                                         dirInfo.GetFiles(fileInfo.Name)[0].Name);
129                 else
130                 {
131                     return fileInfo.FullName;
132                 }
133             }
134             catch (FileNotFoundException)
135             {
136                 //An exception can occur if a file is deleted right after the Exists call
137                 return fileInfo.FullName;
138
139             }
140         }
141
142         public static string GetProperCapitalization(this FileSystemInfo info)
143         {
144             if (info is FileInfo)
145                 return (info as FileInfo).GetProperCapitalization();
146             if (info is DirectoryInfo)
147                 return (info as DirectoryInfo).GetProperCapitalization();
148             throw new NotSupportedException("Unexpected parameter type");
149         }
150
151         public static DirectoryInfo WithProperCapitalization(this DirectoryInfo dirInfo)
152         {
153             if (dirInfo==null)
154                 throw new ArgumentNullException("dirInfo");
155             Contract.EndContractBlock();
156
157             var path = dirInfo.GetProperCapitalization();
158             return new DirectoryInfo(path);
159         }
160
161         public static FileInfo WithProperCapitalization(this FileInfo fileInfo)
162         {
163             if (fileInfo==null)
164                 throw new ArgumentNullException("fileInfo");
165             Contract.EndContractBlock();
166
167             var path = fileInfo.GetProperCapitalization();
168             return new FileInfo(path);
169         }
170
171         public static FileSystemInfo WithProperCapitalization(this FileSystemInfo info)
172         {
173             if (info==null)
174                 throw new ArgumentNullException("info");
175             Contract.EndContractBlock();
176
177             if (info is FileInfo)
178                 return (info as FileInfo).WithProperCapitalization();
179             if (info is DirectoryInfo)
180                 return (info as DirectoryInfo).WithProperCapitalization();
181
182             throw new NotSupportedException("Unexpected parameter type");
183         }
184     }
185 }