Ejemplo n.º 1
0
    public static string CalculateWithSubfolders(string folder, ILocalFileSystem localFileSystem)
    {
        IEnumerable <string> allFiles = localFileSystem.ListFiles(folder);

        var sb = new StringBuilder();

        foreach (string file in allFiles.OrderBy(identity))
        {
            sb.Append(file);
            sb.Append(localFileSystem.GetLastWriteTime(file).Ticks);
        }

        foreach (string subfolder in localFileSystem.ListSubdirectories(folder).OrderBy(identity))
        {
            sb.Append(subfolder);
            sb.Append(Calculate(subfolder, localFileSystem));
        }

        var hash = new StringBuilder();

        byte[] bytes = Crypto.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));
        foreach (byte t in bytes)
        {
            hash.Append(t.ToString("x2"));
        }

        return(hash.ToString());
    }
Ejemplo n.º 2
0
    private async Task <Either <BaseError, Unit> > ScanEpisodes(
        LibraryPath libraryPath,
        string ffmpegPath,
        string ffprobePath,
        Season season,
        string seasonPath,
        CancellationToken cancellationToken)
    {
        var allSeasonFiles = _localFileSystem.ListSubdirectories(seasonPath)
                             .Map(_localFileSystem.ListFiles)
                             .Flatten()
                             .Append(_localFileSystem.ListFiles(seasonPath))
                             .Filter(f => VideoFileExtensions.Contains(Path.GetExtension(f)))
                             .Filter(f => !Path.GetFileName(f).StartsWith("._"))
                             .OrderBy(identity)
                             .ToList();

        foreach (string file in allSeasonFiles)
        {
            // TODO: figure out how to rebuild playlists
            Either <BaseError, Episode> maybeEpisode = await _televisionRepository
                                                       .GetOrAddEpisode(season, libraryPath, file)
                                                       .BindT(
                episode => UpdateStatistics(new MediaItemScanResult <Episode>(episode), ffmpegPath, ffprobePath)
                .MapT(_ => episode))
                                                       .BindT(UpdateMetadata)
                                                       .BindT(e => UpdateThumbnail(e, cancellationToken))
                                                       .BindT(UpdateSubtitles)
                                                       .BindT(e => FlagNormal(new MediaItemScanResult <Episode>(e)))
                                                       .MapT(r => r.Item);

            foreach (BaseError error in maybeEpisode.LeftToSeq())
            {
                _logger.LogWarning("Error processing episode at {Path}: {Error}", file, error.Value);
            }

            foreach (Episode episode in maybeEpisode.RightToSeq())
            {
                await _searchIndex.UpdateItems(_searchRepository, new List <MediaItem> {
                    episode
                });
            }
        }

        // TODO: remove missing episodes?

        return(Unit.Default);
    }
Ejemplo n.º 3
0
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        using IServiceScope scope       = _serviceScopeFactory.CreateScope();
        await using TvContext dbContext = scope.ServiceProvider.GetRequiredService <TvContext>();

        IRuntimeInfo runtimeInfo = scope.ServiceProvider.GetRequiredService <IRuntimeInfo>();

        if (runtimeInfo != null && runtimeInfo.IsOSPlatform(OSPlatform.Linux) &&
            Directory.Exists("/dev/dri"))
        {
            ILocalFileSystem localFileSystem = scope.ServiceProvider.GetRequiredService <ILocalFileSystem>();
            IMemoryCache     memoryCache     = scope.ServiceProvider.GetRequiredService <IMemoryCache>();

            var devices = localFileSystem.ListFiles("/dev/dri")
                          .Filter(s => s.StartsWith("/dev/dri/render"))
                          .ToList();

            memoryCache.Set("ffmpeg.render_devices", devices);
        }
    }
Ejemplo n.º 4
0
        private async Task <Unit> ScanEpisodes(
            LibraryPath libraryPath,
            string ffprobePath,
            Season season,
            string seasonPath)
        {
            foreach (string file in _localFileSystem.ListFiles(seasonPath)
                     .Filter(f => VideoFileExtensions.Contains(Path.GetExtension(f))).OrderBy(identity))
            {
                // TODO: figure out how to rebuild playlists
                Either <BaseError, Episode> maybeEpisode = await _televisionRepository
                                                           .GetOrAddEpisode(season, libraryPath, file)
                                                           .BindT(
                    episode => UpdateStatistics(new MediaItemScanResult <Episode>(episode), ffprobePath)
                    .MapT(_ => episode))
                                                           .BindT(UpdateMetadata)
                                                           .BindT(UpdateThumbnail);

                maybeEpisode.IfLeft(
                    error => _logger.LogWarning("Error processing episode at {Path}: {Error}", file, error.Value));
            }

            return(Unit.Default);
        }
