1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Refactored setting attributes on media/metadata files to its own service

This commit is contained in:
Mark McDowall 2014-11-18 07:38:31 -08:00
parent f8c002db9f
commit fd71bd6969
4 changed files with 116 additions and 98 deletions

View File

@ -27,21 +27,21 @@ public class EpisodeFileMovingService : IMoveEpisodeFiles
private readonly IUpdateEpisodeFileService _updateEpisodeFileService;
private readonly IBuildFileNames _buildFileNames;
private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService;
private readonly IMediaFileAttributeService _mediaFileAttributeService;
private readonly Logger _logger;
public EpisodeFileMovingService(IEpisodeService episodeService,
IUpdateEpisodeFileService updateEpisodeFileService,
IBuildFileNames buildFileNames,
IDiskProvider diskProvider,
IConfigService configService,
IMediaFileAttributeService mediaFileAttributeService,
Logger logger)
{
_episodeService = episodeService;
_updateEpisodeFileService = updateEpisodeFileService;
_buildFileNames = buildFileNames;
_diskProvider = diskProvider;
_configService = configService;
_mediaFileAttributeService = mediaFileAttributeService;
_logger = logger;
}
@ -107,11 +107,11 @@ private EpisodeFile TransferFile(EpisodeFile episodeFile, Series series, List<Ep
_logger.ErrorException("Unable to create directory: " + directoryName, ex);
}
SetFolderPermissions(directoryName);
_mediaFileAttributeService.SetFolderPermissions(directoryName);
if (!directoryName.PathEquals(series.Path))
{
SetFolderPermissions(series.Path);
_mediaFileAttributeService.SetFolderPermissions(series.Path);
}
}
@ -132,13 +132,13 @@ private EpisodeFile TransferFile(EpisodeFile episodeFile, Series series, List<Ep
try
{
SetFolderLastWriteTime(series.Path, episodeFile.DateAdded);
_mediaFileAttributeService.SetFolderLastWriteTime(series.Path, episodeFile.DateAdded);
if (series.SeasonFolder)
{
var seasonFolder = Path.GetDirectoryName(destinationFilename);
SetFolderLastWriteTime(seasonFolder, episodeFile.DateAdded);
_mediaFileAttributeService.SetFolderLastWriteTime(seasonFolder, episodeFile.DateAdded);
}
}
@ -147,68 +147,9 @@ private EpisodeFile TransferFile(EpisodeFile episodeFile, Series series, List<Ep
_logger.WarnException("Unable to set last write time", ex);
}
//We should only run this on Windows
if (OsInfo.IsWindows)
{
//Wrapped in Try/Catch to prevent this from causing issues with remote NAS boxes, the move worked, which is more important.
try
{
_diskProvider.InheritFolderPermissions(destinationFilename);
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException || ex is InvalidOperationException)
{
_logger.Debug("Unable to apply folder permissions to: ", destinationFilename);
_logger.DebugException(ex.Message, ex);
}
else
{
throw;
}
}
}
else
{
SetPermissions(destinationFilename, _configService.FileChmod);
}
_mediaFileAttributeService.SetFilePermissions(destinationFilename);
return episodeFile;
}
private void SetPermissions(string path, string permissions)
{
if (!_configService.SetPermissionsLinux)
{
return;
}
try
{
_diskProvider.SetPermissions(path, permissions, _configService.ChownUser, _configService.ChownGroup);
}
catch (Exception ex)
{
_logger.WarnException("Unable to apply permissions to: " + path, ex);
_logger.DebugException(ex.Message, ex);
}
}
private void SetFolderPermissions(string path)
{
SetPermissions(path, _configService.FolderChmod);
}
private void SetFolderLastWriteTime(String path, DateTime time)
{
if (OsInfo.IsMono) return;
_logger.Debug("Setting last write time on series folder: {0}", path);
_diskProvider.FolderSetLastWriteTime(path, time);
}
}
}

View File

