Hash for checking upload/download, waiting screen

This commit is contained in:
Aurélie Delhaie
2022-06-16 22:44:53 +02:00
parent 24a04e8c02
commit 2a9fd72e14
17 changed files with 691 additions and 80 deletions

View File

@@ -79,5 +79,16 @@ namespace OpenSaveCloudClient.Core
return sha.ComputeHash(inputStream);
}
public static string? HashFile(string path)
{
FileInfo fileInfo = new(path);
byte[]? hash = HashFile(fileInfo);
if (hash == null)
{
throw new InvalidOperationException("HashFile: file to get hash");
}
return BitConverter.ToString(hash).Replace("-", "");
}
}
}

View File

@@ -424,6 +424,13 @@ namespace OpenSaveCloudClient.Core
FileStream stream = File.OpenRead(filePath);
try
{
string? hash = HashTool.HashFile(filePath);
if (hash == null)
{
logManager.AddError("Failed to get hash of archive");
taskManager.UpdateTaskStatus(uuidTask, AsyncTaskStatus.Failed);
return false;
}
MultipartFormDataContent multipartFormContent = new();
var fileStreamContent = new StreamContent(stream);
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
@@ -433,6 +440,7 @@ namespace OpenSaveCloudClient.Core
client.DefaultRequestHeaders.Add("Authorization", "bearer " + token);
client.DefaultRequestHeaders.Add("X-Upload-Key", uploadToken);
client.DefaultRequestHeaders.Add("X-Game-Save-Hash", newHash);
client.DefaultRequestHeaders.Add("X-Hash", hash);
HttpResponseMessage response = client.PostAsync(string.Format("{0}:{1}/api/v1/game/upload", host, port), multipartFormContent).Result;
if (response.IsSuccessStatusCode)
{
@@ -479,13 +487,34 @@ namespace OpenSaveCloudClient.Core
{
await response.Content.CopyToAsync(fs);
}
if (Directory.Exists(unzipPath))
if (response.Headers.TryGetValues("X-Hash", out IEnumerable<string>? hashValue))
{
Directory.Delete(unzipPath, true);
if (hashValue != null)
{
string hash = hashValue.First();
string? localHash = HashTool.HashFile(filePath);
if (localHash != null)
{
if (hash != localHash)
{
logManager.AddError("The hash does not match");
taskManager.UpdateTaskStatus(uuidTask, AsyncTaskStatus.Failed);
return false;
}
if (Directory.Exists(unzipPath))
{
Directory.Delete(unzipPath, true);
}
ZipFile.ExtractToDirectory(filePath, unzipPath);
taskManager.UpdateTaskStatus(uuidTask, AsyncTaskStatus.Ended);
return true;
}
}
}
ZipFile.ExtractToDirectory(filePath, unzipPath);
taskManager.UpdateTaskStatus(uuidTask, AsyncTaskStatus.Ended);
return true;
logManager.AddError("Server does not send the hash of the archive");
taskManager.UpdateTaskStatus(uuidTask, AsyncTaskStatus.Failed);
return false;
}
else
{

View File

@@ -11,6 +11,7 @@ namespace OpenSaveCloudClient.Core
{
private static TaskManager instance;
private static LogManager logManager;
private readonly System.Timers.Timer timer;
private readonly Dictionary<string, AsyncTaskInformation> _tasks;
@@ -20,6 +21,7 @@ namespace OpenSaveCloudClient.Core
private TaskManager()
{
logManager = LogManager.GetInstance();
_tasks = new Dictionary<string, AsyncTaskInformation>();
mut = new Mutex();
timer = new System.Timers.Timer
@@ -41,19 +43,34 @@ namespace OpenSaveCloudClient.Core
public string StartTask(string label, bool undefined, int progressMax)
{
string uuid = Guid.NewGuid().ToString();
AsyncTaskInformation ati = new(uuid, label, undefined, progressMax);
_tasks.Add(uuid, ati);
TaskChangedEventArgs args = new()
mut.WaitOne();
try
{
TaskInformation = ati
};
OnTaskChanged(args);
return uuid;
string uuid = Guid.NewGuid().ToString();
AsyncTaskInformation ati = new(uuid, label, undefined, progressMax);
_tasks.Add(uuid, ati);
TaskChangedEventArgs args = new()
{
TaskInformation = ati
};
OnTaskChanged(args);
return uuid;
}
catch (Exception ex)
{
logManager.AddError(ex);
throw;
}
finally
{
mut.ReleaseMutex();
}
}
public void UpdateTaskProgress(string uuid, int progress)
{
mut.WaitOne();
try
{
AsyncTaskInformation task = _tasks[uuid];
@@ -65,14 +82,19 @@ namespace OpenSaveCloudClient.Core
};
OnTaskChanged(args);
}
catch(Exception)
catch(Exception ex)
{
logManager.AddError(ex);
}
finally
{
mut.ReleaseMutex();
}
}
public void UpdateTaskStatus(string uuid, AsyncTaskStatus status)
{
mut.WaitOne();
try
{
AsyncTaskInformation task = _tasks[uuid];
@@ -88,9 +110,13 @@ namespace OpenSaveCloudClient.Core
};
OnTaskChanged(args);
}
catch (Exception)
catch (Exception ex)
{
logManager.AddError(ex);
}
finally
{
mut.ReleaseMutex();
}
}
@@ -99,22 +125,8 @@ namespace OpenSaveCloudClient.Core
mut.WaitOne();
try
{
List<string> toDelete = new();
foreach (KeyValuePair<string, AsyncTaskInformation> task in _tasks)
{
if (task.Value.Status == AsyncTaskStatus.Ended)
{
toDelete.Add(task.Key);
}
}
foreach (string uuid in toDelete)
{
_tasks.Remove(uuid);
}
if (toDelete.Count > 0)
{
OnTaskCleared(new TaskClearedEventArgs());
}
_tasks.Clear();
OnTaskCleared(new TaskClearedEventArgs());
}
finally
{