<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 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>
</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#">
        
<!---Verify upload integrity. To do it, get the source file, compute hash value for it,
and compare the result with the hash value calculated by Image Uploader. 

NOTE: In real-life application a single hash method is typically enough.--->

<!---Create a COM-object "Aurigma.HashGenerator".--->
<cfobject type="COM" name="hashGenerator" class="Aurigma.HashGenerator" action="CREATE">

<!---Get SHA1 Hash.--->

<!---Check if this value is actually uploaded.--->
<cfif IsDefined("Form.HashCodeSHA_1")>

	<!---Compute SHA1 hash. Since Image Uploader sends the hash value encoded with Base 64 algorithm
	the result should be also encoded with Base 64.--->
	<cfset actualSHA1Hash = hashGenerator.GetShaFromFile("#absGalleryPath##fileName#") />

	<!---If the result differs from the uploaded one, the upload should be interpreted as corrupted.--->
	<cfif Form.HashCodeSHA_1 is not actualSHA1Hash>
		<cfabort showError = "corrupted">
	</cfif>
</cfif>	


<!---Get MD5 hash.--->

<!---Check if this value is actually uploaded.--->
<cfif IsDefined("Form.HashCodeMD5_1")>
	<!---Compute MD5 hash. Since Image Uploader sends the hash value encoded with Base 64 algorithm
	the result should be also encoded with Base 64.--->
	<cfset actualMD5Hash = hashGenerator.GetMD5FromFile("#absGalleryPath##fileName#") />

	<!---If the result differs from the uploaded one, the upload should be interpreted as corrupted.--->
	<cfif Form.HashCodeMD5_1 is not actualMD5Hash>
		<cfabort showError = "corrupted">	
	</cfif>
</cfif>

<!---If we reach this line, it means that file was not corrupted during upload. 
In this case we should save it to disk.--->