Statistics
| Branch: | Revision:

root / trunk / Pithos.Interfaces / FileInfoExtensions.cs @ 039a89e5

History | View | Annotate | Download (6.2 kB)

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
            Contract.Ensures(!String.IsNullOrWhiteSpace(Contract.Result<string>()));
59
            Contract.EndContractBlock();
60
            
61
            if (String.IsNullOrWhiteSpace(fileName))
62
                return String.Empty;
63

    
64
            var dirInfo = new DirectoryInfo(fileName);
65
            return dirInfo.GetProperCapitalization();
66
        }
67

    
68

    
69
        public static string GetProperCapitalization(this DirectoryInfo dirInfo)
70
        {
71
            if (dirInfo == null)
72
                throw new ArgumentNullException("dirInfo");
73
            Contract.EndContractBlock();
74
            Contract.Assume(!String.IsNullOrWhiteSpace(dirInfo.FullName));
75

    
76
            var parentDirInfo = dirInfo.Parent;
77
            if (null == parentDirInfo)
78
                return dirInfo.Name;
79

    
80
            try
81
            {
82

    
83

    
84
                if (dirInfo.Exists)
85
                    return Path.Combine(GetProperDirectoryCapitalization(parentDirInfo.FullName),
86
                                        parentDirInfo.GetDirectories(dirInfo.Name)[0].Name);
87
                else
88
                {
89
                    return dirInfo.FullName;
90
                }
91
            }
92
            catch (DirectoryNotFoundException)
93
            {
94
                //An exception can occur if a directory is deleted right after the Exists call
95
                return dirInfo.FullName;
96
            }
97
        }
98

    
99

    
100
        public static string GetProperFilePathCapitalization(string fileName)
101
        {
102
            Contract.Ensures(!String.IsNullOrWhiteSpace(Contract.Result<string>()));
103
            Contract.EndContractBlock();
104
            if (String.IsNullOrWhiteSpace(fileName))
105
                return String.Empty;
106

    
107

    
108
            var fileInfo = new FileInfo(fileName);
109
            return fileInfo.GetProperCapitalization();
110
        }
111

    
112
        public static string GetProperCapitalization(this FileInfo fileInfo)
113
        {
114
            if (fileInfo == null)
115
                throw new ArgumentNullException("fileInfo");
116
            Contract.EndContractBlock();
117

    
118

    
119
            var dirInfo = fileInfo.Directory;
120

    
121
            //Directory will not be null for an absolute path
122
            Contract.Assume(dirInfo != null);
123

    
124
            try
125
            {
126

    
127
                if (fileInfo.Exists)
128
                    return Path.Combine(GetProperDirectoryCapitalization(dirInfo.FullName),
129
                                        dirInfo.GetFiles(fileInfo.Name)[0].Name);
130
                else
131
                {
132
                    return fileInfo.FullName;
133
                }
134
            }
135
            catch (FileNotFoundException)
136
            {
137
                //An exception can occur if a file is deleted right after the Exists call
138
                return fileInfo.FullName;
139

    
140
            }
141
        }
142

    
143
        public static string GetProperCapitalization(this FileSystemInfo info)
144
        {
145
            if (info is FileInfo)
146
                return (info as FileInfo).GetProperCapitalization();
147
            if (info is DirectoryInfo)
148
                return (info as DirectoryInfo).GetProperCapitalization();
149
            throw new NotSupportedException("Unexpected parameter type");
150
        }
151

    
152
        public static DirectoryInfo WithProperCapitalization(this DirectoryInfo dirInfo)
153
        {
154
            if (dirInfo==null)
155
                throw new ArgumentNullException("dirInfo");
156
            Contract.EndContractBlock();
157

    
158
            var path = dirInfo.GetProperCapitalization();
159
            return new DirectoryInfo(path);
160
        }
161

    
162
        public static FileInfo WithProperCapitalization(this FileInfo fileInfo)
163
        {
164
            if (fileInfo==null)
165
                throw new ArgumentNullException("fileInfo");
166
            Contract.EndContractBlock();
167

    
168
            var path = fileInfo.GetProperCapitalization();
169
            return new FileInfo(path);
170
        }
171

    
172
        public static FileSystemInfo WithProperCapitalization(this FileSystemInfo info)
173
        {
174
            if (info==null)
175
                throw new ArgumentNullException("info");
176
            Contract.EndContractBlock();
177

    
178
            if (info is FileInfo)
179
                return (info as FileInfo).WithProperCapitalization();
180
            if (info is DirectoryInfo)
181
                return (info as DirectoryInfo).WithProperCapitalization();
182

    
183
            throw new NotSupportedException("Unexpected parameter type");
184
        }
185
    }
186
}