< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.ResolverHelper
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/ResolverHelper.cs
Line coverage
60%
Covered lines: 26
Uncovered lines: 17
Coverable lines: 43
Total lines: 158
Line coverage: 60.4%
Branch coverage
53%
Covered branches: 17
Total branches: 32
Branch coverage: 53.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SetInitialItemValues(...)0%4260%
SetInitialItemValues(...)83.33%66100%
EnsureName(...)83.33%66100%
EnsureDates(...)50%10.37866.66%
SetDateCreated(...)50%7.33666.66%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Library/ResolverHelper.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Linq;
 4using MediaBrowser.Controller.Entities;
 5using MediaBrowser.Controller.Library;
 6using MediaBrowser.Controller.Providers;
 7using MediaBrowser.Model.IO;
 8
 9namespace Emby.Server.Implementations.Library
 10{
 11    /// <summary>
 12    /// Class ResolverHelper.
 13    /// </summary>
 14    public static class ResolverHelper
 15    {
 16        /// <summary>
 17        /// Sets the initial item values.
 18        /// </summary>
 19        /// <param name="item">The item.</param>
 20        /// <param name="parent">The parent.</param>
 21        /// <param name="libraryManager">The library manager.</param>
 22        /// <param name="directoryService">The directory service.</param>
 23        /// <returns>True if initializing was successful.</returns>
 24        /// <exception cref="ArgumentException">Item must have a path.</exception>
 25        public static bool SetInitialItemValues(BaseItem item, Folder? parent, ILibraryManager libraryManager, IDirector
 26        {
 27            // This version of the below method has no ItemResolveArgs, so we have to require the path already being set
 028            ArgumentException.ThrowIfNullOrEmpty(item.Path);
 29
 30            // If the resolver didn't specify this
 031            if (parent is not null)
 32            {
 033                item.SetParent(parent);
 34            }
 35
 036            item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
 37
 038            item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) ||
 039                item.GetParents().Any(i => i.IsLocked);
 40
 41            // Make sure DateCreated and DateModified have values
 042            var fileInfo = directoryService.GetFileSystemEntry(item.Path);
 043            if (fileInfo is null)
 44            {
 045                return false;
 46            }
 47
 048            SetDateCreated(item, fileInfo);
 49
 050            EnsureName(item, fileInfo);
 51
 052            return true;
 53        }
 54
 55        /// <summary>
 56        /// Sets the initial item values.
 57        /// </summary>
 58        /// <param name="item">The item.</param>
 59        /// <param name="args">The args.</param>
 60        /// <param name="fileSystem">The file system.</param>
 61        /// <param name="libraryManager">The library manager.</param>
 62        public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args, IFileSystem fileSystem, ILibraryMan
 63        {
 64            // If the resolver didn't specify this
 6365            if (string.IsNullOrEmpty(item.Path))
 66            {
 4867                item.Path = args.Path;
 68            }
 69
 70            // If the resolver didn't specify this
 6371            if (args.Parent is not null)
 72            {
 273                item.SetParent(args.Parent);
 74            }
 75
 6376            item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
 77
 78            // Make sure the item has a name
 6379            EnsureName(item, args.FileInfo);
 80
 6381            item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) ||
 6382                item.GetParents().Any(i => i.IsLocked);
 83
 84            // Make sure DateCreated and DateModified have values
 6385            EnsureDates(fileSystem, item, args);
 6386        }
 87
 88        /// <summary>
 89        /// Ensures the name.
 90        /// </summary>
 91        private static void EnsureName(BaseItem item, FileSystemMetadata fileInfo)
 92        {
 93            // If the subclass didn't supply a name, add it here
 6394            if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
 95            {
 4896                item.Name = fileInfo.IsDirectory ? fileInfo.Name : Path.GetFileNameWithoutExtension(fileInfo.Name);
 97            }
 6398        }
 99
 100        /// <summary>
 101        /// Ensures DateCreated and DateModified have values.
 102        /// </summary>
 103        /// <param name="fileSystem">The file system.</param>
 104        /// <param name="item">The item.</param>
 105        /// <param name="args">The args.</param>
 106        private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
 107        {
 108            // See if a different path came out of the resolver than what went in
 63109            if (!fileSystem.AreEqual(args.Path, item.Path))
 110            {
 17111                var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
 112
 17113                if (childData is not null)
 114                {
 0115                    SetDateCreated(item, childData);
 116                }
 117                else
 118                {
 17119                    var fileData = fileSystem.GetFileSystemInfo(item.Path);
 120
 17121                    if (fileData.Exists)
 122                    {
 0123                        SetDateCreated(item, fileData);
 124                    }
 125                }
 126            }
 127            else
 128            {
 46129                SetDateCreated(item, args.FileInfo);
 130            }
 63131        }
 132
 133        private static void SetDateCreated(BaseItem item, FileSystemMetadata? info)
 134        {
 46135            var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
 136
 46137            if (config.UseFileCreationTimeForDateAdded)
 138            {
 139                // directoryService.getFile may return null
 46140                if (info is not null)
 141                {
 46142                    var dateCreated = info.CreationTimeUtc;
 143
 46144                    if (dateCreated.Equals(DateTime.MinValue))
 145                    {
 0146                        dateCreated = DateTime.UtcNow;
 147                    }
 148
 46149                    item.DateCreated = dateCreated;
 150                }
 151            }
 152            else
 153            {
 0154                item.DateCreated = DateTime.UtcNow;
 155            }
 0156        }
 157    }
 158}