Simplified Conditionals and returns

This commit is contained in:
Patrick Barron 2020-04-05 14:44:14 -04:00
parent 80cfcf5643
commit add0a2088d
12 changed files with 299 additions and 372 deletions

View File

@ -86,12 +86,9 @@ namespace MediaBrowser.Api
return Array.Empty<string>();
}
if (removeEmpty)
{
return value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
}
return value.Split(separator);
return removeEmpty
? value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries)
: value.Split(separator);
}
public SemaphoreSlim GetTranscodingLock(string outputPath)
@ -487,16 +484,9 @@ namespace MediaBrowser.Api
/// <returns>Task.</returns>
internal Task KillTranscodingJobs(string deviceId, string playSessionId, Func<string, bool> deleteFiles)
{
return KillTranscodingJobs(j =>
{
if (!string.IsNullOrWhiteSpace(playSessionId))
{
return string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase);
}
return string.Equals(deviceId, j.DeviceId, StringComparison.OrdinalIgnoreCase);
}, deleteFiles);
return KillTranscodingJobs(j => !string.IsNullOrWhiteSpace(playSessionId)
? string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase)
: string.Equals(deviceId, j.DeviceId, StringComparison.OrdinalIgnoreCase), deleteFiles);
}
/// <summary>

View File

@ -58,12 +58,9 @@ namespace MediaBrowser.Api
public static string[] SplitValue(string value, char delim)
{
if (value == null)
{
return Array.Empty<string>();
}
return value.Split(new[] { delim }, StringSplitOptions.RemoveEmptyEntries);
return value == null
? Array.Empty<string>()
: value.Split(new[] { delim }, StringSplitOptions.RemoveEmptyEntries);
}
public static Guid[] GetGuids(string value)
@ -97,19 +94,10 @@ namespace MediaBrowser.Api
var authenticatedUser = auth.User;
// If they're going to update the record of another user, they must be an administrator
if (!userId.Equals(auth.UserId))
if ((!userId.Equals(auth.UserId) && !authenticatedUser.Policy.IsAdministrator)
|| (restrictUserPreferences && !authenticatedUser.Policy.EnableUserPreferenceAccess))
{
if (!authenticatedUser.Policy.IsAdministrator)
{
throw new SecurityException("Unauthorized access.");
}
}
else if (restrictUserPreferences)
{
if (!authenticatedUser.Policy.EnableUserPreferenceAccess)
{
throw new SecurityException("Unauthorized access.");
}
throw new SecurityException("Unauthorized access.");
}
}

View File

@ -116,12 +116,9 @@ namespace MediaBrowser.Api
{
var val = Filters;
if (string.IsNullOrEmpty(val))
{
return new ItemFilter[] { };
}
return val.Split(',').Select(v => (ItemFilter)Enum.Parse(typeof(ItemFilter), v, true));
return string.IsNullOrEmpty(val)
? new ItemFilter[] { }
: val.Split(',').Select(v => (ItemFilter)Enum.Parse(typeof(ItemFilter), v, true));
}
/// <summary>
@ -175,12 +172,9 @@ namespace MediaBrowser.Api
{
var val = Filters;
if (string.IsNullOrEmpty(val))
{
return new ItemFilter[] { };
}
return val.Split(',').Select(v => (ItemFilter)Enum.Parse(typeof(ItemFilter), v, true));
return string.IsNullOrEmpty(val)
? new ItemFilter[] { }
: val.Split(',').Select(v => (ItemFilter)Enum.Parse(typeof(ItemFilter), v, true));
}
}

View File

@ -155,16 +155,14 @@ namespace MediaBrowser.Api.Devices
Id = id
});
}
else
return _deviceManager.AcceptCameraUpload(deviceId, request.RequestStream, new LocalFileInfo
{
return _deviceManager.AcceptCameraUpload(deviceId, request.RequestStream, new LocalFileInfo
{
MimeType = Request.ContentType,
Album = album,
Name = name,
Id = id
});
}
MimeType = Request.ContentType,
Album = album,
Name = name,
Id = id
});
}
}
}

View File

