Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove code for pre-installed plugins & properly check if file exists #740

Merged
merged 2 commits into from Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -246,13 +246,14 @@ public object GetConfiguration(string key)

private object LoadConfiguration(string path, Type configurationType)
{
try
if (!File.Exists(path))
{
return XmlSerializer.DeserializeFromFile(configurationType, path);
return Activator.CreateInstance(configurationType);
}
catch (FileNotFoundException)

try
{
return Activator.CreateInstance(configurationType);
return XmlSerializer.DeserializeFromFile(configurationType, path);
}
catch (IOException)
{
Expand Down
75 changes: 1 addition & 74 deletions Emby.Server.Implementations/ApplicationHost.cs
Expand Up @@ -1564,7 +1564,7 @@ public void Restart()
/// <returns>IEnumerable{Assembly}.</returns>
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
// This will prevent the .dll file from getting locked, and allow us to replace it when needed
Expand Down Expand Up @@ -1615,79 +1615,6 @@ public void Restart()

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)
{
try
Expand Down
30 changes: 10 additions & 20 deletions Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs
Expand Up @@ -286,28 +286,18 @@ public async Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, string

private HttpResponseInfo GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url)
{
try
{
if (_fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow)
{
var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true);

return new HttpResponseInfo
{
ResponseUrl = url,
Content = stream,
StatusCode = HttpStatusCode.OK,
ContentLength = stream.Length
};
}
}
catch (FileNotFoundException) // REVIEW: @bond Is this really faster?
{

}
catch (DirectoryNotFoundException)
if (File.Exists(responseCachePath)
&& _fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow)
{
var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true);

return new HttpResponseInfo
{
ResponseUrl = url,
Content = stream,
StatusCode = HttpStatusCode.OK,
ContentLength = stream.Length
};
}

return null;
Expand Down
47 changes: 21 additions & 26 deletions Emby.Server.Implementations/Library/LibraryManager.cs
Expand Up @@ -395,38 +395,33 @@ public void DeleteItem(BaseItem item, DeleteOptions options, BaseItem parent, bo

foreach (var fileSystemInfo in item.GetDeletePaths().ToList())
{
try
if (File.Exists(fileSystemInfo.FullName))
{
_logger.LogDebug("Deleting path {path}", fileSystemInfo.FullName);
if (fileSystemInfo.IsDirectory)
try
{
_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 (FileNotFoundException)
{
// may have already been deleted manually by user
}
catch (DirectoryNotFoundException)
{
// may have already been deleted manually by user
}
catch (IOException)
{
if (isRequiredForDelete)
{
throw;
}
}
catch (UnauthorizedAccessException)
{
if (isRequiredForDelete)
catch (UnauthorizedAccessException)
{
throw;
if (isRequiredForDelete)
{
throw;
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions Emby.Server.Implementations/Library/UserManager.cs
Expand Up @@ -1029,17 +1029,18 @@ public UserPolicy GetUserPolicy(User user)
{
var path = GetPolicyFilePath(user);

if (!File.Exists(path))
{
return GetDefaultPolicy(user);
}

try
{
lock (_policySyncLock)
{
return (UserPolicy)_xmlSerializer.DeserializeFromFile(typeof(UserPolicy), path);
}
}
catch (FileNotFoundException)
{
return GetDefaultPolicy(user);
}
catch (IOException)
{
return GetDefaultPolicy(user);
Expand Down Expand Up @@ -1128,17 +1129,18 @@ public UserConfiguration GetUserConfiguration(User user)
{
var path = GetConfigurationFilePath(user);

if (!File.Exists(path))
{
return new UserConfiguration();
}

try
{
lock (_configSyncLock)
{
return (UserConfiguration)_xmlSerializer.DeserializeFromFile(typeof(UserConfiguration), path);
}
}
catch (FileNotFoundException)
{
return new UserConfiguration();
}
catch (IOException)
{
return new UserConfiguration();
Expand Down
21 changes: 12 additions & 9 deletions Emby.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs
Expand Up @@ -101,17 +101,20 @@ public async Task Execute(CancellationToken cancellationToken, IProgress<double>

List<string> previouslyFailedImages;

try
if (File.Exists(failHistoryPath))
{
previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
.ToList();
}
catch (FileNotFoundException)
{
previouslyFailedImages = new List<string>();
try
{
previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
.ToList();
}
catch (IOException)
{
previouslyFailedImages = new List<string>();
}
}
catch (IOException)
else
{
previouslyFailedImages = new List<string>();
}
Expand Down
47 changes: 15 additions & 32 deletions Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs
Expand Up @@ -129,21 +129,16 @@ public TaskResult LastExecutionResult
{
if (_lastExecutionResult == null && !_readFromFile)
{
try
if (File.Exists(path))
{
_lastExecutionResult = JsonSerializer.DeserializeFromFile<TaskResult>(path);
}
catch (DirectoryNotFoundException)
{
// File doesn't exist. No biggie
}
catch (FileNotFoundException)
{
// File doesn't exist. No biggie
}
catch (Exception ex)
{
Logger.LogError(ex, "Error deserializing {path}", path);
try
{
_lastExecutionResult = JsonSerializer.DeserializeFromFile<TaskResult>(path);
}
catch (Exception ex)
{
Logger.LogError(ex, "Error deserializing {File}", path);
}
}
_readFromFile = true;
}
Expand Down Expand Up @@ -532,28 +527,16 @@ private string GetConfigurationFilePath()

private TaskTriggerInfo[] LoadTriggerSettings()
{
try
{
var list = JsonSerializer.DeserializeFromFile<IEnumerable<TaskTriggerInfo>>(GetConfigurationFilePath());

if (list != null)
{
return list.ToArray();
}
}
catch (FileNotFoundException)
{
// File doesn't exist. No biggie. Return defaults.
}
catch (DirectoryNotFoundException)
string path = GetConfigurationFilePath();
if (!File.Exists(path))
{
// File doesn't exist. No biggie. Return defaults.
GetDefaultTriggers();
}
catch
{

}
return GetDefaultTriggers();
var list = JsonSerializer.DeserializeFromFile<TaskTriggerInfo[]>(path);

return list ?? GetDefaultTriggers();
}

private TaskTriggerInfo[] GetDefaultTriggers()
Expand Down