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

pull/4/head
Mark McDowall 10 years ago
parent f8c002db9f
commit fd71bd6969

@ -27,21 +27,21 @@ namespace NzbDrone.Core.MediaFiles
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 @@ namespace NzbDrone.Core.MediaFiles
_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 @@ namespace NzbDrone.Core.MediaFiles
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 @@ namespace NzbDrone.Core.MediaFiles
_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);
}
}
}
}

@ -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);
}
}
}
}

@ -7,7 +7,6 @@ using NLog;
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 @@ using NzbDrone.Core.Tv;
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 @@ namespace NzbDrone.Core.Metadata
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 @@ namespace NzbDrone.Core.Metadata
IEpisodeService episodeService,
IDiskProvider diskProvider,
IHttpClient httpClient,
IConfigService configService,
IMediaFileAttributeService mediaFileAttributeService,
IEventAggregator eventAggregator,
Logger logger)
{
@ -52,7 +50,7 @@ namespace NzbDrone.Core.Metadata
_episodeService = episodeService;
_diskProvider = diskProvider;
_httpClient = httpClient;
_configService = configService;
_mediaFileAttributeService = mediaFileAttributeService;
_eventAggregator = eventAggregator;
_logger = logger;
}
@ -337,7 +335,7 @@ namespace NzbDrone.Core.Metadata
try
{
_httpClient.DownloadFile(url, path);
SetFilePermissions(path);
_mediaFileAttributeService.SetFilePermissions(path);
}
catch (WebException e)
{
@ -352,27 +350,7 @@ namespace NzbDrone.Core.Metadata
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);
}
}
}

@ -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">

Loading…
Cancel
Save