private void FinishFile(ProcessingFileInfo file) { if (ProcessingFiles.TryRemove(file.File, out _)) { Interlocked.Increment(ref FinishedFiles); } }
private void FileError(ProcessingFileInfo file, Exception e, string msg) { file.State = ProcessingState.Error; Logger.Error(e, msg); Errors.Add(new DownloadError(msg, e)); FinishFile(file); }
private async Task <List <Match> > DownloadResizedFile(ProcessingFileInfo file, IIqdbApi iqdbApi) { var resizedFile = ImageResizer.Resize( file.File, IqdbApi.api.IqdbApi.MaxImageWidth, IqdbApi.api.IqdbApi.MaxImageHeight ); var matches = await iqdbApi.SearchFile(resizedFile, Options.Default); File.Delete(resizedFile); return(matches); }
private void ProcessMatches(ProcessingFileInfo file, IReadOnlyCollection <Match> matches) { var bests = matches != null?_matchRanker.OrderBest(matches) : null; foreach (var best in bests) { Logger.Info($"Parsing '{file.File}'"); file.State = ProcessingState.Parsing; ParseResult result = null; lock (ServiceType.GetTypeByUrl(best.Url)) { try { var parseTask = _parser.Parse(best.Url); parseTask.Wait(_cancelToken); result = parseTask.Result; } catch (Exception e) { Logger.Error(e, $"Error while parsing file '{file.File}'"); } } if (result == null) { continue; } var newFilePath = MoveFile(file, _settings.DownloadedDirPath); try { JsonHelper.SaveJson(newFilePath, best, result); } catch (Exception e) { FileError(file, e, $"Error while saving data for file '{file.File}'"); } FinishFile(file); return; } Logger.Info($"No match for '{file.File}'"); file.State = ProcessingState.Saving; MoveFile(file, _settings.NoMatchDirPath); FinishFile(file); }
public async Task Start() { using (var iqdbApi = new IqdbApi.api.IqdbApi()) { var last = _files.Count > 0 ? _files.Last() : null; foreach (var file in _files) { Logger.Info($"Fetching '{file}'"); var processingFile = new ProcessingFileInfo(file); ProcessingFiles.GetOrAdd(processingFile.File, processingFile); await DownloadFile(processingFile, iqdbApi); if (!file.Equals(last)) { await Task.Delay(_settings.RequestDelayMs); } } } }
private string MoveFile(ProcessingFileInfo file, string dest) { if (dest == null) { return(file.File); } try { var fileName = Path.GetFileName(file.File); var destFile = Path.Combine(dest, fileName); File.Move(file.File, destFile); return(destFile); } catch (Exception e) { FileError(file, e, $"Error while moving file '{file.File}' to '{dest}'"); } return(file.File); }
private async Task DownloadFile(ProcessingFileInfo file, IIqdbApi iqdbApi) { try { List <Match> matches; try { file.State = ProcessingState.Fetching; matches = await iqdbApi.SearchFile(file.File, Options.Default); } catch (FileSizeLimitException e) { matches = await DownloadResizedFile(file, iqdbApi); } Task.Run(() => ProcessMatches(file, matches), _cancelToken); } catch (Exception e) { FileError(file, e, $"Error while downloading file '{file.File}'"); } }