public async Task <BaseApiResult <string> > GetDraftDir() { string lastDraftPath = AppManager.RunningData.LastDraftPath; if (!string.IsNullOrEmpty(lastDraftPath)) { string projectPath = Path.Combine(lastDraftPath, "project.json"); if (!File.Exists(projectPath) && Directory.Exists(lastDraftPath)) { await Task.Run(() => { var files = Directory.GetFiles(lastDraftPath); foreach (var file in files) { try { //删除老文件 File.Delete(file); } catch (Exception ex) { Debug.WriteLine(ex); continue; } } }); //返回上一次的临时目录,防止创建太多无用目录 return(BaseApiResult <string> .SuccessState(lastDraftPath)); } } var r = WallpaperApi.GetDraftDir(AppManager.UserSetting.Wallpaper.WallpaperSaveDir); AppManager.RunningData.LastDraftPath = r; await AppManager.SaveRunningData(AppManager.RunningData); return(BaseApiResult <string> .SuccessState(r)); }
//下载壁纸 public Task <BaseApiResult> DownloadWallpaper(string wallpaper, string cover, WallpaperProjectInfo info) { if (wallpaper == null) { return(Task.FromResult(BaseApiResult.ErrorState(ErrorType.Failed))); } string destFolder = null; if (info != null && info.ID != null) { destFolder = Path.Combine(AppManager.UserSetting.Wallpaper.WallpaperSaveDir, info.ID); } else { destFolder = WallpaperApi.GetDraftDir(AppManager.UserSetting.Wallpaper.WallpaperSaveDir); } if (info == null) { info = new WallpaperProjectInfo(); } CancellationTokenSource cts = new(); _ = Task.Run(async() => { RaiseLimiter _raiseLimiter = new(); var wpProgressInfo = new Progress <(float competed, float total)>((e) => { _raiseLimiter.Execute(async() => { var client = _hubEventEmitter.AllClient(); await client.SendAsync("DownloadWallpaperProgressChanged", new { path = wallpaper, e.competed, e.total, percent = e.competed / e.total * 90, completed = false }); }, 1000); }); var coverProgressInfo = new Progress <(float competed, float total)>((e) => { _raiseLimiter.Execute(async() => { var client = _hubEventEmitter.AllClient(); var percent = e.competed / e.total * 10 + 90; await client.SendAsync("DownloadWallpaperProgressChanged", new { path = cover, e.competed, e.total, percent, completed = percent == 100 }); }, 1000); }); info.File = Path.GetFileName(wallpaper); string destWp = Path.Combine(destFolder, info.File); await NetworkHelper.DownloadFileAsync(wallpaper, destWp, cts.Token, wpProgressInfo); if (cover != null) { info.Preview = Path.GetFileName(cover); string destCover = Path.Combine(destFolder, info.Preview); await NetworkHelper.DownloadFileAsync(cover, destCover, cts.Token, coverProgressInfo); } //生成json await UpdateProjectInfo(destFolder, info); }); return(Task.FromResult(BaseApiResult.SuccessState())); }