-
Notifications
You must be signed in to change notification settings - Fork 39
/
PlayListEngine.cs
361 lines (323 loc) · 14.6 KB
/
PlayListEngine.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using MetadataExtractor;
using MetadataExtractor.Formats.Exif;
using MetadataExtractor.Formats.Iptc;
using MetadataExtractor.Formats.Xmp;
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Dynaframe3.Models;
namespace Dynaframe3
{
class PlayListEngine
{
public MediaFile? CurrentMediaFile { get; set; }
public int MediaIndex = 0;
public List<int> Playlist;
public PlayListEngine()
{
Playlist = new List<int>();
}
public void GoToFirstFile()
{
MediaIndex = 0;
GetCurrentFile();
}
public void RebuildPlaylist()
{
// TODO:
// Add Filters
// Add Sorting
Stopwatch sw = new Stopwatch();
sw.Start();
Logger.LogComment("Rebuilding playlist called...rebuilding the playlist...");
Playlist.Clear();
// filter based on file paths
foreach (string dir in AppSettings.Default.CurrentPlayList)
{
using (var db = new MediaDataContext())
{
var files = db.MediaFiles.Where(d => d.Path.StartsWith(dir));
foreach (var file in files)
{
Playlist.Add(file.Id);
}
}
}
// Add in toplevel directories
// TODO: This can be filtered out here.
foreach (string dir in AppSettings.Default.SearchDirectories)
{
using (var db = new MediaDataContext())
{
var files = db.MediaFiles.Where(d => d.Directory == dir);
foreach (var file in files)
{
Playlist.Add(file.Id);
}
}
}
// Filter based on tags
if (!String.IsNullOrEmpty(AppSettings.Default.InclusiveTagFilters))
{
string[] filters = AppSettings.Default.InclusiveTagFilters.Split(';');
foreach (string filter in filters)
{
using (var db = new MediaDataContext())
{
var files = db.MediaFiles.Where(d => d.Tags != null).Where(f => f.Tags.Contains(filter));
foreach (var file in files)
{
Playlist.Add(file.Id);
}
}
}
}
Playlist = Playlist.Distinct().ToList();
if (AppSettings.Default.Shuffle)
{
Random r = new Random((int)DateTime.Now.Ticks);
Playlist = Helpers.Shuffle<int>(Playlist, r).ToList();
}
sw.Stop();
Logger.LogComment("Rebuilding playlist took: " + sw.ElapsedMilliseconds + "ms.");
Logger.LogComment("Playlist has: " + Playlist.Count + " items now.");
}
public MediaFile GetCurrentFile()
{
using (var db = new MediaDataContext())
{
try
{
if (MediaIndex >= Playlist.Count())
{
MediaIndex = 0;
}
Logger.LogComment("Skipping to item: " + Playlist[MediaIndex]);
MediaFile file = db.MediaFiles.Where(f => f.Id == Playlist[MediaIndex]).Single();
Logger.LogComment("Returning item# : " + MediaIndex + " File: " + file.Path);
CurrentMediaFile = file;
return file;
}
catch (Exception exc)
{
MediaIndex++;
return GetCurrentFile();
}
}
}
public void DumpPlaylistToLog()
{
Logger.LogComment("--------------------- START DUMP --------------------------");
Logger.LogComment("Dumping log of: " + Playlist.Count() + " items.");
using (var db = new MediaDataContext())
{
for (int i = 0; 0 < Playlist.Count; i++)
{
try
{
MediaFile file = db.MediaFiles.Where(mf => mf.Id == Playlist[i]).Single();
Logger.LogComment(file.Id + " - File: " + file.Path);
}
catch (Exception)
{
// If the playlist is modified this will throw. Ignore for now.
}
}
}
Logger.LogComment("Size: " + sizeof(int) * Playlist.Count);
Logger.LogComment("--------------------- END DUMP --------------------------");
}
public void InitializeDatabase()
{
Logger.LogComment("InitializeDatabase() - Building Database");
AddItemsToDatabase(AppSettings.Default.CurrentPlayList, SearchOption.AllDirectories);
AddItemsToDatabase(AppSettings.Default.SearchDirectories, SearchOption.TopDirectoryOnly);
}
public void AddItemsToDatabase(List<string> Folders, SearchOption searchOptions)
{
// Filter folders and issues out now..
string[] folders = new string[] { "" };
if (AppSettings.Default.IgnoreFolders.Length > 0)
{
folders = AppSettings.Default.IgnoreFolders.Split(",");
}
// First remove ignore folders from Folders array which is passed in
foreach (string folderException in folders)
{
Folders = Folders.Except(Folders.Where(f => new DirectoryInfo(f).Name.StartsWith(folderException))).ToList();
}
// Go through remaining folders and get files
foreach (string Folder in Folders)
{
if (!System.IO.Directory.Exists(Folder))
{
Logger.LogComment("ERROR: Selected Playlist Folder Missing: " + Folder);
continue;
}
try
{
IEnumerable<string> files = PlayListEngineHelper.GetFilesByExtensions(new DirectoryInfo(Folder), searchOptions, PlayListEngineHelper.GetSupportedExtensions());
// Filter here
// Filter 1: MAC Computers create files that end in .jpg that start with "._"
files = files.Except(files.Where(f => new FileInfo(f).Name.StartsWith("._")));
using (var db = new MediaDataContext())
{
foreach (string file in files)
{
// For each file, see if the path exists in the database
bool FileExists = true;
MediaFile mediaFile = null;
var mFile = db.MediaFiles
.Where(f => f.Path == file).ToList();
if ((mFile != null) && (mFile.Count() > 0))
{
mediaFile = mFile.FirstOrDefault();
}
// If not create a new blank one and enter the path
if (mediaFile == null)
{
mediaFile = new MediaFile();
mediaFile.Path = file;
mediaFile.Directory = new FileInfo(file).Directory.FullName;
FileExists = false; // The file didn't exist, so note that here
}
// Now we can fill out/update the rest
mediaFile.Type = PlayListEngineHelper.GetPlayListItemTypeFromPath(file).ToString();
if (mediaFile.Type == "Image")
{
GetMetaData(ref mediaFile);
}
// If it exists, do an update, else add new.
if (FileExists)
{
db.MediaFiles.Update(mediaFile);
}
else
{
db.MediaFiles.Add(mediaFile);
}
db.SaveChanges();
}
Logger.LogComment("Count of files in the database: " + db.MediaFiles.Count());
}
}
catch (Exception exc)
{
Logger.LogComment("WARNING: Unable to load file or files from Folder " + Folder + " Exception: " + exc.ToString());
}
}
}
/// <summary>
/// Gets the exif data and fills out the object so that we can get that data into the database. Very time intensive operation.
/// </summary>
/// <param name="mediaFile"></param>
public void GetMetaData(ref MediaFile mediaFile)
{
try
{
{
IEnumerable<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(mediaFile.Path);
MetadataExtractor.Directory? dir = directories.Where(d => d.Name == "Exif IFD0").FirstOrDefault();
if (dir != null)
{
mediaFile.Title = GetTagByName("Windows XP Title", dir);
mediaFile.Author = GetTagByName("Windows XP Author", dir);
mediaFile.Comment = GetTagByName("Windows XP Comment", dir);
mediaFile.Tags = GetTagByName("Windows XP Keywords", dir);
}
}
}
catch (Exception exc)
{
Logger.LogComment("Exception looking up tag data. File: " + mediaFile.Path + " Exception: " + exc.Message);
}
}
public string GetTagByName(string tagName, MetadataExtractor.Directory dir)
{
Tag?val = dir.Tags.Where(t => t.Name == tagName).FirstOrDefault();
if (val != null)
{
return val.Description;
}
return "";
}
/// <summary>
/// Goes to Next Playlist Item. Will rebuild Playlist if nothing is found.
/// </summary>
public void GoToNext()
{
MediaIndex++;
GetCurrentFile();
}
/// <summary>
/// Goes to the previous point in the list
/// </summary>
public void GoToPrevious()
{
MediaIndex--;
GetCurrentFile();
}
/// <summary>
/// This is for playlist syncing. This gets called when a playlist is handed in on the
/// command via setfile. It' checks to find a file that is as close as possible on this unit.
/// </summary>
/// <param name="path">Remote machine path which was passed in</param>
/// <returns>a full path to a file</returns>
public string ConvertFileNameToLocal(string path)
{
// TODO: Disabled for now. Need to rethink syncing.
/* Logger.LogComment("ConvertFileNameToLocal (For frame syncing) Entering. Path passed in was: " + path);
// Note: Path is from a remote machine
// Formula is:
// 1) If an exact match is found..return that.
// 2) If the root folder and file name is found, return that (probably common case)
// 3) If the file name is found...return that
// 4) If folder is found, return random file from that folder
// 5) If nothing is found, return a random file (Not sure what to do here)
// Sample for below uses:
// passed in: c:\geektoolkit\pictures\scifi\mandalorian.jpg
//
// Test 1: Full path found on this machine: c:\geektoolkit\pictures\scifi\mandalorian.jpg
var testpath = CurrentPlayListItems.Where(p => p.Path.ToUpper() == path.ToUpper()).FirstOrDefault();
if (testpath != null)
{
Logger.LogComment("SYNC: (case 1) Full path found! Returning: " + testpath);
return testpath.Path;
}
// case 2: Match from just the directory name, and a matching image was found: scifi\mandalorian.jpg
// Note: this is 'golden path' and how I expect this to be used.
testpath = CurrentPlayListItems.Where(p => p.Path.ToUpper().Contains(new FileInfo(path).Directory.Name.ToUpper()) && new FileInfo(p.Path).Name.ToUpper() == new FileInfo(path).Name.ToUpper()).FirstOrDefault();
if (testpath != null)
{
Logger.LogComment("SYNC: (case 2) Folder/Filename found! Returning: " + testpath.Path);
return testpath.Path;
}
// case 3: Match any file in the playlist against mandalorian.jpg
testpath = CurrentPlayListItems.Where(p => new FileInfo(p.Path).Name.ToUpper() == new FileInfo(path).Name.ToUpper()).FirstOrDefault();
if (testpath != null)
{
Logger.LogComment("SYNC: (case 3) Filename only found! Returning: " + testpath.Path);
return testpath.Path;
}
// case 4: Match a folder with the same name as the subfolder/playlist: scifi
testpath = CurrentPlayListItems.Where(p => new FileInfo(p.Path).Directory.Name.ToUpper() == new FileInfo(path).Directory.Name.ToUpper()).FirstOrDefault();
if (testpath != null)
{
// note: in this case, the testpath is likely just a foldername. We have to get a file out of it.
Logger.LogComment("SYNC: (case 4) - Found folder!");
string file = PlayListEngineHelper.GetRandomFileFromFolder(testpath.Path);
Logger.LogComment("SYNC: (case 4) Folder only found! Returning: " + file);
return file;
}
// case 5: just return something to show
Logger.LogComment("SYNC: (case 5) - NO matches found for path, DirectoryName, Filename, continuing on with current playlist..returning" + CurrentPlayListItem.Path);
return CurrentPlayListItem.Path;
*/
return "";
}
}
}