Skip to content

Commit

Permalink
LDEV-3122 - manually adapt PR 1636
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Feb 19, 2024
1 parent 0664ca7 commit 58a0cb8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
36 changes: 19 additions & 17 deletions core/src/main/java/lucee/runtime/tag/FileTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.1.0.51-SNAPSHOT"/>
<property name="version" value="6.1.0.52-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.1.0.51-SNAPSHOT</version>
<version>6.1.0.52-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down
64 changes: 64 additions & 0 deletions test/tickets/LDEV3122.cfc
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 58a0cb8

Please sign in to comment.