Miscellaneous code cleanup

This commit is contained in:
Patrick Barron 2020-04-05 15:56:08 -04:00
parent add0a2088d
commit e1958e3902
5 changed files with 44 additions and 95 deletions

View File

@ -305,9 +305,15 @@ namespace MediaBrowser.Api
Directory.CreateDirectory(Path.GetDirectoryName(fullCachePath));
using (var stream = result.Content)
using (var filestream = new FileStream(fullCachePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true))
{
await stream.CopyToAsync(filestream).ConfigureAwait(false);
using var fileStream = new FileStream(fullCachePath,
FileMode.Create,
FileAccess.Write,
FileShare.Read,
IODefaults.FileStreamBufferSize,
true);
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
}
Directory.CreateDirectory(Path.GetDirectoryName(pointerCachePath));

View File

@ -544,8 +544,7 @@ namespace MediaBrowser.Api.Library
foreach (var type in types)
{
ImageOption[] defaultImageOptions = null;
TypeOptions.DefaultImageOptions.TryGetValue(type, out defaultImageOptions);
TypeOptions.DefaultImageOptions.TryGetValue(type, out var defaultImageOptions);
typeOptions.Add(new LibraryTypeOptions
{
@ -592,8 +591,6 @@ namespace MediaBrowser.Api.Library
public object Get(GetSimilarItems request)
{
var user = !request.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(request.UserId) : null;
var item = string.IsNullOrEmpty(request.Id) ?
(!request.UserId.Equals(Guid.Empty) ? _libraryManager.GetUserRootFolder() :
_libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);
@ -651,7 +648,7 @@ namespace MediaBrowser.Api.Library
// ExcludeArtistIds
if (!string.IsNullOrEmpty(request.ExcludeArtistIds))
{
query.ExcludeArtistIds = BaseApiService.GetGuids(request.ExcludeArtistIds);
query.ExcludeArtistIds = GetGuids(request.ExcludeArtistIds);
}
List<BaseItem> itemsResult;
@ -672,7 +669,6 @@ namespace MediaBrowser.Api.Library
var result = new QueryResult<BaseItemDto>
{
Items = returnList,
TotalRecordCount = itemsResult.Count
};
@ -1075,36 +1071,18 @@ namespace MediaBrowser.Api.Library
throw new ResourceNotFoundException("Item not found.");
}
BaseItem[] themeItems = Array.Empty<BaseItem>();
IEnumerable<BaseItem> themeItems = item.GetThemeSongs();
while (true)
while (!themeItems.Any() && request.InheritFromParent && item.GetParent() != null)
{
themeItems = item.GetThemeSongs().ToArray();
if (themeItems.Length > 0)
{
break;
}
if (!request.InheritFromParent)
{
break;
}
var parent = item.GetParent();
if (parent == null)
{
break;
}
item = parent;
item = item.GetParent();
themeItems = item.GetThemeSongs();
}
var dtoOptions = GetDtoOptions(_authContext, request);
var dtos = themeItems
.Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item));
var items = dtos.ToArray();
var items = themeItems
.Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item))
.ToArray();
return new ThemeMediaResult
{
@ -1121,9 +1099,7 @@ namespace MediaBrowser.Api.Library
/// <returns>System.Object.</returns>
public object Get(GetThemeVideos request)
{
var result = GetThemeVideos(request);
return ToOptimizedResult(result);
return ToOptimizedResult(GetThemeVideos(request));
}
public ThemeMediaResult GetThemeVideos(GetThemeVideos request)
@ -1141,36 +1117,19 @@ namespace MediaBrowser.Api.Library
throw new ResourceNotFoundException("Item not found.");
}
BaseItem[] themeItems = Array.Empty<BaseItem>();
IEnumerable<BaseItem> themeItems = item.GetThemeVideos();
while (true)
while (!themeItems.Any() && request.InheritFromParent && item.GetParent() != null)
{
themeItems = item.GetThemeVideos().ToArray();
if (themeItems.Length > 0)
{
break;
}
if (!request.InheritFromParent)
{
break;
}
var parent = item.GetParent();
if (parent == null)
{
break;
}
item = parent;
item = item.GetParent();
themeItems = item.GetThemeVideos();
}
var dtoOptions = GetDtoOptions(_authContext, request);
var dtos = themeItems
.Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item));
var items = dtos.ToArray();
var items = themeItems
.Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item))
.ToArray();
return new ThemeMediaResult
{

View File

@ -240,17 +240,12 @@ namespace MediaBrowser.Api.Playback.Hls
protected Stream GetPlaylistFileStream(string path)
{
var tmpPath = path + ".tmp";
tmpPath = path;
try
{
return new FileStream(tmpPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, FileOptions.SequentialScan);
}
catch (IOException)
{
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, FileOptions.SequentialScan);
}
return new FileStream(path,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite,
IODefaults.FileStreamBufferSize,
FileOptions.SequentialScan);
}
protected override string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding)