Ejemplo n.º 5
0
    public async Task <Either <BaseError, Unit> > ScanFolder(
        LibraryPath libraryPath,
        string ffprobePath,
        string ffmpegPath,
        decimal progressMin,
        decimal progressMax,
        CancellationToken cancellationToken)
    {
        try
        {
            decimal progressSpread = progressMax - progressMin;

            var foldersCompleted = 0;

            var folderQueue = new Queue <string>();

            if (ShouldIncludeFolder(libraryPath.Path))
            {
                folderQueue.Enqueue(libraryPath.Path);
            }

            foreach (string folder in _localFileSystem.ListSubdirectories(libraryPath.Path)
                     .Filter(ShouldIncludeFolder)
                     .OrderBy(identity))
            {
                folderQueue.Enqueue(folder);
            }

            while (folderQueue.Count > 0)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(new ScanCanceled());
                }

                decimal percentCompletion = (decimal)foldersCompleted / (foldersCompleted + folderQueue.Count);
                await _mediator.Publish(
                    new LibraryScanProgress(libraryPath.LibraryId, progressMin + percentCompletion *progressSpread),
                    cancellationToken);

                string songFolder = folderQueue.Dequeue();
                foldersCompleted++;

                var filesForEtag = _localFileSystem.ListFiles(songFolder).ToList();

                var allFiles = filesForEtag
                               .Filter(f => AudioFileExtensions.Contains(Path.GetExtension(f)))
                               .Filter(f => !Path.GetFileName(f).StartsWith("._"))
                               .ToList();

                foreach (string subdirectory in _localFileSystem.ListSubdirectories(songFolder)
                         .Filter(ShouldIncludeFolder)
                         .OrderBy(identity))
                {
                    folderQueue.Enqueue(subdirectory);
                }

                string etag = FolderEtag.Calculate(songFolder, _localFileSystem);
                Option <LibraryFolder> knownFolder = libraryPath.LibraryFolders
                                                     .Filter(f => f.Path == songFolder)
                                                     .HeadOrNone();

                // skip folder if etag matches
                if (!allFiles.Any() || await knownFolder.Map(f => f.Etag ?? string.Empty).IfNoneAsync(string.Empty) ==
                    etag)
                {
                    continue;
                }

                _logger.LogDebug(
                    "UPDATE: Etag has changed for folder {Folder}",
                    songFolder);

                foreach (string file in allFiles.OrderBy(identity))
                {
                    Either <BaseError, MediaItemScanResult <Song> > maybeSong = await _songRepository
                                                                                .GetOrAdd(libraryPath, file)
                                                                                .BindT(video => UpdateStatistics(video, ffmpegPath, ffprobePath))
                                                                                .BindT(video => UpdateMetadata(video, ffprobePath))
                                                                                .BindT(video => UpdateThumbnail(video, ffmpegPath, cancellationToken))
                                                                                .BindT(FlagNormal);

                    foreach (BaseError error in maybeSong.LeftToSeq())
                    {
                        _logger.LogWarning("Error processing song at {Path}: {Error}", file, error.Value);
                    }

                    foreach (MediaItemScanResult <Song> result in maybeSong.RightToSeq())
                    {
                        if (result.IsAdded)
                        {
                            await _searchIndex.AddItems(_searchRepository, new List <MediaItem> {
                                result.Item
                            });
                        }
                        else if (result.IsUpdated)
                        {
                            await _searchIndex.UpdateItems(_searchRepository, new List <MediaItem> {
                                result.Item
                            });
                        }

                        await _libraryRepository.SetEtag(libraryPath, knownFolder, songFolder, etag);
                    }
                }
            }

            foreach (string path in await _songRepository.FindSongPaths(libraryPath))
            {
                if (!_localFileSystem.FileExists(path))
                {
                    _logger.LogInformation("Flagging missing song at {Path}", path);
                    List <int> songIds = await FlagFileNotFound(libraryPath, path);

                    await _searchIndex.RebuildItems(_searchRepository, songIds);
                }
                else if (Path.GetFileName(path).StartsWith("._"))
                {
                    _logger.LogInformation("Removing dot underscore file at {Path}", path);
                    List <int> songIds = await _songRepository.DeleteByPath(libraryPath, path);

                    await _searchIndex.RemoveItems(songIds);
                }
            }

            await _libraryRepository.CleanEtagsForLibraryPath(libraryPath);

            return(Unit.Default);
        }
        catch (Exception ex) when(ex is TaskCanceledException or OperationCanceledException)
        {
            return(new ScanCanceled());
        }
        finally
        {
            _searchIndex.Commit();
        }
    }
