Merge pull request #740 from Bond-009/deadcode

Remove code for pre-installed plugins & properly check if file exists
This commit is contained in:
Andrew Rabert 2019-01-27 14:04:39 -05:00 committed by GitHub
commit b4893b9ac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 173 deletions

View File

@ -246,14 +246,15 @@ namespace Emby.Server.Implementations.AppBase
private object LoadConfiguration(string path, Type configurationType) private object LoadConfiguration(string path, Type configurationType)
{ {
if (!File.Exists(path))
{
return Activator.CreateInstance(configurationType);
}
try try
{ {
return XmlSerializer.DeserializeFromFile(configurationType, path); return XmlSerializer.DeserializeFromFile(configurationType, path);
} }
catch (FileNotFoundException)
{
return Activator.CreateInstance(configurationType);
}
catch (IOException) catch (IOException)
{ {
return Activator.CreateInstance(configurationType); return Activator.CreateInstance(configurationType);

View File

@ -1564,7 +1564,7 @@ namespace Emby.Server.Implementations
/// <returns>IEnumerable{Assembly}.</returns> /// <returns>IEnumerable{Assembly}.</returns>
protected List<Tuple<Assembly, string>> GetComposablePartAssemblies() protected List<Tuple<Assembly, string>> GetComposablePartAssemblies()
{ {
var list = GetPluginAssemblies(); var list = GetPluginAssemblies(ApplicationPaths.PluginsPath);
// Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
// This will prevent the .dll file from getting locked, and allow us to replace it when needed // This will prevent the .dll file from getting locked, and allow us to replace it when needed
@ -1615,79 +1615,6 @@ namespace Emby.Server.Implementations
protected abstract IEnumerable<Assembly> GetAssembliesWithPartsInternal(); protected abstract IEnumerable<Assembly> GetAssembliesWithPartsInternal();
/// <summary>
/// Gets the plugin assemblies.
/// </summary>
/// <returns>IEnumerable{Assembly}.</returns>
private List<Tuple<Assembly, string>> GetPluginAssemblies()
{
// Copy pre-installed plugins
var sourcePath = Path.Combine(ApplicationPaths.ApplicationResourcesPath, "plugins");
CopyPlugins(sourcePath, ApplicationPaths.PluginsPath);
return GetPluginAssemblies(ApplicationPaths.PluginsPath);
}
private void CopyPlugins(string source, string target)
{
List<string> files;
try
{
files = Directory.EnumerateFiles(source, "*.dll", SearchOption.TopDirectoryOnly)
.ToList();
}
catch (DirectoryNotFoundException)
{
return;
}
if (files.Count == 0)
{
return;
}
foreach (var sourceFile in files)
{
var filename = Path.GetFileName(sourceFile);
var targetFile = Path.Combine(target, filename);
var targetFileExists = File.Exists(targetFile);
if (!targetFileExists && ServerConfigurationManager.Configuration.UninstalledPlugins.Contains(filename, StringComparer.OrdinalIgnoreCase))
{
continue;
}
if (targetFileExists && GetDllVersion(targetFile) >= GetDllVersion(sourceFile))
{
continue;
}
Directory.CreateDirectory(target);
File.Copy(sourceFile, targetFile, true);
}
}
private Version GetDllVersion(string path)
{
try
{
var result = Version.Parse(FileVersionInfo.GetVersionInfo(path).FileVersion);
Logger.LogInformation("File {Path} has version {Version}", path, result);
return result;
}
catch (Exception ex)
{
Logger.LogError(ex, "Error getting version number from {Path}", path);
return new Version(1, 0);
}
}
private List<Tuple<Assembly, string>> GetPluginAssemblies(string path) private List<Tuple<Assembly, string>> GetPluginAssemblies(string path)
{ {
try try

View File

@ -286,28 +286,18 @@ namespace Emby.Server.Implementations.HttpClientManager
private HttpResponseInfo GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) private HttpResponseInfo GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url)
{ {
try if (File.Exists(responseCachePath)
&& _fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow)
{ {
if (_fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow) var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true);
return new HttpResponseInfo
{ {
var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true); ResponseUrl = url,
Content = stream,
return new HttpResponseInfo StatusCode = HttpStatusCode.OK,
{ ContentLength = stream.Length
ResponseUrl = url, };
Content = stream,
StatusCode = HttpStatusCode.OK,
ContentLength = stream.Length
};
}
}
catch (FileNotFoundException) // REVIEW: @bond Is this really faster?
{
}
catch (DirectoryNotFoundException)
{
} }
return null; return null;

View File

@ -395,38 +395,33 @@ namespace Emby.Server.Implementations.Library
foreach (var fileSystemInfo in item.GetDeletePaths().ToList()) foreach (var fileSystemInfo in item.GetDeletePaths().ToList())
{ {
try if (File.Exists(fileSystemInfo.FullName))
{ {
_logger.LogDebug("Deleting path {path}", fileSystemInfo.FullName); try
if (fileSystemInfo.IsDirectory)
{ {
_fileSystem.DeleteDirectory(fileSystemInfo.FullName, true); _logger.LogDebug("Deleting path {path}", fileSystemInfo.FullName);
if (fileSystemInfo.IsDirectory)
{
_fileSystem.DeleteDirectory(fileSystemInfo.FullName, true);
}
else
{
_fileSystem.DeleteFile(fileSystemInfo.FullName);
}
} }
else catch (IOException)
{ {
_fileSystem.DeleteFile(fileSystemInfo.FullName); if (isRequiredForDelete)
{
throw;
}
} }
} catch (UnauthorizedAccessException)
catch (FileNotFoundException)
{
// may have already been deleted manually by user
}
catch (DirectoryNotFoundException)
{
// may have already been deleted manually by user
}
catch (IOException)
{
if (isRequiredForDelete)
{ {
throw; if (isRequiredForDelete)
} {
} throw;
catch (UnauthorizedAccessException) }
{
if (isRequiredForDelete)
{
throw;
} }
} }

View File

@ -1029,6 +1029,11 @@ namespace Emby.Server.Implementations.Library
{ {
var path = GetPolicyFilePath(user); var path = GetPolicyFilePath(user);
if (!File.Exists(path))
{
return GetDefaultPolicy(user);
}
try try
{ {
lock (_policySyncLock) lock (_policySyncLock)
@ -1036,10 +1041,6 @@ namespace Emby.Server.Implementations.Library
return (UserPolicy)_xmlSerializer.DeserializeFromFile(typeof(UserPolicy), path); return (UserPolicy)_xmlSerializer.DeserializeFromFile(typeof(UserPolicy), path);
} }
} }
catch (FileNotFoundException)
{
return GetDefaultPolicy(user);
}
catch (IOException) catch (IOException)
{ {
return GetDefaultPolicy(user); return GetDefaultPolicy(user);
@ -1128,6 +1129,11 @@ namespace Emby.Server.Implementations.Library
{ {
var path = GetConfigurationFilePath(user); var path = GetConfigurationFilePath(user);
if (!File.Exists(path))
{
return new UserConfiguration();
}
try try
{ {
lock (_configSyncLock) lock (_configSyncLock)
@ -1135,10 +1141,6 @@ namespace Emby.Server.Implementations.Library
return (UserConfiguration)_xmlSerializer.DeserializeFromFile(typeof(UserConfiguration), path); return (UserConfiguration)_xmlSerializer.DeserializeFromFile(typeof(UserConfiguration), path);
} }
} }
catch (FileNotFoundException)
{
return new UserConfiguration();
}
catch (IOException) catch (IOException)
{ {
return new UserConfiguration(); return new UserConfiguration();

View File

@ -101,17 +101,20 @@ namespace Emby.Server.Implementations.ScheduledTasks
List<string> previouslyFailedImages; List<string> previouslyFailedImages;
try if (File.Exists(failHistoryPath))
{ {
previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath) try
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) {
.ToList(); previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
.ToList();
}
catch (IOException)
{
previouslyFailedImages = new List<string>();
}
} }
catch (FileNotFoundException) else
{
previouslyFailedImages = new List<string>();
}
catch (IOException)
{ {
previouslyFailedImages = new List<string>(); previouslyFailedImages = new List<string>();
} }

View File

@ -129,21 +129,16 @@ namespace Emby.Server.Implementations.ScheduledTasks
{ {
if (_lastExecutionResult == null && !_readFromFile) if (_lastExecutionResult == null && !_readFromFile)
{ {
try if (File.Exists(path))
{ {
_lastExecutionResult = JsonSerializer.DeserializeFromFile<TaskResult>(path); try
} {
catch (DirectoryNotFoundException) _lastExecutionResult = JsonSerializer.DeserializeFromFile<TaskResult>(path);
{ }
// File doesn't exist. No biggie catch (Exception ex)
} {
catch (FileNotFoundException) Logger.LogError(ex, "Error deserializing {File}", path);
{ }
// File doesn't exist. No biggie
}
catch (Exception ex)
{
Logger.LogError(ex, "Error deserializing {path}", path);
} }
_readFromFile = true; _readFromFile = true;
} }
@ -532,28 +527,16 @@ namespace Emby.Server.Implementations.ScheduledTasks
private TaskTriggerInfo[] LoadTriggerSettings() private TaskTriggerInfo[] LoadTriggerSettings()
{ {
try string path = GetConfigurationFilePath();
{ if (!File.Exists(path))
var list = JsonSerializer.DeserializeFromFile<IEnumerable<TaskTriggerInfo>>(GetConfigurationFilePath());
if (list != null)
{
return list.ToArray();
}
}
catch (FileNotFoundException)
{ {
// File doesn't exist. No biggie. Return defaults. // File doesn't exist. No biggie. Return defaults.
GetDefaultTriggers();
} }
catch (DirectoryNotFoundException)
{
// File doesn't exist. No biggie. Return defaults.
}
catch
{
} var list = JsonSerializer.DeserializeFromFile<TaskTriggerInfo[]>(path);
return GetDefaultTriggers();
return list ?? GetDefaultTriggers();
} }
private TaskTriggerInfo[] GetDefaultTriggers() private TaskTriggerInfo[] GetDefaultTriggers()