private void MoveWorker() { try { while (!_operationsPending.IsCompleted) { var moveOp = _operationsPending.Take(); moveOp.CurrentState = FileMoveState.Moving; try { if (Directory.Exists(moveOp.Source)) { var parentDir = Path.GetDirectoryName(moveOp.Destination); if (!Directory.Exists(parentDir)) { Directory.CreateDirectory(parentDir); } Directory.Move(moveOp.Source, moveOp.Destination); } else if (File.Exists(moveOp.Source)) { File.Move(moveOp.Source, moveOp.Destination); } else { throw new FileNotFoundException("Could not find the source file", moveOp.Source); } Task.Run(() => { _plex.RefreshSectionAsync(moveOp.PlexSection, moveOp.Destination).FireForget(_logger); _jDownloader.RemoveDownloadPackageAsync(Path.GetFileName(moveOp.Source)).FireForget(_logger); }); moveOp.CurrentState = FileMoveState.Success; } catch (Exception ex) { _logger.LogWarning(ex, "Move operation failed"); moveOp.ErrorMessage = ex.Message; moveOp.CurrentState = FileMoveState.Failed; } moveOp.Finished = DateTime.Now; } } catch (Exception ex) { _logger.LogError(ex, "File mover thread crashed due to an exception - Files will no longer be moved."); throw; } }
public async Task <bool> AddSubtitleAsync(string fileName, byte[] content, Series series) { if (!GetEpisodeFromSrt(fileName, out var season, out var episode)) { // Todo: find better solution for returning errors throw new ArgumentException("No season and episode information could be extracted from the given filename."); } var videoPath = await _plex.GetFilePathOfEpisode(series, season, episode); if (videoPath == null) { var dir = Path.Combine(_settings.Files_SeriesPath, series.DirectoryName, $"S{season:00}"); if (Directory.Exists(dir)) { videoPath = FindEpisodeInDirectory(dir, season, episode); } } if (videoPath == null) { throw new VideoDependencyNotFoundException(series, season, episode); } var target = Path.Combine(Path.GetDirectoryName(videoPath), Path.GetFileNameWithoutExtension(videoPath) + ".srt"); try { await File.WriteAllBytesAsync(target, content); _plex.RefreshSectionAsync(PlexSection.Series, Path.GetDirectoryName(target)).FireForget(_logger); return(true); } catch (Exception ex) { _logger.LogWarning(ex, $"Could not write content to {target}"); throw ex; } }