public Tuple <byte[], string> CreateSearchablePdf(byte[] fileData, PdfMeta metaData) { try { int PageCountStart = GetPages(fileData); string sessionName = TempData.Instance.CreateNewSession(); OnCompressorEvent?.Invoke("Created Session:" + sessionName); string inputDataFilePath = TempData.Instance.CreateTempFile(sessionName, ".pdf"); string outputDataFilePath = TempData.Instance.CreateTempFile(sessionName, ".pdf"); if (fileData == null || fileData.Length == 0) { throw new Exception("No Data in fileData"); } using (FileStream writer = new FileStream(inputDataFilePath, FileMode.Create, FileAccess.Write)) { writer.Write(fileData, 0, fileData.Length); writer.Flush(true); } OnCompressorEvent?.Invoke(sessionName + " Wrote binary to file"); OnCompressorEvent?.Invoke(sessionName + " Begin Compress and Ocr"); string pageBody = CompressAndOcr(sessionName, inputDataFilePath, outputDataFilePath, metaData); string outputFileName = outputDataFilePath; if (PdfSettings.CompressFinalPdf) { OnCompressorEvent?.Invoke(sessionName + " Compressing output"); GhostScript gs = new GhostScript(GhostScriptPath, PdfSettings.Dpi); outputFileName = gs.CompressPdf(outputDataFilePath, sessionName, PdfSettings.PdfCompatibilityLevel, PdfSettings.DistillerMode, PdfSettings.DistillerOptions); } byte[] outFile = File.ReadAllBytes(outputFileName); int PageCountEnd = GetPages(outFile); OnCompressorEvent?.Invoke(sessionName + " Destroying session"); TempData.Instance.DestroySession(sessionName); if (PageCountEnd != PageCountStart) { throw new PageCountMismatchException("Page count is different", PageCountStart, PageCountEnd); } return(new Tuple <byte[], string>(outFile, pageBody)); } catch (Exception e) { OnExceptionOccurred?.Invoke(this, e); throw new FailedToGenerateException("Error in: CreateSearchablePdf", e); } }
private string CompressAndOcr(string sessionName, string inputFileName, string outputFileName, PdfMeta meta) { string pageBody = ""; OnCompressorEvent?.Invoke(sessionName + " Creating PDF Reader"); PdfReader reader = new PdfReader(inputFileName, PdfSettings.Dpi, GhostScriptPath); OnCompressorEvent?.Invoke(sessionName + " Creating PDF Writer"); PdfCreator writer = new PdfCreator(PdfSettings, outputFileName, meta, PdfSettings.Dpi) { PdfSettings = { WriteTextMode = PdfSettings.WriteTextMode } }; try { for (int i = 1; i <= reader.PageCount; i++) { OnCompressorEvent?.Invoke(sessionName + " Processing page " + i + " of " + reader.PageCount); string img = reader.GetPageImage(i, sessionName, this); if (OnPreProcessImage != null) { img = OnPreProcessImage(img); } Bitmap chk = new Bitmap(img); Rectangle pageSize = new Rectangle(0, 0, chk.Width, chk.Height); chk.Dispose(); pageBody = pageBody + writer.AddPage(img, PdfMode.Ocr, sessionName, pageSize); } writer.SaveAndClose(); writer.Dispose(); reader.Dispose(); return(pageBody); } catch (Exception x) { OnCompressorEvent?.Invoke(sessionName + " Image not supported in " + Path.GetFileName(inputFileName) + ". Skipping"); writer.SaveAndClose(); writer.Dispose(); reader.Dispose(); OnExceptionOccurred?.Invoke(this, x); throw; } }
/// <summary> /// Searches for tracks /// </summary> /// <param name="query">The query to be searched</param> public async void SearchTracks(string query) { try { IsLoading = true; var searchedTracks = await vkAPI.SearchAudio(query, APIConstants.TRUE, 0, 100); SearchResults.Tracks = new ObservableCollection<Track>(searchedTracks); PlaybackSource = PlaybackSources.SEARCH_RESULTS; IsLoading = false; } catch (Exception ex) { IsLoading = false; OnExceptionOccurred?.Invoke(this, ex); } }
private void ExecuteTask() { var currentTaskId = Thread.CurrentThread.ManagedThreadId; string debugStr = $"Thread -- {currentTaskId}"; while (!_isFinished && !_cts.IsCancellationRequested) { LogInfoWriter.GetInstance().Info($"{debugStr} --Check Start [taskInfoList:{_taskInfoList.Count()}, hasCompleteTaskInfoList:{_hasCompleteTaskInfoList.Count()}, runningTaskInfoDic:{_runningTaskInfoDic.Count()}]"); Thread.Sleep(1000); lock (_lockObj) { //所有任务已完成 if (_taskInfoList.Count() <= 0) { LogInfoWriter.GetInstance().Info($"{debugStr} --Check taskInfoList.Count() <= 100 \r\n"); _isFinished = true; return; } //任务失败数超过任务要求的最大可失败数 LogInfoWriter.GetInstance().Info($"{debugStr} --Check hasCompleteTaskInfoList.Count(s => s.IsFailed):{_hasCompleteTaskInfoList.Count(s => s.IsFailed)}"); if (_hasCompleteTaskInfoList.Count(s => s.IsFailed) >= _taskFailedMaxCount) { LogInfoWriter.GetInstance().Info($"{debugStr} --Check hasCompleteTaskInfoList.Count(s => s.IsFailed) >= {_taskFailedMaxCount} \r\n"); _isFinished = true; return; } //正在进行的任务数 = 最大可并发任务 则跳过获取新任务 继续等待当前运行的任务完成 LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Count():{_runningTaskInfoDic.Count()}"); if (_runningTaskInfoDic.Count() == _concurrencyRunTaskMaxcount) { LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Count() == {_concurrencyRunTaskMaxcount} \r\n"); continue; } //正在运行的任务中存在失败的任务且还有其他任务未结束 则不去获取新任务 LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Values.Any(s => s.IsFailed):{_runningTaskInfoDic.Values.Any(s => s.IsFailed)}"); LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Values.Any(s => !s.IsCompleted):{_runningTaskInfoDic.Values.Any(s => !s.IsCompleted)}"); if (_runningTaskInfoDic.Values.Any(s => s.IsFailed) && _runningTaskInfoDic.Values.Any(s => !s.IsCompleted)) { LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Values.Any(s => s.IsFailed) == _runningTaskInfoDic.Values.Any(s => !s.IsCompleted) == true \r\n"); continue; } //如果当前正在运行任务小于最大 并发任务数 且当前线程没有正在运行的任务 则去拉取新任务执行 if (_runningTaskInfoDic.Count < _concurrencyRunTaskMaxcount && !_runningTaskInfoDic.ContainsKey(currentTaskId)) { var t = _taskInfoList.First(s => !s.IsCompleted); LogInfoWriter.GetInstance().Info($"{debugStr} --Get GetTask Success :{t.TaskName}"); _runningTaskInfoDic.Add(currentTaskId, t); _taskInfoList.Remove(t); LogInfoWriter.GetInstance().Info($"{debugStr} --Get taskInfoList.Count():{_taskInfoList.Count()}"); LogInfoWriter.GetInstance().Info($"{debugStr} --Get runningTaskInfoDic.Count():{_runningTaskInfoDic.Count()}\r\n"); } } if (_runningTaskInfoDic.ContainsKey(currentTaskId)) { var task = _runningTaskInfoDic[currentTaskId]; try { LogInfoWriter.GetInstance().Info($"{debugStr} --Execute Start"); OnBeforeStart?.Invoke(task); task.TaskAction?.Invoke(); Interlocked.Increment(ref _successTaskCount); OnAfterStart?.Invoke(task); } catch (Exception e) { task.ExecuteFailed(e); Interlocked.Increment(ref _failedTaskCount); OnExceptionOccurred?.Invoke(e); LogInfoWriter.GetInstance().Info($"{debugStr} --Execute Failed"); } finally { task.HasCompleted(); Interlocked.Increment(ref _completedTaskCount); OnCompleted?.Invoke(task); LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute runningTaskInfoDic.Values.Any(s => s.IsFailed):{_runningTaskInfoDic.Values.Any(s => s.IsFailed)}!"); LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute hasCompleteTaskInfoList.Any(s => s.IsFailed)):{_hasCompleteTaskInfoList.Any(s => s.IsFailed)}!"); LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute runningTaskInfoDic.Values.Any(s=>!s.IsCompleted):{_runningTaskInfoDic.Values.Any(s => !s.IsCompleted)}!"); while ((_runningTaskInfoDic.Values.Any(s => s.IsFailed) || _hasCompleteTaskInfoList.Any(s => s.IsFailed)) && _runningTaskInfoDic.Values.Any(s => !s.IsCompleted)) { LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute Wait [taskInfoList:{_taskInfoList.Count()}, hasCompleteTaskInfoList:{_hasCompleteTaskInfoList.Count()}, runningTaskInfoDic:{_runningTaskInfoDic.Count()}]"); LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute Wait there has some task failed ,so wait all current running task has finished!"); LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute Wait [runningTaskInfoDic:{_runningTaskInfoDic.Values.Select(s=>s.TaskName + ":[" +s.IsFailed+","+s.IsCompleted+"]").Aggregate((s,r)=>s+" - "+r)}]"); Thread.Sleep(1000); } if (_runningTaskInfoDic.ContainsKey(currentTaskId)) { LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute Add [runningTaskInfoDic:{_runningTaskInfoDic.Values.Select(s => s.TaskName + ":[" + s.IsFailed + "," + s.IsCompleted + "]").Aggregate((s, r) => s + " - " + r)}]"); LogInfoWriter.GetInstance().Info($"{debugStr} --Execute Add {task.TaskName} HasComplete and Remove running"); _runningTaskInfoDic.Remove(currentTaskId); _hasCompleteTaskInfoList.Add(task); } LogInfoWriter.GetInstance().Info($"{debugStr} --Execute End"); } } LogInfoWriter.GetInstance().Info($"{debugStr} --Check End [taskInfoList:{_taskInfoList.Count()}, hasCompleteTaskInfoList:{_hasCompleteTaskInfoList.Count()}, runningTaskInfoDic:{_runningTaskInfoDic.Count()}]"); } }