-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathFileManager.cs
76 lines (67 loc) · 2.39 KB
/
FileManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//-----------------------------------------------------------------------
// <copyright file="FileManager.cs" company="(none)">
// Copyright © 2013 John Gietzen and the WebGit .NET Authors. All rights reserved.
// </copyright>
// <author>John Gietzen</author>
//-----------------------------------------------------------------------
namespace WebGitNet
{
using System.IO;
using System.Web;
public class FileManager
{
private readonly DirectoryInfo dirInfo;
private readonly string rootPath;
public FileManager(string path)
{
path = Path.GetDirectoryName(Path.Combine(path, "slug"));
this.dirInfo = new DirectoryInfo(path);
this.rootPath = path + Path.DirectorySeparatorChar;
}
public DirectoryInfo DirectoryInfo
{
get
{
return this.dirInfo;
}
}
public ResourceInfo GetResourceInfo(string resourcePath)
{
var fullPath = this.FindFullPath(resourcePath);
var info = new ResourceInfo { FullPath = fullPath };
if (!fullPath.StartsWith(this.rootPath))
{
info.Type = ResourceType.NotFound;
}
else if (File.Exists(fullPath))
{
info.Type = ResourceType.File;
info.FileSystemInfo = new FileInfo(fullPath);
}
else if (Directory.Exists(fullPath))
{
info.Type = ResourceType.Directory;
info.FileSystemInfo = new DirectoryInfo(fullPath);
}
else
{
info.Type = ResourceType.NotFound;
info.FileSystemInfo = fullPath.EndsWith(Path.DirectorySeparatorChar.ToString())
? (FileSystemInfo)new DirectoryInfo(fullPath)
: new FileInfo(fullPath);
}
if (info.Type != ResourceType.NotFound)
{
info.LocalPath = fullPath.Substring(this.rootPath.Length).Replace(@"\", @"/");
info.Name = info.FileSystemInfo.Name;
}
return info;
}
private string FindFullPath(string url)
{
string decodedUrl = HttpUtility.UrlDecode(url);
var path = Path.Combine(this.rootPath, decodedUrl);
return Path.GetFullPath(path);
}
}
}