2019-01-24 16:43:03 +08: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 17:19:50 +08:00
|
|
|
|
|
2019-01-29 17:34:10 +08:00
|
|
|
|
using System.IO;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.IO.Network;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
|
{
|
|
|
|
|
public abstract class APIDownloadRequest : APIRequest
|
|
|
|
|
{
|
2019-01-29 17:34:10 +08:00
|
|
|
|
private string filename;
|
|
|
|
|
|
2019-06-13 18:46:48 +08:00
|
|
|
|
/// <summary>
|
2019-06-13 19:25:41 +08:00
|
|
|
|
/// Used to set the extension of the file returned by this request.
|
2019-06-13 18:46:48 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual string FileExtension { get; } = @".tmp";
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
protected override WebRequest CreateWebRequest()
|
|
|
|
|
{
|
2019-06-13 18:46:48 +08:00
|
|
|
|
var file = Path.GetTempFileName();
|
|
|
|
|
|
|
|
|
|
File.Move(file, filename = Path.ChangeExtension(file, FileExtension));
|
|
|
|
|
|
|
|
|
|
var request = new FileWebRequest(filename, Uri);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
request.DownloadProgress += request_Progress;
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 18:08:45 +08:00
|
|
|
|
private void request_Progress(long current, long total) => API.Schedule(() => Progressed?.Invoke(current, total));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
protected APIDownloadRequest()
|
|
|
|
|
{
|
|
|
|
|
base.Success += onSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onSuccess()
|
|
|
|
|
{
|
2019-01-29 17:34:10 +08:00
|
|
|
|
Success?.Invoke(filename);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 18:08:45 +08:00
|
|
|
|
public event APIProgressHandler Progressed;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-01-29 17:34:10 +08:00
|
|
|
|
public new event APISuccessHandler<string> Success;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|