internal static void DownloadRestart(DownloadWrapper download) { var temp_dl_path = download.DownloadInfo.DownloadFullPath + ".dl"; File.Delete(temp_dl_path); download.Status = DownloadTaskStatus.Paused; DownloadStart(download); }
public static bool CheckIfContained(DownloadWrapper download) { //check if (DownloadList.FirstOrDefault(x => x.DownloadInfo.CheckIfSame(download.DownloadInfo) && download.DownloadInfo.DownloadId < 0 && (x.DownloadInfo.DownloadId != download.DownloadInfo.DownloadId)) is DownloadWrapper _) { return(true); } return(false); }
private void Populate(IEnumerable <Patch> patches) { var enumerable = patches as IList <Patch> ?? patches.ToList(); foreach (var patch in enumerable.Where(p => p.FileDownloadInfo.FileInfoType != FileInfoType.Directory)) { var downloadWrapper = new DownloadWrapper(patch, _patcherContext); _downloadWrappers.Add(downloadWrapper.Patch.FileDownloadInfo.FileName, downloadWrapper); foreach (var filePartInfo in patch.FileDownloadInfo.FileParts) { _fileParts.Enqueue(filePartInfo); } } }
public static void DownloadPause(DownloadWrapper download) { if (download.Status == DownloadTaskStatus.Paused || download.Status == DownloadTaskStatus.Finished) { Log.Info($"Download task {download.DownloadInfo.FileName} has already been paused/finished."); return; } download.Status = DownloadTaskStatus.Paused; download.CancelTokenSource?.Cancel(); if (FileStreamHolder.TryGetValue(download, out var stream)) { stream.Dispose(); } Log.Info($"Paused downloading task :{download.DownloadInfo.FileName}"); }
internal static void DownloadDelete(DownloadWrapper download) { DownloadPause(download); var temp_dl_path = download.DownloadInfo.DownloadFullPath + ".dl"; File.Delete(temp_dl_path); DownloadList.Remove(download); var db = LocalDBContext.Instance; if (download.IsSaveInDB && db.Downloads.Remove(download.DownloadInfo) is Download) { Log.Info("Deleted entity record in DB"); db.SaveChanges(); } Log.Info($"Deleted download task :{download.DownloadInfo.FileName}"); }
public static void DownloadStart(DownloadWrapper download) { if (download.Status == DownloadTaskStatus.Started || download.Status == DownloadTaskStatus.Finished) { Log.Info($"Download task {download.DownloadInfo.FileName} has already been started/finished."); return; } //check if (string.IsNullOrWhiteSpace(download.DownloadInfo.DownloadFullPath)) { var message = "此任务出现格式错误,没钦定下载路径"; download.ErrorMessage = message; DownloadPause(download); throw new Exception(message); } if (!DownloadList.Contains(download)) { DownloadList.Add(download); } download.ErrorMessage = string.Empty; var cancel_token_source = new CancellationTokenSource(); download.CancelTokenSource = cancel_token_source; var task = Task.Run(() => OnDownloadTaskStart(download), cancel_token_source.Token); download.Status = DownloadTaskStatus.Started; lock (RunningDownloadTask) { RunningDownloadTask.Add(download); } Log.Info($"Started downloading task :{download.DownloadInfo.FileName}"); }
private static void OnDownloadTaskStart(DownloadWrapper download) { FileStream file_stream = null; try { var temp_dl_path = download.DownloadInfo.DownloadFullPath + ".dl"; Directory.CreateDirectory(Path.GetDirectoryName(download.DownloadInfo.DownloadFullPath)); file_stream = FileStreamHolder[download] = File.OpenWrite(temp_dl_path); download.CurrentDownloadedLength = file_stream.Length; file_stream.Seek(download.CurrentDownloadedLength, SeekOrigin.Begin); WebResponse response = null; try { response = RequestHelper.CreateDeafult(download.DownloadInfo.DownloadUrl, request => { if (download.CurrentDownloadedLength > 0) { request.AddRange(download.CurrentDownloadedLength); } }); } catch (Exception e) when(e.Message.Contains("416")) { Log.Error("Redownload file because of HttpCode416..."); file_stream?.Dispose(); File.Delete(temp_dl_path); file_stream = FileStreamHolder[download] = File.OpenWrite(temp_dl_path); download.CurrentDownloadedLength = file_stream.Length; file_stream.Seek(download.CurrentDownloadedLength, SeekOrigin.Begin); response = RequestHelper.CreateDeafult(download.DownloadInfo.DownloadUrl); } using var response_stream = response.GetResponseStream(); var buffer = new byte[1024]; int read_bytes = 0; do { read_bytes = response_stream.Read(buffer, 0, buffer.Length); if (download.Status != DownloadTaskStatus.Started || !file_stream.CanWrite) { Log.Debug($"Notice that download task {download.DownloadInfo.FileName} still continue to write when task status is not started."); return; } file_stream.Write(buffer, 0, read_bytes); file_stream.Flush(); download.CurrentDownloadedLength += read_bytes; } while (read_bytes != 0); //downloading finished file_stream.Dispose(); if (File.Exists(download.DownloadInfo.DownloadFullPath)) { File.Delete(download.DownloadInfo.DownloadFullPath); } File.Move(temp_dl_path, download.DownloadInfo.DownloadFullPath); download.Status = DownloadTaskStatus.Finished; Log.Info($"Downloading task {download.DownloadInfo.FileName} now finished."); } catch (Exception e) { Log.Error(e.Message); ExceptionHelper.DebugThrow(e); //error ocured download.ErrorMessage = e.Message; DownloadPause(download); Toast.ShowMessage($"无法开始下载此图片:{e.Message}"); } finally { file_stream?.Dispose(); } }
private void ShowRedownloadConfimPanel(Button sender, DownloadWrapper download_task) { var panel = (sender.Parent as FrameworkElement).FindName("RedownloadPanel") as Popup; panel.IsOpen = true; }