Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cfml-.equals
var local.tempFile = createObject("java", "java.io.File").init(getTempDirectory() & arguments.filenameExample);
// throws "Invalid CFML construct" at .equals()
if (!local.tempFile.getCanonicalFile().getName().equals(local.tempFile)) {...}
// no error when assigned to a variable
var local.tempFileName = local.tempFile.getCanonicalFile().getName();
if (!local.tempFileName.equals(arguments.filenameExample)) {...}
@ryanguill

This comment has been minimized.

Copy link

ryanguill commented Oct 24, 2014

This is certainly a strange issue - for reference it does not error in railo, but I was able to reproduce in cf10.

Regardless, im not sure what you are trying to achieve with this code though - it looks like you are wanting to compare the name of the temp file with the file object itself - it returns false (NO) in both cases - if you are really wanting to compare the name of the file it seems like there should be an easier way.

@ryanguill

This comment has been minimized.

Copy link

ryanguill commented Oct 24, 2014

so digging into this a bit more, here is a simplified use case:

a = "foo";

function getA() {
    return a;
}

writedump(a.equals(a));//true
writedump(getA().equals(a));

the issue is actually a compile error, coldfusion gets confused on the .equals line. Its not that the type doesnt have an equals method, coldfusion just cant parse the syntax for some reason.

@tristanlee85

This comment has been minimized.

Copy link
Owner Author

tristanlee85 commented Oct 24, 2014

@ryanguill,

Good catch on the code. I had the argument for .equals() wrong when I typed this out. I should have just copied and pasted my actual app code. The point of the code is to be able the validate if the filename can be written to the disk as is. Here's the full example:

    try {
        local.tempFile = createObject("java", "java.io.File").init(getTempDirectory() & arguments.filenameExample);
        local.tempFileName = local.tempFile.getCanonicalFile().getName();

        // The name of the file in canonical form should equal the filenameExample
        if (!local.tempFileName.equals(arguments.filenameExample)) {
            throw (message = "File Name provided is invalid");  
        }

        // Write the file to disk to validate it's an acceptable name
        local.tempFile.createNewFile();
    } catch (any e) {
        addError("filenameExample", e.message);

        // Delete the file if written
        if (structKeyExists(local, "tempFile") && local.tempFile.isFile() && local.tempFile.exists()) {
            local.tempFile.delete();    
        }
    }

hfgdfdh$R(*^&^...^.pdf is a writable file for Unix platforms.

hfgdfdh$R(*/^&^...^.pdf is invalid because the canonical form of the File's name is ^&^...^.pdf (due to the forward slash).

And I am not shocked at all that is works on Railo, but not CF10. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.