Browse Source

Merge branch 'master' into using-declarations

Patrick Barron 4 years ago
parent
commit
e55f35b62e
100 changed files with 1320 additions and 1045 deletions
  1. 0 59
      .ci/azure-pipelines-api-client.yml
  2. 0 4
      .ci/azure-pipelines-package.yml
  3. 1 1
      .ci/azure-pipelines-test.yml
  4. 0 3
      .ci/azure-pipelines.yml
  5. 7 1
      .github/dependabot.yml
  6. 64 0
      .github/workflows/automation.yml
  7. 43 0
      .github/workflows/label-commenter-config.yml
  8. 22 0
      .github/workflows/label-commenter.yml
  9. 17 0
      .github/workflows/merge-conflicts.yml
  10. 27 0
      .github/workflows/rebase.yml
  11. 3 0
      CONTRIBUTORS.md
  12. 2 2
      Dockerfile
  13. 1 1
      Dockerfile.arm
  14. 1 1
      Dockerfile.arm64
  15. 2 0
      Emby.Dlna/Configuration/DlnaOptions.cs
  16. 0 1
      Emby.Dlna/ConfigurationExtension.cs
  17. 1 1
      Emby.Dlna/ConnectionManager/ControlHandler.cs
  18. 2 0
      Emby.Dlna/ContentDirectory/ContentDirectoryService.cs
  19. 16 33
      Emby.Dlna/ContentDirectory/ControlHandler.cs
  20. 2 0
      Emby.Dlna/ControlRequest.cs
  21. 2 0
      Emby.Dlna/ControlResponse.cs
  22. 26 11
      Emby.Dlna/Didl/DidlBuilder.cs
  23. 1 1
      Emby.Dlna/Didl/StringWriterWithEncoding.cs
  24. 0 1
      Emby.Dlna/DlnaConfigurationFactory.cs
  25. 49 76
      Emby.Dlna/DlnaManager.cs
  26. 1 1
      Emby.Dlna/Emby.Dlna.csproj
  27. 2 0
      Emby.Dlna/EventSubscriptionResponse.cs
  28. 2 0
      Emby.Dlna/Eventing/DlnaEventManager.cs
  29. 2 0
      Emby.Dlna/Eventing/EventSubscription.cs
  30. 10 5
      Emby.Dlna/Main/DlnaEntryPoint.cs
  31. 1 1
      Emby.Dlna/MediaReceiverRegistrar/ControlHandler.cs
  32. 0 1
      Emby.Dlna/MediaReceiverRegistrar/MediaReceiverRegistrarXmlBuilder.cs
  33. 35 8
      Emby.Dlna/PlayTo/Device.cs
  34. 2 0
      Emby.Dlna/PlayTo/DeviceInfo.cs
  35. 2 0
      Emby.Dlna/PlayTo/MediaChangedEventArgs.cs
  36. 8 10
      Emby.Dlna/PlayTo/PlayToController.cs
  37. 8 1
      Emby.Dlna/PlayTo/PlayToManager.cs
  38. 2 0
      Emby.Dlna/PlayTo/PlaybackProgressEventArgs.cs
  39. 2 0
      Emby.Dlna/PlayTo/PlaybackStartEventArgs.cs
  40. 2 0
      Emby.Dlna/PlayTo/PlaybackStoppedEventArgs.cs
  41. 2 0
      Emby.Dlna/PlayTo/PlaylistItem.cs
  42. 2 0
      Emby.Dlna/PlayTo/PlaylistItemFactory.cs
  43. 17 9
      Emby.Dlna/PlayTo/SsdpHttpClient.cs
  44. 9 11
      Emby.Dlna/PlayTo/TransportCommands.cs
  45. 2 0
      Emby.Dlna/PlayTo/uBaseObject.cs
  46. 3 0
      Emby.Dlna/Profiles/DefaultProfile.cs
  47. 2 1
      Emby.Dlna/Server/DescriptionXmlBuilder.cs
  48. 3 3
      Emby.Dlna/Service/BaseControlHandler.cs
  49. 4 2
      Emby.Dlna/Ssdp/DeviceDiscovery.cs
  50. 3 3
      Emby.Dlna/Ssdp/SsdpExtensions.cs
  51. 0 1
      Emby.Drawing/Emby.Drawing.csproj
  52. 102 23
      Emby.Drawing/ImageProcessor.cs
  53. 1 1
      Emby.Drawing/NullImageEncoder.cs
  54. 0 1
      Emby.Naming/Emby.Naming.csproj
  55. 5 0
      Emby.Naming/TV/EpisodeResolver.cs
  56. 9 2
      Emby.Naming/Video/CleanStringParser.cs
  57. 8 7
      Emby.Naming/Video/VideoListResolver.cs
  58. 2 1
      Emby.Naming/Video/VideoResolver.cs
  59. 2 5
      Emby.Notifications/Emby.Notifications.csproj
  60. 2 5
      Emby.Photos/Emby.Photos.csproj
  61. 2 2
      Emby.Server.Implementations/AppBase/ConfigurationHelper.cs
  62. 65 73
      Emby.Server.Implementations/ApplicationHost.cs
  63. 1 2
      Emby.Server.Implementations/Channels/ChannelManager.cs
  64. 35 14
      Emby.Server.Implementations/Collections/CollectionManager.cs
  65. 0 1
      Emby.Server.Implementations/ConfigurationOptions.cs
  66. 198 201
      Emby.Server.Implementations/Data/SqliteItemRepository.cs
  67. 3 12
      Emby.Server.Implementations/Dto/DtoService.cs
  68. 6 8
      Emby.Server.Implementations/Emby.Server.Implementations.csproj
  69. 11 0
      Emby.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs
  70. 0 1
      Emby.Server.Implementations/HttpServer/Security/AuthService.cs
  71. 1 1
      Emby.Server.Implementations/HttpServer/Security/SessionContext.cs
  72. 1 1
      Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
  73. 4 0
      Emby.Server.Implementations/HttpServer/WebSocketManager.cs
  74. 49 53
      Emby.Server.Implementations/IO/ManagedFileSystem.cs
  75. 6 7
      Emby.Server.Implementations/IStartupOptions.cs
  76. 0 8
      Emby.Server.Implementations/Images/ArtistImageProvider.cs
  77. 0 1
      Emby.Server.Implementations/Images/DynamicImageProvider.cs
  78. 1 3
      Emby.Server.Implementations/Images/PlaylistImageProvider.cs
  79. 111 83
      Emby.Server.Implementations/Library/LibraryManager.cs
  80. 1 1
      Emby.Server.Implementations/Library/LiveStreamHelper.cs
  81. 12 15
      Emby.Server.Implementations/Library/MediaSourceManager.cs
  82. 1 2
      Emby.Server.Implementations/Library/MusicManager.cs
  83. 71 3
      Emby.Server.Implementations/Library/PathExtensions.cs
  84. 21 43
      Emby.Server.Implementations/Library/ResolverHelper.cs
  85. 5 0
      Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs
  86. 2 2
      Emby.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs
  87. 1 1
      Emby.Server.Implementations/Library/Resolvers/Books/BookResolver.cs
  88. 6 6
      Emby.Server.Implementations/Library/Resolvers/ItemResolver.cs
  89. 105 105
      Emby.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs
  90. 6 6
      Emby.Server.Implementations/Library/Resolvers/PhotoAlbumResolver.cs
  91. 1 1
      Emby.Server.Implementations/Library/Resolvers/PhotoResolver.cs
  92. 2 1
      Emby.Server.Implementations/Library/Resolvers/PlaylistResolver.cs
  93. 19 25
      Emby.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs
  94. 1 1
      Emby.Server.Implementations/Library/Resolvers/TV/SeasonResolver.cs
  95. 4 37
      Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs
  96. 0 1
      Emby.Server.Implementations/Library/SearchEngine.cs
  97. 5 5
      Emby.Server.Implementations/Library/UserDataManager.cs
  98. 0 1
      Emby.Server.Implementations/Library/UserViewManager.cs
  99. 4 2
      Emby.Server.Implementations/LiveTv/EmbyTV/DirectRecorder.cs
  100. 13 18
      Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs

+ 0 - 59
.ci/azure-pipelines-api-client.yml

@@ -1,59 +0,0 @@
-parameters:
-  - name: LinuxImage
-    type: string
-    default: "ubuntu-latest"
-  - name: GeneratorVersion
-    type: string
-    default: "5.0.1"
-
-jobs:
-- job: GenerateApiClients
-  displayName: 'Generate Api Clients'
-  condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
-  dependsOn: Test
-
-  pool:
-    vmImage: "${{ parameters.LinuxImage }}"
-
-  steps:
-    - task: DownloadPipelineArtifact@2
-      displayName: 'Download OpenAPI Spec Artifact'
-      inputs:
-        source: 'current'
-        artifact: "OpenAPI Spec"
-        path: "$(System.ArtifactsDirectory)/openapispec"
-        runVersion: "latest"
-
-    - task: CmdLine@2
-      displayName: 'Download OpenApi Generator'
-      inputs:
-        script: "wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${{ parameters.GeneratorVersion }}/openapi-generator-cli-${{ parameters.GeneratorVersion }}.jar -O openapi-generator-cli.jar"
-
-## Authenticate with npm registry
-    - task: npmAuthenticate@0
-      inputs:
-        workingFile: ./.npmrc
-        customEndpoint: 'jellyfin-bot for NPM'
-
-## Generate npm api client
-    - task: CmdLine@2
-      displayName: 'Build stable typescript axios client'
-      inputs:
-        script: "bash ./apiclient/templates/typescript/axios/generate.sh $(System.ArtifactsDirectory)"
-
-## Run npm install
-    - task: Npm@1
-      displayName: 'Install npm dependencies'
-      inputs:
-        command: install
-        workingDir: ./apiclient/generated/typescript/axios
-
-## Publish npm packages
-    - task: Npm@1
-      displayName: 'Publish stable typescript axios client'
-      inputs:
-        command: custom
-        customCommand: publish --access public
-        publishRegistry: useExternalRegistry
-        publishEndpoint: 'jellyfin-bot for NPM'
-        workingDir: ./apiclient/generated/typescript/axios

+ 0 - 4
.ci/azure-pipelines-package.yml

@@ -160,7 +160,6 @@ jobs:
   dependsOn:
   - BuildPackage
   - BuildDocker
-  condition: and(succeeded('BuildPackage'), succeeded('BuildDocker'))
 
   pool:
     vmImage: 'ubuntu-latest'
@@ -186,9 +185,6 @@ jobs:
 
 - job: PublishNuget
   displayName: 'Publish NuGet packages'
-  dependsOn:
-  - BuildPackage
-  condition: succeeded('BuildPackage')
 
   pool:
     vmImage: 'ubuntu-latest'

+ 1 - 1
.ci/azure-pipelines-test.yml

@@ -94,5 +94,5 @@ jobs:
         displayName: 'Publish OpenAPI Artifact'
         condition: and(succeeded(), eq(variables['Agent.OS'], 'Linux'))
         inputs:
-          targetPath: "tests/Jellyfin.Api.Tests/bin/Release/net5.0/openapi.json"
+          targetPath: "tests/Jellyfin.Server.Integration.Tests/bin/Release/net5.0/openapi.json"
           artifactName: 'OpenAPI Spec'

+ 0 - 3
.ci/azure-pipelines.yml

@@ -61,6 +61,3 @@ jobs:
 
 - ${{ if or(startsWith(variables['Build.SourceBranch'], 'refs/tags/v'), startsWith(variables['Build.SourceBranch'], 'refs/heads/master')) }}:
   - template: azure-pipelines-package.yml
-
-- ${{ if or(startsWith(variables['Build.SourceBranch'], 'refs/tags/v'), startsWith(variables['Build.SourceBranch'], 'refs/heads/master')) }}:
-  - template: azure-pipelines-api-client.yml

+ 7 - 1
.github/dependabot.yml

@@ -6,4 +6,10 @@ updates:
     interval: weekly
     time: '12:00'
   open-pull-requests-limit: 10
-  
+
+- package-ecosystem: github-actions
+  directory: '/'
+  schedule:
+    interval: weekly
+    time: '12:00'
+  open-pull-requests-limit: 10

+ 64 - 0
.github/workflows/automation.yml

@@ -0,0 +1,64 @@
+name: Automation
+
+on:
+  pull_request:
+
+jobs:
+  main:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Does PR has the stable backport label?
+        uses: Dreamcodeio/does-pr-has-label@v1.2
+        id: checkLabel
+        with:
+          label: stable backport
+
+      - name: Remove from 'Current Release' project
+        uses: alex-page/github-project-automation-plus@v0.7.1
+        if: (github.event.pull_request || github.event.issue.pull_request) && !steps.checkLabel.outputs.hasLabel
+        continue-on-error: true
+        with:
+          project: Current Release
+          action: delete
+          repo-token: ${{ secrets.GH_TOKEN }}
+
+      - name: Add to 'Release Next' project
+        uses: alex-page/github-project-automation-plus@v0.7.1
+        if: (github.event.pull_request || github.event.issue.pull_request) && github.event.action == 'opened'
+        continue-on-error: true
+        with:
+          project: Release Next
+          column: In progress
+          repo-token: ${{ secrets.GH_TOKEN }}
+
+      - name: Add to 'Current Release' project
+        uses: alex-page/github-project-automation-plus@v0.7.1
+        if: (github.event.pull_request || github.event.issue.pull_request) && steps.checkLabel.outputs.hasLabel
+        continue-on-error: true
+        with:
+          project: Current Release
+          column: In progress
+          repo-token: ${{ secrets.GH_TOKEN }}
+
+      - name: Check number of comments from the team member
+        if: github.event.issue.pull_request == '' && github.event.comment.author_association == 'MEMBER'
+        id: member_comments
+        run: echo "::set-output name=number::$(curl -s ${{ github.event.issue.comments_url }} | jq '.[] | select(.author_association == "MEMBER") | .author_association' | wc -l)"
+
+      - name: Move issue to needs triage
+        uses: alex-page/github-project-automation-plus@v0.7.1
+        if: github.event.issue.pull_request == '' && github.event.comment.author_association == 'MEMBER' && steps.member_comments.outputs.number <= 1
+        continue-on-error: true
+        with:
+          project: Issue Triage for Main Repo
+          column: Needs triage
+          repo-token: ${{ secrets.GH_TOKEN }}
+
+      - name: Add issue to triage project
+        uses: alex-page/github-project-automation-plus@v0.7.1
+        if: github.event.issue.pull_request == '' && github.event.action == 'opened'
+        continue-on-error: true
+        with:
+          project: Issue Triage for Main Repo
+          column: Pending response
+          repo-token: ${{ secrets.GH_TOKEN }}

+ 43 - 0
.github/workflows/label-commenter-config.yml

@@ -0,0 +1,43 @@
+comment:
+  header: Hello @{{ issue.user.login }}
+  footer: "\
+    ---\n\n
+    > This is an automated comment created by the [peaceiris/actions-label-commenter]. \
+    Responding to the bot or mentioning it won't have any effect.\n\n
+    [peaceiris/actions-label-commenter]: https://github.com/peaceiris/actions-label-commenter
+    "
+
+labels:
+  - name: stable backport
+    labeled:
+      pr:
+        body: |
+          This pull request has been tagged as a stable backport. It will be cherry-picked into the next stable point release.
+
+          Please observe the following:
+
+            * Any dependent PRs that this PR requires **must** be tagged for stable backporting as well.
+
+            * Any issue(s) this PR fixes or closes **should** target the current stable release or a previous stable release to which a fix has not yet entered the current stable release.
+          
+            * This PR **must** be test cherry-picked against the current release branch (`release-X.Y.z` where X and Y are numbers). It must apply cleanly, or a diff of the expected change must be provided.
+              
+              To do this, run the following commands from your local copy of the Jellyfin repository:
+              
+                1. `git checkout master`
+
+                1. `git merge --no-ff <myPullRequestBranch>`
+
+                1. `git log` -> `commit xxxxxxxxx`, grab hash
+
+                1. `git checkout release-X.Y.z` replacing X and Y with the *current* stable version (e.g. `release-10.7.z`)
+
+                1. `git cherry-pick -sx -m1 <hash>`
+
+              Ensure the `cherry-pick` applies cleanly. If it does not, fix any merge conflicts *preserving as much of the original code as possible*, and make note of the resulting diff.
+
+              Test your changes with a build to ensure they are successful. If not, adjust the diff accordingly.
+
+              **Do not** push your merges to either branch. Use `git reset --hard HEAD~1` to revert both branches to their original state.
+
+              Reply to this PR with a comment beginning "Cherry-pick test completed." and including the merge-conflict-fixing diff(s) if applicable.

+ 22 - 0
.github/workflows/label-commenter.yml

@@ -0,0 +1,22 @@
+name: Label Commenter
+
+on:
+  issues:
+    types:
+      - labeled
+      - unlabeled
+  pull_request_target:
+    types:
+      - labeled
+      - unlabeled
+
+jobs:
+  comment:
+    runs-on: ubuntu-20.04
+    steps:
+      - uses: actions/checkout@v2
+        with:
+          ref: master
+
+      - name: Label Commenter
+        uses: peaceiris/actions-label-commenter@v1

+ 17 - 0
.github/workflows/merge-conflicts.yml

@@ -0,0 +1,17 @@
+name: 'Merge Conflicts'
+
+on:
+  push:
+    branches:
+      - master
+  pull_request_target:
+    types:
+      - synchronize
+jobs:
+  triage:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: eps1lon/actions-label-merge-conflict@v2.0.1
+        with:
+          dirtyLabel: 'merge conflict'
+          repoToken: ${{ secrets.GH_TOKEN }}

+ 27 - 0
.github/workflows/rebase.yml

@@ -0,0 +1,27 @@
+name: Automatic Rebase
+on:
+  issue_comment:
+
+jobs:
+  rebase:
+    name: Rebase
+    if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '@jellyfin-bot rebase') && github.event.comment.author_association == 'MEMBER'
+    runs-on: ubuntu-latest
+    steps:
+      - name: Notify as seen
+        uses: peter-evans/create-or-update-comment@v1.4.5
+        with:
+          token: ${{ secrets.GH_TOKEN }}
+          comment-id: ${{ github.event.comment.id }}
+          reactions: '+1'
+
+      - name: Checkout the latest code
+        uses: actions/checkout@v2
+        with:
+          token: ${{ secrets.GH_TOKEN }}
+          fetch-depth: 0
+
+      - name: Automatic Rebase
+        uses: cirrus-actions/rebase@1.4
+        env:
+          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

+ 3 - 0
CONTRIBUTORS.md

