Fixed: Renaming Episodes will never overwrite existing files.

pull/2/head
Taloth Saldono 11 years ago committed by Mark McDowall
parent 125e69da6d
commit e5e00fd346

@ -63,7 +63,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
File.WriteAllText(source2, "SourceFile2"); File.WriteAllText(source2, "SourceFile2");
Subject.MoveFile(source1, destination); Subject.MoveFile(source1, destination);
Subject.MoveFile(source2, destination); Subject.MoveFile(source2, destination, true);
File.Exists(destination).Should().BeTrue(); File.Exists(destination).Should().BeTrue();
} }
@ -75,7 +75,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
File.WriteAllText(source, "SourceFile1"); File.WriteAllText(source, "SourceFile1");
Subject.MoveFile(source, source); Subject.MoveFile(source, source, true);
File.Exists(source).Should().BeTrue(); File.Exists(source).Should().BeTrue();
ExceptionVerification.ExpectedWarns(1); ExceptionVerification.ExpectedWarns(1);
@ -148,7 +148,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
File.SetAttributes(source, FileAttributes.ReadOnly); File.SetAttributes(source, FileAttributes.ReadOnly);
File.SetAttributes(destination, FileAttributes.ReadOnly); File.SetAttributes(destination, FileAttributes.ReadOnly);
Subject.MoveFile(source, destination); Subject.MoveFile(source, destination, true);
} }
[Test] [Test]

@ -220,7 +220,7 @@ namespace NzbDrone.Common.Disk
} }
case TransferAction.Move: case TransferAction.Move:
{ {
MoveFile(sourceFile.FullName, destFile); MoveFile(sourceFile.FullName, destFile, true);
break; break;
} }
} }
@ -251,7 +251,7 @@ namespace NzbDrone.Common.Disk
File.Copy(source, destination, overwrite); File.Copy(source, destination, overwrite);
} }
public void MoveFile(string source, string destination) public void MoveFile(string source, string destination, bool overwrite = false)
{ {
Ensure.That(source, () => source).IsValidPath(); Ensure.That(source, () => source).IsValidPath();
Ensure.That(destination, () => destination).IsValidPath(); Ensure.That(destination, () => destination).IsValidPath();
@ -262,7 +262,7 @@ namespace NzbDrone.Common.Disk
return; return;
} }
if (FileExists(destination)) if (FileExists(destination) && overwrite)
{ {
DeleteFile(destination); DeleteFile(destination);
} }

@ -29,7 +29,7 @@ namespace NzbDrone.Common.Disk
void MoveFolder(string source, string destination); void MoveFolder(string source, string destination);
void DeleteFile(string path); void DeleteFile(string path);
void CopyFile(string source, string destination, bool overwrite = false); void CopyFile(string source, string destination, bool overwrite = false);
void MoveFile(string source, string destination); void MoveFile(string source, string destination, bool overwrite = false);
void DeleteFolder(string path, bool recursive); void DeleteFolder(string path, bool recursive);
string ReadAllText(string filePath); string ReadAllText(string filePath);
void WriteAllText(string filename, string contents); void WriteAllText(string filename, string contents);

@ -47,7 +47,23 @@ namespace NzbDrone.Core.Test.ProviderTests.RecycleBinProviderTests
Mocker.Resolve<RecycleBinProvider>().DeleteFile(path); Mocker.Resolve<RecycleBinProvider>().DeleteFile(path);
Mocker.GetMock<IDiskProvider>().Verify(v => v.MoveFile(path, @"C:\Test\Recycle Bin\S01E01.avi".AsOsAgnostic()), Times.Once()); Mocker.GetMock<IDiskProvider>().Verify(v => v.MoveFile(path, @"C:\Test\Recycle Bin\S01E01.avi".AsOsAgnostic(), true), Times.Once());
}
[Test]
public void should_use_alternative_name_if_already_exists()
{
WithRecycleBin();
var path = @"C:\Test\TV\30 Rock\S01E01.avi".AsOsAgnostic();
Mocker.GetMock<IDiskProvider>()
.Setup(v => v.FileExists(@"C:\Test\Recycle Bin\S01E01.avi".AsOsAgnostic()))
.Returns(true);
Mocker.Resolve<RecycleBinProvider>().DeleteFile(path);
Mocker.GetMock<IDiskProvider>().Verify(v => v.MoveFile(path, @"C:\Test\Recycle Bin\S01E01_2.avi".AsOsAgnostic(), true), Times.Once());
} }
[Test] [Test]

@ -84,10 +84,25 @@ namespace NzbDrone.Core.MediaFiles
else else
{ {
var destination = Path.Combine(recyclingBin, new FileInfo(path).Name); var fileInfo = new FileInfo(path);
var destination = Path.Combine(recyclingBin, fileInfo.Name);
var index = 1;
while (_diskProvider.FileExists(destination))
{
index++;
if (fileInfo.Extension.IsNullOrWhiteSpace())
{
destination = Path.Combine(recyclingBin, fileInfo.Name + "_" + index);
}
else
{
destination = Path.Combine(recyclingBin, Path.GetFileNameWithoutExtension(fileInfo.Name) + "_" + index + fileInfo.Extension);
}
}
logger.Debug("Moving '{0}' to '{1}'", path, destination); logger.Debug("Moving '{0}' to '{1}'", path, destination);
_diskProvider.MoveFile(path, destination); _diskProvider.MoveFile(path, destination, true);
_diskProvider.FileSetLastWriteTimeUtc(destination, DateTime.UtcNow); _diskProvider.FileSetLastWriteTimeUtc(destination, DateTime.UtcNow);
logger.Debug("File has been moved to the recycling bin: {0}", destination); logger.Debug("File has been moved to the recycling bin: {0}", destination);
} }

Loading…
Cancel
Save