-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBlobCollection.cs
More file actions
192 lines (181 loc) · 6.48 KB
/
Copy pathBlobCollection.cs
File metadata and controls
192 lines (181 loc) · 6.48 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using BytecodeApi.Extensions;
using System.Collections;
namespace BytecodeApi.Data;
/// <summary>
/// Represents a collection of <see cref="Blob" /> objects.
/// </summary>
public sealed class BlobCollection : ICollection<Blob>
{
private readonly List<Blob> Blobs;
/// <summary>
/// Gets the <see cref="Blob" /> at the specified index.
/// </summary>
/// <param name="index">The index at which to retrieve the <see cref="Blob" />.</param>
public Blob this[int index]
{
get
{
Check.IndexOutOfRange(index, Count);
return Blobs[index];
}
set
{
Check.IndexOutOfRange(index, Count);
Blobs[index] = value;
}
}
/// <summary>
/// Gets the <see cref="Blob" /> with the specified case sensitive name and throws an exception, if it was not found.
/// </summary>
/// <param name="name">A <see cref="string" /> specifying the name of the <see cref="Blob" />.</param>
public Blob this[string name]
{
get
{
Check.ArgumentNull(name);
return Blobs.FirstOrDefault(b => b.Name == name) ?? throw Throw.KeyNotFound($"A blob with the name '{name}' was not found.");
}
}
/// <summary>
/// Gets the number of elements contained in the <see cref="BlobCollection" />.
/// </summary>
public int Count => Blobs.Count;
/// <summary>
/// Gets a value indicating whether the <see cref="BlobCollection" /> is read-only.
/// </summary>
public bool IsReadOnly => false;
/// <summary>
/// Initializes a new instance of the <see cref="BlobCollection" /> class.
/// </summary>
public BlobCollection()
{
Blobs = [];
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobCollection" /> class with the specified collection of blobs.
/// </summary>
/// <param name="blobs">A collection of <see cref="Blob" /> objects to add to this <see cref="BlobCollection" />.</param>
public BlobCollection(IEnumerable<Blob> blobs) : this()
{
Check.ArgumentNull(blobs);
Blobs.AddRange(blobs);
}
/// <summary>
/// Creates a <see cref="BlobCollection" /> from the specified directory.
/// </summary>
/// <param name="path">A <see cref="string" /> specifying the path to a directory from which to create the <see cref="BlobCollection" />. Subdirectories are not searched.</param>
/// <returns>
/// The <see cref="BlobCollection" /> this method creates.
/// </returns>
public static BlobCollection FromDirectory(string path)
{
return FromDirectory(path, false);
}
/// <summary>
/// Creates a <see cref="BlobCollection" /> from the specified directory.
/// </summary>
/// <param name="path">A <see cref="string" /> specifying the path to a directory from which to create the <see cref="BlobCollection" />.</param>
/// <param name="recursive"><see langword="true" /> to search subdirectories recursively.</param>
/// <returns>
/// The <see cref="BlobCollection" /> this method creates.
/// </returns>
public static BlobCollection FromDirectory(string path, bool recursive)
{
Check.ArgumentNull(path);
Check.DirectoryNotFound(path);
return new
(
new DirectoryInfo(path)
.GetFiles("*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(file => Blob.FromFile(file.FullName))
);
}
/// <summary>
/// Computes the size, in bytes, of all <see cref="Blob" /> objects within this <see cref="BlobCollection" />.
/// </summary>
/// <returns>
/// The size, in bytes, of all <see cref="Blob" /> objects within this <see cref="BlobCollection" />.
/// </returns>
public long ComputeSize()
{
return Blobs.Sum(blob => blob.Content?.Length ?? 0);
}
/// <summary>
/// Writes the contents of all <see cref="Blob" /> objects to the specified directory, where <see cref="Blob.Name" /> is used as the filename and <see cref="Blob.Content" /> is written to the file. Existing files are overwritten.
/// </summary>
/// <param name="path">A <see cref="string" /> specifying the path to a directory to which this <see cref="BlobCollection" /> is written to.</param>
public void SaveToDirectory(string path)
{
Check.ArgumentNull(path);
Check.DirectoryNotFound(path);
if (Blobs.FirstOrDefault(blob => !Validate.FileName(blob.Name)) is Blob illegalBlob)
{
throw Blob.CreateIllegalFilenameException(illegalBlob);
}
foreach (Blob blob in Blobs)
{
File.WriteAllBytes(Path.Combine(path, blob.Name), blob.Content);
}
}
/// <summary>
/// Adds a <see cref="Blob" /> to the end of the <see cref="BlobCollection" />.
/// </summary>
/// <param name="item">The <see cref="Blob" /> to be added to the end of the <see cref="BlobCollection" />.</param>
public void Add(Blob item)
{
Check.ArgumentNull(item);
Blobs.Add(item);
}
/// <summary>
/// Removes the first occurrence of a specific <see cref="Blob" /> from the <see cref="BlobCollection" />.
/// </summary>
/// <param name="item">The <see cref="Blob" /> to remove from the <see cref="BlobCollection" />.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="item" /> is successfully removed;
/// otherwise, <see langword="false" />.
/// This method also returns <see langword="false" />, if <paramref name="item" /> was not found in the <see cref="BlobCollection" />.
/// </returns>
public bool Remove(Blob item)
{
return Blobs.Remove(item);
}
/// <summary>
/// Removes all elements from the <see cref="BlobCollection" />.
/// </summary>
public void Clear()
{
Blobs.Clear();
}
/// <summary>
/// Determines whether an element is in the <see cref="BlobCollection" />.
/// </summary>
/// <param name="item">The <see cref="Blob" /> to locate in the <see cref="BlobCollection" />.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="item" /> is found in the <see cref="BlobCollection" />;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Contains(Blob item)
{
return Blobs.Contains(item);
}
void ICollection<Blob>.CopyTo(Blob[] array, int arrayIndex)
{
Check.ArgumentNull(array);
Check.IndexOutOfRange(arrayIndex, array.Length - Count + 1);
Blobs.CopyTo(array, arrayIndex);
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="BlobCollection" />.
/// </summary>
/// <returns>
/// An enumerator that can be used to iterate through the <see cref="BlobCollection" />.
/// </returns>
public IEnumerator<Blob> GetEnumerator()
{
return Blobs.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Blobs.GetEnumerator();
}
}