2014-09-11 20:24:00 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common.Disk;
|
2014-12-02 06:26:25 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2014-09-11 20:24:00 +00:00
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
|
using NzbDrone.Core.ThingiProvider;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.RemotePathMappings
|
|
|
|
|
{
|
|
|
|
|
public interface IRemotePathMappingService
|
|
|
|
|
{
|
|
|
|
|
List<RemotePathMapping> All();
|
|
|
|
|
RemotePathMapping Add(RemotePathMapping mapping);
|
|
|
|
|
void Remove(int id);
|
|
|
|
|
RemotePathMapping Get(int id);
|
|
|
|
|
RemotePathMapping Update(RemotePathMapping mapping);
|
|
|
|
|
|
2014-10-12 22:00:03 +00:00
|
|
|
|
OsPath RemapRemoteToLocal(String host, OsPath remotePath);
|
|
|
|
|
OsPath RemapLocalToRemote(String host, OsPath localPath);
|
2014-09-11 20:24:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RemotePathMappingService : IRemotePathMappingService
|
|
|
|
|
{
|
|
|
|
|
private readonly IRemotePathMappingRepository _remotePathMappingRepository;
|
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly ICached<List<RemotePathMapping>> _cache;
|
|
|
|
|
|
|
|
|
|
public RemotePathMappingService(IDownloadClientRepository downloadClientRepository,
|
|
|
|
|
IRemotePathMappingRepository remotePathMappingRepository,
|
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
|
Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_remotePathMappingRepository = remotePathMappingRepository;
|
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
|
|
_cache = cacheManager.GetCache<List<RemotePathMapping>>(GetType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RemotePathMapping> All()
|
|
|
|
|
{
|
|
|
|
|
return _cache.Get("all", () => _remotePathMappingRepository.All().ToList(), TimeSpan.FromSeconds(10));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RemotePathMapping Add(RemotePathMapping mapping)
|
|
|
|
|
{
|
2014-10-12 22:00:03 +00:00
|
|
|
|
mapping.LocalPath = new OsPath(mapping.LocalPath).AsDirectory().FullPath;
|
|
|
|
|
mapping.RemotePath = new OsPath(mapping.RemotePath).AsDirectory().FullPath;
|
2014-09-11 20:24:00 +00:00
|
|
|
|
|
|
|
|
|
var all = All();
|
|
|
|
|
|
|
|
|
|
ValidateMapping(all, mapping);
|
|
|
|
|
|
|
|
|
|
var result = _remotePathMappingRepository.Insert(mapping);
|
|
|
|
|
|
|
|
|
|
_cache.Clear();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Remove(int id)
|
|
|
|
|
{
|
|
|
|
|
_remotePathMappingRepository.Delete(id);
|
|
|
|
|
|
|
|
|
|
_cache.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RemotePathMapping Get(int id)
|
|
|
|
|
{
|
|
|
|
|
return _remotePathMappingRepository.Get(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RemotePathMapping Update(RemotePathMapping mapping)
|
|
|
|
|
{
|
|
|
|
|
var existing = All().Where(v => v.Id != mapping.Id).ToList();
|
|
|
|
|
|
|
|
|
|
ValidateMapping(existing, mapping);
|
|
|
|
|
|
|
|
|
|
var result = _remotePathMappingRepository.Update(mapping);
|
|
|
|
|
|
|
|
|
|
_cache.Clear();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ValidateMapping(List<RemotePathMapping> existing, RemotePathMapping mapping)
|
|
|
|
|
{
|
|
|
|
|
if (mapping.Host.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Invalid Host");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 22:00:03 +00:00
|
|
|
|
var remotePath = new OsPath(mapping.RemotePath);
|
|
|
|
|
var localPath = new OsPath(mapping.LocalPath);
|
|
|
|
|
|
|
|
|
|
if (remotePath.IsEmpty)
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Invalid RemotePath");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 22:00:03 +00:00
|
|
|
|
if (localPath.IsEmpty || !localPath.IsRooted)
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Invalid LocalPath");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 22:00:03 +00:00
|
|
|
|
if (!_diskProvider.FolderExists(localPath.FullPath))
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
|
|
|
|
throw new DirectoryNotFoundException("Can't add mount point directory that doesn't exist.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (existing.Exists(r => r.Host == mapping.Host && r.RemotePath == mapping.RemotePath))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("RemotePath already mounted.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 22:00:03 +00:00
|
|
|
|
public OsPath RemapRemoteToLocal(String host, OsPath remotePath)
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
2014-10-12 22:00:03 +00:00
|
|
|
|
if (remotePath.IsEmpty)
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
|
|
|
|
return remotePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var mapping in All())
|
|
|
|
|
{
|
2014-10-12 22:00:03 +00:00
|
|
|
|
if (host == mapping.Host && new OsPath(mapping.RemotePath).Contains(remotePath))
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
2014-10-12 22:00:03 +00:00
|
|
|
|
var localPath = new OsPath(mapping.LocalPath) + (remotePath - new OsPath(mapping.RemotePath));
|
2014-09-11 20:24:00 +00:00
|
|
|
|
|
|
|
|
|
return localPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return remotePath;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 22:00:03 +00:00
|
|
|
|
public OsPath RemapLocalToRemote(String host, OsPath localPath)
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
2014-10-12 22:00:03 +00:00
|
|
|
|
if (localPath.IsEmpty)
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
|
|
|
|
return localPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var mapping in All())
|
|
|
|
|
{
|
2014-10-12 22:00:03 +00:00
|
|
|
|
if (host == mapping.Host && new OsPath(mapping.LocalPath).Contains(localPath))
|
2014-09-11 20:24:00 +00:00
|
|
|
|
{
|
2014-10-12 22:00:03 +00:00
|
|
|
|
var remotePath = new OsPath(mapping.RemotePath) + (localPath - new OsPath(mapping.LocalPath));
|
2014-09-11 20:24:00 +00:00
|
|
|
|
|
|
|
|
|
return remotePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return localPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|