From f5d4e32b93d381675b4292e00a9de4f30bc29833 Mon Sep 17 00:00:00 2001 From: John Andrews Date: Sat, 21 May 2022 21:28:43 +1200 Subject: [PATCH] fixing copy file --- BasicNodes/File/CopyFile.cs | 6 ++++-- BasicNodes/Tests/CopyTests.cs | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/BasicNodes/File/CopyFile.cs b/BasicNodes/File/CopyFile.cs index 213a36bf..4dfcfa3d 100644 --- a/BasicNodes/File/CopyFile.cs +++ b/BasicNodes/File/CopyFile.cs @@ -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) { diff --git a/BasicNodes/Tests/CopyTests.cs b/BasicNodes/Tests/CopyTests.cs index eace809f..fa2f2a7b 100644 --- a/BasicNodes/Tests/CopyTests.cs +++ b/BasicNodes/Tests/CopyTests.cs @@ -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> Mappings = new List> + { + new KeyValuePair("/usr/local/bin/ffmpeg", @"C:\Users\username\AppData\Roaming\FileFlows\Tools\ffmpeg.exe"), + new KeyValuePair("/mnt/tempNAS/media/dvd/sorted", @"\\192.168.1.22\Media\dvd\sorted"), + new KeyValuePair("/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 \ No newline at end of file