wabbajack/Wabbajack.Lib/Downloaders/ManualDownloader.cs

116 lines
3.3 KiB
C#
Raw Normal View History

2019-10-12 22:15:32 +00:00
using System;
using System.Diagnostics;
using System.IO;
2019-10-12 22:15:32 +00:00
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
2019-10-12 22:15:32 +00:00
using Wabbajack.Common;
2020-01-07 13:50:11 +00:00
using Wabbajack.Common.IO;
using Wabbajack.Lib.Validation;
using File = System.IO.File;
2019-10-12 22:15:32 +00:00
namespace Wabbajack.Lib.Downloaders
2019-10-12 22:15:32 +00:00
{
public class ManualDownloader : IDownloader
2019-10-12 22:15:32 +00:00
{
private FileSystemWatcher _watcher;
private Subject<FileEvent> _fileEvents = new Subject<FileEvent>();
private KnownFolder _downloadfolder;
2019-12-22 00:26:51 +00:00
public readonly AsyncLock Lock = new AsyncLock();
class FileEvent
{
public string FullPath { get; set; }
public string Name { get; set; }
public long Size { get; set; }
}
public ManualDownloader()
{
_downloadfolder = new KnownFolder(KnownFolderType.Downloads);
_watcher = new FileSystemWatcher(_downloadfolder.Path);
_watcher.Created += _watcher_Created;
_watcher.Changed += _watcher_Changed;
}
private void _watcher_Changed(object sender, FileSystemEventArgs e)
{
PublishEvent(e);
}
private void _watcher_Created(object sender, FileSystemEventArgs e)
{
PublishEvent(e);
}
private void PublishEvent(FileSystemEventArgs e)
{
try
{
_fileEvents.OnNext(new FileEvent
{
Size = new FileInfo(e.FullPath).Length,
Name = e.Name,
FullPath = e.FullPath
});
}
catch (IOException)
{
}
}
2019-12-07 03:50:50 +00:00
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
2019-10-12 22:15:32 +00:00
{
2019-11-21 15:51:57 +00:00
var url = archiveINI?.General?.manualURL;
return url != null ? new State { Url = url} : null;
2019-10-12 22:15:32 +00:00
}
2019-12-07 02:45:13 +00:00
public async Task Prepare()
2019-10-12 22:15:32 +00:00
{
}
public class State : AbstractDownloadState
{
public string Url { get; set; }
public override object[] PrimaryKey { get => new object[] {Url}; }
2019-10-12 22:15:32 +00:00
public override bool IsWhitelisted(ServerWhitelist whitelist)
{
return true;
}
public override async Task<bool> Download(Archive a, string destination)
2019-10-12 22:15:32 +00:00
{
2020-02-11 00:30:38 +00:00
var (uri, client) = await Utils.Log(await ManuallyDownloadFile.Create(this)).Task;
var state = new HTTPDownloader.State {Url = uri.ToString(), Client = client};
return await state.Download(a, destination);
2019-10-12 22:15:32 +00:00
}
public override async Task<bool> Verify(Archive a)
2019-10-12 22:15:32 +00:00
{
return true;
}
public override IDownloader GetDownloader()
{
return DownloadDispatcher.GetInstance<ManualDownloader>();
}
public override string GetManifestURL(Archive a)
{
return Url;
}
public override string[] GetMetaIni()
{
return new [] {
"[General]",
$"manualURL={Url}"
};
}
2019-10-12 22:15:32 +00:00
}
}
}