<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/" />

<!---First of all, clear data uploaded at previous time.--->

<!---In this code sample each file is uploaded with a separate request. It means that this 
page will be called for each file individually (rather than once for all files). Therefore
we need a way to determine whether this page is called for new upload session or not.

To do it we check a PackageIndex POST field which returns a number of the request in the upload session.
If it is the very first package we should delete all files and thumbnails we uploaded before. 
Otherwise, previously uploaded files belong to the same upload session and we should not delete them.--->

<!---If we receive the first file, we delete all files and thumbnails we uploaded before.--->

<cfif #Form.PackageIndex# is 0>
	<!---Delete source files.--->
	<cfdirectory name="galleryDir" action="list" 
				directory="#absGalleryPath#" filter="*" />
	<cfloop query="galleryDir">
		<cfif FileExists("#absGalleryPath##Name#")>
			<cffile action="delete" file="#absGalleryPath##Name#" />
		</cfif>
	</cfloop>
	
	<!---Delete thumbnails.--->
	<cfdirectory name="thumbnailsDir" action="list" 
				directory="#absThumbnailsPath#" filter="*">
	<cfloop query="thumbnailsDir"> 
		<cfif FileExists("#absThumbnailsPath##Name#")>
			<cffile action="delete" file="#absThumbnailsPath##Name#" />
		</cfif>
	</cfloop>
</cfif>		

<!---NOTE: If you do not want to delete previously uploaded files, just 
remove or comment out the code above.--->
        
<!---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"> 

<cfif #Form.PackageIndex# is 0>
	<cfset descriptions=XmlParse("<files />") />
<cfelse>
	<cfset descriptions=XmlParse("#absGalleryPath#Descriptions.xml") />
</cfif>

<!---Save file info.--->
<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.description=Form.Description_1 />
<cfset ArrayAppend(descriptions.files.XmlChildren, xmlFile) />
				
<cffile action="write" file="#absGalleryPath#Descriptions.xml" 
		output="#toString(descriptions)#" />