@ -0,0 +1,98 @@
using System;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Configuration;
namespace NzbDrone.Core.MediaFiles
{
public interface IMediaFileAttributeService
{
void SetFilePermissions(string path);
void SetFolderPermissions(string path);
void SetFolderLastWriteTime(string path, DateTime time);
}
public class MediaFileAttributeService : IMediaFileAttributeService
{
private readonly IConfigService _configService;
private readonly IDiskProvider _diskProvider;
private readonly Logger _logger;
public MediaFileAttributeService(IConfigService configService,
IDiskProvider diskProvider,
Logger logger)
{
_configService = configService;
_diskProvider = diskProvider;
_logger = logger;
}
public void SetFilePermissions(string path)
{
if (OsInfo.IsWindows)
{
//Wrapped in Try/Catch to prevent this from causing issues with remote NAS boxes
try
{
_diskProvider.InheritFolderPermissions(path);
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException || ex is InvalidOperationException)
{
_logger.Debug("Unable to apply folder permissions to: ", path);
_logger.DebugException(ex.Message, ex);
}
else
{
throw;
}
}
}
else
{
SetMonoPermissions(path, _configService.FileChmod);
}
}
public void SetFolderPermissions(string path)
{
if (OsInfo.IsMono)
{
SetMonoPermissions(path, _configService.FolderChmod);
}
}
public void SetFolderLastWriteTime(string path, DateTime time)
{
if (OsInfo.IsWindows)
{
_logger.Debug("Setting last write time on series folder: {0}", path);
_diskProvider.FolderSetLastWriteTime(path, time);
}
}
private void SetMonoPermissions(string path, string permissions)
{
if (!_configService.SetPermissionsLinux)
{
return;
}
try
{
_diskProvider.SetPermissions(path, permissions, _configService.ChownUser, _configService.ChownGroup);
}
catch (Exception ex)
{
_logger.WarnException("Unable to apply permissions to: " + path, ex);
_logger.DebugException(ex.Message, ex);
}
}
}
}

View File

@ -7,7 +7,6 @@
using NzbDrone.Common;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.MediaFiles;
@ -18,10 +17,9 @@
namespace NzbDrone.Core.Metadata
{
public class MetadataService
: IHandle<MediaCoversUpdatedEvent>,
IHandle<EpisodeImportedEvent>,
IHandle<SeriesRenamedEvent>
public class MetadataService : IHandle<MediaCoversUpdatedEvent>,
IHandle<EpisodeImportedEvent>,
IHandle<SeriesRenamedEvent>
{
private readonly IMetadataFactory _metadataFactory;
private readonly IMetadataFileService _metadataFileService;
@ -30,7 +28,7 @@ public class MetadataService
private readonly IEpisodeService _episodeService;
private readonly IDiskProvider _diskProvider;
private readonly IHttpClient _httpClient;
private readonly IConfigService _configService;
private readonly IMediaFileAttributeService _mediaFileAttributeService;
private readonly IEventAggregator _eventAggregator;
private readonly Logger _logger;
@ -41,7 +39,7 @@ public MetadataService(IMetadataFactory metadataFactory,
IEpisodeService episodeService,
IDiskProvider diskProvider,
IHttpClient httpClient,
IConfigService configService,
IMediaFileAttributeService mediaFileAttributeService,
IEventAggregator eventAggregator,
Logger logger)
{
@ -52,7 +50,7 @@ public MetadataService(IMetadataFactory metadataFactory,
_episodeService = episodeService;
_diskProvider = diskProvider;
_httpClient = httpClient;
_configService = configService;
_mediaFileAttributeService = mediaFileAttributeService;
_eventAggregator = eventAggregator;
_logger = logger;
}
@ -337,7 +335,7 @@ private void DownloadImage(Series series, String url, String path)
try
{
_httpClient.DownloadFile(url, path);
SetFilePermissions(path);
_mediaFileAttributeService.SetFilePermissions(path);
}
catch (WebException e)
{
@ -352,27 +350,7 @@ private void DownloadImage(Series series, String url, String path)
private void SaveMetadataFile(String path, String contents)
{
_diskProvider.WriteAllText(path, contents);
SetFilePermissions(path);
}
private void SetFilePermissions(String path)
{
if (!_configService.SetPermissionsLinux)
{
return;
}
try
{
_diskProvider.SetPermissions(path, _configService.FileChmod, _configService.ChownUser, _configService.ChownGroup);
}
catch (Exception ex)
{
_logger.WarnException("Unable to apply permissions to: " + path, ex);
_logger.DebugException(ex.Message, ex);
}
_mediaFileAttributeService.SetFilePermissions(path);
}
}
}

View File

@ -505,6 +505,7 @@
<Compile Include="MediaFiles\Events\SeriesRenamedEvent.cs" />
<Compile Include="MediaFiles\Events\SeriesScannedEvent.cs" />
<Compile Include="MediaFiles\FileDateType.cs" />
<Compile Include="MediaFiles\MediaFileAttributeService.cs" />
<Compile Include="MediaFiles\MediaFileExtensions.cs" />
<Compile Include="MediaFiles\MediaFileRepository.cs" />
<Compile Include="MediaFiles\MediaFileService.cs">