fixing copy file

This commit is contained in:
John Andrews
2022-05-21 21:28:43 +12:00
parent 47d845e70f
commit f5d4e32b93
2 changed files with 38 additions and 3 deletions

View File

@@ -77,8 +77,10 @@ namespace FileFlows.BasicNodes.File
}
args.Logger.ILog($"CopyFile.Dest[5] '{dest}'");
var destDir = new FileInfo(dest).DirectoryName;
args.CreateDirectoryIfNotExists(destDir ?? String.Empty);
// cant use new FileInfo(dest).Directory.Name here since
// if the folder is a linux folder and this node is running on windows
// /mnt, etc will be converted to c:\mnt and break the destination
var destDir = dest.Substring(0, dest.Replace("\\", "/").LastIndexOf("/"));
if(string.IsNullOrEmpty(DestinationFile) == false)
{

View File

@@ -6,17 +6,25 @@ namespace BasicNodes.Tests;
using FileFlows.BasicNodes.File;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text.RegularExpressions;
[TestClass]
public class CopyTests
{
List<KeyValuePair<string, string>> Mappings = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("/usr/local/bin/ffmpeg", @"C:\Users\username\AppData\Roaming\FileFlows\Tools\ffmpeg.exe"),
new KeyValuePair<string, string>("/mnt/tempNAS/media/dvd/sorted", @"\\192.168.1.22\Media\dvd\sorted"),
new KeyValuePair<string, string>("/mnt/tempNAS/media/dvd/output", @"\\192.168.1.22\Media\dvd\output"),
};
char DirectorySeperatorChar = System.IO.Path.DirectorySeparatorChar;
[TestMethod]
public void CopyTests_Dir_Mapping()
{
var logger = new TestLogger();
var args = new FileFlows.Plugin.NodeParameters(@"c:\test\testfile.mkv", logger, false, string.Empty);
args.PathMapper = s => s;
args.PathMapper = s => Map(s);
CopyFile node = new ();
node.CopyFolder = true;
@@ -26,6 +34,31 @@ public class CopyTests
Assert.AreEqual(2, result);
}
string Map(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;
if (Mappings != null && Mappings.Count > 0)
{
// convert all \ to / for now
path = path.Replace("\\", "/");
foreach (var mapping in Mappings)
{
if (string.IsNullOrEmpty(mapping.Value) || string.IsNullOrEmpty(mapping.Key))
continue;
string pattern = Regex.Escape(mapping.Key.Replace("\\", "/"));
string replacement = mapping.Value.Replace("\\", "/");
path = Regex.Replace(path, "^" + pattern, replacement, RegexOptions.IgnoreCase);
}
// now convert / to path charcter
if (DirectorySeperatorChar != '/')
path = path.Replace('/', DirectorySeperatorChar);
if (path.StartsWith("//")) // special case for SMB paths
path = path.Replace('/', '\\');
}
return path;
}
}
#endif