public override async Task Run(IJobExecutionContext context) { logger.Info("开始处理图片素材事务..."); await Console.Out.WriteLineAsync("开始处理图片素材事务..."); var count = 0; IImageCompressor imageCompressor = _provider.GetService <IImageCompressor>(); IMaterialCenterConfigService _configService = _provider.GetService <IMaterialCenterConfigService>(); MaterialCenterConfig config = await _configService.GetAsync(); //var folder = Path.Combine(config.PictureSavePath, oppositePath); //if (config != null && !string.IsNullOrEmpty(config.PictureSavePath)) //{ // folder = Path.Combine(config.PictureSavePath, oppositePath); //} //取得待压缩列表 using (var dapper = DapperFactory.Create()) { var builder = new QueryBuilder().Select <Picture>().From <Picture>() .Where(new ConditionBuilder().Equal <Picture>(m => m.IsCompressed, false) .And().Equal <Picture>(m => m.IsDeleted, false)); var items = await dapper.QueryListAsync <Picture>(builder); foreach (var item in items) { //判断文件是否存在,如果不存在则自动删除该条数据 if (!File.Exists(Path.Combine(config.PictureSavePath, item.FilePath))) { await dapper.DeleteAsync <Picture>(new { item.Id }); continue; } if ("png".EqualOrdinalIgnoreCase(item.FileType) || "jpg".EqualOrdinalIgnoreCase(item.FileType) || "jpeg".EqualOrdinalIgnoreCase(item.FileType)) { var fileName = item.Id + "." + item.FileType; var filePath = item.Folder + fileName; try { //压缩图片 await imageCompressor.Compress(Path.Combine(config.PictureSavePath, item.FilePath), Path.Combine(config.PictureSavePath, filePath)); //缩略图 var thumbPath = item.Folder + item.Id + "_thumb." + item.FileType; await imageCompressor.Resize(Path.Combine(config.PictureSavePath, item.FilePath), Path.Combine(config.PictureSavePath, thumbPath), new { method = "fit", width = 320, height = 320 }); var fileInfo = new FileInfo(Path.Combine(config.PictureSavePath, item.FilePath)); var model = new { FilePath = filePath, FileName = fileName, ThumbPath = thumbPath, IsCompressed = true, FileSize = fileInfo.Length }; await dapper.UpdateAsync <Picture>(model, new { item.Id }); } catch (Exception ex) { logger.Error(ex); await Console.Out.WriteLineAsync(ex.ToString()); } } else { await dapper.UpdateAsync <Picture>(new { IsCompressed = true, ThumbPath = item.FilePath }, new { item.Id }); } count++; } } logger.Info($"共处理{count}张照片"); await Console.Out.WriteLineAsync($"共处理{count}张照片"); logger.Info($"结束处理图片素材事务..."); await Console.Out.WriteLineAsync("结束处理图片素材事务..."); }
public override async Task Run(IJobExecutionContext context) { logger.Info("开始处理视频素材事务..."); await Console.Out.WriteLineAsync("开始处理视频素材事务..."); var count = 0; IImageCompressor imageCompressor = _provider.GetService <IImageCompressor>(); IMaterialCenterConfigService _configService = _provider.GetService <IMaterialCenterConfigService>(); MaterialCenterConfig config = await _configService.GetAsync(); //var folder = Path.Combine(config.PictureSavePath, oppositePath); //if (config != null && !string.IsNullOrEmpty(config.PictureSavePath)) //{ // folder = Path.Combine(config.PictureSavePath, oppositePath); //} //取得待压缩列表 using (var dapper = DapperFactory.Create()) { var builder = new QueryBuilder().Select <Video>().From <Video>() .Where(new ConditionBuilder().IsNullOrEmpty <Video>(x => x.ThumbPath) .And().Equal <Video>(m => m.IsDeleted, false)); var items = await dapper.QueryListAsync <Video>(builder); foreach (var item in items) { //判断文件是否存在 if (!File.Exists(Path.Combine(config.PictureSavePath, item.FilePath))) { continue; } try { //缩略图 var thumbPath = item.Folder + item.Id + "_thumb.jpg"; var thumb = VideoHelper.GetPreviewImage(Path.Combine(config.PictureSavePath, item.FilePath), Path.Combine(config.PictureSavePath, thumbPath), 320, 180, 1); if (thumb != null) { thumb = thumbPath; } //时长 var duration = VideoHelper.GetVideoDuration(Path.Combine(config.PictureSavePath, item.FilePath)); await dapper.UpdateAsync <Video>(new { ThumbPath = thumb, Duration = duration }, new { item.Id }); } catch (Exception ex) { logger.Error(ex); await Console.Out.WriteLineAsync(ex.ToString()); } count++; } } logger.Info($"共处理{count}个视频"); await Console.Out.WriteLineAsync($"共处理{count}个视频"); logger.Info($"结束处理视频素材事务..."); await Console.Out.WriteLineAsync("结束处理视频素材事务..."); }
public async Task SetAsync(MaterialCenterConfig config) { var key = string.Format(CacheKeyDefinition.Config, nameof(MaterialCenterConfig).ToLower()); await _cache.SetAsync(key, config); }
public async Task OnGetAsync() { Dto = await _service.GetAsync(); }