Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analyzers to MediaBrowser.Providers and minor improvements #2447

Merged
merged 6 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Emby.Server.Implementations/ApplicationHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -852,10 +852,15 @@ protected async Task RegisterResources(IServiceCollection serviceCollection)

serviceCollection.AddSingleton<IDeviceDiscovery>(new DeviceDiscovery(ServerConfigurationManager));

ChapterManager = new ChapterManager(LibraryManager, LoggerFactory, ServerConfigurationManager, ItemRepository);
ChapterManager = new ChapterManager(ItemRepository);
serviceCollection.AddSingleton(ChapterManager);

EncodingManager = new MediaEncoder.EncodingManager(FileSystemManager, LoggerFactory, MediaEncoder, ChapterManager, LibraryManager);
EncodingManager = new MediaEncoder.EncodingManager(
LoggerFactory.CreateLogger<MediaEncoder.EncodingManager>(),
FileSystemManager,
MediaEncoder,
ChapterManager,
LibraryManager);
serviceCollection.AddSingleton(EncodingManager);

var activityLogRepo = GetActivityLogRepository();
Expand Down
11 changes: 7 additions & 4 deletions Emby.Server.Implementations/Data/SqliteItemRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,7 @@ private ChapterInfo GetChapter(IReadOnlyList<IResultSetValue> reader, BaseItem i
/// <summary>
/// Saves the chapters.
/// </summary>
public void SaveChapters(Guid id, List<ChapterInfo> chapters)
public void SaveChapters(Guid id, IReadOnlyList<ChapterInfo> chapters)
{
CheckDisposed();

Expand Down Expand Up @@ -2035,22 +2035,24 @@ public void SaveChapters(Guid id, List<ChapterInfo> chapters)
}
}

private void InsertChapters(byte[] idBlob, List<ChapterInfo> chapters, IDatabaseConnection db)
private void InsertChapters(byte[] idBlob, IReadOnlyList<ChapterInfo> chapters, IDatabaseConnection db)
{
var startIndex = 0;
var limit = 100;
var chapterIndex = 0;

const string StartInsertText = "insert into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values ";
var insertText = new StringBuilder(StartInsertText, 256);

while (startIndex < chapters.Count)
{
var insertText = new StringBuilder("insert into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values ");

var endIndex = Math.Min(chapters.Count, startIndex + limit);

for (var i = startIndex; i < endIndex; i++)
{
insertText.AppendFormat("(@ItemId, @ChapterIndex{0}, @StartPositionTicks{0}, @Name{0}, @ImagePath{0}, @ImageDateModified{0}),", i.ToString(CultureInfo.InvariantCulture));
}

insertText.Length -= 1; // Remove last ,

using (var statement = PrepareStatement(db, insertText.ToString()))
Expand All @@ -2077,6 +2079,7 @@ private void InsertChapters(byte[] idBlob, List<ChapterInfo> chapters, IDatabase
}

startIndex += limit;
insertText.Length = StartInsertText.Length;
}
}

Expand Down
40 changes: 20 additions & 20 deletions Emby.Server.Implementations/MediaEncoder/EncodingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ public class EncodingManager : IEncodingManager
private readonly IChapterManager _chapterManager;
private readonly ILibraryManager _libraryManager;

/// <summary>
/// The first chapter ticks.
/// </summary>
private static readonly long _firstChapterTicks = TimeSpan.FromSeconds(15).Ticks;

public EncodingManager(
ILogger<EncodingManager> logger,
IFileSystem fileSystem,
ILoggerFactory loggerFactory,
IMediaEncoder encoder,
IChapterManager chapterManager, ILibraryManager libraryManager)
IChapterManager chapterManager,
ILibraryManager libraryManager)
{
_logger = logger;
_fileSystem = fileSystem;
_logger = loggerFactory.CreateLogger(nameof(EncodingManager));
_encoder = encoder;
_chapterManager = chapterManager;
_libraryManager = libraryManager;
Expand Down Expand Up @@ -97,12 +103,7 @@ private bool IsEligibleForChapterImageExtraction(Video video)
return video.DefaultVideoStreamIndex.HasValue;
}

/// <summary>
/// The first chapter ticks
/// </summary>
private static readonly long FirstChapterTicks = TimeSpan.FromSeconds(15).Ticks;

