Fixes #240 Co-Authored-By: taloth <taloth@users.noreply.github.com>pull/6/head
parent
9d056006cc
commit
b03b3d8243
@ -0,0 +1,65 @@
|
||||
using System.IO;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Extras.Metadata;
|
||||
using NzbDrone.Core.Extras.Metadata.Consumers.Xbmc;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Core.Music;
|
||||
using NzbDrone.Test.Common;
|
||||
|
||||
namespace NzbDrone.Core.Test.Extras.Metadata.Consumers.Xbmc
|
||||
{
|
||||
[TestFixture]
|
||||
public class FindMetadataFileFixture : CoreTest<XbmcMetadata>
|
||||
{
|
||||
private Artist _artist;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_artist = Builder<Artist>.CreateNew()
|
||||
.With(s => s.Path = @"C:\Test\Music\The.Artist".AsOsAgnostic())
|
||||
.Build();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_null_if_filename_is_not_handled()
|
||||
{
|
||||
var path = Path.Combine(_artist.Path, "file.jpg");
|
||||
|
||||
Subject.FindMetadataFile(_artist, path).Should().BeNull();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_metadata_for_xbmc_nfo()
|
||||
{
|
||||
var path = Path.Combine(_artist.Path, "album.nfo");
|
||||
|
||||
Mocker.GetMock<IDetectXbmcNfo>()
|
||||
.Setup(v => v.IsXbmcNfoFile(path))
|
||||
.Returns(true);
|
||||
|
||||
Subject.FindMetadataFile(_artist, path).Type.Should().Be(MetadataType.AlbumMetadata);
|
||||
|
||||
Mocker.GetMock<IDetectXbmcNfo>()
|
||||
.Verify(v => v.IsXbmcNfoFile(It.IsAny<string>()), Times.Once());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_null_for_scene_nfo()
|
||||
{
|
||||
var path = Path.Combine(_artist.Path, "album.nfo");
|
||||
|
||||
Mocker.GetMock<IDetectXbmcNfo>()
|
||||
.Setup(v => v.IsXbmcNfoFile(path))
|
||||
.Returns(false);
|
||||
|
||||
Subject.FindMetadataFile(_artist, path).Should().BeNull();
|
||||
|
||||
Mocker.GetMock<IDetectXbmcNfo>()
|
||||
.Verify(v => v.IsXbmcNfoFile(It.IsAny<string>()), Times.Once());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using NzbDrone.Common.Disk;
|
||||
|
||||
namespace NzbDrone.Core.Extras.Metadata.Consumers.Xbmc
|
||||
{
|
||||
public interface IDetectXbmcNfo
|
||||
{
|
||||
bool IsXbmcNfoFile(string path);
|
||||
}
|
||||
|
||||
public class XbmcNfoDetector : IDetectXbmcNfo
|
||||
{
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
|
||||
private readonly Regex _regex = new Regex("<(movie|tvshow|episodedetails|artist|album|musicvideo)>", RegexOptions.Compiled);
|
||||
|
||||
public XbmcNfoDetector(IDiskProvider diskProvider)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
}
|
||||
|
||||
public bool IsXbmcNfoFile(string path)
|
||||
{
|
||||
// Lets make sure we're not reading huge files.
|
||||
if (_diskProvider.GetFileSize(path) > 10.Megabytes())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if it contains some of the kodi/xbmc xml tags
|
||||
var content = _diskProvider.ReadAllText(path);
|
||||
|
||||
return _regex.IsMatch(content);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Disk;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.MediaFiles;
|
||||
using NzbDrone.Core.Music;
|
||||
|
||||
namespace NzbDrone.Core.Extras.Others
|
||||
{
|
||||
public interface IOtherExtraFileRenamer
|
||||
{
|
||||
void RenameOtherExtraFile(Artist artist, string path);
|
||||
}
|
||||
|
||||
public class OtherExtraFileRenamer : IOtherExtraFileRenamer
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly IRecycleBinProvider _recycleBinProvider;
|
||||
private readonly IArtistService _artistService;
|
||||
private readonly IOtherExtraFileService _otherExtraFileService;
|
||||
|
||||
public OtherExtraFileRenamer(IOtherExtraFileService otherExtraFileService,
|
||||
IArtistService artistService,
|
||||
IRecycleBinProvider recycleBinProvider,
|
||||
IDiskProvider diskProvider,
|
||||
Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_diskProvider = diskProvider;
|
||||
_recycleBinProvider = recycleBinProvider;
|
||||
_artistService = artistService;
|
||||
_otherExtraFileService = otherExtraFileService;
|
||||
}
|
||||
|
||||
public void RenameOtherExtraFile(Artist artist, string path)
|
||||
{
|
||||
if (!_diskProvider.FileExists(path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var relativePath = artist.Path.GetRelativePath(path);
|
||||
|
||||
var otherExtraFile = _otherExtraFileService.FindByPath(relativePath);
|
||||
if (otherExtraFile != null)
|
||||
{
|
||||
var newPath = path + "-orig";
|
||||
|
||||
// Recycle an existing -orig file.
|
||||
RemoveOtherExtraFile(artist, newPath);
|
||||
|
||||
// Rename the file to .*-orig
|
||||
_diskProvider.MoveFile(path, newPath);
|
||||
otherExtraFile.RelativePath = relativePath + "-orig";
|
||||
otherExtraFile.Extension += "-orig";
|
||||
_otherExtraFileService.Upsert(otherExtraFile);
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveOtherExtraFile(Artist artist, string path)
|
||||
{
|
||||
if (!_diskProvider.FileExists(path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var relativePath = artist.Path.GetRelativePath(path);
|
||||
|
||||
var otherExtraFile = _otherExtraFileService.FindByPath(relativePath);
|
||||
if (otherExtraFile != null)
|
||||
{
|
||||
var subfolder = Path.GetDirectoryName(relativePath);
|
||||
_recycleBinProvider.DeleteFile(path, subfolder);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue