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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Win32;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Controller.Resolvers
{
/// <summary>
/// Class EntityResolutionHelper
/// </summary>
public static class EntityResolutionHelper
{
/// <summary>
/// Any extension in this list is considered a metadata file - can be added to at runtime for extensibility
/// </summary>
public static List<string> MetaExtensions = new List<string>
{
".xml",
".jpg",
".png",
".json",
".data"
};
/// <summary>
/// Any extension in this list is considered a video file - can be added to at runtime for extensibility
/// </summary>
public static List<string> VideoFileExtensions = new List<string>
{
".mkv",
".m2t",
".m2ts",
".img",
".iso",
".ts",
".rmvb",
".mov",
".avi",
".mpg",
".mpeg",
".wmv",
".mp4",
".divx",
".dvr-ms",
".wtv",
".ogm",
".ogv",
".asf",
".m4v",
".flv",
".f4v",
".3gp",
".webm"
};
/// <summary>
/// Determines whether [is video file] [the specified path].
/// </summary>
/// <param name="path">The path.</param>
/// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
public static bool IsVideoFile(string path)
{
var extension = Path.GetExtension(path) ?? string.Empty;
return VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// The audio file extensions
/// </summary>
public static readonly string[] AudioFileExtensions = new[] {
".mp3",
".flac",
".wma",
".aac",
".acc",
".m4a",
".m4b",
".wav",
".ape",
".ogg",
".oga"
};
/// <summary>
/// Determines whether [is audio file] [the specified args].
/// </summary>
/// <param name="args">The args.</param>
/// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
public static bool IsAudioFile(ItemResolveArgs args)
{
return AudioFileExtensions.Contains(Path.GetExtension(args.Path), StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Determines whether [is audio file] [the specified file].
/// </summary>
/// <param name="file">The file.</param>
/// <returns><c>true</c> if [is audio file] [the specified file]; otherwise, <c>false</c>.</returns>
public static bool IsAudioFile(WIN32_FIND_DATA file)
{
return AudioFileExtensions.Contains(Path.GetExtension(file.Path), StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Determine if the supplied file data points to a music album
/// </summary>
/// <param name="data">The data.</param>
/// <returns><c>true</c> if [is music album] [the specified data]; otherwise, <c>false</c>.</returns>
public static bool IsMusicAlbum(WIN32_FIND_DATA data)
{
return ContainsMusic(FileSystem.GetFiles(data.Path));
}
/// <summary>
/// Determine if the supplied reslove args should be considered a music album
/// </summary>
/// <param name="args">The args.</param>
/// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
public static bool IsMusicAlbum(ItemResolveArgs args)
{
// Args points to an album if parent is an Artist folder or it directly contains music
if (args.IsDirectory)
{
//if (args.Parent is MusicArtist) return true; //saves us from testing children twice
if (ContainsMusic(args.FileSystemChildren)) return true;
}
return false;
}
/// <summary>
/// Determine if the supplied list contains what we should consider music
/// </summary>
/// <param name="list">The list.</param>
/// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns>
public static bool ContainsMusic(IEnumerable<WIN32_FIND_DATA> list)
{
// If list contains at least 2 audio files or at least one and no video files consider it to contain music
var foundAudio = 0;
var foundVideo = 0;
foreach (var file in list)
{
if (IsAudioFile(file)) foundAudio++;
if (foundAudio >= 2)
{
return true;
}
if (IsVideoFile(file.Path)) foundVideo++;
}
// or a single audio file and no video files
if (foundAudio > 0 && foundVideo == 0) return true;
return false;
}
/// <summary>
/// Determines whether a path should be ignored based on its contents - called after the contents have been read
/// </summary>
/// <param name="args">The args.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
public static bool ShouldResolvePathContents(ItemResolveArgs args)
{
// Ignore any folders containing a file called .ignore
return !args.ContainsFileSystemEntryByName(".ignore");
}
/// <summary>
/// Ensures DateCreated and DateModified have values
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
public static void EnsureDates(BaseItem item, ItemResolveArgs args)
{
if (!Path.IsPathRooted(item.Path))
{
return;
}
// See if a different path came out of the resolver than what went in
if (!args.Path.Equals(item.Path, StringComparison.OrdinalIgnoreCase))
{
var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
if (childData.HasValue)
{
item.DateCreated = childData.Value.CreationTimeUtc;
item.DateModified = childData.Value.LastWriteTimeUtc;
}
else
{
var fileData = FileSystem.GetFileData(item.Path);
if (fileData.HasValue)
{
item.DateCreated = fileData.Value.CreationTimeUtc;
item.DateModified = fileData.Value.LastWriteTimeUtc;
}
}
}
else
{
item.DateCreated = args.FileInfo.CreationTimeUtc;
item.DateModified = args.FileInfo.LastWriteTimeUtc;
}
}
}
}
|