@ -258,12 +258,7 @@ namespace MediaBrowser.Api
return false;
}
if (!request.IncludeDirectories && isDirectory)
{
return false;
}
return true;
return request.IncludeDirectories || !isDirectory;
});
return entries.Select(f => new FileSystemEntryInfo

View File

@ -348,28 +348,19 @@ namespace MediaBrowser.Api.Library
private string[] GetRepresentativeItemTypes(string contentType)
{
switch (contentType)
return contentType switch
{
case CollectionType.BoxSets:
return new[] { "BoxSet" };
case CollectionType.Playlists:
return new[] { "Playlist" };
case CollectionType.Movies:
return new[] { "Movie" };
case CollectionType.TvShows:
return new[] { "Series", "Season", "Episode" };
case CollectionType.Books:
return new[] { "Book" };
case CollectionType.Music:
return new[] { "MusicAlbum", "MusicArtist", "Audio", "MusicVideo" };
case CollectionType.HomeVideos:
case CollectionType.Photos:
return new[] { "Video", "Photo" };
case CollectionType.MusicVideos:
return new[] { "MusicVideo" };
default:
return new[] { "Series", "Season", "Episode", "Movie" };
}
CollectionType.BoxSets => new[] {"BoxSet"},
CollectionType.Playlists => new[] {"Playlist"},
CollectionType.Movies => new[] {"Movie"},
CollectionType.TvShows => new[] {"Series", "Season", "Episode"},
CollectionType.Books => new[] {"Book"},
CollectionType.Music => new[] {"MusicAlbum", "MusicArtist", "Audio", "MusicVideo"},
CollectionType.HomeVideos => new[] {"Video", "Photo"},
CollectionType.Photos => new[] {"Video", "Photo"},
CollectionType.MusicVideos => new[] {"MusicVideo"},
_ => new[] {"Series", "Season", "Episode", "Movie"}
};
}
private bool IsSaverEnabledByDefault(string name, string[] itemTypes, bool isNewLibrary)
@ -397,22 +388,18 @@ namespace MediaBrowser.Api.Library
{
if (string.Equals(name, "TheMovieDb", StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(type, "Series", StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (string.Equals(type, "Season", StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (string.Equals(type, "Episode", StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (string.Equals(type, "MusicVideo", StringComparison.OrdinalIgnoreCase))
if (string.Equals(type, "Season", StringComparison.OrdinalIgnoreCase)
|| string.Equals(type, "Episode", StringComparison.OrdinalIgnoreCase)
|| string.Equals(type, "MusicVideo", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return true;
}
else if (string.Equals(name, "TheTVDB", StringComparison.OrdinalIgnoreCase))
@ -439,12 +426,8 @@ namespace MediaBrowser.Api.Library
.Where(i => string.Equals(i.ItemType, type, StringComparison.OrdinalIgnoreCase))
.ToArray();
if (metadataOptions.Length == 0)
{
return true;
}
return metadataOptions.Any(i => !i.DisabledMetadataFetchers.Contains(name, StringComparer.OrdinalIgnoreCase));
return metadataOptions.Length == 0
|| metadataOptions.Any(i => !i.DisabledMetadataFetchers.Contains(name, StringComparer.OrdinalIgnoreCase));
}
private bool IsImageFetcherEnabledByDefault(string name, string type, bool isNewLibrary)
@ -919,12 +902,10 @@ namespace MediaBrowser.Api.Library
private BaseItem TranslateParentItem(BaseItem item, User user)
{
if (item.GetParent() is AggregateFolder)
{
return _libraryManager.GetUserRootFolder().GetChildren(user, true).FirstOrDefault(i => i.PhysicalLocations.Contains(item.Path));
}
return item;
return item.GetParent() is AggregateFolder
? _libraryManager.GetUserRootFolder().GetChildren(user, true)
.FirstOrDefault(i => i.PhysicalLocations.Contains(item.Path))
: item;
}
/// <summary>

View File

@ -137,12 +137,7 @@ namespace MediaBrowser.Api.Playback
var ext = outputFileExtension.ToLowerInvariant();
var folder = ServerConfigurationManager.GetTranscodePath();
if (EnableOutputInSubFolder)
{
return Path.Combine(folder, filename, filename + ext);
}
return Path.Combine(folder, filename + ext);
return EnableOutputInSubFolder ? Path.Combine(folder, filename, filename + ext) : Path.Combine(folder, filename + ext);
}
protected virtual string GetDefaultEncoderPreset()
@ -383,195 +378,215 @@ namespace MediaBrowser.Api.Playback
continue;
}
if (i == 0)
switch (i)
{
request.DeviceProfileId = val;
}
else if (i == 1)
{
request.DeviceId = val;
}
else if (i == 2)
{
request.MediaSourceId = val;
}
else if (i == 3)
{
request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
else if (i == 4)
{
if (videoRequest != null)
case 0:
request.DeviceProfileId = val;
break;
case 1:
request.DeviceId = val;
break;
case 2:
request.MediaSourceId = val;
break;
case 3:
request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
break;
case 4:
{
videoRequest.VideoCodec = val;
}
}
else if (i == 5)
{
request.AudioCodec = val;
}
else if (i == 6)
{
if (videoRequest != null)
{
videoRequest.AudioStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
}
}
else if (i == 7)
{
if (videoRequest != null)
{
videoRequest.SubtitleStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
}
}
else if (i == 8)
{
if (videoRequest != null)
{
videoRequest.VideoBitRate = int.Parse(val, CultureInfo.InvariantCulture);
}
}
else if (i == 9)
{
request.AudioBitRate = int.Parse(val, CultureInfo.InvariantCulture);
}
else if (i == 10)
{
request.MaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
}
else if (i == 11)
{
if (videoRequest != null)
{
videoRequest.MaxFramerate = float.Parse(val, CultureInfo.InvariantCulture);
}
}
else if (i == 12)
{
if (videoRequest != null)
{
videoRequest.MaxWidth = int.Parse(val, CultureInfo.InvariantCulture);
}
}
else if (i == 13)
{
if (videoRequest != null)
{
videoRequest.MaxHeight = int.Parse(val, CultureInfo.InvariantCulture);
}
}
else if (i == 14)
{
request.StartTimeTicks = long.Parse(val, CultureInfo.InvariantCulture);
}
else if (i == 15)
{
if (videoRequest != null)
{
videoRequest.Level = val;
}
}
else if (i == 16)
{
if (videoRequest != null)
{
videoRequest.MaxRefFrames = int.Parse(val, CultureInfo.InvariantCulture);
}
}
else if (i == 17)
{
if (videoRequest != null)
{
videoRequest.MaxVideoBitDepth = int.Parse(val, CultureInfo.InvariantCulture);
}
}
else if (i == 18)
{
if (videoRequest != null)
{
videoRequest.Profile = val;
}
}
else if (i == 19)
{
// cabac no longer used
}
else if (i == 20)
{
request.PlaySessionId = val;
}
else if (i == 21)
{
// api_key
}
else if (i == 22)
{
request.LiveStreamId = val;
}
else if (i == 23)
{
// Duplicating ItemId because of MediaMonkey
}
else if (i == 24)
{
if (videoRequest != null)
{
videoRequest.CopyTimestamps = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
}
else if (i == 25)
{
if (!string.IsNullOrWhiteSpace(val) && videoRequest != null)
{
if (Enum.TryParse(val, out SubtitleDeliveryMethod method))
if (videoRequest != null)
{
videoRequest.SubtitleMethod = method;
videoRequest.VideoCodec = val;
}
break;
}
}
else if (i == 26)
{
request.TranscodingMaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
}
else if (i == 27)
{
if (videoRequest != null)
case 5:
request.AudioCodec = val;
break;
case 6:
{
videoRequest.EnableSubtitlesInManifest = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
if (videoRequest != null)
{
videoRequest.AudioStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
}
break;
}
}
else if (i == 28)
{
request.Tag = val;
}
else if (i == 29)
{
if (videoRequest != null)
case 7:
{
videoRequest.RequireAvc = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
if (videoRequest != null)
{
videoRequest.SubtitleStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
}
break;
}
}
else if (i == 30)
{
request.SubtitleCodec = val;
}
else if (i == 31)
{
if (videoRequest != null)
case 8:
{
videoRequest.RequireNonAnamorphic = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
if (videoRequest != null)
{
videoRequest.VideoBitRate = int.Parse(val, CultureInfo.InvariantCulture);
}
break;
}
}
else if (i == 32)
{
if (videoRequest != null)
case 9:
request.AudioBitRate = int.Parse(val, CultureInfo.InvariantCulture);
break;
case 10:
request.MaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
break;
case 11:
{
videoRequest.DeInterlace = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
if (videoRequest != null)
{
videoRequest.MaxFramerate = float.Parse(val, CultureInfo.InvariantCulture);
}
break;
}
}
else if (i == 33)
{
request.TranscodeReasons = val;
case 12:
{
if (videoRequest != null)
{
videoRequest.MaxWidth = int.Parse(val, CultureInfo.InvariantCulture);
}
break;
}
case 13:
{
if (videoRequest != null)
{
videoRequest.MaxHeight = int.Parse(val, CultureInfo.InvariantCulture);
}
break;
}
case 14:
request.StartTimeTicks = long.Parse(val, CultureInfo.InvariantCulture);
break;
case 15:
{
if (videoRequest != null)
{
videoRequest.Level = val;
}
break;
}
case 16:
{
if (videoRequest != null)
{
videoRequest.MaxRefFrames = int.Parse(val, CultureInfo.InvariantCulture);
}
break;
}
case 17:
{
if (videoRequest != null)
{
videoRequest.MaxVideoBitDepth = int.Parse(val, CultureInfo.InvariantCulture);
}
break;
}
case 18:
{
if (videoRequest != null)
{
videoRequest.Profile = val;
}
break;
}
case 19:
// cabac no longer used
break;
case 20:
request.PlaySessionId = val;
break;
case 21:
// api_key
break;
case 22:
request.LiveStreamId = val;
break;
case 23:
// Duplicating ItemId because of MediaMonkey
break;
case 24:
{
if (videoRequest != null)
{
videoRequest.CopyTimestamps = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
break;
}
case 25:
{
if (!string.IsNullOrWhiteSpace(val) && videoRequest != null)
{
if (Enum.TryParse(val, out SubtitleDeliveryMethod method))
{
videoRequest.SubtitleMethod = method;
}
}
break;
}
case 26:
request.TranscodingMaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
break;
case 27:
{
if (videoRequest != null)
{
videoRequest.EnableSubtitlesInManifest = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
break;
}
case 28:
request.Tag = val;
break;
case 29:
{
if (videoRequest != null)
{
videoRequest.RequireAvc = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
break;
}
case 30:
request.SubtitleCodec = val;
break;
case 31:
{
if (videoRequest != null)
{
videoRequest.RequireNonAnamorphic = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
break;
}
case 32:
{
if (videoRequest != null)
{
videoRequest.DeInterlace = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
break;
}
case 33:
request.TranscodeReasons = val;
break;
}
}
}
@ -624,14 +639,9 @@ namespace MediaBrowser.Api.Playback
throw new ArgumentException("Invalid timeseek header");
}
int index = value.IndexOf('-');
if (index == -1)
{
value = value.Substring(Npt.Length);
}
else
{
value = value.Substring(Npt.Length, index - Npt.Length);
}
value = index == -1
? value.Substring(Npt.Length)
: value.Substring(Npt.Length, index - Npt.Length);
if (value.IndexOf(':') == -1)
{

View File

@ -662,17 +662,9 @@ namespace MediaBrowser.Api.Playback
};
}).ThenBy(i =>
{
if (maxBitrate.HasValue)
if (maxBitrate.HasValue && i.Bitrate.HasValue)
{
if (i.Bitrate.HasValue)
{
if (i.Bitrate.Value <= maxBitrate.Value)
{
return 0;
}
return 2;
}
return i.Bitrate.Value <= maxBitrate.Value ? 0 : 2;
}
return 1;

View File

@ -234,46 +234,44 @@ namespace MediaBrowser.Api
SetThumbImageInfo(result, item);
SetBackdropImageInfo(result, item);
if (item is LiveTvProgram program)
switch (item)
{
result.StartDate = program.StartDate;
}
if (item is IHasSeries hasSeries)
{
result.Series = hasSeries.SeriesName;
}
if (item is Series series)
{
if (series.Status.HasValue)
case IHasSeries hasSeries:
result.Series = hasSeries.SeriesName;
break;
case LiveTvProgram program:
result.StartDate = program.StartDate;
break;
case Series series:
{
result.Status = series.Status.Value.ToString();
if (series.Status.HasValue)
{
result.Status = series.Status.Value.ToString();
}
break;
}
}
case MusicAlbum album:
result.Artists = album.Artists;
result.AlbumArtist = album.AlbumArtist;
break;
case Audio song:
result.AlbumArtist = song.AlbumArtists.FirstOrDefault();
result.Artists = song.Artists;
if (item is MusicAlbum album)
{
result.Artists = album.Artists;
result.AlbumArtist = album.AlbumArtist;
}
MusicAlbum musicAlbum = song.AlbumEntity;
if (item is Audio song)
{
result.AlbumArtist = song.AlbumArtists.FirstOrDefault();
result.Artists = song.Artists;
if (musicAlbum != null)
{
result.Album = musicAlbum.Name;
result.AlbumId = musicAlbum.Id;
}
else
{
result.Album = song.Album;
}
album = song.AlbumEntity;
if (album != null)
{
result.Album = album.Name;
result.AlbumId = album.Id;
}
else
{
result.Album = song.Album;
}
break;
}
if (!item.ChannelId.Equals(Guid.Empty))
@ -289,12 +287,9 @@ namespace MediaBrowser.Api
{
var itemWithImage = item.HasImage(ImageType.Thumb) ? item : null;
if (itemWithImage == null)
if (itemWithImage == null && item is Episode)
{
if (item is Episode)
{
itemWithImage = GetParentWithImage<Series>(item, ImageType.Thumb);
}
itemWithImage = GetParentWithImage<Series>(item, ImageType.Thumb);
}
if (itemWithImage == null)

View File

@ -168,12 +168,8 @@ namespace MediaBrowser.Api.System
.First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
// For older files, assume fully static
if (file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1))
{
return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.Read);
}
return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.ReadWrite);
return ResultFactory.GetStaticFileResult(Request, file.FullName,
file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite);
}
/// <summary>

View File

@ -126,12 +126,7 @@ namespace MediaBrowser.Api.UserLibrary
protected override QueryResult<(BaseItem, ItemCounts)> GetItems(GetItemsByName request, InternalItemsQuery query)
{
if (request is GetAlbumArtists)
{
return LibraryManager.GetAlbumArtists(query);
}
return LibraryManager.GetArtists(query);
return request is GetAlbumArtists ? LibraryManager.GetAlbumArtists(query) : LibraryManager.GetArtists(query);
}
/// <summary>

View File

@ -396,12 +396,10 @@ namespace MediaBrowser.Api.UserLibrary
public VideoType[] GetVideoTypes()
{
if (string.IsNullOrEmpty(VideoTypes))
{
return Array.Empty<VideoType>();
}
return VideoTypes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(v => (VideoType)Enum.Parse(typeof(VideoType), v, true)).ToArray();
return string.IsNullOrEmpty(VideoTypes)
? Array.Empty<VideoType>()
: VideoTypes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(v => (VideoType)Enum.Parse(typeof(VideoType), v, true)).ToArray();
}
/// <summary>
@ -412,12 +410,10 @@ namespace MediaBrowser.Api.UserLibrary
{
var val = Filters;
if (string.IsNullOrEmpty(val))
{
return new ItemFilter[] { };
}
return val.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(v => (ItemFilter)Enum.Parse(typeof(ItemFilter), v, true)).ToArray();
return string.IsNullOrEmpty(val)
? new ItemFilter[] { }
: val.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).
Select(v => (ItemFilter)Enum.Parse(typeof(ItemFilter), v, true)).ToArray();
}
/// <summary>
@ -428,12 +424,9 @@ namespace MediaBrowser.Api.UserLibrary
{
var val = ImageTypes;
if (string.IsNullOrEmpty(val))
{
return new ImageType[] { };
}
return val.Split(',').Select(v => (ImageType)Enum.Parse(typeof(ImageType), v, true)).ToArray();
return string.IsNullOrEmpty(val)
? new ImageType[] { }
: val.Split(',').Select(v => (ImageType)Enum.Parse(typeof(ImageType), v, true)).ToArray();
}
/// <summary>