Ejemplo n.º 6
0
    private async Task <Either <BaseError, Unit> > ScanMusicVideos(
        LibraryPath libraryPath,
        string ffmpegPath,
        string ffprobePath,
        Artist artist,
        string artistFolder,
        CancellationToken cancellationToken)
    {
        var folderQueue = new Queue <string>();

        folderQueue.Enqueue(artistFolder);

        while (folderQueue.Count > 0)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(new ScanCanceled());
            }

            string musicVideoFolder = folderQueue.Dequeue();
            // _logger.LogDebug("Scanning music video folder {Folder}", musicVideoFolder);

            var allFiles = _localFileSystem.ListFiles(musicVideoFolder)
                           .Filter(f => VideoFileExtensions.Contains(Path.GetExtension(f)))
                           .Filter(f => !Path.GetFileName(f).StartsWith("._"))
                           .ToList();

            foreach (string subdirectory in _localFileSystem.ListSubdirectories(musicVideoFolder)
                     .OrderBy(identity))
            {
                folderQueue.Enqueue(subdirectory);
            }

            string etag = FolderEtag.Calculate(musicVideoFolder, _localFileSystem);
            Option <LibraryFolder> knownFolder = libraryPath.LibraryFolders
                                                 .Filter(f => f.Path == musicVideoFolder)
                                                 .HeadOrNone();

            // skip folder if etag matches
            if (await knownFolder.Map(f => f.Etag ?? string.Empty).IfNoneAsync(string.Empty) == etag)
            {
                continue;
            }

            foreach (string file in allFiles.OrderBy(identity))
            {
                // TODO: figure out how to rebuild playouts
                Either <BaseError, MediaItemScanResult <MusicVideo> > maybeMusicVideo = await _musicVideoRepository
                                                                                        .GetOrAdd(artist, libraryPath, file)
                                                                                        .BindT(musicVideo => UpdateStatistics(musicVideo, ffmpegPath, ffprobePath))
                                                                                        .BindT(UpdateMetadata)
                                                                                        .BindT(result => UpdateThumbnail(result, cancellationToken))
                                                                                        .BindT(UpdateSubtitles)
                                                                                        .BindT(FlagNormal);

                foreach (BaseError error in maybeMusicVideo.LeftToSeq())
                {
                    _logger.LogWarning("Error processing music video at {Path}: {Error}", file, error.Value);
                }

                foreach (MediaItemScanResult <MusicVideo> result in maybeMusicVideo.RightToSeq())
                {
                    if (result.IsAdded)
                    {
                        await _searchIndex.AddItems(_searchRepository, new List <MediaItem> {
                            result.Item
                        });
                    }
                    else if (result.IsUpdated)
                    {
                        await _searchIndex.UpdateItems(_searchRepository, new List <MediaItem> {
                            result.Item
                        });
                    }

                    await _libraryRepository.SetEtag(libraryPath, knownFolder, musicVideoFolder, etag);
                }
            }
        }

        return(Unit.Default);
    }
Ejemplo n.º 7
0
        public async Task <Either <BaseError, Unit> > ScanFolder(LibraryPath libraryPath, string ffprobePath)
        {
            if (!_localFileSystem.IsLibraryPathAccessible(libraryPath))
            {
                return(new MediaSourceInaccessible());
            }

            var folderQueue = new Queue <string>();

            foreach (string folder in _localFileSystem.ListSubdirectories(libraryPath.Path).OrderBy(identity))
            {
                folderQueue.Enqueue(folder);
            }

            while (folderQueue.Count > 0)
            {
                string movieFolder = folderQueue.Dequeue();

                var allFiles = _localFileSystem.ListFiles(movieFolder)
                               .Filter(f => VideoFileExtensions.Contains(Path.GetExtension(f)))
                               .Filter(
                    f => !ExtraFiles.Any(
                        e => Path.GetFileNameWithoutExtension(f).EndsWith(e, StringComparison.OrdinalIgnoreCase)))
                               .ToList();

                if (allFiles.Count == 0)
                {
                    foreach (string subdirectory in _localFileSystem.ListSubdirectories(movieFolder).OrderBy(identity))
                    {
                        folderQueue.Enqueue(subdirectory);
                    }

                    continue;
                }

                foreach (string file in allFiles.OrderBy(identity))
                {
                    // TODO: figure out how to rebuild playlists
                    Either <BaseError, MediaItemScanResult <Movie> > maybeMovie = await _movieRepository
                                                                                  .GetOrAdd(libraryPath, file)
                                                                                  .BindT(movie => UpdateStatistics(movie, ffprobePath))
                                                                                  .BindT(UpdateMetadata)
                                                                                  .BindT(movie => UpdateArtwork(movie, ArtworkKind.Poster))
                                                                                  .BindT(movie => UpdateArtwork(movie, ArtworkKind.FanArt));

                    await maybeMovie.Match(
                        async result =>
                    {
                        if (result.IsAdded)
                        {
                            await _searchIndex.AddItems(new List <MediaItem> {
                                result.Item
                            });
                        }
                        else if (result.IsUpdated)
                        {
                            await _searchIndex.UpdateItems(new List <MediaItem> {
                                result.Item
                            });
                        }
                    },
                        error =>
                    {
                        _logger.LogWarning("Error processing movie at {Path}: {Error}", file, error.Value);
                        return(Task.CompletedTask);
                    });
                }
            }

            foreach (string path in await _movieRepository.FindMoviePaths(libraryPath))
            {
                if (!_localFileSystem.FileExists(path))
                {
                    _logger.LogInformation("Removing missing movie at {Path}", path);
                    List <int> ids = await _movieRepository.DeleteByPath(libraryPath, path);

                    await _searchIndex.RemoveItems(ids);
                }
            }

            return(Unit.Default);
        }