@@ -17,6 +17,7 @@
  - [bugfixin](https://github.com/bugfixin)
  - [chaosinnovator](https://github.com/chaosinnovator)
  - [ckcr4lyf](https://github.com/ckcr4lyf)
+ - [cocool97](https://github.com/cocool97)
  - [ConfusedPolarBear](https://github.com/ConfusedPolarBear)
  - [crankdoofus](https://github.com/crankdoofus)
  - [crobibero](https://github.com/crobibero)
@@ -49,6 +50,7 @@
  - [h1nk](https://github.com/h1nk)
  - [hawken93](https://github.com/hawken93)
  - [HelloWorld017](https://github.com/HelloWorld017)
+ - [ikomhoog](https://github.com/ikomhoog)
  - [jftuga](https://github.com/jftuga)
  - [joern-h](https://github.com/joern-h)
  - [joshuaboniface](https://github.com/joshuaboniface)
@@ -104,6 +106,7 @@
  - [shemanaev](https://github.com/shemanaev)
  - [skaro13](https://github.com/skaro13)
  - [sl1288](https://github.com/sl1288)
+ - [Smith00101010](https://github.com/Smith00101010)
  - [sorinyo2004](https://github.com/sorinyo2004)
  - [sparky8251](https://github.com/sparky8251)
  - [spookbits](https://github.com/spookbits)

+ 2 - 2
Dockerfile

@@ -5,10 +5,10 @@ ARG JELLYFIN_WEB_VERSION=master
 RUN apk add curl git zlib zlib-dev autoconf g++ make libpng-dev gifsicle alpine-sdk automake libtool make gcc musl-dev nasm python \
  && curl -L https://github.com/jellyfin/jellyfin-web/archive/${JELLYFIN_WEB_VERSION}.tar.gz | tar zxf - \
  && cd jellyfin-web-* \
- && yarn install \
+ && npm ci --no-audit \
  && mv dist /dist
 
-FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION}-buster-slim as builder
+FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} as builder
 WORKDIR /repo
 COPY . .
 ENV DOTNET_CLI_TELEMETRY_OPTOUT=1

+ 1 - 1
Dockerfile.arm

@@ -10,7 +10,7 @@ ARG JELLYFIN_WEB_VERSION=master
 RUN apk add curl git zlib zlib-dev autoconf g++ make libpng-dev gifsicle alpine-sdk automake libtool make gcc musl-dev nasm python \
  && curl -L https://github.com/jellyfin/jellyfin-web/archive/${JELLYFIN_WEB_VERSION}.tar.gz | tar zxf - \
  && cd jellyfin-web-* \
- && yarn install \
+ && npm ci --no-audit \
  && mv dist /dist
 
 

+ 1 - 1
Dockerfile.arm64

@@ -10,7 +10,7 @@ ARG JELLYFIN_WEB_VERSION=master
 RUN apk add curl git zlib zlib-dev autoconf g++ make libpng-dev gifsicle alpine-sdk automake libtool make gcc musl-dev nasm python \
  && curl -L https://github.com/jellyfin/jellyfin-web/archive/${JELLYFIN_WEB_VERSION}.tar.gz | tar zxf - \
  && cd jellyfin-web-* \
- && yarn install \
+ && npm ci --no-audit \
  && mv dist /dist
 
 

+ 2 - 0
Emby.Dlna/Configuration/DlnaOptions.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 namespace Emby.Dlna.Configuration

+ 0 - 1
Emby.Dlna/ConfigurationExtension.cs

@@ -1,4 +1,3 @@
-#nullable enable
 #pragma warning disable CS1591
 
 using Emby.Dlna.Configuration;

+ 1 - 1
Emby.Dlna/ConnectionManager/ControlHandler.cs

@@ -31,7 +31,7 @@ namespace Emby.Dlna.ConnectionManager
         }
 
         /// <inheritdoc />
-        protected override void WriteResult(string methodName, IDictionary<string, string> methodParams, XmlWriter xmlWriter)
+        protected override void WriteResult(string methodName, IReadOnlyDictionary<string, string> methodParams, XmlWriter xmlWriter)
         {
             if (string.Equals(methodName, "GetProtocolInfo", StringComparison.OrdinalIgnoreCase))
             {

+ 2 - 0
Emby.Dlna/ContentDirectory/ContentDirectoryService.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;

+ 16 - 33
Emby.Dlna/ContentDirectory/ControlHandler.cs

@@ -1,5 +1,6 @@
+#nullable disable
+
 using System;
-using System.Collections;
 using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
@@ -7,7 +8,6 @@ using System.Linq;
 using System.Text;
 using System.Threading;
 using System.Xml;
-using Emby.Dlna.Configuration;
 using Emby.Dlna.Didl;
 using Emby.Dlna.Service;
 using Jellyfin.Data.Entities;
@@ -121,7 +121,7 @@ namespace Emby.Dlna.ContentDirectory
         }
 
         /// <inheritdoc />
-        protected override void WriteResult(string methodName, IDictionary<string, string> methodParams, XmlWriter xmlWriter)
+        protected override void WriteResult(string methodName, IReadOnlyDictionary<string, string> methodParams, XmlWriter xmlWriter)
         {
             if (xmlWriter == null)
             {
@@ -201,8 +201,8 @@ namespace Emby.Dlna.ContentDirectory
         /// <summary>
         /// Adds a "XSetBookmark" element to the xml document.
         /// </summary>
-        /// <param name="sparams">The <see cref="IDictionary"/>.</param>
-        private void HandleXSetBookmark(IDictionary<string, string> sparams)
+        /// <param name="sparams">The method parameters.</param>
+        private void HandleXSetBookmark(IReadOnlyDictionary<string, string> sparams)
         {
             var id = sparams["ObjectID"];
 
@@ -305,35 +305,18 @@ namespace Emby.Dlna.ContentDirectory
             return builder.ToString();
         }
 
-        /// <summary>
-        /// Returns the value in the key of the dictionary, or defaultValue if it doesn't exist.
-        /// </summary>
-        /// <param name="sparams">The <see cref="IDictionary"/>.</param>
-        /// <param name="key">The key.</param>
-        /// <param name="defaultValue">The defaultValue.</param>
-        /// <returns>The <see cref="string"/>.</returns>
-        public static string GetValueOrDefault(IDictionary<string, string> sparams, string key, string defaultValue)
-        {
-            if (sparams != null && sparams.TryGetValue(key, out string val))
-            {
-                return val;
-            }
-
-            return defaultValue;
-        }
-
         /// <summary>
         /// Builds the "Browse" xml response.
         /// </summary>
         /// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
-        /// <param name="sparams">The <see cref="IDictionary"/>.</param>
+        /// <param name="sparams">The method parameters.</param>
         /// <param name="deviceId">The device Id to use.</param>
-        private void HandleBrowse(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
+        private void HandleBrowse(XmlWriter xmlWriter, IReadOnlyDictionary<string, string> sparams, string deviceId)
         {
             var id = sparams["ObjectID"];
             var flag = sparams["BrowseFlag"];
-            var filter = new Filter(GetValueOrDefault(sparams, "Filter", "*"));
-            var sortCriteria = new SortCriteria(GetValueOrDefault(sparams, "SortCriteria", string.Empty));
+            var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
+            var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", string.Empty));
 
             var provided = 0;
 
@@ -435,9 +418,9 @@ namespace Emby.Dlna.ContentDirectory
         /// Builds the response to the "X_BrowseByLetter request.
         /// </summary>
         /// <param name="xmlWriter">The <see cref="XmlWriter"/>.</param>
-        /// <param name="sparams">The <see cref="IDictionary"/>.</param>
+        /// <param name="sparams">The method parameters.</param>
         /// <param name="deviceId">The device id.</param>
-        private void HandleXBrowseByLetter(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
+        private void HandleXBrowseByLetter(XmlWriter xmlWriter, IReadOnlyDictionary<string, string> sparams, string deviceId)
         {
             // TODO: Implement this method
             HandleSearch(xmlWriter, sparams, deviceId);
@@ -447,13 +430,13 @@ namespace Emby.Dlna.ContentDirectory
         /// Builds a response to the "Search" request.
         /// </summary>
         /// <param name="xmlWriter">The xmlWriter<see cref="XmlWriter"/>.</param>
-        /// <param name="sparams">The sparams<see cref="IDictionary"/>.</param>
+        /// <param name="sparams">The method parameters.</param>
         /// <param name="deviceId">The deviceId<see cref="string"/>.</param>
-        private void HandleSearch(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
+        private void HandleSearch(XmlWriter xmlWriter, IReadOnlyDictionary<string, string> sparams, string deviceId)
         {
-            var searchCriteria = new SearchCriteria(GetValueOrDefault(sparams, "SearchCriteria", string.Empty));
-            var sortCriteria = new SortCriteria(GetValueOrDefault(sparams, "SortCriteria", string.Empty));
-            var filter = new Filter(GetValueOrDefault(sparams, "Filter", "*"));
+            var searchCriteria = new SearchCriteria(sparams.GetValueOrDefault("SearchCriteria", string.Empty));
+            var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", string.Empty));
+            var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
 
             // sort example: dc:title, dc:date
 

+ 2 - 0
Emby.Dlna/ControlRequest.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System.IO;

+ 2 - 0
Emby.Dlna/ControlResponse.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System.Collections.Generic;

+ 26 - 11
Emby.Dlna/Didl/DidlBuilder.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;
@@ -208,7 +210,8 @@ namespace Emby.Dlna.Didl
             var targetWidth = streamInfo.TargetWidth;
             var targetHeight = streamInfo.TargetHeight;
 
-            var contentFeatureList = new ContentFeatureBuilder(_profile).BuildVideoHeader(
+            var contentFeatureList = ContentFeatureBuilder.BuildVideoHeader(
+                _profile,
                 streamInfo.Container,
                 streamInfo.TargetVideoCodec.FirstOrDefault(),
                 streamInfo.TargetAudioCodec.FirstOrDefault(),
@@ -599,7 +602,8 @@ namespace Emby.Dlna.Didl
                 ? MimeTypes.GetMimeType(filename)
                 : mediaProfile.MimeType;
 
-            var contentFeatures = new ContentFeatureBuilder(_profile).BuildAudioHeader(
+            var contentFeatures = ContentFeatureBuilder.BuildAudioHeader(
+                _profile,
                 streamInfo.Container,
                 streamInfo.TargetAudioCodec.FirstOrDefault(),
                 targetAudioBitrate,
@@ -974,15 +978,28 @@ namespace Emby.Dlna.Didl
                 return;
             }
 
-            var albumartUrlInfo = GetImageUrl(imageInfo, _profile.MaxAlbumArtWidth, _profile.MaxAlbumArtHeight, "jpg");
+            // TODO: Remove these default values
+            var albumArtUrlInfo = GetImageUrl(
+                imageInfo,
+                _profile.MaxAlbumArtWidth ?? 10000,
+                _profile.MaxAlbumArtHeight ?? 10000,
+                "jpg");
 
             writer.WriteStartElement("upnp", "albumArtURI", NsUpnp);
-            writer.WriteAttributeString("dlna", "profileID", NsDlna, _profile.AlbumArtPn);
-            writer.WriteString(albumartUrlInfo.url);
+            if (!string.IsNullOrEmpty(_profile.AlbumArtPn))
+            {
+                writer.WriteAttributeString("dlna", "profileID", NsDlna, _profile.AlbumArtPn);
+            }
+
+            writer.WriteString(albumArtUrlInfo.url);
             writer.WriteFullEndElement();
 
-            // TOOD: Remove these default values
-            var iconUrlInfo = GetImageUrl(imageInfo, _profile.MaxIconWidth ?? 48, _profile.MaxIconHeight ?? 48, "jpg");
+            // TODO: Remove these default values
+            var iconUrlInfo = GetImageUrl(
+                imageInfo,
+                _profile.MaxIconWidth ?? 48,
+                _profile.MaxIconHeight ?? 48,
+                "jpg");
             writer.WriteElementString("upnp", "icon", NsUpnp, iconUrlInfo.url);
 
             if (!_profile.EnableAlbumArtInDidl)
@@ -1033,8 +1050,7 @@ namespace Emby.Dlna.Didl
             var width = albumartUrlInfo.width ?? maxWidth;
             var height = albumartUrlInfo.height ?? maxHeight;
 
-            var contentFeatures = new ContentFeatureBuilder(_profile)
-                .BuildImageHeader(format, width, height, imageInfo.IsDirectStream, org_Pn);
+            var contentFeatures = ContentFeatureBuilder.BuildImageHeader(_profile, format, width, height, imageInfo.IsDirectStream, org_Pn);
 
             writer.WriteAttributeString(
                 "protocolInfo",
@@ -1206,8 +1222,7 @@ namespace Emby.Dlna.Didl
 
             if (width.HasValue && height.HasValue)
             {
-                var newSize = DrawingUtils.Resize(
-                        new ImageDimensions(width.Value, height.Value), 0, 0, maxWidth, maxHeight);
+                var newSize = DrawingUtils.Resize(new ImageDimensions(width.Value, height.Value), 0, 0, maxWidth, maxHeight);
 
                 width = newSize.Width;
                 height = newSize.Height;

+ 1 - 1
Emby.Dlna/Didl/StringWriterWithEncoding.cs

@@ -9,7 +9,7 @@ namespace Emby.Dlna.Didl
 {
     public class StringWriterWithEncoding : StringWriter
     {
-        private readonly Encoding _encoding;
+        private readonly Encoding? _encoding;
 
         public StringWriterWithEncoding()
         {

+ 0 - 1
Emby.Dlna/DlnaConfigurationFactory.cs

@@ -1,4 +1,3 @@
-#nullable enable
 #pragma warning disable CS1591
 
 using System.Collections.Generic;

+ 49 - 76
Emby.Dlna/DlnaManager.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;
@@ -36,7 +38,7 @@ namespace Emby.Dlna
         private readonly ILogger<DlnaManager> _logger;
         private readonly IServerApplicationHost _appHost;
         private static readonly Assembly _assembly = typeof(DlnaManager).Assembly;
-        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.GetOptions();
+        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
 
         private readonly Dictionary<string, Tuple<InternalProfileInfo, DeviceProfile>> _profiles = new Dictionary<string, Tuple<InternalProfileInfo, DeviceProfile>>(StringComparer.Ordinal);
 
@@ -111,7 +113,7 @@ namespace Emby.Dlna
 
             if (profile != null)
             {
-                _logger.LogDebug("Found matching device profile: {0}", profile.Name);
+                _logger.LogDebug("Found matching device profile: {ProfileName}", profile.Name);
             }
             else
             {
@@ -126,92 +128,57 @@ namespace Emby.Dlna
             var builder = new StringBuilder();
 
             builder.AppendLine("No matching device profile found. The default will need to be used.");
-            builder.Append("FriendlyName:").AppendLine(profile.FriendlyName);
-            builder.Append("Manufacturer:").AppendLine(profile.Manufacturer);
-            builder.Append("ManufacturerUrl:").AppendLine(profile.ManufacturerUrl);
-            builder.Append("ModelDescription:").AppendLine(profile.ModelDescription);
-            builder.Append("ModelName:").AppendLine(profile.ModelName);
-            builder.Append("ModelNumber:").AppendLine(profile.ModelNumber);
-            builder.Append("ModelUrl:").AppendLine(profile.ModelUrl);
-            builder.Append("SerialNumber:").AppendLine(profile.SerialNumber);
+            builder.Append("FriendlyName: ").AppendLine(profile.FriendlyName);
+            builder.Append("Manufacturer: ").AppendLine(profile.Manufacturer);
+            builder.Append("ManufacturerUrl: ").AppendLine(profile.ManufacturerUrl);
+            builder.Append("ModelDescription: ").AppendLine(profile.ModelDescription);
+            builder.Append("ModelName: ").AppendLine(profile.ModelName);
+            builder.Append("ModelNumber: ").AppendLine(profile.ModelNumber);
+            builder.Append("ModelUrl: ").AppendLine(profile.ModelUrl);
+            builder.Append("SerialNumber: ").AppendLine(profile.SerialNumber);
 
             _logger.LogInformation(builder.ToString());
         }
 
-        private bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
+        /// <summary>
+        /// Attempts to match a device with a profile.
+        /// Rules:
+        /// - If the profile field has no value, the field matches irregardless of its contents.
+        /// - the profile field can be an exact match, or a reg exp.
+        /// </summary>
+        /// <param name="deviceInfo">The <see cref="DeviceIdentification"/> of the device.</param>
+        /// <param name="profileInfo">The <see cref="DeviceIdentification"/> of the profile.</param>
+        /// <returns><b>True</b> if they match.</returns>
+        public bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
         {
-            if (!string.IsNullOrEmpty(profileInfo.FriendlyName))
-            {
-                if (deviceInfo.FriendlyName == null || !IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
-                {
-                    return false;
-                }
-            }
-
-            if (!string.IsNullOrEmpty(profileInfo.Manufacturer))
-            {
-                if (deviceInfo.Manufacturer == null || !IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
-                {
-                    return false;
-                }
-            }
-
-            if (!string.IsNullOrEmpty(profileInfo.ManufacturerUrl))
-            {
-                if (deviceInfo.ManufacturerUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
-                {
-                    return false;
-                }
-            }
-
-            if (!string.IsNullOrEmpty(profileInfo.ModelDescription))
-            {
-                if (deviceInfo.ModelDescription == null || !IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
-                {
-                    return false;
-                }
-            }
-
-            if (!string.IsNullOrEmpty(profileInfo.ModelName))
-            {
-                if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
-                {
-                    return false;
-                }
-            }
-
-            if (!string.IsNullOrEmpty(profileInfo.ModelNumber))
-            {
-                if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
-                {
-                    return false;
-                }
-            }
+            return IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName)
+                && IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer)
+                && IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl)
+                && IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription)
+                && IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName)
+                && IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber)
+                && IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl)
+                && IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber);
+        }
 
-            if (!string.IsNullOrEmpty(profileInfo.ModelUrl))
+        private bool IsRegexOrSubstringMatch(string input, string pattern)
+        {
+            if (string.IsNullOrEmpty(pattern))
             {
-                if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
-                {
-                    return false;
-                }
+                // In profile identification: An empty pattern matches anything.
+                return true;
             }
 
-            if (!string.IsNullOrEmpty(profileInfo.SerialNumber))
+            if (string.IsNullOrEmpty(input))
             {
-                if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
-                {
-                    return false;
-                }
+                // The profile contains a value, and the device doesn't.
+                return false;
             }
 
-            return true;
-        }
-
-        private bool IsRegexOrSubstringMatch(string input, string pattern)
-        {
             try
             {
-                return input.Contains(pattern, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
+                return input.Equals(pattern, StringComparison.OrdinalIgnoreCase)
+                    || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
             }
             catch (ArgumentException ex)
             {
@@ -333,7 +300,12 @@ namespace Emby.Dlna
                 throw new ArgumentNullException(nameof(id));
             }
 
-            var info = GetProfileInfosInternal().First(i => string.Equals(i.Info.Id, id, StringComparison.OrdinalIgnoreCase));
+            var info = GetProfileInfosInternal().FirstOrDefault(i => string.Equals(i.Info.Id, id, StringComparison.OrdinalIgnoreCase));
+
+            if (info == null)
+            {
+                return null;
+            }
 
             return ParseProfileFile(info.Path, info.Info.Type);
         }
@@ -395,7 +367,8 @@ namespace Emby.Dlna
                     {
                         Directory.CreateDirectory(systemProfilesPath);
 
-                        using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
+                        // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
+                        using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                         {
                             await stream.CopyToAsync(fileStream).ConfigureAwait(false);
                         }

+ 1 - 1
Emby.Dlna/Emby.Dlna.csproj

@@ -21,11 +21,11 @@
     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
     <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
+    <Nullable>enable</Nullable>
   </PropertyGroup>
 
   <!-- Code Analyzers-->
   <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="All" />
     <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
     <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
     <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />

+ 2 - 0
Emby.Dlna/EventSubscriptionResponse.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System.Collections.Generic;

+ 2 - 0
Emby.Dlna/Eventing/DlnaEventManager.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;

+ 2 - 0
Emby.Dlna/Eventing/EventSubscription.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;

+ 10 - 5
Emby.Dlna/Main/DlnaEntryPoint.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;
@@ -5,7 +7,6 @@ using System.Globalization;
 using System.Linq;
 using System.Net.Http;
 using System.Net.Sockets;
-using System.Threading;
 using System.Threading.Tasks;
 using Emby.Dlna.PlayTo;
 using Emby.Dlna.Ssdp;
@@ -128,7 +129,8 @@ namespace Emby.Dlna.Main
 
             _netConfig = config.GetConfiguration<NetworkConfiguration>("network");
             _disabled = appHost.ListenWithHttps && _netConfig.RequireHttps;
-            if (_disabled)
+
+            if (_disabled && _config.GetDlnaConfiguration().EnableServer)
             {
                 _logger.LogError("The DLNA specification does not support HTTPS.");
             }
@@ -316,9 +318,12 @@ namespace Emby.Dlna.Main
                 _logger.LogInformation("Registering publisher for {0} on {1}", fullService, address);
 
                 var uri = new UriBuilder(_appHost.GetSmartApiUrl(address.Address) + descriptorUri);
-                // DLNA will only work over http, so we must reset to http:// : {port}
-                uri.Scheme = "http://";
-                uri.Port = _netConfig.HttpServerPortNumber;
+                if (!string.IsNullOrEmpty(_appHost.PublishedServerUrl))
+                {
+                    // DLNA will only work over http, so we must reset to http:// : {port}.
+                    uri.Scheme = "http";
+                    uri.Port = _netConfig.HttpServerPortNumber;
+                }
 
                 var device = new SsdpRootDevice
                 {

+ 1 - 1
Emby.Dlna/MediaReceiverRegistrar/ControlHandler.cs

@@ -24,7 +24,7 @@ namespace Emby.Dlna.MediaReceiverRegistrar
         }
 
         /// <inheritdoc />
-        protected override void WriteResult(string methodName, IDictionary<string, string> methodParams, XmlWriter xmlWriter)
+        protected override void WriteResult(string methodName, IReadOnlyDictionary<string, string> methodParams, XmlWriter xmlWriter)
         {
             if (string.Equals(methodName, "IsAuthorized", StringComparison.OrdinalIgnoreCase))
             {

+ 0 - 1
Emby.Dlna/MediaReceiverRegistrar/MediaReceiverRegistrarXmlBuilder.cs

@@ -1,7 +1,6 @@
 using System.Collections.Generic;
 using Emby.Dlna.Common;
 using Emby.Dlna.Service;
-using MediaBrowser.Model.Dlna;
 
 namespace Emby.Dlna.MediaReceiverRegistrar
 {

+ 35 - 8
Emby.Dlna/PlayTo/Device.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;
@@ -219,7 +221,7 @@ namespace Emby.Dlna.PlayTo
         {
             var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
 
-            var command = rendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetMute");
+            var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetMute");
             if (command == null)
             {
                 return false;
@@ -259,7 +261,7 @@ namespace Emby.Dlna.PlayTo
         {
             var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
 
-            var command = rendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetVolume");
+            var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetVolume");
             if (command == null)
             {
                 return;
@@ -290,7 +292,7 @@ namespace Emby.Dlna.PlayTo
         {
             var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
 
-            var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Seek");
+            var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Seek");
             if (command == null)
             {
                 return;
@@ -323,7 +325,7 @@ namespace Emby.Dlna.PlayTo
 
             _logger.LogDebug("{0} - SetAvTransport Uri: {1} DlnaHeaders: {2}", Properties.Name, url, header);
 
-            var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "SetAVTransportURI");
+            var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "SetAVTransportURI");
             if (command == null)
             {
                 return;
@@ -403,6 +405,10 @@ namespace Emby.Dlna.PlayTo
         public async Task SetPlay(CancellationToken cancellationToken)
         {
             var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
+            if (avCommands == null)
+            {
+                return;
+            }
 
             await SetPlay(avCommands, cancellationToken).ConfigureAwait(false);
 
@@ -413,7 +419,7 @@ namespace Emby.Dlna.PlayTo
         {
             var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
 
-            var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Stop");
+            var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Stop");
             if (command == null)
             {
                 return;
@@ -437,7 +443,7 @@ namespace Emby.Dlna.PlayTo
         {
             var avCommands = await GetAVProtocolAsync(cancellationToken).ConfigureAwait(false);
 
-            var command = avCommands.ServiceActions.FirstOrDefault(c => c.Name == "Pause");
+            var command = avCommands?.ServiceActions.FirstOrDefault(c => c.Name == "Pause");
             if (command == null)
             {
                 return;
@@ -565,7 +571,7 @@ namespace Emby.Dlna.PlayTo
 
             var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
 
-            var command = rendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");
+            var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");
             if (command == null)
             {
                 return;
@@ -615,7 +621,7 @@ namespace Emby.Dlna.PlayTo
 
             var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
 
-            var command = rendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetMute");
+            var command = rendererCommands?.ServiceActions.FirstOrDefault(c => c.Name == "GetMute");
             if (command == null)
             {
                 return;
@@ -702,6 +708,10 @@ namespace Emby.Dlna.PlayTo
             }
 
             var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
+            if (rendererCommands == null)
+            {
+                return null;
+            }
 
             var result = await new SsdpHttpClient(_httpClientFactory).SendCommandAsync(
                 Properties.BaseUrl,
@@ -770,6 +780,11 @@ namespace Emby.Dlna.PlayTo
 
             var rendererCommands = await GetRenderingProtocolAsync(cancellationToken).ConfigureAwait(false);
 
+            if (rendererCommands == null)
+            {
+                return (false, null);
+            }
+
             var result = await new SsdpHttpClient(_httpClientFactory).SendCommandAsync(
                 Properties.BaseUrl,
                 service,
@@ -951,6 +966,10 @@ namespace Emby.Dlna.PlayTo
             var httpClient = new SsdpHttpClient(_httpClientFactory);
 
             var document = await httpClient.GetDataAsync(url, cancellationToken).ConfigureAwait(false);
+            if (document == null)
+            {
+                return null;
+            }
 
             AvCommands = TransportCommands.Create(document);
             return AvCommands;
@@ -979,6 +998,10 @@ namespace Emby.Dlna.PlayTo
             var httpClient = new SsdpHttpClient(_httpClientFactory);
             _logger.LogDebug("Dlna Device.GetRenderingProtocolAsync");
             var document = await httpClient.GetDataAsync(url, cancellationToken).ConfigureAwait(false);
+            if (document == null)
+            {
+                return null;
+            }
 
             RendererCommands = TransportCommands.Create(document);
             return RendererCommands;
@@ -1010,6 +1033,10 @@ namespace Emby.Dlna.PlayTo
             var ssdpHttpClient = new SsdpHttpClient(httpClientFactory);
 
             var document = await ssdpHttpClient.GetDataAsync(url.ToString(), cancellationToken).ConfigureAwait(false);
+            if (document == null)
+            {
+                return null;
+            }
 
             var friendlyNames = new List<string>();
 

+ 2 - 0
Emby.Dlna/PlayTo/DeviceInfo.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System.Collections.Generic;

+ 2 - 0
Emby.Dlna/PlayTo/MediaChangedEventArgs.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;

+ 8 - 10
Emby.Dlna/PlayTo/PlayToController.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;
@@ -132,7 +134,7 @@ namespace Emby.Dlna.PlayTo
 
         private async void OnDeviceMediaChanged(object sender, MediaChangedEventArgs e)
         {
-            if (_disposed)
+            if (_disposed || string.IsNullOrEmpty(e.OldMediaInfo.Url))
             {
                 return;
             }
@@ -499,8 +501,8 @@ namespace Emby.Dlna.PlayTo
 
             if (streamInfo.MediaType == DlnaProfileType.Audio)
             {
-                return new ContentFeatureBuilder(profile)
-                    .BuildAudioHeader(
+                return ContentFeatureBuilder.BuildAudioHeader(
+                        profile,
                         streamInfo.Container,
                         streamInfo.TargetAudioCodec.FirstOrDefault(),
                         streamInfo.TargetAudioBitrate,
@@ -514,8 +516,8 @@ namespace Emby.Dlna.PlayTo
 
             if (streamInfo.MediaType == DlnaProfileType.Video)
             {
-                var list = new ContentFeatureBuilder(profile)
-                    .BuildVideoHeader(
+                var list = ContentFeatureBuilder.BuildVideoHeader(
+                        profile,
                         streamInfo.Container,
                         streamInfo.TargetVideoCodec.FirstOrDefault(),
                         streamInfo.TargetAudioCodec.FirstOrDefault(),
@@ -943,11 +945,7 @@ namespace Emby.Dlna.PlayTo
                 request.DeviceId = values.GetValueOrDefault("DeviceId");
                 request.MediaSourceId = values.GetValueOrDefault("MediaSourceId");
                 request.LiveStreamId = values.GetValueOrDefault("LiveStreamId");
-
-                // Be careful, IsDirectStream==true by default (Static != false or not in query).
-                // See initialization of StreamingRequestDto in AudioController.GetAudioStream() method : Static = @static ?? true.
-                request.IsDirectStream = !string.Equals("false", values.GetValueOrDefault("Static"), StringComparison.OrdinalIgnoreCase);
-
+                request.IsDirectStream = string.Equals("true", values.GetValueOrDefault("Static"), StringComparison.OrdinalIgnoreCase);
                 request.AudioStreamIndex = GetIntValue(values, "AudioStreamIndex");
                 request.SubtitleStreamIndex = GetIntValue(values, "SubtitleStreamIndex");
                 request.StartPositionTicks = GetLongValue(values, "StartPositionTicks");

+ 8 - 1
Emby.Dlna/PlayTo/PlayToManager.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;
@@ -178,12 +180,17 @@ namespace Emby.Dlna.PlayTo
             if (controller == null)
             {
                 var device = await Device.CreateuPnpDeviceAsync(uri, _httpClientFactory, _logger, cancellationToken).ConfigureAwait(false);
+                if (device == null)
+                {
+                    _logger.LogError("Ignoring device as xml response is invalid.");
+                    return;
+                }
 
                 string deviceName = device.Properties.Name;
 
                 _sessionManager.UpdateDeviceName(sessionInfo.Id, deviceName);
 
-                string serverAddress = _appHost.GetSmartApiUrl(info.LocalIpAddress);
+                string serverAddress = _appHost.GetSmartApiUrl(info.RemoteIpAddress);
 
                 controller = new PlayToController(
                     sessionInfo,

+ 2 - 0
Emby.Dlna/PlayTo/PlaybackProgressEventArgs.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;

+ 2 - 0
Emby.Dlna/PlayTo/PlaybackStartEventArgs.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;

+ 2 - 0
Emby.Dlna/PlayTo/PlaybackStoppedEventArgs.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;

+ 2 - 0
Emby.Dlna/PlayTo/PlaylistItem.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using MediaBrowser.Model.Dlna;

+ 2 - 0
Emby.Dlna/PlayTo/PlaylistItemFactory.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System.IO;

+ 17 - 9
Emby.Dlna/PlayTo/SsdpHttpClient.cs

@@ -1,8 +1,9 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;
 using System.Globalization;
-using System.IO;
 using System.Net.Http;
 using System.Net.Mime;
 using System.Text;
@@ -45,10 +46,10 @@ namespace Emby.Dlna.PlayTo
                     cancellationToken)
                 .ConfigureAwait(false);
             await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
-            using var reader = new StreamReader(stream, Encoding.UTF8);
-            return XDocument.Parse(
-                await reader.ReadToEndAsync().ConfigureAwait(false),
-                LoadOptions.PreserveWhitespace);
+            return await XDocument.LoadAsync(
+                stream,
+                LoadOptions.PreserveWhitespace,
+                cancellationToken).ConfigureAwait(false);
         }
 
         private static string NormalizeServiceUrl(string baseUrl, string serviceUrl)
@@ -94,10 +95,17 @@ namespace Emby.Dlna.PlayTo
             options.Headers.TryAddWithoutValidation("FriendlyName.DLNA.ORG", FriendlyName);
             using var response = await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
             await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
-            using var reader = new StreamReader(stream, Encoding.UTF8);
-            return XDocument.Parse(
-                await reader.ReadToEndAsync().ConfigureAwait(false),
-                LoadOptions.PreserveWhitespace);
+            try
+            {
+                return await XDocument.LoadAsync(
+                    stream,
+                    LoadOptions.PreserveWhitespace,
+                    cancellationToken).ConfigureAwait(false);
+            }
+            catch
+            {
+                return null;
+            }
         }
 
         private async Task<HttpResponseMessage> PostSoapDataAsync(

+ 9 - 11
Emby.Dlna/PlayTo/TransportCommands.cs

@@ -13,12 +13,10 @@ namespace Emby.Dlna.PlayTo
     public class TransportCommands
     {
         private const string CommandBase = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "<SOAP-ENV:Body>" + "<m:{0} xmlns:m=\"{1}\">" + "{2}" + "</m:{0}>" + "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
-        private List<StateVariable> _stateVariables = new List<StateVariable>();
-        private List<ServiceAction> _serviceActions = new List<ServiceAction>();
 
-        public List<StateVariable> StateVariables => _stateVariables;
+        public List<StateVariable> StateVariables { get; } = new List<StateVariable>();
 
-        public List<ServiceAction> ServiceActions => _serviceActions;
+        public List<ServiceAction> ServiceActions { get; } = new List<ServiceAction>();
 
         public static TransportCommands Create(XDocument document)
         {
@@ -48,7 +46,7 @@ namespace Emby.Dlna.PlayTo
         {
             var serviceAction = new ServiceAction
             {
-                Name = container.GetValue(UPnpNamespaces.Svc + "name"),
+                Name = container.GetValue(UPnpNamespaces.Svc + "name") ?? string.Empty,
             };
 
             var argumentList = serviceAction.ArgumentList;
@@ -70,9 +68,9 @@ namespace Emby.Dlna.PlayTo
 
             return new Argument
             {
-                Name = container.GetValue(UPnpNamespaces.Svc + "name"),
-                Direction = container.GetValue(UPnpNamespaces.Svc + "direction"),
-                RelatedStateVariable = container.GetValue(UPnpNamespaces.Svc + "relatedStateVariable")
+                Name = container.GetValue(UPnpNamespaces.Svc + "name") ?? string.Empty,
+                Direction = container.GetValue(UPnpNamespaces.Svc + "direction") ?? string.Empty,
+                RelatedStateVariable = container.GetValue(UPnpNamespaces.Svc + "relatedStateVariable") ?? string.Empty
             };
         }
 
@@ -91,8 +89,8 @@ namespace Emby.Dlna.PlayTo
 
             return new StateVariable
             {
-                Name = container.GetValue(UPnpNamespaces.Svc + "name"),
-                DataType = container.GetValue(UPnpNamespaces.Svc + "dataType"),
+                Name = container.GetValue(UPnpNamespaces.Svc + "name") ?? string.Empty,
+                DataType = container.GetValue(UPnpNamespaces.Svc + "dataType") ?? string.Empty,
                 AllowedValues = allowedValues
             };
         }
@@ -168,7 +166,7 @@ namespace Emby.Dlna.PlayTo
             return string.Format(CultureInfo.InvariantCulture, CommandBase, action.Name, xmlNamesapce, stateString);
         }
 
-        private string BuildArgumentXml(Argument argument, string value, string commandParameter = "")
+        private string BuildArgumentXml(Argument argument, string? value, string commandParameter = "")
         {
             var state = StateVariables.FirstOrDefault(a => string.Equals(a.Name, argument.RelatedStateVariable, StringComparison.OrdinalIgnoreCase));
 

+ 2 - 0
Emby.Dlna/PlayTo/uBaseObject.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;

+ 3 - 0
Emby.Dlna/Profiles/DefaultProfile.cs

@@ -1,5 +1,7 @@
 #pragma warning disable CS1591
 
+using System;
+using System.Globalization;
 using System.Linq;
 using MediaBrowser.Model.Dlna;
 
@@ -10,6 +12,7 @@ namespace Emby.Dlna.Profiles
     {
         public DefaultProfile()
         {
+            Id = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
             Name = "Generic Device";
 
             ProtocolInfo = "http-get:*:video/mpeg:*,http-get:*:video/mp4:*,http-get:*:video/vnd.dlna.mpeg-tts:*,http-get:*:video/avi:*,http-get:*:video/x-matroska:*,http-get:*:video/x-ms-wmv:*,http-get:*:video/wtv:*,http-get:*:audio/mpeg:*,http-get:*:audio/mp3:*,http-get:*:audio/mp4:*,http-get:*:audio/x-ms-wma:*,http-get:*:audio/wav:*,http-get:*:audio/L16:*,http-get:*:image/jpeg:*,http-get:*:image/png:*,http-get:*:image/gif:*,http-get:*:image/tiff:*";

+ 2 - 1
Emby.Dlna/Server/DescriptionXmlBuilder.cs

@@ -250,7 +250,8 @@ namespace Emby.Dlna.Server
 
             url = _serverAddress.TrimEnd('/') + "/dlna/" + _serverUdn + "/" + url.TrimStart('/');
 
-            return SecurityElement.Escape(url);
+            // TODO: @bond remove null-coalescing operator when https://github.com/dotnet/runtime/pull/52442 is merged/released
+            return SecurityElement.Escape(url) ?? string.Empty;
         }
 
         private IEnumerable<DeviceIcon> GetIcons()

+ 3 - 3
Emby.Dlna/Service/BaseControlHandler.cs

@@ -47,7 +47,7 @@ namespace Emby.Dlna.Service
 
         private async Task<ControlResponse> ProcessControlRequestInternalAsync(ControlRequest request)
         {
-            ControlRequestInfo requestInfo = null;
+            ControlRequestInfo? requestInfo = null;
 
             using (var streamReader = new StreamReader(request.InputXml, Encoding.UTF8))
             {
@@ -151,7 +151,7 @@ namespace Emby.Dlna.Service
 
         private async Task<ControlRequestInfo> ParseBodyTagAsync(XmlReader reader)
         {
-            string namespaceURI = null, localName = null;
+            string? namespaceURI = null, localName = null;
 
             await reader.MoveToContentAsync().ConfigureAwait(false);
             await reader.ReadAsync().ConfigureAwait(false);
@@ -210,7 +210,7 @@ namespace Emby.Dlna.Service
             }
         }
 
-        protected abstract void WriteResult(string methodName, IDictionary<string, string> methodParams, XmlWriter xmlWriter);
+        protected abstract void WriteResult(string methodName, IReadOnlyDictionary<string, string> methodParams, XmlWriter xmlWriter);
 
         private void LogRequest(ControlRequest request)
         {

+ 4 - 2
Emby.Dlna/Ssdp/DeviceDiscovery.cs

@@ -1,3 +1,5 @@
+#nullable disable
+
 #pragma warning disable CS1591
 
 using System;
@@ -69,7 +71,7 @@ namespace Emby.Dlna.Ssdp
         {
             lock (_syncLock)
             {
-                if (_listenerCount > 0 && _deviceLocator == null)
+                if (_listenerCount > 0 && _deviceLocator == null && _commsServer != null)
                 {
                     _deviceLocator = new SsdpDeviceLocator(_commsServer);
 
@@ -104,7 +106,7 @@ namespace Emby.Dlna.Ssdp
                 {
                     Location = e.DiscoveredDevice.DescriptionLocation,
                     Headers = headers,
-                    LocalIpAddress = e.LocalIpAddress
+                    RemoteIpAddress = e.RemoteIpAddress
                 });
 
             DeviceDiscoveredInternal?.Invoke(this, args);

+ 3 - 3
Emby.Dlna/Ssdp/SsdpExtensions.cs

@@ -7,21 +7,21 @@ namespace Emby.Dlna.Ssdp
 {
     public static class SsdpExtensions
     {
-        public static string GetValue(this XElement container, XName name)
+        public static string? GetValue(this XElement container, XName name)
         {
             var node = container.Element(name);
 
             return node?.Value;
         }
 
-        public static string GetAttributeValue(this XElement container, XName name)
+        public static string? GetAttributeValue(this XElement container, XName name)
         {
             var node = container.Attribute(name);
 
             return node?.Value;
         }
 
-        public static string GetDescendantValue(this XElement container, XName name)
+        public static string? GetDescendantValue(this XElement container, XName name)
             => container.Descendants(name).FirstOrDefault()?.Value;
     }
 }

+ 0 - 1
Emby.Drawing/Emby.Drawing.csproj

@@ -25,7 +25,6 @@
 
   <!-- Code analysers-->
   <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="All" />
     <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
     <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
     <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />

+ 102 - 23
Emby.Drawing/ImageProcessor.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
 using System.Linq;
+using System.Text;
 using System.Threading.Tasks;
 using Jellyfin.Data.Entities;
 using MediaBrowser.Common.Extensions;
@@ -171,21 +172,31 @@ namespace Emby.Drawing
                 return (originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
             }
 
-            ImageDimensions newSize = ImageHelper.GetNewImageSize(options, null);
             int quality = options.Quality;
 
             ImageFormat outputFormat = GetOutputFormat(options.SupportedOutputFormats, requiresTransparency);
-            string cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.Blur, options.BackgroundColor, options.ForegroundLayer);
+            string cacheFilePath = GetCacheFilePath(
+                originalImagePath,
+                options.Width,
+                options.Height,
+                options.MaxWidth,
+                options.MaxHeight,
+                options.FillWidth,
+                options.FillHeight,
+                quality,
+                dateModified,
+                outputFormat,
+                options.AddPlayedIndicator,
+                options.PercentPlayed,
+                options.UnplayedCount,
+                options.Blur,
+                options.BackgroundColor,
+                options.ForegroundLayer);
 
             try
             {
                 if (!File.Exists(cacheFilePath))
                 {
-                    if (options.CropWhiteSpace && !SupportsTransparency(originalImagePath))
-                    {
-                        options.CropWhiteSpace = false;
-                    }
-
                     string resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, cacheFilePath, autoOrient, orientation, quality, options, outputFormat);
 
                     if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase))
@@ -246,48 +257,111 @@ namespace Emby.Drawing
         /// <summary>
         /// Gets the cache file path based on a set of parameters.
         /// </summary>
-        private string GetCacheFilePath(string originalPath, ImageDimensions outputSize, int quality, DateTime dateModified, ImageFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, int? blur, string backgroundColor, string foregroundLayer)
+        private string GetCacheFilePath(
+            string originalPath,
+            int? width,
+            int? height,
+            int? maxWidth,
+            int? maxHeight,
+            int? fillWidth,
+            int? fillHeight,
+            int quality,
+            DateTime dateModified,
+            ImageFormat format,
+            bool addPlayedIndicator,
+            double percentPlayed,
+            int? unwatchedCount,
+            int? blur,
+            string backgroundColor,
+            string foregroundLayer)
         {
-            var filename = originalPath
-                + "width=" + outputSize.Width
-                + "height=" + outputSize.Height
-                + "quality=" + quality
-                + "datemodified=" + dateModified.Ticks
-                + "f=" + format;
+            var filename = new StringBuilder(256);
+            filename.Append(originalPath);
+
+            filename.Append(",quality=");
+            filename.Append(quality);
+
+            filename.Append(",datemodified=");
+            filename.Append(dateModified.Ticks);
+
+            filename.Append(",f=");
+            filename.Append(format);
+
+            if (width.HasValue)
+            {
+                filename.Append(",width=");
+                filename.Append(width.Value);
+            }
+
+            if (height.HasValue)
+            {
+                filename.Append(",height=");
+                filename.Append(height.Value);
+            }
+
+            if (maxWidth.HasValue)
+            {
+                filename.Append(",maxwidth=");
+                filename.Append(maxWidth.Value);
+            }
+
+            if (maxHeight.HasValue)
+            {
+                filename.Append(",maxheight=");
+                filename.Append(maxHeight.Value);
+            }
+
+            if (fillWidth.HasValue)
+            {
+                filename.Append(",fillwidth=");
+                filename.Append(fillWidth.Value);
+            }
+
+            if (fillHeight.HasValue)
+            {
+                filename.Append(",fillheight=");
+                filename.Append(fillHeight.Value);
+            }
 
             if (addPlayedIndicator)
             {
-                filename += "pl=true";
+                filename.Append(",pl=true");
             }
 
             if (percentPlayed > 0)
             {
-                filename += "p=" + percentPlayed;
+                filename.Append(",p=");
+                filename.Append(percentPlayed);
             }
 
             if (unwatchedCount.HasValue)
             {
-                filename += "p=" + unwatchedCount.Value;
+                filename.Append(",p=");
+                filename.Append(unwatchedCount.Value);
             }
 
             if (blur.HasValue)
             {
-                filename += "blur=" + blur.Value;
+                filename.Append(",blur=");
+                filename.Append(blur.Value);
             }
 
             if (!string.IsNullOrEmpty(backgroundColor))
             {
-                filename += "b=" + backgroundColor;
+                filename.Append(",b=");
+                filename.Append(backgroundColor);
             }
 
             if (!string.IsNullOrEmpty(foregroundLayer))
             {
-                filename += "fl=" + foregroundLayer;
+                filename.Append(",fl=");
+                filename.Append(foregroundLayer);
             }
 
-            filename += "v=" + Version;
+            filename.Append(",v=");
+            filename.Append(Version);
 
-            return GetCachePath(ResizedImageCachePath, filename, "." + format.ToString().ToLowerInvariant());
+            return GetCachePath(ResizedImageCachePath, filename.ToString(), "." + format.ToString().ToLowerInvariant());
         }
 
         /// <inheritdoc />
@@ -352,8 +426,13 @@ namespace Emby.Drawing
         }
 
         /// <inheritdoc />
-        public string GetImageCacheTag(User user)
+        public string? GetImageCacheTag(User user)
         {
+            if (user.ProfileImage == null)
+            {
+                return null;
+            }
+
             return (user.ProfileImage.Path + user.ProfileImage.LastModified.Ticks).GetMD5()
                 .ToString("N", CultureInfo.InvariantCulture);
         }

+ 1 - 1
Emby.Drawing/NullImageEncoder.cs

@@ -32,7 +32,7 @@ namespace Emby.Drawing
             => throw new NotImplementedException();
 
         /// <inheritdoc />
-        public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientation? orientation, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
+        public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientation? orientation, int quality, ImageProcessingOptions options, ImageFormat outputFormat)
         {
             throw new NotImplementedException();
         }

+ 0 - 1
Emby.Naming/Emby.Naming.csproj

@@ -44,7 +44,6 @@
 
   <!-- Code Analyzers-->
   <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <!-- TODO: <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="All" /> -->
     <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
     <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
     <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />

+ 5 - 0
Emby.Naming/TV/EpisodeResolver.cs

@@ -68,6 +68,11 @@ namespace Emby.Naming.TV
             var parsingResult = new EpisodePathParser(_options)
                 .Parse(path, isDirectory, isNamed, isOptimistic, supportsAbsoluteNumbers, fillExtendedInfo);
 
+            if (!parsingResult.Success && !isStub)
+            {
+                return null;
+            }
+
             return new EpisodeInfo(path)
             {
                 Container = container,

+ 9 - 2
Emby.Naming/Video/CleanStringParser.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
 using System.Text.RegularExpressions;
 
 namespace Emby.Naming.Video
@@ -16,8 +17,14 @@ namespace Emby.Naming.Video
         /// <param name="expressions">List of regex to parse name and year from.</param>
         /// <param name="newName">Parsing result string.</param>
         /// <returns>True if parsing was successful.</returns>
-        public static bool TryClean(string name, IReadOnlyList<Regex> expressions, out ReadOnlySpan<char> newName)
+        public static bool TryClean([NotNullWhen(true)] string? name, IReadOnlyList<Regex> expressions, out ReadOnlySpan<char> newName)
         {
+            if (string.IsNullOrEmpty(name))
+            {
+                newName = ReadOnlySpan<char>.Empty;
+                return false;
+            }
+
             var len = expressions.Count;
             for (int i = 0; i < len; i++)
             {
@@ -41,7 +48,7 @@ namespace Emby.Naming.Video
                 return true;
             }
 
-            newName = string.Empty;
+            newName = ReadOnlySpan<char>.Empty;
             return false;
         }
     }

+ 8 - 7
Emby.Naming/Video/VideoListResolver.cs

@@ -221,20 +221,21 @@ namespace Emby.Naming.Video
             string testFilename = Path.GetFileNameWithoutExtension(testFilePath);
             if (testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase))
             {
-                if (CleanStringParser.TryClean(testFilename, _options.CleanStringRegexes, out var cleanName))
+                // Remove the folder name before cleaning as we don't care about cleaning that part
+                if (folderName.Length <= testFilename.Length)
                 {
-                    testFilename = cleanName.ToString();
+                    testFilename = testFilename.Substring(folderName.Length).Trim();
                 }
 
-                if (folderName.Length <= testFilename.Length)
+                if (CleanStringParser.TryClean(testFilename, _options.CleanStringRegexes, out var cleanName))
                 {
-                    testFilename = testFilename.Substring(folderName.Length).Trim();
+                    testFilename = cleanName.Trim().ToString();
                 }
 
+                // The CleanStringParser should have removed common keywords etc.
                 return string.IsNullOrEmpty(testFilename)
-                   || testFilename[0] == '-'
-                   || testFilename[0] == '_'
-                   || string.IsNullOrWhiteSpace(Regex.Replace(testFilename, @"\[([^]]*)\]", string.Empty));
+                       || testFilename[0] == '-'
+                       || Regex.IsMatch(testFilename, @"^\[([^]]*)\]");
             }
 
             return false;

+ 2 - 1
Emby.Naming/Video/VideoResolver.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Diagnostics.CodeAnalysis;
 using System.IO;
 using System.Linq;
 using Emby.Naming.Common;
@@ -146,7 +147,7 @@ namespace Emby.Naming.Video
         /// <param name="name">Raw name.</param>
         /// <param name="newName">Clean name.</param>
         /// <returns>True if cleaning of name was successful.</returns>
-        public bool TryCleanString(string name, out ReadOnlySpan<char> newName)
+        public bool TryCleanString([NotNullWhen(true)] string? name, out ReadOnlySpan<char> newName)
         {
             return CleanStringParser.TryClean(name, _options.CleanStringRegexes, out newName);
         }

+ 2 - 5
Emby.Notifications/Emby.Notifications.csproj

@@ -11,6 +11,8 @@
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
     <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
     <Nullable>enable</Nullable>
+    <AnalysisMode>AllEnabledByDefault</AnalysisMode>
+    <CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
   </PropertyGroup>
 
   <ItemGroup>
@@ -25,14 +27,9 @@
 
   <!-- Code analyzers-->
   <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="All" />
     <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
     <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
     <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
   </ItemGroup>
 
-  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-
 </Project>

+ 2 - 5
Emby.Photos/Emby.Photos.csproj

@@ -24,18 +24,15 @@
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
     <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
     <Nullable>enable</Nullable>
+    <AnalysisMode>AllEnabledByDefault</AnalysisMode>
+    <CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
   </PropertyGroup>
 
   <!-- Code Analyzers-->
   <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="All" />
     <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
     <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
     <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
   </ItemGroup>
 
-  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-
 </Project>

+ 2 - 2
Emby.Server.Implementations/AppBase/ConfigurationHelper.cs

@@ -3,7 +3,6 @@
 using System;
 using System.IO;
 using System.Linq;
-using MediaBrowser.Common.Extensions;
 using MediaBrowser.Model.Serialization;
 
 namespace Emby.Server.Implementations.AppBase
@@ -53,7 +52,8 @@ namespace Emby.Server.Implementations.AppBase
 
                 Directory.CreateDirectory(directory);
                 // Save it after load in case we got new items
-                using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
+                // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
+                using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                 {
                     fs.Write(newBytes, 0, newBytesLen);
                 }

+ 65 - 73
Emby.Server.Implementations/ApplicationHost.cs

@@ -10,8 +10,6 @@ using System.Net;
 using System.Reflection;
 using System.Runtime.InteropServices;
 using System.Security.Cryptography.X509Certificates;
-using System.Text;
-using System.Text.Json;
 using System.Threading;
 using System.Threading.Tasks;
 using Emby.Dlna;
@@ -43,6 +41,7 @@ using Emby.Server.Implementations.Serialization;
 using Emby.Server.Implementations.Session;
 using Emby.Server.Implementations.SyncPlay;
 using Emby.Server.Implementations.TV;
+using Emby.Server.Implementations.Udp;
 using Emby.Server.Implementations.Updates;
 using Jellyfin.Api.Helpers;
 using Jellyfin.Networking.Configuration;
@@ -50,7 +49,6 @@ using Jellyfin.Networking.Manager;
 using MediaBrowser.Common;
 using MediaBrowser.Common.Configuration;
 using MediaBrowser.Common.Events;
-using MediaBrowser.Common.Json;
 using MediaBrowser.Common.Net;
 using MediaBrowser.Common.Plugins;
 using MediaBrowser.Common.Updates;
@@ -99,6 +97,7 @@ using MediaBrowser.Providers.Subtitles;
 using MediaBrowser.XbmcMetadata.Providers;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Logging;
 using Prometheus.DotNetRuntime;
@@ -118,6 +117,7 @@ namespace Emby.Server.Implementations
         private static readonly string[] _relevantEnvVarPrefixes = { "JELLYFIN_", "DOTNET_", "ASPNETCORE_" };
 
         private readonly IFileSystem _fileSystemManager;
+        private readonly IConfiguration _startupConfig;
         private readonly IXmlSerializer _xmlSerializer;
         private readonly IStartupOptions _startupOptions;
         private readonly IPluginManager _pluginManager;
@@ -126,7 +126,6 @@ namespace Emby.Server.Implementations
         private IMediaEncoder _mediaEncoder;
         private ISessionManager _sessionManager;
         private string[] _urlPrefixes;
-        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.GetOptions();
 
         /// <summary>
         /// Gets a value indicating whether this instance can self restart.
@@ -211,7 +210,7 @@ namespace Emby.Server.Implementations
         /// Gets or sets the configuration manager.
         /// </summary>
         /// <value>The configuration manager.</value>
-        protected IConfigurationManager ConfigurationManager { get; set; }
+        public ServerConfigurationManager ConfigurationManager { get; set; }
 
         /// <summary>
         /// Gets or sets the service provider.
@@ -229,10 +228,9 @@ namespace Emby.Server.Implementations
         public int HttpsPort { get; private set; }
 
         /// <summary>
-        /// Gets the server configuration manager.
+        /// Gets the value of the PublishedServerUrl setting.
         /// </summary>
-        /// <value>The server configuration manager.</value>
-        public IServerConfigurationManager ServerConfigurationManager => (IServerConfigurationManager)ConfigurationManager;
+        public string PublishedServerUrl => _startupOptions.PublishedServerUrl ?? _startupConfig[UdpServer.AddressOverrideConfigKey];
 
         /// <summary>
         /// Initializes a new instance of the <see cref="ApplicationHost"/> class.
@@ -240,51 +238,37 @@ namespace Emby.Server.Implementations
         /// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param>
         /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
         /// <param name="options">Instance of the <see cref="IStartupOptions"/> interface.</param>
+        /// <param name="startupConfig">The <see cref="IConfiguration" /> interface.</param>
         /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
         /// <param name="serviceCollection">Instance of the <see cref="IServiceCollection"/> interface.</param>
         public ApplicationHost(
             IServerApplicationPaths applicationPaths,
             ILoggerFactory loggerFactory,
             IStartupOptions options,
+            IConfiguration startupConfig,
             IFileSystem fileSystem,
             IServiceCollection serviceCollection)
         {
-            _xmlSerializer = new MyXmlSerializer();
-
-            ServiceCollection = serviceCollection;
-
             ApplicationPaths = applicationPaths;
             LoggerFactory = loggerFactory;
+            _startupOptions = options;
+            _startupConfig = startupConfig;
             _fileSystemManager = fileSystem;
-
-            ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, _xmlSerializer, _fileSystemManager);
-            // Have to migrate settings here as migration subsystem not yet initialised.
-            MigrateNetworkConfiguration();
-
-            // Have to pre-register the NetworkConfigurationFactory, as the configuration sub-system is not yet initialised.
-            ConfigurationManager.RegisterConfiguration<NetworkConfigurationFactory>();
-            NetManager = new NetworkManager((IServerConfigurationManager)ConfigurationManager, LoggerFactory.CreateLogger<NetworkManager>());
+            ServiceCollection = serviceCollection;
 
             Logger = LoggerFactory.CreateLogger<ApplicationHost>();
-
-            _startupOptions = options;
-
-            // Initialize runtime stat collection
-            if (ServerConfigurationManager.Configuration.EnableMetrics)
-            {
-                DotNetRuntimeStatsBuilder.Default().StartCollecting();
-            }
-
             fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
 
             ApplicationVersion = typeof(ApplicationHost).Assembly.GetName().Version;
             ApplicationVersionString = ApplicationVersion.ToString(3);
             ApplicationUserAgent = Name.Replace(' ', '-') + "/" + ApplicationVersionString;
 
+            _xmlSerializer = new MyXmlSerializer();
+            ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, _xmlSerializer, _fileSystemManager);
             _pluginManager = new PluginManager(
                 LoggerFactory.CreateLogger<PluginManager>(),
                 this,
-                ServerConfigurationManager.Configuration,
+                ConfigurationManager.Configuration,
                 ApplicationPaths.PluginsPath,
                 ApplicationVersion);
         }
@@ -299,9 +283,9 @@ namespace Emby.Server.Implementations
             if (!File.Exists(path))
             {
                 var networkSettings = new NetworkConfiguration();
-                ClassMigrationHelper.CopyProperties(ServerConfigurationManager.Configuration, networkSettings);
+                ClassMigrationHelper.CopyProperties(ConfigurationManager.Configuration, networkSettings);
                 _xmlSerializer.SerializeToFile(networkSettings, path);
-                Logger?.LogDebug("Successfully migrated network settings.");
+                Logger.LogDebug("Successfully migrated network settings.");
             }
         }
 
@@ -351,10 +335,7 @@ namespace Emby.Server.Implementations
         {
             get
             {
-                if (_deviceId == null)
-                {
-                    _deviceId = new DeviceId(ApplicationPaths, LoggerFactory);
-                }
+                _deviceId ??= new DeviceId(ApplicationPaths, LoggerFactory);
 
                 return _deviceId.Value;
             }
@@ -386,10 +367,7 @@ namespace Emby.Server.Implementations
         /// <returns>System.Object.</returns>
         protected object CreateInstanceSafe(Type type)
         {
-            if (_creatingInstances == null)
-            {
-                _creatingInstances = new List<Type>();
-            }
+            _creatingInstances ??= new List<Type>();
 
             if (_creatingInstances.IndexOf(type) != -1)
             {
@@ -460,7 +438,7 @@ namespace Emby.Server.Implementations
         }
 
         /// <inheritdoc />
-        public IReadOnlyCollection<T> GetExports<T>(CreationDelegate defaultFunc, bool manageLifetime = true)
+        public IReadOnlyCollection<T> GetExports<T>(CreationDelegateFactory defaultFunc, bool manageLifetime = true)
         {
             // Convert to list so this isn't executed for each iteration
             var parts = GetExportTypes<T>()
@@ -484,8 +462,9 @@ namespace Emby.Server.Implementations
         /// Runs the startup tasks.
         /// </summary>
         /// <returns><see cref="Task" />.</returns>
-        public async Task RunStartupTasksAsync()
+        public async Task RunStartupTasksAsync(CancellationToken cancellationToken)
         {
+            cancellationToken.ThrowIfCancellationRequested();
             Logger.LogInformation("Running startup tasks");
 
             Resolve<ITaskManager>().AddTasks(GetExports<IScheduledTask>(false));
@@ -499,14 +478,21 @@ namespace Emby.Server.Implementations
 
             var entryPoints = GetExports<IServerEntryPoint>();
 
+            cancellationToken.ThrowIfCancellationRequested();
+
             var stopWatch = new Stopwatch();
             stopWatch.Start();
+
             await Task.WhenAll(StartEntryPoints(entryPoints, true)).ConfigureAwait(false);
             Logger.LogInformation("Executed all pre-startup entry points in {Elapsed:g}", stopWatch.Elapsed);
 
             Logger.LogInformation("Core startup complete");
             CoreStartupHasCompleted = true;
+
+            cancellationToken.ThrowIfCancellationRequested();
+
             stopWatch.Restart();
+
             await Task.WhenAll(StartEntryPoints(entryPoints, false)).ConfigureAwait(false);
             Logger.LogInformation("Executed all post-startup entry points in {Elapsed:g}", stopWatch.Elapsed);
             stopWatch.Stop();
@@ -530,7 +516,21 @@ namespace Emby.Server.Implementations
         /// <inheritdoc/>
         public void Init()
         {
-            var networkConfiguration = ServerConfigurationManager.GetNetworkConfiguration();
+            DiscoverTypes();
+
+            ConfigurationManager.AddParts(GetExports<IConfigurationFactory>());
+
+            // Have to migrate settings here as migration subsystem not yet initialised.
+            MigrateNetworkConfiguration();
+            NetManager = new NetworkManager(ConfigurationManager, LoggerFactory.CreateLogger<NetworkManager>());
+
+            // Initialize runtime stat collection
+            if (ConfigurationManager.Configuration.EnableMetrics)
+            {
+                DotNetRuntimeStatsBuilder.Default().StartCollecting();
+            }
+
+            var networkConfiguration = ConfigurationManager.GetNetworkConfiguration();
             HttpPort = networkConfiguration.HttpServerPortNumber;
             HttpsPort = networkConfiguration.HttpsPortNumber;
 
@@ -548,8 +548,6 @@ namespace Emby.Server.Implementations
             };
             Certificate = GetCertificate(CertificateInfo);
 
-            DiscoverTypes();
-
             RegisterServices();
 
             _pluginManager.RegisterServices(ServiceCollection);
@@ -564,7 +562,8 @@ namespace Emby.Server.Implementations
 
             ServiceCollection.AddMemoryCache();
 
-            ServiceCollection.AddSingleton(ConfigurationManager);
+            ServiceCollection.AddSingleton<IServerConfigurationManager>(ConfigurationManager);
+            ServiceCollection.AddSingleton<IConfigurationManager>(ConfigurationManager);
             ServiceCollection.AddSingleton<IApplicationHost>(this);
             ServiceCollection.AddSingleton<IPluginManager>(_pluginManager);
             ServiceCollection.AddSingleton<IApplicationPaths>(ApplicationPaths);
@@ -591,8 +590,6 @@ namespace Emby.Server.Implementations
             ServiceCollection.AddSingleton<IServerApplicationHost>(this);
             ServiceCollection.AddSingleton<IServerApplicationPaths>(ApplicationPaths);
 
-            ServiceCollection.AddSingleton(ServerConfigurationManager);
-
             ServiceCollection.AddSingleton<ILocalizationManager, LocalizationManager>();
 
             ServiceCollection.AddSingleton<IBlurayExaminer, BdInfoExaminer>();
@@ -604,12 +601,8 @@ namespace Emby.Server.Implementations
 
             ServiceCollection.AddSingleton<IAuthenticationRepository, AuthenticationRepository>();
 
-            // TODO: Refactor to eliminate the circular dependency here so that Lazy<T> isn't required
-            ServiceCollection.AddTransient(provider => new Lazy<IDtoService>(provider.GetRequiredService<IDtoService>));
-
-            // TODO: Refactor to eliminate the circular dependency here so that Lazy<T> isn't required
-            ServiceCollection.AddTransient(provider => new Lazy<EncodingHelper>(provider.GetRequiredService<EncodingHelper>));
             ServiceCollection.AddSingleton<IMediaEncoder, MediaBrowser.MediaEncoding.Encoder.MediaEncoder>();
+            ServiceCollection.AddSingleton<EncodingHelper>();
 
             // TODO: Refactor to eliminate the circular dependencies here so that Lazy<T> isn't required
             ServiceCollection.AddTransient(provider => new Lazy<ILibraryMonitor>(provider.GetRequiredService<ILibraryMonitor>));
@@ -674,14 +667,14 @@ namespace Emby.Server.Implementations
 
             ServiceCollection.AddSingleton<ISubtitleEncoder, MediaBrowser.MediaEncoding.Subtitles.SubtitleEncoder>();
 
-            ServiceCollection.AddSingleton<EncodingHelper>();
-
             ServiceCollection.AddSingleton<IAttachmentExtractor, MediaBrowser.MediaEncoding.Attachments.AttachmentExtractor>();
 
             ServiceCollection.AddSingleton<TranscodingJobHelper>();
             ServiceCollection.AddScoped<MediaInfoHelper>();
             ServiceCollection.AddScoped<AudioHelper>();
             ServiceCollection.AddScoped<DynamicHlsHelper>();
+
+            ServiceCollection.AddSingleton<IDirectoryService, DirectoryService>();
         }
 
         /// <summary>
@@ -779,7 +772,7 @@ namespace Emby.Server.Implementations
         {
             // For now there's no real way to inject these properly
             BaseItem.Logger = Resolve<ILogger<BaseItem>>();
-            BaseItem.ConfigurationManager = ServerConfigurationManager;
+            BaseItem.ConfigurationManager = ConfigurationManager;
             BaseItem.LibraryManager = Resolve<ILibraryManager>();
             BaseItem.ProviderManager = Resolve<IProviderManager>();
             BaseItem.LocalizationManager = Resolve<ILocalizationManager>();
@@ -801,13 +794,12 @@ namespace Emby.Server.Implementations
         /// </summary>
         private void FindParts()
         {
-            if (!ServerConfigurationManager.Configuration.IsPortAuthorized)
+            if (!ConfigurationManager.Configuration.IsPortAuthorized)
             {
-                ServerConfigurationManager.Configuration.IsPortAuthorized = true;
+                ConfigurationManager.Configuration.IsPortAuthorized = true;
                 ConfigurationManager.SaveConfiguration();
             }
 
-            ConfigurationManager.AddParts(GetExports<IConfigurationFactory>());
             _pluginManager.CreatePlugins();
 
             _urlPrefixes = GetUrlPrefixes().ToArray();
@@ -911,7 +903,7 @@ namespace Emby.Server.Implementations
         protected void OnConfigurationUpdated(object sender, EventArgs e)
         {
             var requiresRestart = false;
-            var networkConfiguration = ServerConfigurationManager.GetNetworkConfiguration();
+            var networkConfiguration = ConfigurationManager.GetNetworkConfiguration();
 
             // Don't do anything if these haven't been set yet
             if (HttpPort != 0 && HttpsPort != 0)
@@ -920,10 +912,10 @@ namespace Emby.Server.Implementations
                 if (networkConfiguration.HttpServerPortNumber != HttpPort ||
                     networkConfiguration.HttpsPortNumber != HttpsPort)
                 {
-                    if (ServerConfigurationManager.Configuration.IsPortAuthorized)
+                    if (ConfigurationManager.Configuration.IsPortAuthorized)
                     {
-                        ServerConfigurationManager.Configuration.IsPortAuthorized = false;
-                        ServerConfigurationManager.SaveConfiguration();
+                        ConfigurationManager.Configuration.IsPortAuthorized = false;
+                        ConfigurationManager.SaveConfiguration();
 
                         requiresRestart = true;
                     }
@@ -1139,16 +1131,16 @@ namespace Emby.Server.Implementations
         }
 
         /// <inheritdoc/>
-        public bool ListenWithHttps => Certificate != null && ServerConfigurationManager.GetNetworkConfiguration().EnableHttps;
+        public bool ListenWithHttps => Certificate != null && ConfigurationManager.GetNetworkConfiguration().EnableHttps;
 
         /// <inheritdoc/>
         public string GetSmartApiUrl(IPAddress ipAddress, int? port = null)
         {
             // Published server ends with a /
-            if (_startupOptions.PublishedServerUrl != null)
+            if (!string.IsNullOrEmpty(PublishedServerUrl))
             {
                 // Published server ends with a '/', so we need to remove it.
-                return _startupOptions.PublishedServerUrl.ToString().Trim('/');
+                return PublishedServerUrl.Trim('/');
             }
 
             string smart = NetManager.GetBindInterface(ipAddress, out port);
@@ -1165,10 +1157,10 @@ namespace Emby.Server.Implementations
         public string GetSmartApiUrl(HttpRequest request, int? port = null)
         {
             // Published server ends with a /
-            if (_startupOptions.PublishedServerUrl != null)
+            if (!string.IsNullOrEmpty(PublishedServerUrl))
             {
                 // Published server ends with a '/', so we need to remove it.
-                return _startupOptions.PublishedServerUrl.ToString().Trim('/');
+                return PublishedServerUrl.Trim('/');
             }
 
             string smart = NetManager.GetBindInterface(request, out port);
@@ -1185,10 +1177,10 @@ namespace Emby.Server.Implementations
         public string GetSmartApiUrl(string hostname, int? port = null)
         {
             // Published server ends with a /
-            if (_startupOptions.PublishedServerUrl != null)
+            if (!string.IsNullOrEmpty(PublishedServerUrl))
             {
                 // Published server ends with a '/', so we need to remove it.
-                return _startupOptions.PublishedServerUrl.ToString().Trim('/');
+                return PublishedServerUrl.Trim('/');
             }
 
             string smart = NetManager.GetBindInterface(hostname, out port);
@@ -1223,14 +1215,14 @@ namespace Emby.Server.Implementations
                 Scheme = scheme ?? (ListenWithHttps ? Uri.UriSchemeHttps : Uri.UriSchemeHttp),
                 Host = host,
                 Port = port ?? (ListenWithHttps ? HttpsPort : HttpPort),
-                Path = ServerConfigurationManager.GetNetworkConfiguration().BaseUrl
+                Path = ConfigurationManager.GetNetworkConfiguration().BaseUrl
             }.ToString().TrimEnd('/');
         }
 
         public string FriendlyName =>
-            string.IsNullOrEmpty(ServerConfigurationManager.Configuration.ServerName)
+            string.IsNullOrEmpty(ConfigurationManager.Configuration.ServerName)
                 ? Environment.MachineName
-                : ServerConfigurationManager.Configuration.ServerName;
+                : ConfigurationManager.Configuration.ServerName;
 
         /// <summary>
         /// Shuts down.

+ 1 - 2
Emby.Server.Implementations/Channels/ChannelManager.cs

@@ -3,7 +3,6 @@ using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
 using System.Linq;
-using System.Text;
 using System.Text.Json;
 using System.Threading;
 using System.Threading.Tasks;
@@ -49,7 +48,7 @@ namespace Emby.Server.Implementations.Channels
         private readonly IProviderManager _providerManager;
         private readonly IMemoryCache _memoryCache;
         private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(1, 1);
-        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.GetOptions();
+        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="ChannelManager"/> class.

+ 35 - 14
Emby.Server.Implementations/Collections/CollectionManager.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.Globalization;
 using System.IO;
 using System.Linq;
 using System.Threading;
@@ -8,11 +7,9 @@ using System.Threading.Tasks;
 using Jellyfin.Data.Entities;
 using MediaBrowser.Common.Configuration;
 using MediaBrowser.Controller.Collections;
-using MediaBrowser.Controller.Configuration;
 using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Entities.Movies;
 using MediaBrowser.Controller.Library;
-using MediaBrowser.Controller.Plugins;
 using MediaBrowser.Controller.Providers;
 using MediaBrowser.Model.Configuration;
 using MediaBrowser.Model.Entities;
@@ -124,7 +121,7 @@ namespace Emby.Server.Implementations.Collections
 
         private IEnumerable<BoxSet> GetCollections(User user)
         {
-            var folder = GetCollectionsFolder(false).Result;
+            var folder = GetCollectionsFolder(false).GetAwaiter().GetResult();
 
             return folder == null
                 ? Enumerable.Empty<BoxSet>()
@@ -319,11 +316,11 @@ namespace Emby.Server.Implementations.Collections
         {
             var results = new Dictionary<Guid, BaseItem>();
 
-            var allBoxsets = GetCollections(user).ToList();
+            var allBoxSets = GetCollections(user).ToList();
 
             foreach (var item in items)
             {
-                if (!(item is ISupportsBoxSetGrouping))
+                if (item is not ISupportsBoxSetGrouping)
                 {
                     results[item.Id] = item;
                 }
@@ -331,20 +328,44 @@ namespace Emby.Server.Implementations.Collections
                 {
                     var itemId = item.Id;
 
-                    var currentBoxSets = allBoxsets
-                        .Where(i => i.ContainsLinkedChildByItemId(itemId))
-                        .ToList();
+                    var itemIsInBoxSet = false;
+                    foreach (var boxSet in allBoxSets)
+                    {
+                        if (!boxSet.ContainsLinkedChildByItemId(itemId))
+                        {
+                            continue;
+                        }
+
+                        itemIsInBoxSet = true;
+
+                        results.TryAdd(boxSet.Id, boxSet);
+                    }
 
-                    if (currentBoxSets.Count > 0)
+                    // skip any item that is in a box set
+                    if (itemIsInBoxSet)
                     {
-                        foreach (var boxset in currentBoxSets)
+                        continue;
+                    }
+
+                    var alreadyInResults = false;
+                    // this is kind of a performance hack because only Video has alternate versions that should be in a box set?
+                    if (item is Video video)
+                    {
+                        foreach (var childId in video.GetLocalAlternateVersionIds())
                         {
-                            results[boxset.Id] = boxset;
+                            if (!results.ContainsKey(childId))
+                            {
+                                continue;
+                            }
+
+                            alreadyInResults = true;
+                            break;
                         }
                     }
-                    else
+
+                    if (!alreadyInResults)
                     {
-                        results[item.Id] = item;
+                        results[itemId] = item;
                     }
                 }
             }

+ 0 - 1
Emby.Server.Implementations/ConfigurationOptions.cs

@@ -1,5 +1,4 @@
 using System.Collections.Generic;
-using Emby.Server.Implementations.HttpServer;
 using static MediaBrowser.Controller.Extensions.ConfigurationExtensions;
 
 namespace Emby.Server.Implementations

+ 198 - 201
Emby.Server.Implementations/Data/SqliteItemRepository.cs

@@ -1,6 +1,7 @@
 #pragma warning disable CS1591
 
 using System;
+using System.Buffers.Text;
 using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
@@ -88,7 +89,7 @@ namespace Emby.Server.Implementations.Data
             _imageProcessor = imageProcessor;
 
             _typeMapper = new TypeMapper();
-            _jsonOptions = JsonDefaults.GetOptions();
+            _jsonOptions = JsonDefaults.Options;
 
             DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db");
         }
@@ -502,7 +503,7 @@ namespace Emby.Server.Implementations.Data
                     using (var saveImagesStatement = base.PrepareStatement(db, "Update TypedBaseItems set Images=@Images where guid=@Id"))
                     {
                         saveImagesStatement.TryBind("@Id", item.Id.ToByteArray());
-                        saveImagesStatement.TryBind("@Images", SerializeImages(item));
+                        saveImagesStatement.TryBind("@Images", SerializeImages(item.ImageInfos));
 
                         saveImagesStatement.MoveNext();
                     }
@@ -897,8 +898,8 @@ namespace Emby.Server.Implementations.Data
             saveItemStatement.TryBind("@ExternalSeriesId", item.ExternalSeriesId);
             saveItemStatement.TryBind("@Tagline", item.Tagline);
 
-            saveItemStatement.TryBind("@ProviderIds", SerializeProviderIds(item));
-            saveItemStatement.TryBind("@Images", SerializeImages(item));
+            saveItemStatement.TryBind("@ProviderIds", SerializeProviderIds(item.ProviderIds));
+            saveItemStatement.TryBind("@Images", SerializeImages(item.ImageInfos));
 
             if (item.ProductionLocations.Length > 0)
             {
@@ -968,10 +969,10 @@ namespace Emby.Server.Implementations.Data
             saveItemStatement.MoveNext();
         }
 
-        private static string SerializeProviderIds(BaseItem item)
+        internal static string SerializeProviderIds(Dictionary<string, string> providerIds)
         {
             StringBuilder str = new StringBuilder();
-            foreach (var i in item.ProviderIds)
+            foreach (var i in providerIds)
             {
                 // Ideally we shouldn't need this IsNullOrWhiteSpace check,
                 // but we're seeing some cases of bad data slip through
@@ -995,35 +996,25 @@ namespace Emby.Server.Implementations.Data
             return str.ToString();
         }
 
-        private static void DeserializeProviderIds(string value, BaseItem item)
+        internal static void DeserializeProviderIds(string value, IHasProviderIds item)
         {
             if (string.IsNullOrWhiteSpace(value))
             {
                 return;
             }
 
-            if (item.ProviderIds.Count > 0)
+            foreach (var part in value.SpanSplit('|'))
             {
-                return;
-            }
-
-            var parts = value.Split('|', StringSplitOptions.RemoveEmptyEntries);
-
-            foreach (var part in parts)
-            {
-                var idParts = part.Split('=');
-
-                if (idParts.Length == 2)
+                var providerDelimiterIndex = part.IndexOf('=');
+                if (providerDelimiterIndex != -1 && providerDelimiterIndex == part.LastIndexOf('='))
                 {
-                    item.SetProviderId(idParts[0], idParts[1]);
+                    item.SetProviderId(part.Slice(0, providerDelimiterIndex).ToString(), part.Slice(providerDelimiterIndex + 1).ToString());
                 }
             }
         }
 
-        private string SerializeImages(BaseItem item)
+        internal string SerializeImages(ItemImageInfo[] images)
         {
-            var images = item.ImageInfos;
-
             if (images.Length == 0)
             {
                 return null;
@@ -1045,21 +1036,15 @@ namespace Emby.Server.Implementations.Data
             return str.ToString();
         }
 
-        private void DeserializeImages(string value, BaseItem item)
+        internal ItemImageInfo[] DeserializeImages(string value)
         {
             if (string.IsNullOrWhiteSpace(value))
             {
-                return;
-            }
-
-            if (item.ImageInfos.Length > 0)
-            {
-                return;
+                return Array.Empty<ItemImageInfo>();
             }
 
-            var parts = value.Split('|' , StringSplitOptions.RemoveEmptyEntries);
             var list = new List<ItemImageInfo>();
-            foreach (var part in parts)
+            foreach (var part in value.SpanSplit('|'))
             {
                 var image = ItemImageInfoFromValueString(part);
 
@@ -1069,15 +1054,14 @@ namespace Emby.Server.Implementations.Data
                 }
             }
 
-            item.ImageInfos = list.ToArray();
+            return list.ToArray();
         }
 
-        public void AppendItemImageInfo(StringBuilder bldr, ItemImageInfo image)
+        private void AppendItemImageInfo(StringBuilder bldr, ItemImageInfo image)
         {
             const char Delimiter = '*';
 
             var path = image.Path ?? string.Empty;
-            var hash = image.BlurHash ?? string.Empty;
 
             bldr.Append(GetPathToSave(path))
                 .Append(Delimiter)
@@ -1087,48 +1071,105 @@ namespace Emby.Server.Implementations.Data
                 .Append(Delimiter)
                 .Append(image.Width)
                 .Append(Delimiter)
-                .Append(image.Height)
-                .Append(Delimiter)
-                // Replace delimiters with other characters.
-                // This can be removed when we migrate to a proper DB.
-                .Append(hash.Replace('*', '/').Replace('|', '\\'));
+                .Append(image.Height);
+
+            var hash = image.BlurHash;
+            if (!string.IsNullOrEmpty(hash))
+            {
+                bldr.Append(Delimiter)
+                    // Replace delimiters with other characters.
+                    // This can be removed when we migrate to a proper DB.
+                    .Append(hash.Replace('*', '/').Replace('|', '\\'));
+            }
         }
 
-        public ItemImageInfo ItemImageInfoFromValueString(string value)
+        internal ItemImageInfo ItemImageInfoFromValueString(ReadOnlySpan<char> value)
         {
-            var parts = value.Split('*', StringSplitOptions.None);
+            var nextSegment = value.IndexOf('*');
+            if (nextSegment == -1)
+            {
+                return null;
+            }
 
-            if (parts.Length < 3)
+            ReadOnlySpan<char> path = value[..nextSegment];
+            value = value[(nextSegment + 1)..];
+            nextSegment = value.IndexOf('*');
+            if (nextSegment == -1)
             {
                 return null;
             }
 
-            var image = new ItemImageInfo();
+            ReadOnlySpan<char> dateModified = value[..nextSegment];
+            value = value[(nextSegment + 1)..];
+            nextSegment = value.IndexOf('*');
+            if (nextSegment == -1)
+            {
+                nextSegment = value.Length;
+            }
+
+            ReadOnlySpan<char> imageType = value[..nextSegment];
 
-            image.Path = RestorePath(parts[0]);
+            var image = new ItemImageInfo
+            {
+                Path = RestorePath(path.ToString())
+            };
 
-            if (long.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out var ticks))
+            if (long.TryParse(dateModified, NumberStyles.Any, CultureInfo.InvariantCulture, out var ticks))
             {
                 image.DateModified = new DateTime(ticks, DateTimeKind.Utc);
             }
 
-            if (Enum.TryParse(parts[2], true, out ImageType type))
+            if (Enum.TryParse(imageType.ToString(), true, out ImageType type))
             {
                 image.Type = type;
             }
 
-            if (parts.Length >= 5)
+            // Optional parameters: width*height*blurhash
+            if (nextSegment + 1 < value.Length - 1)
             {
-                if (int.TryParse(parts[3], NumberStyles.Integer, CultureInfo.InvariantCulture, out var width)
-                    && int.TryParse(parts[4], NumberStyles.Integer, CultureInfo.InvariantCulture, out var height))
+                value = value[(nextSegment + 1)..];
+                nextSegment = value.IndexOf('*');
+                if (nextSegment == -1 || nextSegment == value.Length)
+                {
+                    return image;
+                }
+
+                ReadOnlySpan<char> widthSpan = value[..nextSegment];
+
+                value = value[(nextSegment + 1)..];
+                nextSegment = value.IndexOf('*');
+                if (nextSegment == -1)
+                {
+                    nextSegment = value.Length;
+                }
+
+                ReadOnlySpan<char> heightSpan = value[..nextSegment];
+
+                if (int.TryParse(widthSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var width)
+                    && int.TryParse(heightSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var height))
                 {
                     image.Width = width;
                     image.Height = height;
                 }
 
-                if (parts.Length >= 6)
+                if (nextSegment < value.Length - 1)
                 {
-                    image.BlurHash = parts[5].Replace('/', '*').Replace('\\', '|');
+                    value = value[(nextSegment + 1)..];
+                    var length = value.Length;
+
+                    Span<char> blurHashSpan = stackalloc char[length];
+                    for (int i = 0; i < length; i++)
+                    {
+                        var c = value[i];
+                        blurHashSpan[i] = c switch
+                        {
+                            '/' => '*',
+                            '\\' => '|',
+                            _ => c
+                        };
+                    }
+
+                    image.BlurHash = new string(blurHashSpan);
                 }
             }
 
@@ -1311,7 +1352,14 @@ namespace Emby.Server.Implementations.Data
 
             if (!reader.IsDBNull(index))
             {
-                item.ChannelId = new Guid(reader.GetString(index));
+                if (!Utf8Parser.TryParse(reader[index].ToBlob(), out Guid value, out _, standardFormat: 'N'))
+                {
+                    var str = reader.GetString(index);
+                    Logger.LogWarning("{ChannelId} isn't in the expected format", str);
+                    value = new Guid(str);
+                }
+
+                item.ChannelId = value;
             }
 
             index++;
@@ -1790,7 +1838,7 @@ namespace Emby.Server.Implementations.Data
                 index++;
             }
 
-            if (!reader.IsDBNull(index))
+            if (item.ProviderIds.Count == 0 && !reader.IsDBNull(index))
             {
                 DeserializeProviderIds(reader.GetString(index), item);
             }
@@ -1799,9 +1847,9 @@ namespace Emby.Server.Implementations.Data
 
             if (query.DtoOptions.EnableImages)
             {
-                if (!reader.IsDBNull(index))
+                if (item.ImageInfos.Length == 0 && !reader.IsDBNull(index))
                 {
-                    DeserializeImages(reader.GetString(index), item);
+                    item.ImageInfos = DeserializeImages(reader.GetString(index));
                 }
 
                 index++;
@@ -2116,30 +2164,7 @@ namespace Emby.Server.Implementations.Data
                     || query.IsLiked.HasValue;
         }
 
-        private readonly ItemFields[] _allFields = Enum.GetNames(typeof(ItemFields))
-            .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true))
-            .ToArray();
-
-        private string[] GetColumnNamesFromField(ItemFields field)
-        {
-            switch (field)
-            {
-                case ItemFields.Settings:
-                    return new[] { "IsLocked", "PreferredMetadataCountryCode", "PreferredMetadataLanguage", "LockedFields" };
-                case ItemFields.ServiceName:
-                    return new[] { "ExternalServiceId" };
-                case ItemFields.SortName:
-                    return new[] { "ForcedSortName" };
-                case ItemFields.Taglines:
-                    return new[] { "Tagline" };
-                case ItemFields.Tags:
-                    return new[] { "Tags" };
-                case ItemFields.IsHD:
-                    return Array.Empty<string>();
-                default:
-                    return new[] { field.ToString() };
-            }
-        }
+        private readonly ItemFields[] _allFields = Enum.GetValues<ItemFields>();
 
         private bool HasField(InternalItemsQuery query, ItemFields name)
         {
@@ -2329,9 +2354,32 @@ namespace Emby.Server.Implementations.Data
             {
                 if (!HasField(query, field))
                 {
-                    foreach (var fieldToRemove in GetColumnNamesFromField(field))
+                    switch (field)
                     {
-                        list.Remove(fieldToRemove);
+                        case ItemFields.Settings:
+                            list.Remove("IsLocked");
+                            list.Remove("PreferredMetadataCountryCode");
+                            list.Remove("PreferredMetadataLanguage");
+                            list.Remove("LockedFields");
+                            break;
+                        case ItemFields.ServiceName:
+                            list.Remove("ExternalServiceId");
+                            break;
+                        case ItemFields.SortName:
+                            list.Remove("ForcedSortName");
+                            break;
+                        case ItemFields.Taglines:
+                            list.Remove("Tagline");
+                            break;
+                        case ItemFields.Tags:
+                            list.Remove("Tags");
+                            break;
+                        case ItemFields.IsHD:
+                            // do nothing
+                            break;
+                        default:
+                            list.Remove(field.ToString());
+                            break;
                     }
                 }
             }
@@ -2578,9 +2626,9 @@ namespace Emby.Server.Implementations.Data
             }
 
             var commandText = "select "
-                            + string.Join(',', GetFinalColumnsToSelect(query, new[] { "count(distinct PresentationUniqueKey)" }))
-                            + GetFromText()
-                            + GetJoinUserDataText(query);
+                              + string.Join(',', GetFinalColumnsToSelect(query, new[] { "count(distinct PresentationUniqueKey)" }))
+                              + GetFromText()
+                              + GetJoinUserDataText(query);
 
             var whereClauses = GetWhereClauses(query, null);
             if (whereClauses.Count != 0)
@@ -2721,87 +2769,22 @@ namespace Emby.Server.Implementations.Data
 
         private string FixUnicodeChars(string buffer)
         {
-            if (buffer.IndexOf('\u2013', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u2013', '-'); // en dash
-            }
-
-            if (buffer.IndexOf('\u2014', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u2014', '-'); // em dash
-            }
-
-            if (buffer.IndexOf('\u2015', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u2015', '-'); // horizontal bar
-            }
-
-            if (buffer.IndexOf('\u2017', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u2017', '_'); // double low line
-            }
-
-            if (buffer.IndexOf('\u2018', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u2018', '\''); // left single quotation mark
-            }
-
-            if (buffer.IndexOf('\u2019', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u2019', '\''); // right single quotation mark
-            }
-
-            if (buffer.IndexOf('\u201a', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u201a', ','); // single low-9 quotation mark
-            }
-
-            if (buffer.IndexOf('\u201b', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u201b', '\''); // single high-reversed-9 quotation mark
-            }
-
-            if (buffer.IndexOf('\u201c', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u201c', '\"'); // left double quotation mark
-            }
-
-            if (buffer.IndexOf('\u201d', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u201d', '\"'); // right double quotation mark
-            }
-
-            if (buffer.IndexOf('\u201e', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u201e', '\"'); // double low-9 quotation mark
-            }
-
-            if (buffer.IndexOf('\u2026', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace("\u2026", "...", StringComparison.Ordinal); // horizontal ellipsis
-            }
-
-            if (buffer.IndexOf('\u2032', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u2032', '\''); // prime
-            }
-
-            if (buffer.IndexOf('\u2033', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u2033', '\"'); // double prime
-            }
-
-            if (buffer.IndexOf('\u0060', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u0060', '\''); // grave accent
-            }
-
-            if (buffer.IndexOf('\u00B4', StringComparison.Ordinal) > -1)
-            {
-                buffer = buffer.Replace('\u00B4', '\''); // acute accent
-            }
-
-            return buffer;
+            buffer = buffer.Replace('\u2013', '-'); // en dash
+            buffer = buffer.Replace('\u2014', '-'); // em dash
+            buffer = buffer.Replace('\u2015', '-'); // horizontal bar
+            buffer = buffer.Replace('\u2017', '_'); // double low line
+            buffer = buffer.Replace('\u2018', '\''); // left single quotation mark
+            buffer = buffer.Replace('\u2019', '\''); // right single quotation mark
+            buffer = buffer.Replace('\u201a', ','); // single low-9 quotation mark
+            buffer = buffer.Replace('\u201b', '\''); // single high-reversed-9 quotation mark
+            buffer = buffer.Replace('\u201c', '\"'); // left double quotation mark
+            buffer = buffer.Replace('\u201d', '\"'); // right double quotation mark
+            buffer = buffer.Replace('\u201e', '\"'); // double low-9 quotation mark
+            buffer = buffer.Replace("\u2026", "...", StringComparison.Ordinal); // horizontal ellipsis
+            buffer = buffer.Replace('\u2032', '\''); // prime
+            buffer = buffer.Replace('\u2033', '\"'); // double prime
+            buffer = buffer.Replace('\u0060', '\''); // grave accent
+            return buffer.Replace('\u00B4', '\''); // acute accent
         }
 
         private void AddItem(List<BaseItem> items, BaseItem newItem)
@@ -3584,11 +3567,11 @@ namespace Emby.Server.Implementations.Data
                 statement?.TryBind("@IsFolder", query.IsFolder);
             }
 
-            var includeTypes = query.IncludeItemTypes.SelectMany(MapIncludeItemTypes).ToArray();
+            var includeTypes = query.IncludeItemTypes.Select(MapIncludeItemTypes).Where(x => x != null).ToArray();
             // Only specify excluded types if no included types are specified
             if (includeTypes.Length == 0)
             {
-                var excludeTypes = query.ExcludeItemTypes.SelectMany(MapIncludeItemTypes).ToArray();
+                var excludeTypes = query.ExcludeItemTypes.Select(MapIncludeItemTypes).Where(x => x != null).ToArray();
                 if (excludeTypes.Length == 1)
                 {
                     whereClauses.Add("type<>@type");
@@ -4532,7 +4515,7 @@ namespace Emby.Server.Implementations.Data
                 whereClauses.Add(GetProviderIdClause(query.HasTvdbId.Value, "tvdb"));
             }
 
-            var includedItemByNameTypes = GetItemByNameTypesInQuery(query).SelectMany(MapIncludeItemTypes).ToList();
+            var includedItemByNameTypes = GetItemByNameTypesInQuery(query);
             var enableItemsByName = (query.IncludeItemsByName ?? false) && includedItemByNameTypes.Count > 0;
 
             var queryTopParentIds = query.TopParentIds;
@@ -4790,27 +4773,27 @@ namespace Emby.Server.Implementations.Data
 
             if (IsTypeInQuery(nameof(Person), query))
             {
-                list.Add(nameof(Person));
+                list.Add(typeof(Person).FullName);
             }
 
             if (IsTypeInQuery(nameof(Genre), query))
             {
-                list.Add(nameof(Genre));
+                list.Add(typeof(Genre).FullName);
             }
 
             if (IsTypeInQuery(nameof(MusicGenre), query))
             {
-                list.Add(nameof(MusicGenre));
+                list.Add(typeof(MusicGenre).FullName);
             }
 
             if (IsTypeInQuery(nameof(MusicArtist), query))
             {
-                list.Add(nameof(MusicArtist));
+                list.Add(typeof(MusicArtist).FullName);
             }
 
             if (IsTypeInQuery(nameof(Studio), query))
             {
-                list.Add(nameof(Studio));
+                list.Add(typeof(Studio).FullName);
             }
 
             return list;
@@ -4915,15 +4898,10 @@ namespace Emby.Server.Implementations.Data
             typeof(AggregateFolder)
         };
 
-        public void UpdateInheritedValues(CancellationToken cancellationToken)
-        {
-            UpdateInheritedTags(cancellationToken);
-        }
-
-        private void UpdateInheritedTags(CancellationToken cancellationToken)
+        public void UpdateInheritedValues()
         {
             string sql = string.Join(
-                ";",
+                ';',
                 new string[]
                 {
                     "delete from itemvalues where type = 6",
@@ -4946,37 +4924,38 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
             }
         }
 
-        private static Dictionary<string, string[]> GetTypeMapDictionary()
+        private static Dictionary<string, string> GetTypeMapDictionary()
         {
-            var dict = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
+            var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
 
             foreach (var t in _knownTypes)
             {
-                dict[t.Name] = new[] { t.FullName };
+                dict[t.Name] = t.FullName ;
             }
 
-            dict["Program"] = new[] { typeof(LiveTvProgram).FullName };
-            dict["TvChannel"] = new[] { typeof(LiveTvChannel).FullName };
+            dict["Program"] = typeof(LiveTvProgram).FullName;
+            dict["TvChannel"] = typeof(LiveTvChannel).FullName;
 
             return dict;
         }
 
         // Not crazy about having this all the way down here, but at least it's in one place
-        private readonly Dictionary<string, string[]> _types = GetTypeMapDictionary();
+        private readonly Dictionary<string, string> _types = GetTypeMapDictionary();
 
-        private string[] MapIncludeItemTypes(string value)
+        private string MapIncludeItemTypes(string value)
         {
-            if (_types.TryGetValue(value, out string[] result))
+            if (_types.TryGetValue(value, out string result))
             {
                 return result;
             }
 
             if (IsValidType(value))
             {
-                return new[] { value };
+                return value;
             }
 
-            return Array.Empty<string>();
+            Logger.LogWarning("Unknown item type: {ItemType}", value);
+            return null;
         }
 
         public void DeleteItem(Guid id)
@@ -5279,31 +5258,46 @@ AND Type = @InternalPersonType)");
 
         public List<string> GetStudioNames()
         {
-            return GetItemValueNames(new[] { 3 }, new List<string>(), new List<string>());
+            return GetItemValueNames(new[] { 3 }, Array.Empty<string>(), Array.Empty<string>());
         }
 
         public List<string> GetAllArtistNames()
         {
-            return GetItemValueNames(new[] { 0, 1 }, new List<string>(), new List<string>());
+            return GetItemValueNames(new[] { 0, 1 }, Array.Empty<string>(), Array.Empty<string>());
         }
 
         public List<string> GetMusicGenreNames()
         {
-            return GetItemValueNames(new[] { 2 }, new List<string> { "Audio", "MusicVideo", "MusicAlbum", "MusicArtist" }, new List<string>());
+            return GetItemValueNames(
+                new[] { 2 },
+                new string[]
+                {
+                    typeof(Audio).FullName,
+                    typeof(MusicVideo).FullName,
+                    typeof(MusicAlbum).FullName,
+                    typeof(MusicArtist).FullName
+                },
+                Array.Empty<string>());
         }
 
         public List<string> GetGenreNames()
         {
-            return GetItemValueNames(new[] { 2 }, new List<string>(), new List<string> { "Audio", "MusicVideo", "MusicAlbum", "MusicArtist" });
+            return GetItemValueNames(
+                new[] { 2 },
+                Array.Empty<string>(),
+                new string[]
+                {
+                    typeof(Audio).FullName,
+                    typeof(MusicVideo).FullName,
+                    typeof(MusicAlbum).FullName,
+                    typeof(MusicArtist).FullName
+                });
         }
 
-        private List<string> GetItemValueNames(int[] itemValueTypes, List<string> withItemTypes, List<string> excludeItemTypes)
+        private List<string> GetItemValueNames(int[] itemValueTypes, IReadOnlyList<string> withItemTypes, IReadOnlyList<string> excludeItemTypes)
         {
             CheckDisposed();
 
-            withItemTypes = withItemTypes.SelectMany(MapIncludeItemTypes).ToList();
-            excludeItemTypes = excludeItemTypes.SelectMany(MapIncludeItemTypes).ToList();
-
             var now = DateTime.UtcNow;
 
             var typeClause = itemValueTypes.Length == 1 ?
@@ -5415,7 +5409,6 @@ AND Type = @InternalPersonType)");
                 ItemIds = query.ItemIds,
                 TopParentIds = query.TopParentIds,
                 ParentId = query.ParentId,
-                IsPlayed = query.IsPlayed,
                 IsAiring = query.IsAiring,
                 IsMovie = query.IsMovie,
                 IsSports = query.IsSports,
@@ -5441,6 +5434,7 @@ AND Type = @InternalPersonType)");
 
             var outerQuery = new InternalItemsQuery(query.User)
             {
+                IsPlayed = query.IsPlayed,
                 IsFavorite = query.IsFavorite,
                 IsFavoriteOrLiked = query.IsFavoriteOrLiked,
                 IsLiked = query.IsLiked,
@@ -5809,7 +5803,10 @@ AND Type = @InternalPersonType)");
                 var endIndex = Math.Min(people.Count, startIndex + Limit);
                 for (var i = startIndex; i < endIndex; i++)
                 {
-                    insertText.AppendFormat("(@ItemId, @Name{0}, @Role{0}, @PersonType{0}, @SortOrder{0}, @ListOrder{0}),", i.ToString(CultureInfo.InvariantCulture));
+                    insertText.AppendFormat(
+                        CultureInfo.InvariantCulture,
+                        "(@ItemId, @Name{0}, @Role{0}, @PersonType{0}, @SortOrder{0}, @ListOrder{0}),",
+                        i.ToString(CultureInfo.InvariantCulture));
                 }
 
                 // Remove last comma
@@ -6261,7 +6258,7 @@ AND Type = @InternalPersonType)");
             CheckDisposed();
             if (id == Guid.Empty)
             {
-                throw new ArgumentException(nameof(id));
+                throw new ArgumentException("Guid can't be empty.", nameof(id));
             }
 
             if (attachments == null)

+ 3 - 12
Emby.Server.Implementations/Dto/DtoService.cs

@@ -665,10 +665,7 @@ namespace Emby.Server.Implementations.Dto
             var tag = GetImageCacheTag(item, image);
             if (!string.IsNullOrEmpty(image.BlurHash))
             {
-                if (dto.ImageBlurHashes == null)
-                {
-                    dto.ImageBlurHashes = new Dictionary<ImageType, Dictionary<string, string>>();
-                }
+                dto.ImageBlurHashes ??= new Dictionary<ImageType, Dictionary<string, string>>();
 
                 if (!dto.ImageBlurHashes.ContainsKey(image.Type))
                 {
@@ -702,10 +699,7 @@ namespace Emby.Server.Implementations.Dto
 
             if (hashes.Count > 0)
             {
-                if (dto.ImageBlurHashes == null)
-                {
-                    dto.ImageBlurHashes = new Dictionary<ImageType, Dictionary<string, string>>();
-                }
+                dto.ImageBlurHashes ??= new Dictionary<ImageType, Dictionary<string, string>>();
 
                 dto.ImageBlurHashes[imageType] = hashes;
             }
@@ -898,10 +892,7 @@ namespace Emby.Server.Implementations.Dto
                     dto.Taglines = new string[] { item.Tagline };
                 }
 
-                if (dto.Taglines == null)
-                {
-                    dto.Taglines = Array.Empty<string>();
-                }
+                dto.Taglines ??= Array.Empty<string>();
             }
 
             dto.Type = item.GetBaseItemKind();

+ 6 - 8
Emby.Server.Implementations/Emby.Server.Implementations.csproj

@@ -27,10 +27,11 @@
     <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
     <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
     <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.3" />
     <PackageReference Include="Mono.Nat" Version="3.0.1" />
-    <PackageReference Include="prometheus-net.DotNetRuntime" Version="3.4.1" />
-    <PackageReference Include="sharpcompress" Version="0.28.1" />
-    <PackageReference Include="SQLitePCL.pretty.netstandard" Version="2.1.0" />
+    <PackageReference Include="prometheus-net.DotNetRuntime" Version="4.0.0" />
+    <PackageReference Include="sharpcompress" Version="0.28.2" />
+    <PackageReference Include="SQLitePCL.pretty.netstandard" Version="2.2.0" />
     <PackageReference Include="DotNet.Glob" Version="3.1.2" />
   </ItemGroup>
 
@@ -45,20 +46,17 @@
     <TreatWarningsAsErrors Condition=" '$(Configuration)' == 'Release'">true</TreatWarningsAsErrors>
     <!-- https://github.com/microsoft/ApplicationInsights-dotnet/issues/2047 -->
     <NoWarn>AD0001</NoWarn>
+    <AnalysisMode Condition=" '$(Configuration)' == 'Debug' ">AllEnabledByDefault</AnalysisMode>
+    <CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
   </PropertyGroup>
 
   <!-- Code Analyzers-->
   <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="All" />
     <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
     <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
     <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
   </ItemGroup>
 
-  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
-    <CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
-  </PropertyGroup>
-
   <ItemGroup>
     <EmbeddedResource Include="Localization\iso6392.txt" />
     <EmbeddedResource Include="Localization\countries.json" />

+ 11 - 0
Emby.Server.Implementations/EntryPoints/UdpServerEntryPoint.cs

@@ -1,5 +1,6 @@
 #nullable enable
 
+using System;
 using System.Net.Sockets;
 using System.Threading;
 using System.Threading.Tasks;
@@ -51,6 +52,8 @@ namespace Emby.Server.Implementations.EntryPoints
         /// <inheritdoc />
         public Task RunAsync()
         {
+            CheckDisposed();
+
             try
             {
                 _udpServer = new UdpServer(_logger, _appHost, _config);
@@ -64,6 +67,14 @@ namespace Emby.Server.Implementations.EntryPoints
             return Task.CompletedTask;
         }
 
+        private void CheckDisposed()
+        {
+            if (_disposed)
+            {
+                throw new ObjectDisposedException(this.GetType().Name);
+            }
+        }
+
         /// <inheritdoc />
         public void Dispose()
         {

+ 0 - 1
Emby.Server.Implementations/HttpServer/Security/AuthService.cs

@@ -1,6 +1,5 @@
 #pragma warning disable CS1591
 
-using System;
 using Jellyfin.Data.Enums;
 using MediaBrowser.Controller.Authentication;
 using MediaBrowser.Controller.Net;

+ 1 - 1
Emby.Server.Implementations/HttpServer/Security/SessionContext.cs

@@ -28,7 +28,7 @@ namespace Emby.Server.Implementations.HttpServer.Security
             var authorization = _authContext.GetAuthorizationInfo(requestContext);
 
             var user = authorization.User;
-            return _sessionManager.LogSessionActivity(authorization.Client, authorization.Version, authorization.DeviceId, authorization.Device, requestContext.GetNormalizedRemoteIp(), user);
+            return _sessionManager.LogSessionActivity(authorization.Client, authorization.Version, authorization.DeviceId, authorization.Device, requestContext.GetNormalizedRemoteIp().ToString(), user);
         }
 
         public SessionInfo GetSession(object requestContext)

+ 1 - 1
Emby.Server.Implementations/HttpServer/WebSocketConnection.cs

@@ -56,7 +56,7 @@ namespace Emby.Server.Implementations.HttpServer
             RemoteEndPoint = remoteEndPoint;
             QueryString = query;
 
-            _jsonOptions = JsonDefaults.GetOptions();
+            _jsonOptions = JsonDefaults.Options;
             LastActivityDate = DateTime.Now;
         }
 

+ 4 - 0
Emby.Server.Implementations/HttpServer/WebSocketManager.cs

@@ -14,15 +14,18 @@ namespace Emby.Server.Implementations.HttpServer
     public class WebSocketManager : IWebSocketManager
     {
         private readonly IWebSocketListener[] _webSocketListeners;
+        private readonly IAuthService _authService;
         private readonly ILogger<WebSocketManager> _logger;
         private readonly ILoggerFactory _loggerFactory;
 
         public WebSocketManager(
+            IAuthService authService,
             IEnumerable<IWebSocketListener> webSocketListeners,
             ILogger<WebSocketManager> logger,
             ILoggerFactory loggerFactory)
         {
             _webSocketListeners = webSocketListeners.ToArray();
+            _authService = authService;
             _logger = logger;
             _loggerFactory = loggerFactory;
         }
@@ -30,6 +33,7 @@ namespace Emby.Server.Implementations.HttpServer
         /// <inheritdoc />
         public async Task WebSocketRequestHandler(HttpContext context)
         {
+            _ = _authService.Authenticate(context.Request);
             try
             {
                 _logger.LogInformation("WS {IP} request", context.Connection.RemoteIpAddress);

+ 49 - 53
Emby.Server.Implementations/IO/ManagedFileSystem.cs

@@ -2,11 +2,10 @@
 
 using System;
 using System.Collections.Generic;
-using System.Diagnostics;
 using System.Globalization;
 using System.IO;
 using System.Linq;
-using System.Text;
+using System.Runtime.InteropServices;
 using MediaBrowser.Common.Configuration;
 using MediaBrowser.Model.IO;
 using MediaBrowser.Model.System;
@@ -24,7 +23,7 @@ namespace Emby.Server.Implementations.IO
 
         private readonly List<IShortcutHandler> _shortcutHandlers = new List<IShortcutHandler>();
         private readonly string _tempPath;
-        private readonly bool _isEnvironmentCaseInsensitive;
+        private static readonly bool _isEnvironmentCaseInsensitive = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
 
         public ManagedFileSystem(
             ILogger<ManagedFileSystem> logger,
@@ -32,8 +31,6 @@ namespace Emby.Server.Implementations.IO
         {
             Logger = logger;
             _tempPath = applicationPaths.TempDirectory;
-
-            _isEnvironmentCaseInsensitive = OperatingSystem.Id == OperatingSystemId.Windows;
         }
 
         public virtual void AddShortcutHandler(IShortcutHandler handler)
@@ -72,7 +69,7 @@ namespace Emby.Server.Implementations.IO
             }
 
             var extension = Path.GetExtension(filename);
-            var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
+            var handler = _shortcutHandlers.Find(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
 
             return handler?.Resolve(filename);
         }
@@ -249,13 +246,20 @@ namespace Emby.Server.Implementations.IO
                     // Issue #2354 get the size of files behind symbolic links
                     if (fileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint))
                     {
-                        using (Stream thisFileStream = File.OpenRead(fileInfo.FullName))
+                        try
                         {
-                            result.Length = thisFileStream.Length;
+                            using (Stream thisFileStream = File.OpenRead(fileInfo.FullName))
+                            {
+                                result.Length = thisFileStream.Length;
+                            }
+                        }
+                        catch (FileNotFoundException ex)
+                        {
+                            // Dangling symlinks cannot be detected before opening the file unfortunately...
+                            Logger.LogError(ex, "Reading the file size of the symlink at {Path} failed. Marking the file as not existing.", fileInfo.FullName);
+                            result.Exists = false;
                         }
                     }
-
-                    result.DirectoryName = fileInfo.DirectoryName;
                 }
 
                 result.CreationTimeUtc = GetCreationTimeUtc(info);
@@ -294,16 +298,37 @@ namespace Emby.Server.Implementations.IO
         /// <param name="filename">The filename.</param>
         /// <returns>System.String.</returns>
         /// <exception cref="ArgumentNullException">The filename is null.</exception>
-        public virtual string GetValidFilename(string filename)
+        public string GetValidFilename(string filename)
         {
-            var builder = new StringBuilder(filename);
-
-            foreach (var c in Path.GetInvalidFileNameChars())
+            var invalid = Path.GetInvalidFileNameChars();
+            var first = filename.IndexOfAny(invalid);
+            if (first == -1)
             {
-                builder = builder.Replace(c, ' ');
+                // Fast path for clean strings
+                return filename;
             }
 
-            return builder.ToString();
+            return string.Create(
+                filename.Length,
+                (filename, invalid, first),
+                (chars, state) =>
+                {
+                    state.filename.AsSpan().CopyTo(chars);
+
+                    chars[state.first++] = ' ';
+
+                    var len = chars.Length;
+                    foreach (var c in state.invalid)
+                    {
+                        for (int i = state.first; i < len; i++)
+                        {
+                            if (chars[i] == c)
+                            {
+                                chars[i] = ' ';
+                            }
+                        }
+                    }
+                });
         }
 
         /// <summary>
@@ -487,26 +512,9 @@ namespace Emby.Server.Implementations.IO
                 throw new ArgumentNullException(nameof(path));
             }
 
-            var separatorChar = Path.DirectorySeparatorChar;
-
-            return path.IndexOf(parentPath.TrimEnd(separatorChar) + separatorChar, StringComparison.OrdinalIgnoreCase) != -1;
-        }
-
-        public virtual bool IsRootPath(string path)
-        {
-            if (string.IsNullOrEmpty(path))
-            {
-                throw new ArgumentNullException(nameof(path));
-            }
-
-            var parent = Path.GetDirectoryName(path);
-
-            if (!string.IsNullOrEmpty(parent))
-            {
-                return false;
-            }
-
-            return true;
+            return path.Contains(
+                Path.TrimEndingDirectorySeparator(parentPath) + Path.DirectorySeparatorChar,
+                _isEnvironmentCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
         }
 
         public virtual string NormalizePath(string path)
@@ -521,7 +529,7 @@ namespace Emby.Server.Implementations.IO
                 return path;
             }
 
-            return path.TrimEnd(Path.DirectorySeparatorChar);
+            return Path.TrimEndingDirectorySeparator(path);
         }
 
         public virtual bool AreEqual(string path1, string path2)
@@ -536,7 +544,10 @@ namespace Emby.Server.Implementations.IO
                 return false;
             }
 
-            return string.Equals(NormalizePath(path1), NormalizePath(path2), StringComparison.OrdinalIgnoreCase);
+            return string.Equals(
+                NormalizePath(path1),
+                NormalizePath(path2),
+                _isEnvironmentCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
         }
 
         public virtual string GetFileNameWithoutExtension(FileSystemMetadata info)
@@ -689,20 +700,5 @@ namespace Emby.Server.Implementations.IO
                 AttributesToSkip = 0
             };
         }
-
-        private static void RunProcess(string path, string args, string workingDirectory)
-        {
-            using (var process = Process.Start(new ProcessStartInfo
-            {
-                Arguments = args,
-                FileName = path,
-                CreateNoWindow = true,
-                WorkingDirectory = workingDirectory,
-                WindowStyle = ProcessWindowStyle.Normal
-            }))
-            {
-                process.WaitForExit();
-            }
-        }
     }
 }

+ 6 - 7
Emby.Server.Implementations/IStartupOptions.cs

@@ -1,6 +1,5 @@
 #pragma warning disable CS1591
-
-using System;
+#nullable enable
 
 namespace Emby.Server.Implementations
 {
@@ -9,7 +8,7 @@ namespace Emby.Server.Implementations
         /// <summary>
         /// Gets the value of the --ffmpeg command line option.
         /// </summary>
-        string FFmpegPath { get; }
+        string? FFmpegPath { get; }
 
         /// <summary>
         /// Gets the value of the --service command line option.
@@ -19,21 +18,21 @@ namespace Emby.Server.Implementations
         /// <summary>
         /// Gets the value of the --package-name command line option.
         /// </summary>
-        string PackageName { get; }
+        string? PackageName { get; }
 
         /// <summary>
         /// Gets the value of the --restartpath command line option.
         /// </summary>
-        string RestartPath { get; }
+        string? RestartPath { get; }
 
         /// <summary>
         /// Gets the value of the --restartargs command line option.
         /// </summary>
-        string RestartArgs { get; }
+        string? RestartArgs { get; }
 
         /// <summary>
         /// Gets the value of the --published-server-url command line option.
         /// </summary>
-        Uri PublishedServerUrl { get; }
+        string? PublishedServerUrl { get; }
     }
 }

+ 0 - 8
Emby.Server.Implementations/Images/ArtistImageProvider.cs

@@ -2,20 +2,12 @@
 
 using System;
 using System.Collections.Generic;
-using System.Linq;
-using Emby.Server.Implementations.Images;
 using MediaBrowser.Common.Configuration;
 using MediaBrowser.Controller.Drawing;
-using MediaBrowser.Controller.Dto;
 using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Entities.Audio;
-using MediaBrowser.Controller.Entities.Movies;
-using MediaBrowser.Controller.Entities.TV;
-using MediaBrowser.Controller.Playlists;
 using MediaBrowser.Controller.Providers;
-using MediaBrowser.Model.Entities;
 using MediaBrowser.Model.IO;
-using MediaBrowser.Model.Querying;
 
 namespace Emby.Server.Implementations.Images
 {

+ 0 - 1
Emby.Server.Implementations/Images/DynamicImageProvider.cs

@@ -4,7 +4,6 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
-using Emby.Server.Implementations.Images;
 using MediaBrowser.Common.Configuration;
 using MediaBrowser.Controller.Drawing;
 using MediaBrowser.Controller.Dto;

+ 1 - 3
Emby.Server.Implementations/Images/PlaylistImageProvider.cs

@@ -29,9 +29,7 @@ namespace Emby.Server.Implementations.Images
                 {
                     var subItem = i.Item2;
 
-                    var episode = subItem as Episode;
-
-                    if (episode != null)
+                    if (subItem is Episode episode)
                     {
                         var series = episode.Series;
                         if (series != null && series.HasImage(ImageType.Primary))

+ 111 - 83
Emby.Server.Implementations/Library/LibraryManager.cs

@@ -48,6 +48,7 @@ using MediaBrowser.Providers.MediaInfo;
 using Microsoft.Extensions.Caching.Memory;
 using Microsoft.Extensions.Logging;
 using Episode = MediaBrowser.Controller.Entities.TV.Episode;
+using EpisodeInfo = Emby.Naming.TV.EpisodeInfo;
 using Genre = MediaBrowser.Controller.Entities.Genre;
 using Person = MediaBrowser.Controller.Entities.Person;
 using VideoResolver = Emby.Naming.Video.VideoResolver;
@@ -175,10 +176,7 @@ namespace Emby.Server.Implementations.Library
                 {
                     lock (_rootFolderSyncLock)
                     {
-                        if (_rootFolder == null)
-                        {
-                            _rootFolder = CreateRootFolder();
-                        }
+                        _rootFolder ??= CreateRootFolder();
                     }
                 }
 
@@ -196,33 +194,33 @@ namespace Emby.Server.Implementations.Library
         /// Gets or sets the postscan tasks.
         /// </summary>
         /// <value>The postscan tasks.</value>
-        private ILibraryPostScanTask[] PostscanTasks { get; set; }
+        private ILibraryPostScanTask[] PostscanTasks { get; set; } = Array.Empty<ILibraryPostScanTask>();
 
         /// <summary>
         /// Gets or sets the intro providers.
         /// </summary>
         /// <value>The intro providers.</value>
-        private IIntroProvider[] IntroProviders { get; set; }
+        private IIntroProvider[] IntroProviders { get; set; } = Array.Empty<IIntroProvider>();
 
         /// <summary>
         /// Gets or sets the list of entity resolution ignore rules.
         /// </summary>
         /// <value>The entity resolution ignore rules.</value>
-        private IResolverIgnoreRule[] EntityResolutionIgnoreRules { get; set; }
+        private IResolverIgnoreRule[] EntityResolutionIgnoreRules { get; set; } = Array.Empty<IResolverIgnoreRule>();
 
         /// <summary>
         /// Gets or sets the list of currently registered entity resolvers.
         /// </summary>
         /// <value>The entity resolvers enumerable.</value>
-        private IItemResolver[] EntityResolvers { get; set; }
+        private IItemResolver[] EntityResolvers { get; set; } = Array.Empty<IItemResolver>();
 
-        private IMultiItemResolver[] MultiItemResolvers { get; set; }
+        private IMultiItemResolver[] MultiItemResolvers { get; set; } = Array.Empty<IMultiItemResolver>();
 
         /// <summary>
         /// Gets or sets the comparers.
         /// </summary>
         /// <value>The comparers.</value>
-        private IBaseItemComparer[] Comparers { get; set; }
+        private IBaseItemComparer[] Comparers { get; set; } = Array.Empty<IBaseItemComparer>();
 
         public bool IsScanRunning { get; private set; }
 
@@ -558,7 +556,6 @@ namespace Emby.Server.Implementations.Library
             var args = new ItemResolveArgs(_configurationManager.ApplicationPaths, directoryService)
             {
                 Parent = parent,
-                Path = fullPath,
                 FileInfo = fileInfo,
                 CollectionType = collectionType,
                 LibraryOptions = libraryOptions
@@ -684,7 +681,7 @@ namespace Emby.Server.Implementations.Library
 
                         foreach (var item in items)
                         {
-                            ResolverHelper.SetInitialItemValues(item, parent, _fileSystem, this, directoryService);
+                            ResolverHelper.SetInitialItemValues(item, parent, this, directoryService);
                         }
 
                         items.AddRange(ResolveFileList(result.ExtraFiles, directoryService, parent, collectionType, resolvers, libraryOptions));
@@ -1163,7 +1160,7 @@ namespace Emby.Server.Implementations.Library
                 progress.Report(percent * 100);
             }
 
-            _itemRepository.UpdateInheritedValues(cancellationToken);
+            _itemRepository.UpdateInheritedValues();
 
             progress.Report(100);
         }
@@ -1247,7 +1244,7 @@ namespace Emby.Server.Implementations.Library
             {
                 // TODO: @bond use a ReadOnlySpan<char> here when Enum.TryParse supports it
                 // https://github.com/dotnet/runtime/issues/20008
-                if (Enum.TryParse<CollectionTypeOptions>(Path.GetExtension(file), true, out var res))
+                if (Enum.TryParse<CollectionTypeOptions>(Path.GetFileNameWithoutExtension(file), true, out var res))
                 {
                     return res;
                 }
@@ -1914,12 +1911,17 @@ namespace Emby.Server.Implementations.Library
                     }
                     catch (ArgumentException)
                     {
-                        _logger.LogWarning("Cannot get image index for {0}", img.Path);
+                        _logger.LogWarning("Cannot get image index for {ImagePath}", img.Path);
+                        continue;
+                    }
+                    catch (Exception ex) when (ex is InvalidOperationException || ex is IOException)
+                    {
+                        _logger.LogWarning(ex, "Cannot fetch image from {ImagePath}", img.Path);
                         continue;
                     }
-                    catch (InvalidOperationException)
+                    catch (HttpRequestException ex)
                     {
-                        _logger.LogWarning("Cannot fetch image from {0}", img.Path);
+                        _logger.LogWarning(ex, "Cannot fetch image from {ImagePath}. Http status code: {HttpStatus}", img.Path, ex.StatusCode);
                         continue;
                     }
                 }
@@ -1932,7 +1934,7 @@ namespace Emby.Server.Implementations.Library
                 }
                 catch (Exception ex)
                 {
-                    _logger.LogError(ex, "Cannot get image dimensions for {0}", image.Path);
+                    _logger.LogError(ex, "Cannot get image dimensions for {ImagePath}", image.Path);
                     image.Width = 0;
                     image.Height = 0;
                     continue;
@@ -1944,7 +1946,7 @@ namespace Emby.Server.Implementations.Library
                 }
                 catch (Exception ex)
                 {
-                    _logger.LogError(ex, "Cannot compute blurhash for {0}", image.Path);
+                    _logger.LogError(ex, "Cannot compute blurhash for {ImagePath}", image.Path);
                     image.BlurHash = string.Empty;
                 }
 
@@ -1954,7 +1956,7 @@ namespace Emby.Server.Implementations.Library
                 }
                 catch (Exception ex)
                 {
-                    _logger.LogError(ex, "Cannot update DateModified for {0}", image.Path);
+                    _logger.LogError(ex, "Cannot update DateModified for {ImagePath}", image.Path);
                 }
             }
 
@@ -2512,7 +2514,7 @@ namespace Emby.Server.Implementations.Library
         public bool FillMissingEpisodeNumbersFromPath(Episode episode, bool forceRefresh)
         {
             var series = episode.Series;
-            bool? isAbsoluteNaming = series == null ? false : string.Equals(series.DisplayOrder, "absolute", StringComparison.OrdinalIgnoreCase);
+            bool? isAbsoluteNaming = series != null && string.Equals(series.DisplayOrder, "absolute", StringComparison.OrdinalIgnoreCase);
             if (!isAbsoluteNaming.Value)
             {
                 // In other words, no filter applied
@@ -2524,9 +2526,23 @@ namespace Emby.Server.Implementations.Library
             var isFolder = episode.VideoType == VideoType.BluRay || episode.VideoType == VideoType.Dvd;
 
             // TODO nullable - what are we trying to do there with empty episodeInfo?
-            var episodeInfo = episode.IsFileProtocol
-                ? resolver.Resolve(episode.Path, isFolder, null, null, isAbsoluteNaming) ?? new Naming.TV.EpisodeInfo(episode.Path)
-                : new Naming.TV.EpisodeInfo(episode.Path);
+            EpisodeInfo episodeInfo = null;
+            if (episode.IsFileProtocol)
+            {
+                episodeInfo = resolver.Resolve(episode.Path, isFolder, null, null, isAbsoluteNaming);
+                // Resolve from parent folder if it's not the Season folder
+                if (episodeInfo == null && episode.Parent.GetType() == typeof(Folder))
+                {
+                    episodeInfo = resolver.Resolve(episode.Parent.Path, true, null, null, isAbsoluteNaming);
+                    if (episodeInfo != null)
+                    {
+                        // add the container
+                        episodeInfo.Container = Path.GetExtension(episode.Path)?.TrimStart('.');
+                    }
+                }
+            }
+
+            episodeInfo ??= new EpisodeInfo(episode.Path);
 
             try
             {
@@ -2776,6 +2792,7 @@ namespace Emby.Server.Implementations.Library
 
         public string GetPathAfterNetworkSubstitution(string path, BaseItem ownerItem)
         {
+            string newPath;
             if (ownerItem != null)
             {
                 var libraryOptions = GetLibraryOptions(ownerItem);
@@ -2783,15 +2800,9 @@ namespace Emby.Server.Implementations.Library
                 {
                     foreach (var pathInfo in libraryOptions.PathInfos)
                     {
-                        if (string.IsNullOrWhiteSpace(pathInfo.Path) || string.IsNullOrWhiteSpace(pathInfo.NetworkPath))
-                        {
-                            continue;
-                        }
-
-                        var substitutionResult = SubstitutePathInternal(path, pathInfo.Path, pathInfo.NetworkPath);
-                        if (substitutionResult.Item2)
+                        if (path.TryReplaceSubPath(pathInfo.Path, pathInfo.NetworkPath, out newPath))
                         {
-                            return substitutionResult.Item1;
+                            return newPath;
                         }
                     }
                 }
@@ -2800,24 +2811,16 @@ namespace Emby.Server.Implementations.Library
             var metadataPath = _configurationManager.Configuration.MetadataPath;
             var metadataNetworkPath = _configurationManager.Configuration.MetadataNetworkPath;
 
-            if (!string.IsNullOrWhiteSpace(metadataPath) && !string.IsNullOrWhiteSpace(metadataNetworkPath))
+            if (path.TryReplaceSubPath(metadataPath, metadataNetworkPath, out newPath))
             {
-                var metadataSubstitutionResult = SubstitutePathInternal(path, metadataPath, metadataNetworkPath);
-                if (metadataSubstitutionResult.Item2)
-                {
-                    return metadataSubstitutionResult.Item1;
-                }
+                return newPath;
             }
 
             foreach (var map in _configurationManager.Configuration.PathSubstitutions)
             {
-                if (!string.IsNullOrWhiteSpace(map.From))
+                if (path.TryReplaceSubPath(map.From, map.To, out newPath))
                 {
-                    var substitutionResult = SubstitutePathInternal(path, map.From, map.To);
-                    if (substitutionResult.Item2)
-                    {
-                        return substitutionResult.Item1;
-                    }
+                    return newPath;
                 }
             }
 
@@ -2826,47 +2829,12 @@ namespace Emby.Server.Implementations.Library
 
         public string SubstitutePath(string path, string from, string to)
         {
-            return SubstitutePathInternal(path, from, to).Item1;
-        }
-
-        private Tuple<string, bool> SubstitutePathInternal(string path, string from, string to)
-        {
-            if (string.IsNullOrWhiteSpace(path))
-            {
-                throw new ArgumentNullException(nameof(path));
-            }
-
-            if (string.IsNullOrWhiteSpace(from))
-            {
-                throw new ArgumentNullException(nameof(from));
-            }
-
-            if (string.IsNullOrWhiteSpace(to))
+            if (path.TryReplaceSubPath(from, to, out var newPath))
             {
-                throw new ArgumentNullException(nameof(to));
+                return newPath;
             }
 
-            from = from.Trim();
-            to = to.Trim();
-
-            var newPath = path.Replace(from, to, StringComparison.OrdinalIgnoreCase);
-            var changed = false;
-
-            if (!string.Equals(newPath, path, StringComparison.Ordinal))
-            {
-                if (to.IndexOf('/', StringComparison.Ordinal) != -1)
-                {
-                    newPath = newPath.Replace('\\', '/');
-                }
-                else
-                {
-                    newPath = newPath.Replace('/', '\\');
-                }
-
-                changed = true;
-            }
-
-            return new Tuple<string, bool>(newPath, changed);
+            return path;
         }
 
         private void SetExtraTypeFromFilename(Video item)
@@ -2923,6 +2891,12 @@ namespace Emby.Server.Implementations.Library
         }
 
         public void UpdatePeople(BaseItem item, List<PersonInfo> people)
+        {
+            UpdatePeopleAsync(item, people, CancellationToken.None).GetAwaiter().GetResult();
+        }
+
+        /// <inheritdoc />
+        public async Task UpdatePeopleAsync(BaseItem item, List<PersonInfo> people, CancellationToken cancellationToken)
         {
             if (!item.SupportsPeople)
             {
@@ -2930,6 +2904,8 @@ namespace Emby.Server.Implementations.Library
             }
 
             _itemRepository.UpdatePeople(item.Id, people);
+
+            await SavePeopleMetadataAsync(people, cancellationToken).ConfigureAwait(false);
         }
 
         public async Task<ItemImageInfo> ConvertImageToLocal(BaseItem item, ItemImageInfo image, int imageIndex)
@@ -3001,7 +2977,7 @@ namespace Emby.Server.Implementations.Library
 
                 if (collectionType != null)
                 {
-                    var path = Path.Combine(virtualFolderPath, collectionType.ToString() + ".collection");
+                    var path = Path.Combine(virtualFolderPath, collectionType.ToString().ToLowerInvariant() + ".collection");
 
                     File.WriteAllBytes(path, Array.Empty<byte>());
                 }
@@ -3033,6 +3009,58 @@ namespace Emby.Server.Implementations.Library
             }
         }
 
+        private async Task SavePeopleMetadataAsync(IEnumerable<PersonInfo> people, CancellationToken cancellationToken)
+        {
+            var personsToSave = new List<BaseItem>();
+
+            foreach (var person in people)
+            {
+                cancellationToken.ThrowIfCancellationRequested();
+
+                var itemUpdateType = ItemUpdateType.MetadataDownload;
+                var saveEntity = false;
+                var personEntity = GetPerson(person.Name);
+
+                // if PresentationUniqueKey is empty it's likely a new item.
+                if (string.IsNullOrEmpty(personEntity.PresentationUniqueKey))
+                {
+                    personEntity.PresentationUniqueKey = personEntity.CreatePresentationUniqueKey();
+                    saveEntity = true;
+                }
+
+                foreach (var id in person.ProviderIds)
+                {
+                    if (!string.Equals(personEntity.GetProviderId(id.Key), id.Value, StringComparison.OrdinalIgnoreCase))
+                    {
+                        personEntity.SetProviderId(id.Key, id.Value);
+                        saveEntity = true;
+                    }
+                }
+
+                if (!string.IsNullOrWhiteSpace(person.ImageUrl) && !personEntity.HasImage(ImageType.Primary))
+                {
+                    personEntity.SetImage(
+                        new ItemImageInfo
+                        {
+                            Path = person.ImageUrl,
+                            Type = ImageType.Primary
+                        },
+                        0);
+
+                    saveEntity = true;
+                    itemUpdateType = ItemUpdateType.ImageUpdate;
+                }
+
+                if (saveEntity)
+                {
+                    personsToSave.Add(personEntity);
+                    await RunMetadataSavers(personEntity, itemUpdateType).ConfigureAwait(false);
+                }
+            }
+
+            CreateItems(personsToSave, null, CancellationToken.None);
+        }
+
         private void StartScanInBackground()
         {
             Task.Run(() =>

+ 1 - 1
Emby.Server.Implementations/Library/LiveStreamHelper.cs

@@ -25,7 +25,7 @@ namespace Emby.Server.Implementations.Library
         private readonly IMediaEncoder _mediaEncoder;
         private readonly ILogger _logger;
         private readonly IApplicationPaths _appPaths;
-        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.GetOptions();
+        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
 
         public LiveStreamHelper(IMediaEncoder mediaEncoder, ILogger logger, IApplicationPaths appPaths)
         {

+ 12 - 15
Emby.Server.Implementations/Library/MediaSourceManager.cs

@@ -46,7 +46,7 @@ namespace Emby.Server.Implementations.Library
 
         private readonly ConcurrentDictionary<string, ILiveStream> _openStreams = new ConcurrentDictionary<string, ILiveStream>(StringComparer.OrdinalIgnoreCase);
         private readonly SemaphoreSlim _liveStreamSemaphore = new SemaphoreSlim(1, 1);
-        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.GetOptions();
+        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
 
         private IMediaSourceProvider[] _providers;
 
@@ -199,10 +199,15 @@ namespace Emby.Server.Implementations.Library
                     {
                         source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableAudioPlaybackTranscoding);
                     }
+                    else if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
+                    {
+                        source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding);
+                        source.SupportsDirectStream = user.HasPermission(PermissionKind.EnablePlaybackRemuxing);
+                    }
                 }
             }
 
-            return SortMediaSources(list).Where(i => i.Type != MediaSourceType.Placeholder).ToList();
+            return SortMediaSources(list);
         }
 
         public MediaProtocol GetPathProtocol(string path)
@@ -436,7 +441,7 @@ namespace Emby.Server.Implementations.Library
             }
         }
 
-        private static IEnumerable<MediaSourceInfo> SortMediaSources(IEnumerable<MediaSourceInfo> sources)
+        private static List<MediaSourceInfo> SortMediaSources(IEnumerable<MediaSourceInfo> sources)
         {
             return sources.OrderBy(i =>
             {
@@ -451,8 +456,9 @@ namespace Emby.Server.Implementations.Library
             {
                 var stream = i.VideoStream;
 
-                return stream == null || stream.Width == null ? 0 : stream.Width.Value;
+                return stream?.Width ?? 0;
             })
+            .Where(i => i.Type != MediaSourceType.Placeholder)
             .ToList();
         }
 
@@ -584,18 +590,9 @@ namespace Emby.Server.Implementations.Library
 
         public Task<IDirectStreamProvider> GetDirectStreamProviderByUniqueId(string uniqueId, CancellationToken cancellationToken)
         {
-            var info = _openStreams.Values.FirstOrDefault(i =>
-            {
-                var liveStream = i as ILiveStream;
-                if (liveStream != null)
-                {
-                    return string.Equals(liveStream.UniqueId, uniqueId, StringComparison.OrdinalIgnoreCase);
-                }
-
-                return false;
-            });
+            var info = _openStreams.FirstOrDefault(i => i.Value != null && string.Equals(i.Value.UniqueId, uniqueId, StringComparison.OrdinalIgnoreCase));
 
-            return Task.FromResult(info as IDirectStreamProvider);
+            return Task.FromResult(info.Value as IDirectStreamProvider);
         }
 
         public async Task<LiveStreamResponse> OpenLiveStream(LiveStreamRequest request, CancellationToken cancellationToken)

+ 1 - 2
Emby.Server.Implementations/Library/MusicManager.cs

@@ -100,8 +100,7 @@ namespace Emby.Server.Implementations.Library
 
         public List<BaseItem> GetInstantMixFromItem(BaseItem item, User user, DtoOptions dtoOptions)
         {
-            var genre = item as MusicGenre;
-            if (genre != null)
+            if (item is MusicGenre genre)
             {
                 return GetInstantMixFromGenreIds(new[] { item.Id }, user, dtoOptions);
             }

+ 71 - 3
Emby.Server.Implementations/Library/PathExtensions.cs

@@ -1,7 +1,8 @@
 #nullable enable
 
 using System;
-using System.Text.RegularExpressions;
+using System.Diagnostics.CodeAnalysis;
+using MediaBrowser.Common.Providers;
 
 namespace Emby.Server.Implementations.Library
 {
@@ -41,11 +42,78 @@ namespace Emby.Server.Implementations.Library
             // for imdbid we also accept pattern matching
             if (string.Equals(attribute, "imdbid", StringComparison.OrdinalIgnoreCase))
             {
-                var m = Regex.Match(str, "tt([0-9]{7,8})", RegexOptions.IgnoreCase);
-                return m.Success ? m.Value : null;
+                var match = ProviderIdParsers.TryFindImdbId(str, out var imdbId);
+                return match ? imdbId.ToString() : null;
             }
 
             return null;
         }
+
+        /// <summary>
+        /// Replaces a sub path with another sub path and normalizes the final path.
+        /// </summary>
+        /// <param name="path">The original path.</param>
+        /// <param name="subPath">The original sub path.</param>
+        /// <param name="newSubPath">The new sub path.</param>
+        /// <param name="newPath">The result of the sub path replacement</param>
+        /// <returns>The path after replacing the sub path.</returns>
+        /// <exception cref="ArgumentNullException"><paramref name="path" />, <paramref name="newSubPath" /> or <paramref name="newSubPath" /> is empty.</exception>
+        public static bool TryReplaceSubPath(
+            [NotNullWhen(true)] this string? path,
+            [NotNullWhen(true)] string? subPath,
+            [NotNullWhen(true)] string? newSubPath,
+            [NotNullWhen(true)] out string? newPath)
+        {
+            newPath = null;
+
+            if (string.IsNullOrEmpty(path)
+                || string.IsNullOrEmpty(subPath)
+                || string.IsNullOrEmpty(newSubPath)
+                || subPath.Length > path.Length)
+            {
+                return false;
+            }
+
+            char oldDirectorySeparatorChar;
+            char newDirectorySeparatorChar;
+            // True normalization is still not possible https://github.com/dotnet/runtime/issues/2162
+            // The reasoning behind this is that a forward slash likely means it's a Linux path and
+            // so the whole path should be normalized to use / and vice versa for Windows (although Windows doesn't care much).
+            if (newSubPath.Contains('/', StringComparison.Ordinal))
+            {
+                oldDirectorySeparatorChar = '\\';
+                newDirectorySeparatorChar = '/';
+            }
+            else
+            {
+                oldDirectorySeparatorChar = '/';
+                newDirectorySeparatorChar = '\\';
+            }
+
+            path = path.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
+            subPath = subPath.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
+
+            // We have to ensure that the sub path ends with a directory separator otherwise we'll get weird results
+            // when the sub path matches a similar but in-complete subpath
+            var oldSubPathEndsWithSeparator = subPath[^1] == newDirectorySeparatorChar;
+            if (!path.StartsWith(subPath, StringComparison.OrdinalIgnoreCase))
+            {
+                return false;
+            }
+
+            if (path.Length > subPath.Length
+                && !oldSubPathEndsWithSeparator
+                && path[subPath.Length] != newDirectorySeparatorChar)
+            {
+                return false;
+            }
+
+            var newSubPathTrimmed = newSubPath.AsSpan().TrimEnd(newDirectorySeparatorChar);
+            // Ensure that the path with the old subpath removed starts with a leading dir separator
+            int idx = oldSubPathEndsWithSeparator ? subPath.Length - 1 : subPath.Length;
+            newPath = string.Concat(newSubPathTrimmed, path.AsSpan(idx));
+
+            return true;
+        }
     }
 }

+ 21 - 43
Emby.Server.Implementations/Library/ResolverHelper.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 using System;
 using System.IO;
 using System.Linq;
@@ -18,11 +20,10 @@ namespace Emby.Server.Implementations.Library
         /// </summary>
         /// <param name="item">The item.</param>
         /// <param name="parent">The parent.</param>
-        /// <param name="fileSystem">The file system.</param>
         /// <param name="libraryManager">The library manager.</param>
         /// <param name="directoryService">The directory service.</param>
-        /// <exception cref="ArgumentException">Item must have a path</exception>
-        public static void SetInitialItemValues(BaseItem item, Folder parent, IFileSystem fileSystem, ILibraryManager libraryManager, IDirectoryService directoryService)
+        /// <exception cref="ArgumentException">Item must have a path.</exception>
+        public static void SetInitialItemValues(BaseItem item, Folder? parent, ILibraryManager libraryManager, IDirectoryService directoryService)
         {
             // This version of the below method has no ItemResolveArgs, so we have to require the path already being set
             if (string.IsNullOrEmpty(item.Path))
@@ -43,9 +44,14 @@ namespace Emby.Server.Implementations.Library
 
             // Make sure DateCreated and DateModified have values
             var fileInfo = directoryService.GetFile(item.Path);
-            SetDateCreated(item, fileSystem, fileInfo);
+            if (fileInfo == null)
+            {
+                throw new FileNotFoundException("Can't find item path.", item.Path);
+            }
+
+            SetDateCreated(item, fileInfo);
 
-            EnsureName(item, item.Path, fileInfo);
+            EnsureName(item, fileInfo);
         }
 
         /// <summary>
@@ -72,9 +78,9 @@ namespace Emby.Server.Implementations.Library
             item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
 
             // Make sure the item has a name
-            EnsureName(item, item.Path, args.FileInfo);
+            EnsureName(item, args.FileInfo);
 
-            item.IsLocked = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1 ||
+            item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) ||
                 item.GetParents().Any(i => i.IsLocked);
 
             // Make sure DateCreated and DateModified have values
@@ -84,28 +90,15 @@ namespace Emby.Server.Implementations.Library
         /// <summary>
         /// Ensures the name.
         /// </summary>
-        private static void EnsureName(BaseItem item, string fullPath, FileSystemMetadata fileInfo)
+        private static void EnsureName(BaseItem item, FileSystemMetadata fileInfo)
         {
             // If the subclass didn't supply a name, add it here
-            if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(fullPath))
+            if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
             {
-                var fileName = fileInfo == null ? Path.GetFileName(fullPath) : fileInfo.Name;
-
-                item.Name = GetDisplayName(fileName, fileInfo != null && fileInfo.IsDirectory);
+                item.Name = fileInfo.IsDirectory ? fileInfo.Name : Path.GetFileNameWithoutExtension(fileInfo.Name);
             }
         }
 
-        /// <summary>
-        /// Gets the display name.
-        /// </summary>
-        /// <param name="path">The path.</param>
-        /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
-        /// <returns>System.String.</returns>
-        private static string GetDisplayName(string path, bool isDirectory)
-        {
-            return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
-        }
-
         /// <summary>
         /// Ensures DateCreated and DateModified have values.
         /// </summary>
@@ -114,21 +107,6 @@ namespace Emby.Server.Implementations.Library
         /// <param name="args">The args.</param>
         private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
         {
-            if (fileSystem == null)
-            {
-                throw new ArgumentNullException(nameof(fileSystem));
-            }
-
-            if (item == null)
-            {
-                throw new ArgumentNullException(nameof(item));
-            }
-
-            if (args == null)
-            {
-                throw new ArgumentNullException(nameof(args));
-            }
-
             // See if a different path came out of the resolver than what went in
             if (!fileSystem.AreEqual(args.Path, item.Path))
             {
@@ -136,7 +114,7 @@ namespace Emby.Server.Implementations.Library
 
                 if (childData != null)
                 {
-                    SetDateCreated(item, fileSystem, childData);
+                    SetDateCreated(item, childData);
                 }
                 else
                 {
@@ -144,17 +122,17 @@ namespace Emby.Server.Implementations.Library
 
                     if (fileData.Exists)
                     {
-                        SetDateCreated(item, fileSystem, fileData);
+                        SetDateCreated(item, fileData);
                     }
                 }
             }
             else
             {
-                SetDateCreated(item, fileSystem, args.FileInfo);
+                SetDateCreated(item, args.FileInfo);
             }
         }
 
-        private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemMetadata info)
+        private static void SetDateCreated(BaseItem item, FileSystemMetadata? info)
         {
             var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
 
@@ -163,7 +141,7 @@ namespace Emby.Server.Implementations.Library
                 // directoryService.getFile may return null
                 if (info != null)
                 {
-                    var dateCreated = fileSystem.GetCreationTimeUtc(info);
+                    var dateCreated = info.CreationTimeUtc;
 
                     if (dateCreated.Equals(DateTime.MinValue))
                     {

+ 5 - 0
Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs

@@ -201,6 +201,11 @@ namespace Emby.Server.Implementations.Library.Resolvers.Audio
                     continue;
                 }
 
+                if (resolvedItem.Files.Count == 0)
+                {
+                    continue;
+                }
+
                 var firstMedia = resolvedItem.Files[0];
 
                 var libraryItem = new T

+ 2 - 2
Emby.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs

@@ -30,7 +30,7 @@ namespace Emby.Server.Implementations.Library.Resolvers
         /// </summary>
         /// <param name="args">The args.</param>
         /// <returns>`0.</returns>
-        protected override T Resolve(ItemResolveArgs args)
+        public override T Resolve(ItemResolveArgs args)
         {
             return ResolveVideo<T>(args, false);
         }
@@ -42,7 +42,7 @@ namespace Emby.Server.Implementations.Library.Resolvers
         /// <param name="args">The args.</param>
         /// <param name="parseName">if set to <c>true</c> [parse name].</param>
         /// <returns>``0.</returns>
-        protected TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
+        protected virtual TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
               where TVideoType : Video, new()
         {
             var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();

+ 1 - 1
Emby.Server.Implementations/Library/Resolvers/Books/BookResolver.cs

@@ -13,7 +13,7 @@ namespace Emby.Server.Implementations.Library.Resolvers.Books
     {
         private readonly string[] _validExtensions = { ".azw", ".azw3", ".cb7", ".cbr", ".cbt", ".cbz", ".epub", ".mobi", ".pdf" };
 
-        protected override Book Resolve(ItemResolveArgs args)
+        public override Book Resolve(ItemResolveArgs args)
         {
             var collectionType = args.GetCollectionType();
 

+ 6 - 6
Emby.Server.Implementations/Library/Resolvers/ItemResolver.cs

@@ -11,6 +11,12 @@ namespace Emby.Server.Implementations.Library.Resolvers
     public abstract class ItemResolver<T> : IItemResolver
         where T : BaseItem, new()
     {
+        /// <summary>
+        /// Gets the priority.
+        /// </summary>
+        /// <value>The priority.</value>
+        public virtual ResolverPriority Priority => ResolverPriority.First;
+
         /// <summary>
         /// Resolves the specified args.
         /// </summary>
@@ -21,12 +27,6 @@ namespace Emby.Server.Implementations.Library.Resolvers
             return null;
         }
 
-        /// <summary>
-        /// Gets the priority.
-        /// </summary>
-        /// <value>The priority.</value>
-        public virtual ResolverPriority Priority => ResolverPriority.First;
-
         /// <summary>
         /// Sets initial values on the newly resolved item.
         /// </summary>

+ 105 - 105
Emby.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs

@@ -69,6 +69,110 @@ namespace Emby.Server.Implementations.Library.Resolvers.Movies
             return result;
         }
 
+        /// <summary>
+        /// Resolves the specified args.
+        /// </summary>
+        /// <param name="args">The args.</param>
+        /// <returns>Video.</returns>
+        public override Video Resolve(ItemResolveArgs args)
+        {
+            var collectionType = args.GetCollectionType();
+
+            // Find movies with their own folders
+            if (args.IsDirectory)
+            {
+                if (IsInvalid(args.Parent, collectionType))
+                {
+                    return null;
+                }
+
+                var files = args.FileSystemChildren
+                    .Where(i => !LibraryManager.IgnoreFile(i, args.Parent))
+                    .ToList();
+
+                if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
+                {
+                    return FindMovie<MusicVideo>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
+                }
+
+                if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
+                {
+                    return FindMovie<Video>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
+                }
+
+                if (string.IsNullOrEmpty(collectionType))
+                {
+                    // Owned items will be caught by the plain video resolver
+                    if (args.Parent == null)
+                    {
+                        // return FindMovie<Video>(args.Path, args.Parent, files, args.DirectoryService, collectionType);
+                        return null;
+                    }
+
+                    if (args.HasParent<Series>())
+                    {
+                        return null;
+                    }
+
+                    {
+                        return FindMovie<Movie>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
+                    }
+                }
+
+                if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
+                {
+                    return FindMovie<Movie>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
+                }
+
+                return null;
+            }
+
+            // Handle owned items
+            if (args.Parent == null)
+            {
+                return base.Resolve(args);
+            }
+
+            if (IsInvalid(args.Parent, collectionType))
+            {
+                return null;
+            }
+
+            Video item = null;
+
+            if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
+            {
+                item = ResolveVideo<MusicVideo>(args, false);
+            }
+
+            // To find a movie file, the collection type must be movies or boxsets
+            else if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
+            {
+                item = ResolveVideo<Movie>(args, true);
+            }
+            else if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
+                string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
+            {
+                item = ResolveVideo<Video>(args, false);
+            }
+            else if (string.IsNullOrEmpty(collectionType))
+            {
+                if (args.HasParent<Series>())
+                {
+                    return null;
+                }
+
+                item = ResolveVideo<Video>(args, false);
+            }
+
+            if (item != null)
+            {
+                item.IsInMixedFolder = true;
+            }
+
+            return item;
+        }
+
         private MultiItemResolverResult ResolveMultipleInternal(
             Folder parent,
             List<FileSystemMetadata> files,
@@ -216,110 +320,6 @@ namespace Emby.Server.Implementations.Library.Resolvers.Movies
             return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
         }
 
-        /// <summary>
-        /// Resolves the specified args.
-        /// </summary>
-        /// <param name="args">The args.</param>
-        /// <returns>Video.</returns>
-        protected override Video Resolve(ItemResolveArgs args)
-        {
-            var collectionType = args.GetCollectionType();
-
-            // Find movies with their own folders
-            if (args.IsDirectory)
-            {
-                if (IsInvalid(args.Parent, collectionType))
-                {
-                    return null;
-                }
-
-                var files = args.FileSystemChildren
-                    .Where(i => !LibraryManager.IgnoreFile(i, args.Parent))
-                    .ToList();
-
-                if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
-                {
-                    return FindMovie<MusicVideo>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
-                }
-
-                if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
-                {
-                    return FindMovie<Video>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
-                }
-
-                if (string.IsNullOrEmpty(collectionType))
-                {
-                    // Owned items will be caught by the plain video resolver
-                    if (args.Parent == null)
-                    {
-                        // return FindMovie<Video>(args.Path, args.Parent, files, args.DirectoryService, collectionType);
-                        return null;
-                    }
-
-                    if (args.HasParent<Series>())
-                    {
-                        return null;
-                    }
-
-                    {
-                        return FindMovie<Movie>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
-                    }
-                }
-
-                if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
-                {
-                    return FindMovie<Movie>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
-                }
-
-                return null;
-            }
-
-            // Handle owned items
-            if (args.Parent == null)
-            {
-                return base.Resolve(args);
-            }
-
-            if (IsInvalid(args.Parent, collectionType))
-            {
-                return null;
-            }
-
-            Video item = null;
-
-            if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
-            {
-                item = ResolveVideo<MusicVideo>(args, false);
-            }
-
-            // To find a movie file, the collection type must be movies or boxsets
-            else if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
-            {
-                item = ResolveVideo<Movie>(args, true);
-            }
-            else if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
-                string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
-            {
-                item = ResolveVideo<Video>(args, false);
-            }
-            else if (string.IsNullOrEmpty(collectionType))
-            {
-                if (args.HasParent<Series>())
-                {
-                    return null;
-                }
-
-                item = ResolveVideo<Video>(args, false);
-            }
-
-            if (item != null)
-            {
-                item.IsInMixedFolder = true;
-            }
-
-            return item;
-        }
-
         /// <summary>
         /// Sets the initial item values.
         /// </summary>
@@ -376,7 +376,7 @@ namespace Emby.Server.Implementations.Library.Resolvers.Movies
         {
             var multiDiscFolders = new List<FileSystemMetadata>();
 
-            var libraryOptions = args.GetLibraryOptions();
+            var libraryOptions = args.LibraryOptions;
             var supportPhotos = string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && libraryOptions.EnablePhotos;
             var photos = new List<FileSystemMetadata>();
 

+ 6 - 6
Emby.Server.Implementations/Library/Resolvers/PhotoAlbumResolver.cs

@@ -13,7 +13,7 @@ namespace Emby.Server.Implementations.Library.Resolvers
     public class PhotoAlbumResolver : FolderResolver<PhotoAlbum>
     {
         private readonly IImageProcessor _imageProcessor;
-        private ILibraryManager _libraryManager;
+        private readonly ILibraryManager _libraryManager;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="PhotoAlbumResolver"/> class.
@@ -26,6 +26,9 @@ namespace Emby.Server.Implementations.Library.Resolvers
             _libraryManager = libraryManager;
         }
 
+        /// <inheritdoc />
+        public override ResolverPriority Priority => ResolverPriority.Second;
+
         /// <summary>
         /// Resolves the specified args.
         /// </summary>
@@ -39,8 +42,8 @@ namespace Emby.Server.Implementations.Library.Resolvers
                 // Must be an image file within a photo collection
                 var collectionType = args.GetCollectionType();
 
-                if (string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase) ||
-                    (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && args.GetLibraryOptions().EnablePhotos))
+                if (string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase)
+                    || (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && args.LibraryOptions.EnablePhotos))
                 {
                     if (HasPhotos(args))
                     {
@@ -84,8 +87,5 @@ namespace Emby.Server.Implementations.Library.Resolvers
 
             return false;
         }
-
-        /// <inheritdoc />
-        public override ResolverPriority Priority => ResolverPriority.Second;
     }
 }

+ 1 - 1
Emby.Server.Implementations/Library/Resolvers/PhotoResolver.cs

@@ -47,7 +47,7 @@ namespace Emby.Server.Implementations.Library.Resolvers
                 var collectionType = args.CollectionType;
 
                 if (string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase)
-                    || (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && args.GetLibraryOptions().EnablePhotos))
+                    || (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && args.LibraryOptions.EnablePhotos))
                 {
                     if (IsImageFile(args.Path, _imageProcessor))
                     {

+ 2 - 1
Emby.Server.Implementations/Library/Resolvers/PlaylistResolver.cs

@@ -63,7 +63,8 @@ namespace Emby.Server.Implementations.Library.Resolvers
                     {
                         Path = args.Path,
                         Name = Path.GetFileNameWithoutExtension(args.Path),
-                        IsInMixedFolder = true
+                        IsInMixedFolder = true,
+                        PlaylistMediaType = MediaType.Audio
                     };
                 }
             }

+ 19 - 25
Emby.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Linq;
+using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Entities.TV;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Model.Entities;
@@ -11,12 +12,21 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
     /// </summary>
     public class EpisodeResolver : BaseVideoResolver<Episode>
     {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="EpisodeResolver"/> class.
+        /// </summary>
+        /// <param name="libraryManager">The library manager.</param>
+        public EpisodeResolver(ILibraryManager libraryManager)
+            : base(libraryManager)
+        {
+        }
+
         /// <summary>
         /// Resolves the specified args.
         /// </summary>
         /// <param name="args">The args.</param>
         /// <returns>Episode.</returns>
-        protected override Episode Resolve(ItemResolveArgs args)
+        public override Episode Resolve(ItemResolveArgs args)
         {
             var parent = args.Parent;
 
@@ -25,30 +35,23 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
                 return null;
             }
 
-            var season = parent as Season;
-
             // Just in case the user decided to nest episodes.
             // Not officially supported but in some cases we can handle it.
-            if (season == null)
-            {
-                season = parent.GetParents().OfType<Season>().FirstOrDefault();
-            }
 
-            // If the parent is a Season or Series, then this is an Episode if the VideoResolver returns something
+            var season = parent as Season ?? parent.GetParents().OfType<Season>().FirstOrDefault();
+
+            // If the parent is a Season or Series and the parent is not an extras folder, then this is an Episode if the VideoResolver returns something
             // Also handle flat tv folders
-            if (season != null ||
-                string.Equals(args.GetCollectionType(), CollectionType.TvShows, StringComparison.OrdinalIgnoreCase) ||
-                args.HasParent<Series>())
+            if ((season != null ||
+                 string.Equals(args.GetCollectionType(), CollectionType.TvShows, StringComparison.OrdinalIgnoreCase) ||
+                 args.HasParent<Series>())
+                && (parent is Series || !BaseItem.AllExtrasTypesFolderNames.Contains(parent.Name, StringComparer.OrdinalIgnoreCase)))
             {
                 var episode = ResolveVideo<Episode>(args, false);
 
                 if (episode != null)
                 {
-                    var series = parent as Series;
-                    if (series == null)
-                    {
-                        series = parent.GetParents().OfType<Series>().FirstOrDefault();
-                    }
+                    var series = parent as Series ?? parent.GetParents().OfType<Series>().FirstOrDefault();
 
                     if (series != null)
                     {
@@ -74,14 +77,5 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
 
             return null;
         }
-
-        /// <summary>
-        /// Initializes a new instance of the <see cref="EpisodeResolver"/> class.
-        /// </summary>
-        /// <param name="libraryManager">The library manager.</param>
-        public EpisodeResolver(ILibraryManager libraryManager)
-            : base(libraryManager)
-        {
-        }
     }
 }

+ 1 - 1
Emby.Server.Implementations/Library/Resolvers/TV/SeasonResolver.cs

@@ -88,7 +88,7 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
                             CultureInfo.InvariantCulture,
                             _localization.GetLocalizedString("NameSeasonNumber"),
                             seasonNumber,
-                            args.GetLibraryOptions().PreferredMetadataLanguage);
+                            args.LibraryOptions.PreferredMetadataLanguage);
                 }
 
                 return season;

+ 4 - 37
Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs

@@ -19,19 +19,16 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
     /// </summary>
     public class SeriesResolver : FolderResolver<Series>
     {
-        private readonly IFileSystem _fileSystem;
         private readonly ILogger<SeriesResolver> _logger;
         private readonly ILibraryManager _libraryManager;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="SeriesResolver"/> class.
         /// </summary>
-        /// <param name="fileSystem">The file system.</param>
         /// <param name="logger">The logger.</param>
         /// <param name="libraryManager">The library manager.</param>
-        public SeriesResolver(IFileSystem fileSystem, ILogger<SeriesResolver> logger, ILibraryManager libraryManager)
+        public SeriesResolver(ILogger<SeriesResolver> logger, ILibraryManager libraryManager)
         {
-            _fileSystem = fileSystem;
             _logger = logger;
             _libraryManager = libraryManager;
         }
@@ -59,15 +56,6 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
                 var collectionType = args.GetCollectionType();
                 if (string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
                 {
-                    // if (args.ContainsFileSystemEntryByName("tvshow.nfo"))
-                    //{
-                    //    return new Series
-                    //    {
-                    //        Path = args.Path,
-                    //        Name = Path.GetFileName(args.Path)
-                    //    };
-                    //}
-
                     var configuredContentType = _libraryManager.GetConfiguredContentType(args.Path);
                     if (!string.Equals(configuredContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
                     {
@@ -100,7 +88,7 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
                         return null;
                     }
 
-                    if (IsSeriesFolder(args.Path, args.FileSystemChildren, args.DirectoryService, _fileSystem, _logger, _libraryManager, false))
+                    if (IsSeriesFolder(args.Path, args.FileSystemChildren, _logger, _libraryManager, false))
                     {
                         return new Series
                         {
@@ -117,8 +105,6 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
         public static bool IsSeriesFolder(
             string path,
             IEnumerable<FileSystemMetadata> fileSystemChildren,
-            IDirectoryService directoryService,
-            IFileSystem fileSystem,
             ILogger<SeriesResolver> logger,
             ILibraryManager libraryManager,
             bool isTvContentType)
@@ -127,7 +113,7 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
             {
                 if (child.IsDirectory)
                 {
-                    if (IsSeasonFolder(child.FullName, isTvContentType, libraryManager))
+                    if (IsSeasonFolder(child.FullName, isTvContentType))
                     {
                         logger.LogDebug("{Path} is a series because of season folder {Dir}.", path, child.FullName);
                         return true;
@@ -160,32 +146,13 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
             return false;
         }
 
-        /// <summary>
-        /// Determines whether [is place holder] [the specified path].
-        /// </summary>
-        /// <param name="path">The path.</param>
-        /// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
-        /// <exception cref="ArgumentNullException">path</exception>
-        private static bool IsVideoPlaceHolder(string path)
-        {
-            if (string.IsNullOrEmpty(path))
-            {
-                throw new ArgumentNullException(nameof(path));
-            }
-
-            var extension = Path.GetExtension(path);
-
-            return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
-        }
-
         /// <summary>
         /// Determines whether [is season folder] [the specified path].
         /// </summary>
         /// <param name="path">The path.</param>
         /// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</param>
-        /// <param name="libraryManager">The library manager.</param>
         /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
-        private static bool IsSeasonFolder(string path, bool isTvContentType, ILibraryManager libraryManager)
+        private static bool IsSeasonFolder(string path, bool isTvContentType)
         {
             var seasonNumber = SeasonPathParser.Parse(path, isTvContentType, isTvContentType).SeasonNumber;
 

+ 0 - 1
Emby.Server.Implementations/Library/SearchEngine.cs

@@ -12,7 +12,6 @@ using MediaBrowser.Controller.Extensions;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Model.Querying;
 using MediaBrowser.Model.Search;
-using Microsoft.Extensions.Logging;
 using Genre = MediaBrowser.Controller.Entities.Genre;
 using Person = MediaBrowser.Controller.Entities.Person;
 

+ 5 - 5
Emby.Server.Implementations/Library/UserDataManager.cs

@@ -13,8 +13,8 @@ using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Persistence;
 using MediaBrowser.Model.Dto;
 using MediaBrowser.Model.Entities;
-using Book = MediaBrowser.Controller.Entities.Book;
 using AudioBook = MediaBrowser.Controller.Entities.AudioBook;
+using Book = MediaBrowser.Controller.Entities.Book;
 
 namespace Emby.Server.Implementations.Library
 {
@@ -248,15 +248,15 @@ namespace Emby.Server.Implementations.Library
             }
             else if (positionTicks > 0 && hasRuntime && item is AudioBook)
             {
-                var minIn = TimeSpan.FromTicks(positionTicks).TotalMinutes;
-                var minOut = TimeSpan.FromTicks(runtimeTicks - positionTicks).TotalMinutes;
+                var playbackPositionInMinutes = TimeSpan.FromTicks(positionTicks).TotalMinutes;
+                var remainingTimeInMinutes = TimeSpan.FromTicks(runtimeTicks - positionTicks).TotalMinutes;
 
-                if (minIn > _config.Configuration.MinAudiobookResume)
+                if (playbackPositionInMinutes < _config.Configuration.MinAudiobookResume)
                 {
                     // ignore progress during the beginning
                     positionTicks = 0;
                 }
-                else if (minOut < _config.Configuration.MaxAudiobookResume || positionTicks >= runtimeTicks)
+                else if (remainingTimeInMinutes < _config.Configuration.MaxAudiobookResume || positionTicks >= runtimeTicks)
                 {
                     // mark as completed close to the end
                     positionTicks = 0;

+ 0 - 1
Emby.Server.Implementations/Library/UserViewManager.cs

@@ -2,7 +2,6 @@
 
 using System;
 using System.Collections.Generic;
-using System.Globalization;
 using System.Linq;
 using System.Threading;
 using Jellyfin.Data.Entities;

+ 4 - 2
Emby.Server.Implementations/LiveTv/EmbyTV/DirectRecorder.cs

@@ -45,7 +45,8 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
         {
             Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
 
-            using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
+            // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
+            using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None))
             {
                 onStarted();
 
@@ -70,7 +71,8 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
 
             Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
 
-            await using var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read);
+            // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
+            await using var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None);
 
             onStarted();
 

+ 13 - 18
Emby.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs

@@ -17,7 +17,6 @@ using Jellyfin.Data.Enums;
 using Jellyfin.Data.Events;
 using MediaBrowser.Common.Configuration;
 using MediaBrowser.Common.Extensions;
-using MediaBrowser.Common.Net;
 using MediaBrowser.Common.Progress;
 using MediaBrowser.Controller;
 using MediaBrowser.Controller.Configuration;
@@ -802,22 +801,22 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
 
         public ActiveRecordingInfo GetActiveRecordingInfo(string path)
         {
-            if (string.IsNullOrWhiteSpace(path))
+            if (string.IsNullOrWhiteSpace(path) || _activeRecordings.IsEmpty)
             {
                 return null;
             }
 
-            foreach (var recording in _activeRecordings.Values)
+            foreach (var (_, recordingInfo) in _activeRecordings)
             {
-                if (string.Equals(recording.Path, path, StringComparison.Ordinal) && !recording.CancellationTokenSource.IsCancellationRequested)
+                if (string.Equals(recordingInfo.Path, path, StringComparison.Ordinal) && !recordingInfo.CancellationTokenSource.IsCancellationRequested)
                 {
-                    var timer = recording.Timer;
+                    var timer = recordingInfo.Timer;
                     if (timer.Status != RecordingStatus.InProgress)
                     {
                         return null;
                     }
 
-                    return recording;
+                    return recordingInfo;
                 }
             }
 
@@ -1622,9 +1621,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
             }
 
             return _activeRecordings
-                .Values
-                .ToList()
-                .Any(i => string.Equals(i.Path, path, StringComparison.OrdinalIgnoreCase) && !string.Equals(i.Timer.Id, timerId, StringComparison.OrdinalIgnoreCase));
+                .Any(i => string.Equals(i.Value.Path, path, StringComparison.OrdinalIgnoreCase) && !string.Equals(i.Value.Timer.Id, timerId, StringComparison.OrdinalIgnoreCase));
         }
 
         private IRecorder GetRecorder(MediaSourceInfo mediaSource)
@@ -1856,7 +1853,8 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
                 return;
             }
 
-            using (var stream = new FileStream(nfoPath, FileMode.Create, FileAccess.Write, FileShare.Read))
+            // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
+            using (var stream = new FileStream(nfoPath, FileMode.Create, FileAccess.Write, FileShare.None))
             {
                 var settings = new XmlWriterSettings
                 {
@@ -1920,7 +1918,8 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
                 return;
             }
 
-            using (var stream = new FileStream(nfoPath, FileMode.Create, FileAccess.Write, FileShare.Read))
+            // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
+            using (var stream = new FileStream(nfoPath, FileMode.Create, FileAccess.Write, FileShare.None))
             {
                 var settings = new XmlWriterSettings
                 {
@@ -2238,14 +2237,10 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
             var enabledTimersForSeries = new List<TimerInfo>();
             foreach (var timer in allTimers)
             {
-                var existingTimer = _timerProvider.GetTimer(timer.Id);
-
-                if (existingTimer == null)
-                {
-                    existingTimer = string.IsNullOrWhiteSpace(timer.ProgramId)
+                var existingTimer = _timerProvider.GetTimer(timer.Id) 
+                    ?? (string.IsNullOrWhiteSpace(timer.ProgramId)
                         ? null
-                        : _timerProvider.GetTimerByProgramId(timer.ProgramId);
-                }
+                        : _timerProvider.GetTimerByProgramId(timer.ProgramId));
 
                 if (existingTimer == null)
                 {

Some files were not shown because too many files changed in this diff