<cfprocessingdirective pageEncoding="utf-8">
<!---This variable specifies relative path to the folder, where the gallery with uploaded files is located.
Do not forget about the slash in the end of the folder name.--->
<cfset galleryPath="../Gallery/" />
		
<cfset absGalleryPath="#ExpandPath(galleryPath)#" />
<cfset absThumbnailsPath="#absGalleryPath#Thumbnails/" />
        
<!---NOTE: Since each file is uploaded in a separate request (because 
FilesPerOnePackageCount param equals to 1), you should work only 
with POST fields with index = 1 (such as SourceFile_1, FileName_1, etc).--->
<cffile action="UPLOAD" filefield="SourceFile_1" 
	destination="#absGalleryPath#" 
	nameconflict="MakeUnique">
	
<cfset fileName="#serverFile#">
        
<!---Get the thumbnail and save it to disk.--->
<cffile action="UPLOAD" filefield="Thumbnail1_1" 
	destination="#absThumbnailsPath#" 
	nameconflict="MakeUnique">

<!---Rename thumbnail file so that it has .jpg extension--->
<cffile action="rename"
	source="#absThumbnailsPath#/#serverFile#"
	destination="#absThumbnailsPath#/#fileName#.jpg"> 

<!---Get file info and save it to XML file.
NOTE: In this demo this file is used by different upload threads asynchronously. That's why 
it is necessary to synchronize access to this file.--->

<cflock name="#absGalleryPath#Descriptions.xml" timeout=30 type="Exclusive">

	<cfif FileExists("#absGalleryPath#Descriptions.xml")>
		<cfset descriptions=XmlParse("#absGalleryPath#Descriptions.xml") />
	<cfelse>
		<cfset descriptions=XmlParse("<files />") />
	</cfif>
	
	<cfset xmlFile=XmlElemNew(descriptions, "file") />
	<cfset xmlFile.XmlAttributes.name=fileName />
	<cfset xmlFile.XmlAttributes.width=Form.Width_1 />
	<cfset xmlFile.XmlAttributes.height=Form.Height_1 />
	<cfset xmlFile.XmlAttributes.handlerUrl="#CGI.SCRIPT_NAME#?#CGI.QUERY_STRING#" />
	<cfset ArrayAppend(descriptions.files.XmlChildren, xmlFile) />
					
	<cffile action="write" file="#absGalleryPath#Descriptions.xml" 
			output="#toString(descriptions)#" />

</cflock>