-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMetadata.cs
174 lines (151 loc) · 5.76 KB
/
Metadata.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
// ReSharper disable ConvertToUsingDeclaration
namespace Hi3Helper.Http
{
internal class Metadata
{
internal const string MetadataExtension = ".collapseMeta";
public Uri? Url { get; set; }
public string? MetadataFilePath { get; set; }
public string? OutputFilePath { get; set; }
public List<ChunkRange?>? Ranges { get; set; }
public long TargetToCompleteSize { get; set; }
public bool IsCompleted { get; set; }
public long LastEndOffset { get; set; }
[JsonIgnore] private bool IsOnWrite { get; set; }
internal event EventHandler<bool>? UpdateChunkRangesCountEvent;
internal static async ValueTask<Metadata?> ReadLastMetadataAsync(Uri? url, FileInfo outputFilePath,
FileInfo metadataFilePath, long targetToDownloadSize, CancellationToken token)
{
try
{
if (!metadataFilePath.Exists)
{
if (outputFilePath.Exists && outputFilePath.Length == targetToDownloadSize)
{
return null;
}
throw new InvalidDataException();
}
await using (FileStream metadataStream = metadataFilePath.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
if (metadataStream.Length < 64)
{
throw new InvalidDataException();
}
Metadata? lastMetadata =
await JsonSerializer.DeserializeAsync(metadataStream, MetadataJsonContext.Default.Metadata, token);
if (lastMetadata == null
|| lastMetadata.Url == null
|| lastMetadata.Ranges?.Count == 0
|| string.IsNullOrEmpty(lastMetadata.OutputFilePath))
{
throw new InvalidDataException();
}
lastMetadata.MetadataFilePath = metadataFilePath.FullName;
lastMetadata.Ranges?.Sort((x, y) =>
{
// If the source is null, then return -1 (less than)
if (x == null || y == null)
{
return -1;
}
// Compare based on Start
int startComparison = x.Start.CompareTo(y.Start);
return startComparison != 0
? startComparison
:
// If Start is equal, compare based on End
x.End.CompareTo(y.End);
});
lastMetadata.Ranges?.RemoveAll(x => x == null);
return lastMetadata;
}
}
catch (Exception)
{
// Invalid state, then return an empty metadata file
return new Metadata
{
Url = url,
OutputFilePath = outputFilePath.FullName,
TargetToCompleteSize = targetToDownloadSize,
IsCompleted = false,
Ranges = new List<ChunkRange?>(),
MetadataFilePath = metadataFilePath.FullName
};
}
}
internal async Task SaveLastMetadataStateAsync(CancellationToken token)
{
if (string.IsNullOrEmpty(MetadataFilePath) || string.IsNullOrWhiteSpace(MetadataFilePath))
{
throw new NullReferenceException("MetadataFilePath property is null or empty!");
}
if (IsOnWrite)
{
await Task.Delay(200, token);
}
IsOnWrite = true;
FileStream? fileStream = null;
try
{
fileStream = new FileStream(MetadataFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
await JsonSerializer.SerializeAsync(fileStream, this, MetadataJsonContext.Default.Metadata, token);
}
finally
{
IsOnWrite = false;
if (fileStream != null)
{
await fileStream.DisposeAsync();
}
}
}
internal static void DeleteMetadataFile(string outputFilePath)
{
string metadataFilePath = outputFilePath + MetadataExtension;
FileInfo fileInfo = new FileInfo(metadataFilePath);
if (!fileInfo.Exists)
{
return;
}
fileInfo.IsReadOnly = false;
fileInfo.Delete();
}
internal bool PopRange(ChunkRange? range)
{
if (range == null)
{
return false;
}
bool isRemoved = Ranges?.Remove(range) ?? false;
UpdateChunkRangesCountEvent?.Invoke(null, isRemoved);
return isRemoved;
}
internal bool PushRange(ChunkRange? range)
{
if (range == null)
{
return false;
}
if (Ranges?.Contains(range) ?? false)
{
return false;
}
Ranges?.Add(range);
UpdateChunkRangesCountEvent?.Invoke(null, true);
return true;
}
internal void UpdateLastEndOffset(ChunkRange range)
{
LastEndOffset = Math.Max(range.End, LastEndOffset);
}
}
}