blob: 201e282c03c03af00ae2e74663e3bfecf4c1e49d (
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
87
88
89
90
91
92
93
94
95
96
|
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.FileSorting
{
public class SortingScheduledTask : IScheduledTask, IConfigurableScheduledTask
{
private readonly IServerConfigurationManager _config;
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
public SortingScheduledTask(IServerConfigurationManager config, ILogger logger, ILibraryManager libraryManager)
{
_config = config;
_logger = logger;
_libraryManager = libraryManager;
}
public string Name
{
get { return "Sort new files"; }
}
public string Description
{
get { return "Processes new files available in the configured sorting location."; }
}
public string Category
{
get { return "Library"; }
}
public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
{
return Task.Run(() => SortFiles(cancellationToken, progress), cancellationToken);
}
private void SortFiles(CancellationToken cancellationToken, IProgress<double> progress)
{
var numComplete = 0;
var paths = _config.Configuration.FileSortingOptions.TvWatchLocations.ToList();
foreach (var path in paths)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
SortFiles(path);
}
catch (Exception ex)
{
_logger.ErrorException("Error sorting files from {0}", ex, path);
}
numComplete++;
double percent = numComplete;
percent /= paths.Count;
progress.Report(100 * percent);
}
}
private void SortFiles(string path)
{
new TvFileSorter(_libraryManager, _logger).Sort(path, _config.Configuration.FileSortingOptions);
}
public IEnumerable<ITaskTrigger> GetDefaultTriggers()
{
return new ITaskTrigger[]
{
new IntervalTrigger{ Interval = TimeSpan.FromMinutes(5)}
};
}
public bool IsHidden
{
get { return !_config.Configuration.FileSortingOptions.IsEnabled; }
}
public bool IsEnabled
{
get { return _config.Configuration.FileSortingOptions.IsEnabled; }
}
}
}
|