forked from jefking/King.A-Trak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disk.cs
149 lines (135 loc) · 3.68 KB
/
Disk.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
namespace Abc.ATrak
{
using System;
using System.Diagnostics;
using System.IO;
/// <summary>
/// Disk Storage Item
/// </summary>
public class Disk : IStorageItem
{
#region Members
/// <summary>
/// File Data
/// </summary>
private byte[] data = null;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the Disk
/// </summary>
/// <param name="root">Root Path of File</param>
/// <param name="path">Path of File</param>
public Disk(string root, string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException("path");
}
else if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException("path");
}
else
{
this.Path = path;
this.RelativePath = path.Replace(root, string.Empty);
if (File.Exists(this.Path))
{
this.GetData();
this.data = null;
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets the MD5 Hash
/// </summary>
public string MD5
{
get;
private set;
}
/// <summary>
/// Gets the Content Type
/// </summary>
public string ContentType
{
get
{
return ContentTypes.ContentType(this.Path);
}
}
/// <summary>
/// Gets the Path
/// </summary>
public string Path
{
get;
private set;
}
/// <summary>
/// Gets the Relative Path
/// </summary>
public string RelativePath
{
get;
private set;
}
#endregion
#region Methods
/// <summary>
/// Get Data
/// </summary>
/// <returns>Data for object</returns>
public byte[] GetData()
{
if (this.data == null)
{
this.data = File.ReadAllBytes(this.Path); // < 2 gigs
using (var createHash = System.Security.Cryptography.MD5.Create())
{
var hash = createHash.ComputeHash(this.data);
this.MD5 = System.Convert.ToBase64String(hash);
}
}
return this.data;
}
/// <summary>
/// Check to see if item exists
/// </summary>
/// <returns>Exists</returns>
public bool Exists()
{
return File.Exists(this.Path);
}
/// <summary>
/// Save Storage Item
/// </summary>
/// <param name="storageItem">Storage Item</param>
/// <param name="exists">Exists</param>
public void Save(IStorageItem storageItem, bool exists = false)
{
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(this.Path));
File.WriteAllBytes(this.Path, storageItem.GetData());
}
/// <summary>
/// Delete
/// </summary>
public void Delete()
{
File.Delete(this.Path);
Trace.Write(string.Format("{0} deleted.", this.Path));
}
/// <summary>
/// To String
/// </summary>
/// <returns>String reprensentation of object</returns>
public override string ToString()
{
return string.Format("{0}", this.Path);
}
#endregion
}
}