< 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
65%
Covered lines: 32
Uncovered lines: 17
Coverable lines: 49
Total lines: 168
Line coverage: 65.3%
Branch coverage
59%
Covered branches: 25
Total branches: 42
Branch coverage: 59.5%
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%10866.66%
SetDateCreated(...)68.75%181680%

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
 6565            if (string.IsNullOrEmpty(item.Path))
 66            {
 5067                item.Path = args.Path;
 68            }
 69
 70            // If the resolver didn't specify this
 6571            if (args.Parent is not null)
 72            {
 673                item.SetParent(args.Parent);
 74            }
 75
 6576            item.Id = libraryManager.GetNewItemId(item.Path, item.GetType());
 77
 78            // Make sure the item has a name
 6579            EnsureName(item, args.FileInfo);
 80
 6581            item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) ||
 6582                item.GetParents().Any(i => i.IsLocked);
 83
 84            // Make sure DateCreated and DateModified have values
 6585            EnsureDates(fileSystem, item, args);
 6586        }
 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
 6594            if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
 95            {
 5096                item.Name = fileInfo.IsDirectory ? fileInfo.Name : Path.GetFileNameWithoutExtension(fileInfo.Name);
 97            }
 6598        }
 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
 65109            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            {
 48129                SetDateCreated(item, args.FileInfo);
 130            }
 65131        }
 132
 133        private static void SetDateCreated(BaseItem item, FileSystemMetadata? info)
 134        {
 48135            var config = BaseItem.ConfigurationManager.GetMetadataConfiguration();
 136
 48137            if (config.UseFileCreationTimeForDateAdded)
 138            {
 48139                var fileCreationDate = info?.CreationTimeUtc;
 48140                if (fileCreationDate is not null)
 141                {
 48142                    var dateCreated = fileCreationDate;
 48143                    if (dateCreated.Equals(DateTime.MinValue))
 144                    {
 0145                        dateCreated = DateTime.UtcNow;
 146                    }
 147
 48148                    item.DateCreated = dateCreated.Value;
 149                }
 150            }
 151            else
 152            {
 0153                item.DateCreated = DateTime.UtcNow;
 154            }
 155
 48156            if (info is not null && !info.IsDirectory)
 157            {
 0158                item.Size = info.Length;
 159            }
 160
 48161            var fileModificationDate = info?.LastWriteTimeUtc;
 48162            if (fileModificationDate.HasValue)
 163            {
 48164                item.DateModified = fileModificationDate.Value;
 165            }
 48166        }
 167    }
 168}