[closed] Uploaded media assets should not contain spaces


Continuing the discussion from FarCry Workbench Project:

We need to double check (again) that all uploaded media assets are sanitised to not include illegal characters (with respect to the URL). Spaces (and other illegal chars) should be replaced with dashes -.


@justincarter @blair where did we get with a review of media file name sanitisation?


White spaces are still being left in the file name. (FC7.2.4)
There are a couple of instances where in lib.cdn.normalizePath()
reReplaceNoCase(arguments.path, "[^a-z0-9\.\-\_/ ]","", "all")
should possibly be
reReplaceNoCase(arguments.path, "[^a-z0-9\.\-\_/]","", "all")
I’d do a pull request but this does mess with existing files and the formtool displaying the images.
sanitizeFilename() (which should do the job) doesn’t look like it’s being called by anything. Is that right?



I just discovered that a site that I’d updated to 7.x last year (possibly started life as a 5.x) had issues with the image library if the image file names weren’t santitised originally. eg mypic (2).jpg . If you tried to edit the image (eg the title,alt) it would loose the actual image files completely because farcry would sanitise the path in the DB but not the file itself.
So I created a little script to run over the images in the image library and sanitise both the entries in the DB and files themselves.
It might be helpful for someone in crisis (as I was).

<cfset qImages = application.fapi.getcontentobjects(typename="dmImage")>
<cfset oIMG = createobject("component",application.stCOAPI['dmImage'].packagepath)>
<cfset lImageVarNames = "sourceimage,standardimage,thumbnailimage" />
<cfset bTest = true><!--- change this to false when your ready to do the changes --->
<cfif bTest>
<h2>This is just test run</h2>
<cfloop query="qImages">
<cfset sImage = application.fapi.getcontentobject(objectid="#qImages.objectid#")> 
<!--- create a new normalised path and name --->
<cfloop index="i" list="#lImageVarNames#">
    <cfset "New#i#" = application.fc.lib.cdn.normalizePath(evaluate("sImage."&i))>

<!--- rename the file --->
<cfloop index="i" list="#lImageVarNames#">
    <cfif evaluate("New"&i) NEQ evaluate("sImage."&i)>
    <cfoutput><li>Renaming #i# to #evaluate("New"&i)#</li></cfoutput>
        <cfif NOT bTest>
            <cffile  action = "rename" destination = "#ExpandPath(evaluate("New"&i))#" source = "#ExpandPath(evaluate("sImage."&i))#">
    <cfoutput><li>No Rename required for #i#</li></cfoutput>
<cfcatch><cfoutput><li>Error #cfcatch.Detail#</li></cfoutput></cfcatch>
<!--- update the image locations in the struct --->
<cfloop index="i" list="#lImageVarNames#">
    <cfset "sImage.#i#" = evaluate("New"&i)>

<!--- set the data ---->
<cfif NOT bTest>
    <cfset oIMG.setData(stProperties=sImage,user='admin')>


Filenames are now sanitized for file uploads, fixed in this ticket for the head of p700, p710 and p720: