<%@ Language="VBScript" %>
<%
Option Explicit

'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.
Dim strGalleryPath
strGalleryPath = "../Gallery/"


Sub ProcessUpload
	'Create Persits Upload object for upload processing.
	Dim objUpload
	Set objUpload = Server.CreateObject("Persits.Upload")
	objUpload.CodePage = 65001 'UTF8
	objUpload.Save
	
	'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. 	
		
	Dim intPackageIndex
	intPackageIndex = CInt(RemoveLeadingCharacters(objUpload.Form("PackageIndex").Value))

	'If we receive the first file, we delete all files and thumbnails we uploaded before.  	
	If intPackageIndex = 0 Then

		Dim objFolder, objFile

		'Delete source files.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath))
		For Each objFile In objFolder.Files
			objFileSystem.DeleteFile objFile.Path, True
		Next

	End If

	'NOTE: If you do not want to delete previously uploaded files, just 
	'remove or comment out the code above.
	
	'Get the source file. 
	'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).       
	Dim objSourceFile
	Set objSourceFile = objUpload.Files("SourceFile_1")
	Dim strFileName
	strFileName = GetSafeFileName(objSourceFile.FileName)		
	objSourceFile.SaveAs Server.MapPath(strGalleryPath & strFileName)
		
	'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.

	Dim strSourceFilePath
	strSourceFilePath = Server.MapPath(strGalleryPath & strFileName)

	Dim objHashGenerator
	Set objHashGenerator = Server.CreateObject("Aurigma.HashGenerator") 

	'Get SHA1 Hash.
	Dim strPostedSHA1Hash
	strPostedSHA1Hash = RemoveLeadingCharacters(objUpload.Form("HashCodeSHA_1").Value)
		
	'Check if this value is actually uploaded.
	If strPostedSHA1Hash <> "" Then

		'Compute SHA1 hash. Since Image Uploader sends the hash value encoded with Base 64 algorithm
		'the result should be also encoded with Base 64.
		Dim strActualSHA1Hash
		strActualSHA1Hash = objHashGenerator.GetShaFromFile(strSourceFilePath)

		'If the result differs from the uploaded one, the upload should be interpreted as corrupted.		
		If (strPostedSHA1Hash <> strActualSHA1Hash) Then
			'Delete uploaded file
			objFileSystem.DeleteFile strSourceFilePath
			
			Response.Status = 400
			Response.End
			Exit Sub
		End If
		
	End If
	
	'Get MD5 hash.
	Dim strPostedMD5Hash
	strPostedMD5Hash = RemoveLeadingCharacters(objUpload.Form("HashCodeMD5_1").Value)

	'Check if this value is actually uploaded.
	If strPostedMD5Hash <> "" Then

		'Compute MD5 hash. Since Image Uploader sends the hash value encoded with Base 64 algorithm
		'the result should be also encoded with Base 64.
		Dim strActualMD5Hash 
		strActualMD5Hash = objHashGenerator.GetMD5FromFile(strSourceFilePath)
		
		'If the result differs from the uploaded one, the upload should be interpreted as corrupted.
		If (strPostedMD5Hash <> strActualMD5Hash) Then
			'Delete uploaded file
			objFileSystem.DeleteFile strSourceFilePath
			
			Response.Status = 400
			Response.End
			Exit Sub
		End If
	
	End If

	'Get MD2 hash
	Dim strPostedMD2Hash
	strPostedMD2Hash = RemoveLeadingCharacters(objUpload.Form("HashCodeMD2_1").Value)
		
	'Check if this value is actually uploaded.
	'If strPostedMD2Hash <> "" Then
	'
	'	'Compute MD2 hash. Since Image Uploader sends the hash value encoded with Base 64 algorithm
	'	'the result should be also encoded with Base 64.			
	'	Dim strActualMD2Hash
	'	strActualMD2Hash = objHashGenerator.GetMD2FromFile(strSourceFilePath)	
	'
	'	'If the result differs from the uploaded one, the upload should be interpreted as corrupted.			
	'	If strPostedMD2Hash <> strActualMD2Hash Then
	'		'Delete uploaded file
	'		objFileSystem.DeleteFile strSourceFilePath
	'		
	'		Response.Status = 400
	'		Response.End
	'		Exit Sub
	'	End If
	'	
	'End If
		
	'If we reach this line, it means that file was not corrupted during upload. 
	'In this case we should not delete it from disk.
End Sub		

'This method verifies whether file with such name already exists 
'and if so, construct safe filename name (to avoid collision).	
Function GetSafeFileName(strFileName)	
	Dim strNewFileName
	strNewFileName = strFileName
	Dim j 
	j = 1
	While (objFileSystem.FileExists(Server.MapPath(strGalleryPath & strNewFileName)))
		strNewFileName = j & "_" & strFileName
		j = j + 1
	Wend
	GetSafeFileName = strNewFileName
End Function

'WORKAROUND: Persits Upload component adds two #13#10 leading characters during form value parsing.
Function RemoveLeadingCharacters(Value)
	If Len(Value) < 2 Then
		RemoveLeadingCharacters = Value
	Else
		RemoveLeadingCharacters = Right(Value, Len(Value) - 2)
	End If
End Function

Dim objFileSystem
Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")

ProcessUpload
%>