2019-01-24 17:43:03 +09:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2021-01-16 23:07:46 +03:00
|
|
|
|
using System;
|
2019-01-29 18:34:10 +09:00
|
|
|
|
using System.IO;
|
2018-02-15 16:33:33 +09:00
|
|
|
|
using osu.Framework.IO.Network;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-02-14 20:26:49 +09:00
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
|
{
|
|
|
|
|
public abstract class APIDownloadRequest : APIRequest
|
|
|
|
|
{
|
2019-01-29 18:34:10 +09:00
|
|
|
|
private string filename;
|
|
|
|
|
|
2019-06-13 16:16:48 +05:30
|
|
|
|
/// <summary>
|
2019-06-13 16:55:41 +05:30
|
|
|
|
/// Used to set the extension of the file returned by this request.
|
2019-06-13 16:16:48 +05:30
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual string FileExtension { get; } = @".tmp";
|
|
|
|
|
|
2021-07-22 14:23:24 +09:00
|
|
|
|
protected APIDownloadRequest()
|
|
|
|
|
{
|
|
|
|
|
base.Success += () => Success?.Invoke(filename);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 20:26:49 +09:00
|
|
|
|
protected override WebRequest CreateWebRequest()
|
|
|
|
|
{
|
2021-10-27 13:04:41 +09:00
|
|
|
|
string file = Path.GetTempFileName();
|
2019-06-13 16:16:48 +05:30
|
|
|
|
|
|
|
|
|
File.Move(file, filename = Path.ChangeExtension(file, FileExtension));
|
|
|
|
|
|
|
|
|
|
var request = new FileWebRequest(filename, Uri);
|
2018-02-14 20:26:49 +09:00
|
|
|
|
request.DownloadProgress += request_Progress;
|
|
|
|
|
return request;
|
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-01-31 19:08:45 +09:00
|
|
|
|
private void request_Progress(long current, long total) => API.Schedule(() => Progressed?.Invoke(current, total));
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2021-01-17 22:08:28 +03:00
|
|
|
|
protected void TriggerSuccess(string filename)
|
2018-02-14 20:26:49 +09:00
|
|
|
|
{
|
2021-01-16 23:07:46 +03:00
|
|
|
|
if (this.filename != null)
|
|
|
|
|
throw new InvalidOperationException("Attempted to trigger success more than once");
|
|
|
|
|
|
|
|
|
|
this.filename = filename;
|
|
|
|
|
|
|
|
|
|
TriggerSuccess();
|
2018-02-14 20:26:49 +09:00
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-01-31 19:08:45 +09:00
|
|
|
|
public event APIProgressHandler Progressed;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-01-29 18:34:10 +09:00
|
|
|
|
public new event APISuccessHandler<string> Success;
|
2018-02-14 20:26:49 +09:00
|
|
|
|
}
|
2018-02-15 16:33:33 +09:00
|
|
|
|
}
|