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
|
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.FileOrganization;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.FileOrganization;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Server.Implementations.FileOrganization
{
public class TvFolderOrganizer
{
private readonly ILibraryMonitor _libraryMonitor;
private readonly ILibraryManager _libraryManager;
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
private readonly IFileOrganizationService _organizationService;
private readonly IServerConfigurationManager _config;
private readonly IProviderManager _providerManager;
public TvFolderOrganizer(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IFileOrganizationService organizationService, IServerConfigurationManager config, IProviderManager providerManager)
{
_libraryManager = libraryManager;
_logger = logger;
_fileSystem = fileSystem;
_libraryMonitor = libraryMonitor;
_organizationService = organizationService;
_config = config;
_providerManager = providerManager;
}
private bool EnableOrganization(FileSystemMetadata fileInfo, TvFileOrganizationOptions options)
{
var minFileBytes = options.MinFileSizeMb * 1024 * 1024;
try
{
return _libraryManager.IsVideoFile(fileInfo.FullName) && fileInfo.Length >= minFileBytes;
}
catch (Exception ex)
{
_logger.ErrorException("Error organizing file {0}", ex, fileInfo.Name);
}
return false;
}
public async Task Organize(AutoOrganizeOptions options, CancellationToken cancellationToken, IProgress<double> progress)
{
var watchLocations = options.TvOptions.WatchLocations.ToList();
var eligibleFiles = watchLocations.SelectMany(GetFilesToOrganize)
.OrderBy(_fileSystem.GetCreationTimeUtc)
.Where(i => EnableOrganization(i, options.TvOptions))
.ToList();
var processedFolders = new HashSet<string>();
progress.Report(10);
if (eligibleFiles.Count > 0)
{
var numComplete = 0;
foreach (var file in eligibleFiles)
{
var organizer = new EpisodeFileOrganizer(_organizationService, _config, _fileSystem, _logger, _libraryManager,
_libraryMonitor, _providerManager);
try
{
var result = await organizer.OrganizeEpisodeFile(file.FullName, options, options.TvOptions.OverwriteExistingEpisodes, cancellationToken).ConfigureAwait(false);
if (result.Status == FileSortingStatus.Success && !processedFolders.Contains(file.DirectoryName, StringComparer.OrdinalIgnoreCase))
{
processedFolders.Add(file.DirectoryName);
}
}
catch (Exception ex)
{
_logger.ErrorException("Error organizing episode {0}", ex, file.FullName);
}
numComplete++;
double percent = numComplete;
percent /= eligibleFiles.Count;
progress.Report(10 + 89 * percent);
}
}
cancellationToken.ThrowIfCancellationRequested();
progress.Report(99);
foreach (var path in processedFolders)
{
var deleteExtensions = options.TvOptions.LeftOverFileExtensionsToDelete
.Select(i => i.Trim().TrimStart('.'))
.Where(i => !string.IsNullOrEmpty(i))
.Select(i => "." + i)
.ToList();
if (deleteExtensions.Count > 0)
{
DeleteLeftOverFiles(path, deleteExtensions);
}
if (options.TvOptions.DeleteEmptyFolders)
{
if (!IsWatchFolder(path, watchLocations))
{
DeleteEmptyFolders(path);
}
}
}
progress.Report(100);
}
/// <summary>
/// Gets the files to organize.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>IEnumerable{FileInfo}.</returns>
private List<FileSystemMetadata> GetFilesToOrganize(string path)
{
try
{
return _fileSystem.GetFiles(path, true)
.ToList();
}
catch (IOException ex)
{
_logger.ErrorException("Error getting files from {0}", ex, path);
return new List<FileSystemMetadata>();
}
}
/// <summary>
/// Deletes the left over files.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="extensions">The extensions.</param>
private void DeleteLeftOverFiles(string path, IEnumerable<string> extensions)
{
var eligibleFiles = _fileSystem.GetFiles(path, true)
.Where(i => extensions.Contains(i.Extension, StringComparer.OrdinalIgnoreCase))
.ToList();
foreach (var file in eligibleFiles)
{
try
{
_fileSystem.DeleteFile(file.FullName);
}
catch (Exception ex)
{
_logger.ErrorException("Error deleting file {0}", ex, file.FullName);
}
}
}
/// <summary>
/// Deletes the empty folders.
/// </summary>
/// <param name="path">The path.</param>
private void DeleteEmptyFolders(string path)
{
try
{
foreach (var d in _fileSystem.GetDirectoryPaths(path))
{
DeleteEmptyFolders(d);
}
var entries = _fileSystem.GetFileSystemEntryPaths(path);
if (!entries.Any())
{
try
{
_logger.Debug("Deleting empty directory {0}", path);
_fileSystem.DeleteDirectory(path, false);
}
catch (UnauthorizedAccessException) { }
catch (DirectoryNotFoundException) { }
}
}
catch (UnauthorizedAccessException) { }
}
/// <summary>
/// Determines if a given folder path is contained in a folder list
/// </summary>
/// <param name="path">The folder path to check.</param>
/// <param name="watchLocations">A list of folders.</param>
private bool IsWatchFolder(string path, IEnumerable<string> watchLocations)
{
return watchLocations.Contains(path, StringComparer.OrdinalIgnoreCase);
}
}
}
|