private void RunPlotQueue() { FileRepository repo = new FileRepository(); FileTask task = null; while ((task = repo.GetTask("")) != null) { if (task.ExtName.Equals("dwg", StringComparison.OrdinalIgnoreCase)) { try { label2.Text = string.Format("正在获取文件...[{0}]", task.Name); //1.从filestore获取文件 byte[] bytes = FileStoreHelper.GetFile(task.ID); FileStoreHelper.SaveFileBuffer(bytes, Path.Combine(OfficeHelper.GetFolderPath(task.ID, "Files"), task.Name)); //2.设置文件数据库状态为进行中 repo.StartTask(task.ID); //3.开始进行转换 label2.Text = string.Format("正在进行格式打印...[{0}]", task.Name); var imgDTO = OfficeHelper.InitDTO(task.Name, bytes.Length, task.ID); var result = FileConverter.Exec(imgDTO); bool isSucc = false; if (result != null && result.status) { //4.设置转图层次,并生成json文件 imgDTO.Versions[0].ImageZoomLevel = result.ZoomLevel; OfficeHelper.WriteJsonFile(imgDTO); isSucc = true; } Application.DoEvents(); //5.回置状态 label2.Text = "最近:[" + imgDTO.Name + "]打印" + (isSucc ? "成功":"失败") + ",打印时间(" + DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分") + ")"; repo.EndTask(task.ID, isSucc ? ResultStatus.Success:ResultStatus.Error); //6.删除原文件 if (File.Exists(imgDTO.Versions[0].FullPath)) { File.Delete(imgDTO.Versions[0].FullPath); } } catch (Exception ex) { // 记录日志 LogWriter.Info(string.Format(ex.Message, DateTime.Now.ToString(), task.ID, task.Name, ex.StackTrace)); repo.EndTask(task.ID, ResultStatus.Error); } } } }
public void Execute(IJobExecutionContext context) { var taskId = ""; var jobDataTaskId = context.Trigger.JobDataMap["taskId"]; if (jobDataTaskId != null) { taskId = jobDataTaskId.ToString(); } FileRepository repo = new FileRepository(); FileTask task = null; while ((task = repo.GetTask(taskId)) != null) { // 修改开始执行时间 repo.StartTask(task.ID); try { if (!IsConvertFormat(task.ExtName)) { throw new Exception("未知文件类型,没有相关的转换器!"); } string viewMode = AppConfig.GetAppSettings("ViewMode"); if (viewMode != ViewModeDTO.TilePicViewer.ToString()) { string folderPath = OfficeHelper.GetFolderPath(task.ID); var pdfFilePath = Path.Combine(folderPath, task.ID + ".pdf"); var swfFilePath = Path.Combine(folderPath, task.ID + ".swf"); var snapFilePath = Path.Combine(folderPath, task.ID + ".png"); #region 1.转换PDF byte[] pdfBuffer = null; if (!string.IsNullOrEmpty(task.ExtName)) { if (task.ExtName.ToLower() == "pdf") { pdfBuffer = FileStoreHelper.GetFile(task.Name); } else if (task.ExtName.ToLower() == "docx" || task.ExtName.ToLower() == "doc") { // 获取文件 var butter = FileStoreHelper.GetFile(task.Name); pdfBuffer = FileConverter.Word2PDF(butter, pdfFilePath, task.ExtName.ToLower()); } else if (task.ExtName.ToLower() == "xlsx" || task.ExtName.ToLower() == "xls") { // 获取文件 var buffer = FileStoreHelper.GetFile(task.Name); pdfBuffer = FileConverter.Excel2PDF(buffer); } else if (task.ExtName.ToLower() == "txt") { // 获取文件 var buffer = FileStoreHelper.GetFile(task.Name); pdfBuffer = FileConverter.Txt2PDF(buffer); } else if (task.ExtName.ToLower() == "jpg" || task.ExtName.ToLower() == "jpeg" || task.ExtName.ToLower() == "png") { // 获取文件 var buffer = FileStoreHelper.GetFile(task.Name); pdfBuffer = FileConverter.Img2PDF(buffer); } else if (task.ExtName.ToLower() == "tif" || task.ExtName.ToLower() == "tiff") { // 获取文件 var buffer = FileStoreHelper.GetFile(task.Name); pdfBuffer = FileConverter.Tif2PDF(buffer); } else { throw new Exception("未知文件类型,没有相关的转换器!"); } } else { throw new Exception("未定义文件类型!"); } if (pdfBuffer == null) { throw new Exception("PDF文件不存在!"); } #endregion #region 2.生成SWF var pdfPageCount = FileConverter.GetPageCount(pdfBuffer); if (!File.Exists(pdfFilePath)) { FileConverter.SaveFileBuffer(pdfBuffer, pdfFilePath); FileConverter.PDF2SWF(pdfFilePath, swfFilePath, pdfPageCount); } #endregion #region 3.生成缩略图 byte[] snapBuffer = FileConverter.ConvertToSnap(pdfBuffer, "pdf"); FileConverter.SaveFileBuffer(snapBuffer, snapFilePath); #endregion repo.EndTask(task.ID, ResultStatus.Success); } else { //1.获取文件 byte[] bytes = FileStoreHelper.GetFile(task.ID); string filePath = OfficeHelper.GetFolderPath(task.ID, "Files"); FileStoreHelper.SaveFileBuffer(bytes, Path.Combine(filePath, task.Name)); //2.转图 var imgDTO = OfficeHelper.InitDTO(task.Name, bytes.Length, task.ID); FileConverter fileConverter = new FileConverter(); var result = fileConverter.Exec(imgDTO); if (result == null || !result.status) { throw new Exception("转图失败!"); } //3.生成记录文件 imgDTO.Versions[0].ImageZoomLevel = 18; imgDTO.Versions[0].HighHeightUnit = result.HighHeightUnit; OfficeHelper.WriteJsonFile(imgDTO); //4.设置数据库完成 repo.EndTask(task.ID, ResultStatus.Success); //5.删除原始文件 if (File.Exists(imgDTO.Versions[0].FullPath)) { File.Delete(imgDTO.Versions[0].FullPath); } } } catch (Exception ex) { // 记录日志 repo.EndTask(task.ID, ResultStatus.Error); LogWriter.Info(string.Format(ErrInfo, DateTime.Now.ToString(), task.ID, task.Name, ex.Message, ex.StackTrace)); } } //新增处理关联word、excel进程关闭问题 2019-5-29 CloseExit(); }