public async Task<bool> RefreshChapterImages(Video video, IDirectoryService directoryService, List<ChapterInfo> chapters, bool extractImages, bool saveChapters, CancellationToken cancellationToken)
public async Task<bool> RefreshChapterImages(Video video, IDirectoryService directoryService, IReadOnlyList<ChapterInfo> chapters, bool extractImages, bool saveChapters, CancellationToken cancellationToken)
{
if (!IsEligibleForChapterImageExtraction(video))
{
Expand Down Expand Up @@ -135,7 +136,7 @@ public async Task<bool> RefreshChapterImages(Video video, IDirectoryService dire
try
{
// Add some time for the first chapter to make sure we don't end up with a black image
var time = chapter.StartPositionTicks == 0 ? TimeSpan.FromTicks(Math.Min(FirstChapterTicks, video.RunTimeTicks ?? 0)) : TimeSpan.FromTicks(chapter.StartPositionTicks);
var time = chapter.StartPositionTicks == 0 ? TimeSpan.FromTicks(Math.Min(_firstChapterTicks, video.RunTimeTicks ?? 0)) : TimeSpan.FromTicks(chapter.StartPositionTicks);

var protocol = MediaProtocol.File;

Expand All @@ -152,9 +153,9 @@ public async Task<bool> RefreshChapterImages(Video video, IDirectoryService dire
{
_fileSystem.DeleteFile(tempFile);
}
catch
catch (IOException ex)
{

_logger.LogError(ex, "Error deleting {Path}", tempFile);
}

chapter.ImagePath = path;
Expand Down Expand Up @@ -184,7 +185,7 @@ public async Task<bool> RefreshChapterImages(Video video, IDirectoryService dire

if (saveChapters && changesMade)
{
_chapterManager.SaveChapters(video.Id.ToString(), chapters);
_chapterManager.SaveChapters(video.Id, chapters);
}

DeleteDeadImages(currentImages, chapters);
Expand All @@ -199,22 +200,21 @@ private string GetChapterImagePath(Video video, long chapterPositionTicks)
return Path.Combine(GetChapterImagesPath(video), filename);
}

private static List<string> GetSavedChapterImages(Video video, IDirectoryService directoryService)
private static IReadOnlyList<string> GetSavedChapterImages(Video video, IDirectoryService directoryService)
{
var path = GetChapterImagesPath(video);
if (!Directory.Exists(path))
{
return new List<string>();
return Array.Empty<string>();
}

try
{
return directoryService.GetFilePaths(path)
.ToList();
return directoryService.GetFilePaths(path);
}
catch (IOException)
{
return new List<string>();
return Array.Empty<string>();
}
}

Expand All @@ -227,15 +227,15 @@ private void DeleteDeadImages(IEnumerable<string> images, IEnumerable<ChapterInf

foreach (var image in deadImages)
{
_logger.LogDebug("Deleting dead chapter image {path}", image);
_logger.LogDebug("Deleting dead chapter image {Path}", image);

try
{
_fileSystem.DeleteFile(image);
}
catch (IOException ex)
{
_logger.LogError(ex, "Error deleting {path}.", image);
_logger.LogError(ex, "Error deleting {Path}.", image);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions MediaBrowser.Controller/Chapters/IChapterManager.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Collections.Generic;
using MediaBrowser.Model.Entities;

namespace MediaBrowser.Controller.Chapters
{
/// <summary>
/// Interface IChapterManager
/// Interface IChapterManager.
/// </summary>
public interface IChapterManager
{
/// <summary>
/// Saves the chapters.
/// </summary>
void SaveChapters(string itemId, List<ChapterInfo> chapters);
void SaveChapters(Guid itemId, IReadOnlyList<ChapterInfo> chapters);
}
}
6 changes: 3 additions & 3 deletions MediaBrowser.Controller/Entities/Folder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#pragma warning disable CS1591
#pragma warning disable SA1600

using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -28,7 +31,6 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public class Folder : BaseItem
{
public static IUserManager UserManager { get; set; }
public static IUserViewManager UserViewManager { get; set; }

/// <summary>
Expand Down Expand Up @@ -620,7 +622,6 @@ public virtual int GetRecursiveChildCount(User user)
{
EnableImages = false
}

}).TotalRecordCount;
}

Expand Down Expand Up @@ -1672,7 +1673,6 @@ public override void FillUserDataDtoValues(UserItemDataDto dto, UserItemData use
{
EnableImages = false
}

});

double unplayedCount = unplayedQueryResult.TotalRecordCount;
Expand Down
2 changes: 1 addition & 1 deletion MediaBrowser.Controller/Entities/IItemByName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace MediaBrowser.Controller.Entities
{
/// <summary>
/// Marker interface
/// Marker interface.
/// </summary>
public interface IItemByName
{
Expand Down
5 changes: 4 additions & 1 deletion MediaBrowser.Controller/MediaEncoding/IEncodingManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#pragma warning disable CS1591
#pragma warning disable SA1600

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -12,6 +15,6 @@ public interface IEncodingManager
/// <summary>
/// Refreshes the chapter images.
/// </summary>
Task<bool> RefreshChapterImages(Video video, IDirectoryService directoryService, List<ChapterInfo> chapters, bool extractImages, bool saveChapters, CancellationToken cancellationToken);
Task<bool> RefreshChapterImages(Video video, IDirectoryService directoryService, IReadOnlyList<ChapterInfo> chapters, bool extractImages, bool saveChapters, CancellationToken cancellationToken);
}
}
2 changes: 1 addition & 1 deletion MediaBrowser.Controller/Persistence/IItemRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface IItemRepository : IRepository
/// <summary>
/// Saves the chapters.
/// </summary>
void SaveChapters(Guid id, List<ChapterInfo> chapters);
void SaveChapters(Guid id, IReadOnlyList<ChapterInfo> chapters);

/// <summary>
/// Gets the media streams.
Expand Down
8 changes: 3 additions & 5 deletions MediaBrowser.Controller/Providers/DirectoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,10 @@ public FileSystemMetadata GetFile(string path)
//return _fileSystem.GetFileInfo(path);
}

public List<string> GetFilePaths(string path)
{
return GetFilePaths(path, false);
}
public IReadOnlyList<string> GetFilePaths(string path)
=> GetFilePaths(path, false);

public List<string> GetFilePaths(string path, bool clearCache)
public IReadOnlyList<string> GetFilePaths(string path, bool clearCache)
{
if (clearCache || !_filePathCache.TryGetValue(path, out List<string> result))
{
Expand Down
4 changes: 2 additions & 2 deletions MediaBrowser.Controller/Providers/IDirectoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IDirectoryService
List<FileSystemMetadata> GetFiles(string path);
FileSystemMetadata GetFile(string path);

List<string> GetFilePaths(string path);
List<string> GetFilePaths(string path, bool clearCache);
IReadOnlyList<string> GetFilePaths(string path);
IReadOnlyList<string> GetFilePaths(string path, bool clearCache);
}
}
2 changes: 1 addition & 1 deletion MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ private async Task<MediaInfo> GetMediaInfoInternal(string inputPath,
}
}

return new ProbeResultNormalizer(_logger, _fileSystem, _localization).GetMediaInfo(result, videoType, isAudio, primaryPath, protocol);
return new ProbeResultNormalizer(_logger, _localization).GetMediaInfo(result, videoType, isAudio, primaryPath, protocol);
}
}

Expand Down
7 changes: 2 additions & 5 deletions MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;

Expand All @@ -19,13 +18,11 @@ public class ProbeResultNormalizer
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
private readonly ILocalizationManager _localization;

public ProbeResultNormalizer(ILogger logger, IFileSystem fileSystem, ILocalizationManager localization)
public ProbeResultNormalizer(ILogger logger, ILocalizationManager localization)
{
_logger = logger;
_fileSystem = fileSystem;
_localization = localization;
}

Expand All @@ -40,7 +37,7 @@ public MediaInfo GetMediaInfo(InternalMediaInfoResult data, VideoType? videoType
FFProbeHelpers.NormalizeFFProbeResult(data);
SetSize(data, info);

var internalStreams = data.Streams ?? new MediaStreamInfo[] { };
var internalStreams = data.Streams ?? Array.Empty<MediaStreamInfo>();

info.MediaStreams = internalStreams.Select(s => GetMediaStream(isAudio, s, data.Format))
.Where(i => i != null)
Expand Down
23 changes: 7 additions & 16 deletions MediaBrowser.Providers/Chapters/ChapterManager.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
#pragma warning disable CS1591
#pragma warning disable SA1600

using System;
using System.Collections.Generic;
using MediaBrowser.Controller.Chapters;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.Providers.Chapters
{
public class ChapterManager : IChapterManager
{
private readonly ILibraryManager _libraryManager;
private readonly ILogger _logger;
private readonly IServerConfigurationManager _config;
private readonly IItemRepository _itemRepo;

public ChapterManager(
ILibraryManager libraryManager,
ILoggerFactory loggerFactory,
IServerConfigurationManager config,
IItemRepository itemRepo)
public ChapterManager(IItemRepository itemRepo)
{
_libraryManager = libraryManager;
_logger = loggerFactory.CreateLogger(nameof(ChapterManager));
_config = config;
_itemRepo = itemRepo;
}

public void SaveChapters(string itemId, List<ChapterInfo> chapters)
/// <inheritdoc />
public void SaveChapters(Guid itemId, IReadOnlyList<ChapterInfo> chapters)
{
_itemRepo.SaveChapters(new Guid(itemId), chapters);
_itemRepo.SaveChapters(itemId, chapters);
}
}
}
Loading