don't overwrite existing files when converting images/audio.

This commit is contained in:
morkt
2015-08-03 22:33:00 +04:00
parent 54d8d1cd86
commit 3a293baeea
2 changed files with 44 additions and 6 deletions

View File

@@ -173,7 +173,7 @@ namespace GARbro.GUI
if (source_ext == source_format)
return;
string output_name = Path.ChangeExtension (filename, source_format);
using (var output = File.Create (output_name))
using (var output = CreateNewFile (output_name))
{
input.Source.Position = 0;
input.Source.CopyTo (output);
@@ -184,7 +184,7 @@ namespace GARbro.GUI
if (source_ext == "wav")
return;
string output_name = Path.ChangeExtension (filename, "wav");
using (var output = File.Create (output_name))
using (var output = CreateNewFile (output_name))
WavFormat.Write (input, output);
}
}
@@ -204,7 +204,7 @@ namespace GARbro.GUI
return;
try
{
using (var output = File.Create (target_name))
using (var output = CreateNewFile (target_name))
m_image_format.Write (output, image);
}
catch // delete destination file on conversion failure
@@ -215,6 +215,31 @@ namespace GARbro.GUI
}
}
/// <summary>
/// Creates new file with specified filename, or, if it's already exists, tries to open
/// files named "FILENAME.1.EXT", "FILENAME.2.EXT" and so on.
/// <exception cref="System.IOException">Throws exception after 100th failed attempt.</exception>
/// </summary>
public static Stream CreateNewFile (string filename)
{
string name = filename;
var ext = new Lazy<string> (() => Path.GetExtension (filename));
for (int attempt = 1; ; ++attempt)
{
try
{
return File.Open (name, FileMode.CreateNew);
}
catch (IOException) // file already exists
{
if (100 == attempt) // limit number of attempts
throw;
}
name = Path.ChangeExtension (filename, attempt.ToString()+ext.Value);
}
}
void OnConvertComplete (object sender, RunWorkerCompletedEventArgs e)
{
m_main.ResumeWatchDirectoryChanges();