<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.--->

<!--- Check if it is POST request --->
<cfif NOT CGI.REQUEST_METHOD is "POST">
	<cfabort />
</cfif>

<!--- Check that all files are uploaded. 
	"RequestComplete" field is the last field in the POST request from Image Uploader.
	If it exists, then upload process was not aborted. --->
<cfif NOT IsDefined("FORM.RequestComplete")>
	<cfabort />
</cfif>
	
<cfset galleryPath="../UploadedFiles/" />
<cfset absGalleryPath="#ExpandPath(galleryPath)#" />
<cfset absThumbnailsPath="#absGalleryPath#Thumbnails/" />
<cfset xmlFileName="files.xml" />
<!---First of all, clear files and data uploaded at previous time.--->
<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>
	<cfset descriptions=XmlParse("<files></files>") />
	<cffile action="write" file="#absGalleryPath##xmlFileName#" 
		output="#toString(descriptions)#" />
</cfif>
<!---NOTE: If you do not want to delete previously uploaded files, just 
	remove or comment out the code above.--->
<!---Create XML file which will keep information about files (image dimensions, description, etc). 
	XML is used solely for brevity. In real-life application most likely you will use database instead.--->
<cfset descriptions=XmlParse("#absGalleryPath##xmlFileName#") />
<!---Get total number of uploaded files (all files are uploaded in a single package) and
	iterate through uploaded data and save the original file, thumbnail, and description.--->
<cfset fileCount=(#Form.PackageFileCount#-1) />
<cfloop index="i" from="0" to="#fileCount#">
	<cfset name="#Form["SourceName_#i#"]#"/>
	<!--- Get source file and save it to disk. --->
	<cffile action="UPLOAD" filefield="File0_#i#" 
		destination="#absGalleryPath#" 
		nameconflict="MakeUnique">
	<cfset sourceFileName="#serverFile#" />
	<!--- Get first thumbnail (the single thumbnail in this code sample) and save it to disk. --->
	<cffile action="UPLOAD" filefield="File1_#i#" 
		destination="#absThumbnailsPath#" 
		nameconflict="MakeUnique">
	<cfset thumbnailFileName="#serverFile#" />
	<!---Save file info.--->
	<cfset xmlFile=XmlElemNew(descriptions, "file") />
	<cfset xmlFile.XmlAttributes.source=sourceFileName />
	<cfset xmlFile.XmlAttributes.thumbnail=thumbnailFileName />
	<cfset xmlFile.XmlAttributes.name=Form["SourceName_#i#"] />
	<cfset ArrayAppend(descriptions.files.XmlChildren, xmlFile) />
</cfloop>
<cffile action="write" file="#absGalleryPath##xmlFileName#" 
	output="#toString(descriptions)#" />
<cfoutput>#fileCount#</cfoutput>
