From 58a0cb8df26e5b37622fb6d5376f7c8cd053f653 Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Mon, 19 Feb 2024 21:58:41 +0100 Subject: [PATCH] LDEV-3122 - manually adapt PR 1636 --- .../main/java/lucee/runtime/tag/FileTag.java | 36 ++++++----- loader/build.xml | 2 +- loader/pom.xml | 2 +- test/tickets/LDEV3122.cfc | 64 +++++++++++++++++++ 4 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 test/tickets/LDEV3122.cfc diff --git a/core/src/main/java/lucee/runtime/tag/FileTag.java b/core/src/main/java/lucee/runtime/tag/FileTag.java index d786d8c918..871249da04 100755 --- a/core/src/main/java/lucee/runtime/tag/FileTag.java +++ b/core/src/main/java/lucee/runtime/tag/FileTag.java @@ -517,15 +517,14 @@ public static void actionMove(PageContext pageContext, lucee.runtime.security.Se // destination if (destination.isDirectory()) destination = destination.getRealResource(source.getName()); - if (destination.exists()) { + if (nameconflict == NAMECONFLICT_FORCEUNIQUE) destination = forceUnique(destination); + else if (destination.exists()) { // SKIP if (nameconflict == NAMECONFLICT_SKIP) return; // OVERWRITE else if (nameconflict == NAMECONFLICT_OVERWRITE) destination.delete(); // MAKEUNIQUE else if (nameconflict == NAMECONFLICT_MAKEUNIQUE) destination = makeUnique(destination); - // FORCEUNIQUE - else if (nameconflict == NAMECONFLICT_FORCEUNIQUE) destination = forceUnique(destination); // ERROR else throw new ApplicationException("Destination file [" + destination.toString() + "] already exists"); } @@ -578,15 +577,15 @@ public static void actionCopy(PageContext pageContext, lucee.runtime.security.Se // destination if (destination.isDirectory()) destination = destination.getRealResource(source.getName()); - if (destination.exists()) { + // FORCEUNIQUE + if (nameconflict == NAMECONFLICT_FORCEUNIQUE) destination = forceUnique(destination); + else if (destination.exists()) { // SKIP if (nameconflict == NAMECONFLICT_SKIP) return; - // SKIP + // OVERWRITE else if (nameconflict == NAMECONFLICT_OVERWRITE) destination.delete(); // MAKEUNIQUE else if (nameconflict == NAMECONFLICT_MAKEUNIQUE) destination = makeUnique(destination); - // FORCEUNIQUE - else if (nameconflict == NAMECONFLICT_FORCEUNIQUE) destination = forceUnique(destination); // ERROR else throw new ApplicationException("Destination file [" + destination.toString() + "] already exists"); } @@ -628,9 +627,11 @@ private static Resource forceUnique(Resource res) { String name = ResourceUtil.getName(res); String ext = ResourceUtil.getExtension(res, ""); if (!StringUtil.isEmpty(ext)) ext = "." + ext; - while (res.exists()) { + do { + // forceunique always create a new name for fileUpload res = res.getParentResource().getRealResource(name + "_" + HashUtil.create64BitHashAsString(CreateUUID.invoke(), Character.MAX_RADIX) + ext); } + while (res.exists()); return res; } @@ -1018,6 +1019,16 @@ else if (!clientFileName.equalsIgnoreCase(destination.getName())) { cffile.set("attemptedserverfile", destination.getName()); // check nameconflict + if (nameconflict == NAMECONFLICT_FORCEUNIQUE) { + destination = forceUnique(destination); + fileWasRenamed = true; + + cffile.set("serverdirectory", getParent(destination)); + cffile.set("serverfile", destination.getName()); + cffile.set("serverfileext", ResourceUtil.getExtension(destination, "")); + cffile.set("serverfilename", ResourceUtil.getName(destination)); + } + if (destination.exists()) { fileExisted = true; if (nameconflict == NAMECONFLICT_ERROR) { @@ -1042,15 +1053,6 @@ else if (nameconflict == NAMECONFLICT_MAKEUNIQUE) { cffile.set("serverfilename", ResourceUtil.getName(destination)); // } } - else if (nameconflict == NAMECONFLICT_FORCEUNIQUE) { - destination = forceUnique(destination); - fileWasRenamed = true; - - cffile.set("serverdirectory", getParent(destination)); - cffile.set("serverfile", destination.getName()); - cffile.set("serverfileext", ResourceUtil.getExtension(destination, "")); - cffile.set("serverfilename", ResourceUtil.getName(destination)); - } else if (nameconflict == NAMECONFLICT_OVERWRITE) { // fileWasAppended=true; fileWasOverwritten = true; diff --git a/loader/build.xml b/loader/build.xml index eb3b268dac..3fea9a38be 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index f0d1e107a4..e7b56a8705 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.1.0.51-SNAPSHOT + 6.1.0.52-SNAPSHOT jar Lucee Loader Build diff --git a/test/tickets/LDEV3122.cfc b/test/tickets/LDEV3122.cfc new file mode 100644 index 0000000000..d2e25b17a2 --- /dev/null +++ b/test/tickets/LDEV3122.cfc @@ -0,0 +1,64 @@ +component extends="org.lucee.cfml.test.LuceeTestCase" { + function beforeAll() { + variables.path = "#getDirectoryFromPath(getCurrentTemplatePath())#LDEV3122"; + afterAll(); + if(!directoryExists(path)) directoryCreate(path) + } + + function run( testResults , testBox ) { + + describe( "Testcase for LDEV-3122", function() { + it( title="checking cffile nameconflict=makeunique without file exists", body=function() { + fileWrite("#path#\test.txt","LDEV-3122"); + var newDir = "#path#\test_#createUniqueID()#"; + directoryCreate(newDir) + + file action="copy" source="#path#\test.txt" destination=newDir nameconflict="makeunique"; + directory action="list" directory=newDir name="list"; + + expect(list.name).tobe("test.txt"); + }); + + it( title="checking cffile nameconflict=makeunique with file exists", body=function() { + var newDir = "#path#\test_#createUniqueID()#"; + directoryCreate(newDir) + filewrite("#newDir#\testmakeunique.txt","LDEV-3122"); + + file action="copy" source="#newDir#\testmakeunique.txt" destination=newDir nameconflict="makeunique"; + directory action="list" directory=newDir name="list" listinfo="name"; + + expect(find("testmakeunique_",serializeJson(list))).toBeGT(0); + expect(list.recordcount).toBe("2"); + }); + + it( title="checking cffile nameconflict=forceunique without file exists", body=function() { + var newDir = "#path#\test_#createUniqueID()#"; + directoryCreate(newDir) + filewrite("#path#\testforceunique.txt","LDEV-3122"); + + file action="copy" source="#path#\testforceunique.txt" destination=newDir nameconflict="forceunique"; + directory action="list" directory=newDir name="list" listinfo="name"; + + expect(find("testforceunique_",serializejson(list))).toBeGT(0); + expect(list.recordcount).toBe("1"); + }); + + it( title="checking cffile nameconflict=forceunique with file exists", body=function() { + var newDir = "#path#\test_#createUniqueID()#"; + directoryCreate(newDir) + filewrite("#newDir#\testfileforceunique.txt","LDEV-3122"); + + file action="copy" source="#newDir#\testfileforceunique.txt" destination=newDir nameconflict="forceunique"; + directory action="list" directory=newDir name="list" listinfo="name"; + + expect(find("testfileforceunique_",serializejson(list))).toBeGT(0); + expect(list.recordcount).toBe("2"); + }); + }); + + } + + function afterAll() { + if(directoryExists(path)) directoryDelete(path,true); + } +} \ No newline at end of file