CopyFilesToDirectory will now allow for one duplicate copy over and put (2) (#1126)

This commit is contained in:
Joseph Milazzo 2022-02-26 08:02:15 -06:00 committed by GitHub
parent 578a1410e9
commit fe865feca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
@ -354,6 +354,7 @@ namespace API.Services
/// <summary>
/// Copies files to a destination directory. If the destination directory doesn't exist, this will create it.
/// </summary>
/// <remarks>If a file already exists in dest, this will rename as (2). It does not support multiple iterations of this. Overwriting is not supported.</remarks>
/// <param name="filePaths"></param>
/// <param name="directoryPath"></param>
/// <param name="prepend">An optional string to prepend to the target file's name</param>
@ -370,7 +371,16 @@ namespace API.Services
var fileInfo = FileSystem.FileInfo.FromFileName(file);
if (fileInfo.Exists)
{
fileInfo.CopyTo(FileSystem.Path.Join(directoryPath, prepend + fileInfo.Name));
// TODO: I need to handle if file already exists and allow either an overwrite or prepend (2) to it
try
{
fileInfo.CopyTo(FileSystem.Path.Join(directoryPath, prepend + fileInfo.Name));
}
catch (IOException ex)
{
_logger.LogError(ex, "File copy, dest already exists. Appending (2)");
fileInfo.CopyTo(FileSystem.Path.Join(directoryPath, prepend + FileSystem.Path.GetFileNameWithoutExtension(fileInfo.Name) + " (2)" + FileSystem.Path.GetExtension(fileInfo.Name)));
}
}
else
{