aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Library/ResolverHelper.cs
blob: ef303008de7d13a1fb91ef9ab81f94cfd9f5f696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace MediaBrowser.Server.Implementations.Library
{
    /// <summary>
    /// Class ResolverHelper
    /// </summary>
    public static class ResolverHelper
    {
        /// <summary>
        /// Sets the initial item values.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="args">The args.</param>
        public static void SetInitialItemValues(BaseItem item, ItemResolveArgs args)
        {
            item.ResolveArgs = args;

            // If the resolver didn't specify this
            if (string.IsNullOrEmpty(item.Path))
            {
                item.Path = args.Path;
            }

            // If the resolver didn't specify this
            if (args.Parent != null)
            {
                item.Parent = args.Parent;
            }

            item.Id = item.Path.GetMBId(item.GetType());
            item.DisplayMediaType = item.GetType().Name;

            // Make sure the item has a name
            EnsureName(item);

            item.DontFetchMeta = item.Path.IndexOf("[dontfetchmeta]", StringComparison.OrdinalIgnoreCase) != -1;

            // Make sure DateCreated and DateModified have values
            EntityResolutionHelper.EnsureDates(item, args);
        }

        /// <summary>
        /// Ensures the name.
        /// </summary>
        /// <param name="item">The item.</param>
        private static void EnsureName(BaseItem item)
        {
            // If the subclass didn't supply a name, add it here
            if (string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Path))
            {
                //we use our resolve args name here to get the name of the containg folder, not actual video file
                item.Name = GetMBName(item.ResolveArgs.FileInfo.Name, item.ResolveArgs.FileInfo.Attributes.HasFlag(FileAttributes.Directory));
            }
        }

        /// <summary>
        /// The MB name regex
        /// </summary>
        private static readonly Regex MBNameRegex = new Regex("(\\[.*\\])", RegexOptions.Compiled);

        /// <summary>
        /// Strip out attribute items and return just the name we will use for items
        /// </summary>
        /// <param name="path">Assumed to be a file or directory path</param>
        /// <param name="isDirectory">if set to <c>true</c> [is directory].</param>
        /// <returns>The cleaned name</returns>
        private static string GetMBName(string path, bool isDirectory)
        {
            //first just get the file or directory name
            var fn = isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);

            //now - strip out anything inside brackets
            fn = MBNameRegex.Replace(fn, string.Empty);

            return fn;
        }

    }
}