View File

@ -273,7 +273,7 @@ namespace MediaBrowser.Api.UserLibrary
DtoOptions = dtoOptions
};
Func<BaseItem, bool> filter = i => FilterItem(request, i, excludeItemTypes, includeItemTypes, mediaTypes);
bool Filter(BaseItem i) => FilterItem(request, i, excludeItemTypes, includeItemTypes, mediaTypes);
if (parentItem.IsFolder)
{
@ -283,18 +283,18 @@ namespace MediaBrowser.Api.UserLibrary
{
items = request.Recursive ?
folder.GetRecursiveChildren(user, query).ToList() :
folder.GetChildren(user, true).Where(filter).ToList();
folder.GetChildren(user, true).Where(Filter).ToList();
}
else
{
items = request.Recursive ?
folder.GetRecursiveChildren(filter) :
folder.Children.Where(filter).ToList();
folder.GetRecursiveChildren(Filter) :
folder.Children.Where(Filter).ToList();
}
}
else
{
items = new[] { parentItem }.Where(filter).ToList();
items = new[] { parentItem }.Where(Filter).ToList();
}
var extractedItems = GetAllItems(request, items);
@ -345,30 +345,21 @@ namespace MediaBrowser.Api.UserLibrary
private bool FilterItem(GetItemsByName request, BaseItem f, string[] excludeItemTypes, string[] includeItemTypes, string[] mediaTypes)
{
// Exclude item types
if (excludeItemTypes.Length > 0)
if (excludeItemTypes.Length > 0 && excludeItemTypes.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase))
{
if (excludeItemTypes.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase))
{
return false;
}
return false;
}
// Include item types
if (includeItemTypes.Length > 0)
if (includeItemTypes.Length > 0 && !includeItemTypes.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase))
{
if (!includeItemTypes.Contains(f.GetType().Name, StringComparer.OrdinalIgnoreCase))
{
return false;
}
return false;
}
// Include MediaTypes
if (mediaTypes.Length > 0)
if (mediaTypes.Length > 0 && !mediaTypes.Contains(f.MediaType ?? string.Empty, StringComparer.OrdinalIgnoreCase))
{
if (!mediaTypes.Contains(f.MediaType ?? string.Empty, StringComparer.OrdinalIgnoreCase))
{
return false;
}
return false;
}
return true;

View File

@ -138,10 +138,8 @@ namespace MediaBrowser.Api
var videosWithVersions = items.Where(i => i.MediaSourceCount > 1)
.ToList();
var primaryVersion = videosWithVersions.FirstOrDefault() ?? items.OrderBy(i =>
{
return (i.Video3DFormat.HasValue || i.VideoType != Model.Entities.VideoType.VideoFile) ? 1 : 0;
})
var primaryVersion = videosWithVersions.FirstOrDefault() ?? items.OrderBy(i
=> (i.Video3DFormat.HasValue || i.VideoType != Model.Entities.VideoType.VideoFile) ? 1 : 0)
.ThenByDescending(i =>
{
var stream = i.GetDefaultVideoStream();