| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Linq; |
| | | 4 | | using MediaBrowser.Controller.Entities; |
| | | 5 | | using MediaBrowser.Controller.Library; |
| | | 6 | | using MediaBrowser.Controller.Providers; |
| | | 7 | | using MediaBrowser.Model.IO; |
| | | 8 | | |
| | | 9 | | namespace 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 |
| | 0 | 28 | | ArgumentException.ThrowIfNullOrEmpty(item.Path); |
| | | 29 | | |
| | | 30 | | // If the resolver didn't specify this |
| | 0 | 31 | | if (parent is not null) |
| | | 32 | | { |
| | 0 | 33 | | item.SetParent(parent); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | item.Id = libraryManager.GetNewItemId(item.Path, item.GetType()); |
| | | 37 | | |
| | 0 | 38 | | item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) || |
| | 0 | 39 | | item.GetParents().Any(i => i.IsLocked); |
| | | 40 | | |
| | | 41 | | // Make sure DateCreated and DateModified have values |
| | 0 | 42 | | var fileInfo = directoryService.GetFileSystemEntry(item.Path); |
| | 0 | 43 | | if (fileInfo is null) |
| | | 44 | | { |
| | 0 | 45 | | return false; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | SetDateCreated(item, fileInfo); |
| | | 49 | | |
| | 0 | 50 | | EnsureName(item, fileInfo); |
| | | 51 | | |
| | 0 | 52 | | 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 |
| | 67 | 65 | | if (string.IsNullOrEmpty(item.Path)) |
| | | 66 | | { |
| | 52 | 67 | | item.Path = args.Path; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // If the resolver didn't specify this |
| | 67 | 71 | | if (args.Parent is not null) |
| | | 72 | | { |
| | 8 | 73 | | item.SetParent(args.Parent); |
| | | 74 | | } |
| | | 75 | | |
| | 67 | 76 | | item.Id = libraryManager.GetNewItemId(item.Path, item.GetType()); |
| | | 77 | | |
| | | 78 | | // Make sure the item has a name |
| | 67 | 79 | | EnsureName(item, args.FileInfo); |
| | | 80 | | |
| | 67 | 81 | | item.IsLocked = item.Path.Contains("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) || |
| | 67 | 82 | | item.GetParents().Any(i => i.IsLocked); |
| | | 83 | | |
| | | 84 | | // Make sure DateCreated and DateModified have values |
| | 67 | 85 | | EnsureDates(fileSystem, item, args); |
| | 67 | 86 | | } |
| | | 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 |
| | 67 | 94 | | if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path)) |
| | | 95 | | { |
| | 52 | 96 | | item.Name = fileInfo.IsDirectory ? fileInfo.Name : Path.GetFileNameWithoutExtension(fileInfo.Name); |
| | | 97 | | } |
| | 67 | 98 | | } |
| | | 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 |
| | 67 | 109 | | if (!fileSystem.AreEqual(args.Path, item.Path)) |
| | | 110 | | { |
| | 17 | 111 | | var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null; |
| | | 112 | | |
| | 17 | 113 | | if (childData is not null) |
| | | 114 | | { |
| | 0 | 115 | | SetDateCreated(item, childData); |
| | | 116 | | } |
| | | 117 | | else |
| | | 118 | | { |
| | 17 | 119 | | var fileData = fileSystem.GetFileSystemInfo(item.Path); |
| | | 120 | | |
| | 17 | 121 | | if (fileData.Exists) |
| | | 122 | | { |
| | 0 | 123 | | SetDateCreated(item, fileData); |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |
| | | 127 | | else |
| | | 128 | | { |
| | 50 | 129 | | SetDateCreated(item, args.FileInfo); |
| | | 130 | | } |
| | 67 | 131 | | } |
| | | 132 | | |
| | | 133 | | private static void SetDateCreated(BaseItem item, FileSystemMetadata? info) |
| | | 134 | | { |
| | 50 | 135 | | var config = BaseItem.ConfigurationManager.GetMetadataConfiguration(); |
| | | 136 | | |
| | 50 | 137 | | if (config.UseFileCreationTimeForDateAdded) |
| | | 138 | | { |
| | 50 | 139 | | var fileCreationDate = info?.CreationTimeUtc; |
| | 50 | 140 | | if (fileCreationDate is not null) |
| | | 141 | | { |
| | 50 | 142 | | var dateCreated = fileCreationDate; |
| | 50 | 143 | | if (dateCreated == DateTime.MinValue) |
| | | 144 | | { |
| | 50 | 145 | | dateCreated = DateTime.UtcNow; |
| | | 146 | | } |
| | | 147 | | |
| | 50 | 148 | | item.DateCreated = dateCreated.Value; |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | else |
| | | 152 | | { |
| | 0 | 153 | | item.DateCreated = DateTime.UtcNow; |
| | | 154 | | } |
| | | 155 | | |
| | 50 | 156 | | if (info is not null && !info.IsDirectory) |
| | | 157 | | { |
| | 0 | 158 | | item.Size = info.Length; |
| | | 159 | | } |
| | | 160 | | |
| | 50 | 161 | | var fileModificationDate = info?.LastWriteTimeUtc; |
| | 50 | 162 | | if (fileModificationDate.HasValue) |
| | | 163 | | { |
| | 50 | 164 | | item.DateModified = fileModificationDate.Value; |
| | | 165 | | } |
| | 50 | 166 | | } |
| | | 167 | | } |
| | | 168 | | } |