private string GetCurrentStatus(BeatmapDownloadTaskProgressUpdated _) { if (DownloadFlag) { return("Downloading..."); } else { return("Idle"); } }
public async ValueTask Handle(BeatmapDownloadAddressPrepared @event) { var options = OptionFactory.Create(); var downloadProvider = Scope.ResolveNamed <IDownloadProvier>(options.DownloadProvider.Id); // create download task in db var entity = await CreateTask(@event, options.DownloadProvider); var eventTask = entity.Entity.Cast <DownloadBeatmapSetTask>(); entity.Entity.Status = DownloadStatus.NOT_STARTED; // start event EventBus.Publish(new BeatmapDownloadTaskStarted() { Task = eventTask, }); Logger.LogInformation($"Start download #{@event.BeatmapSetId} from {@event.DownloadUrl}"); var targetFile = Path.Combine(OsuGamePath, "Downloads", $"{@event.BeatmapSetId}.osz"); entity.Entity.FullPath = eventTask.FullPath = targetFile; long prevDownloadedBytes = 0; // download notify events var progressEvent = new BeatmapDownloadTaskProgressUpdated() { Task = eventTask, }; void handler(int percentage, long current, long total) { if (current - prevDownloadedBytes < 10240) { return; } prevDownloadedBytes = current; progressEvent.BytesTotal = total; progressEvent.BytesDownloaded = current; progressEvent.PercentCompleted = percentage; // download progress event EventBus.Publish(progressEvent); } downloadProvider.DownloadProgressChanged += handler; // start download entity.Entity.StartedAt = DateTime.Now; entity.Entity.Status = DownloadStatus.DOWNLOADING; var suggestFileName = await downloadProvider.Download(targetFile, @event.DownloadUrl); entity.Entity.CompletedAt = DateTime.Now; entity.Entity.Status = DownloadStatus.COMPLETED; Logger.LogInformation($"Beatmap #{@event.BeatmapSetId} downloaded"); // adjust downloaded file name by response var suggestFilePath = Path.Combine(OsuGamePath, "Downloads", suggestFileName); if (suggestFileName != null && suggestFileName.Length > 0 && suggestFileName.EndsWith(".osz")) { File.Move(targetFile, suggestFilePath); targetFile = suggestFilePath; } entity.Entity.FullPath = eventTask.FullPath = targetFile; DownloadDb.Update(entity.Entity); await DownloadDb.SaveChangesAsync(); await OsuDb.OsuDatabaseBeatmap.AddAsync(new OsuDatabaseBeatmap() { OsuDatabaseId = (await OsuDb.OsuDatabases.FirstAsync()).Id, BeatmapId = @event.BeatmapId, BeatmapSetId = @event.BeatmapSetId, }); await OsuDb.SaveChangesAsync(); // download completed event EventBus.Publish(new BeatmapDownloadTaskCompleted() { Task = eventTask, }); if (options.AutoOpenDownloadedBeatmap) { try { Logger.LogInformation($"Trying open beatmap {targetFile}"); Process.Start(new ProcessStartInfo(targetFile) { UseShellExecute = true }); } catch { Logger.LogWarning($"[Auto-Import] Failed to open .osz file, the file association not working."); } } }