<%@ Language="VBScript" %>
<!--#include file="../config.asp"-->
<%
Server.ScriptTimeout = 450

'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 ProcessABCUpload
	'Create ABCUpload object for upload processing
	Dim objUpload
	Set objUpload = Server.CreateObject("ABCUpload4.XForm")
	objUpload.MaxUploadSize = 20971520
	objUpload.AbsolutePath = True

	'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(objUpload.Form("PackageIndex")(1))

	'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

		'Delete thumbnails.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath) & "\Thumbnails")
		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.

	'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.
	Dim objDescriptions
	Set objDescriptions = Server.CreateObject("Msxml2.DOMDocument.4.0")

	'If we receive the first file then 
	'we start from empty file with descriptions
	If intPackageIndex = 0 Then
		objDescriptions.appendChild objDescriptions.createElement("files")
	Else
		objDescriptions.load Server.MapPath(strGalleryPath & "Descriptions.xml")
	End If

	'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.SafeFileName)		
	objSourceFile.Save Server.MapPath(strGalleryPath & strFileName)

	'Get the thumbnail and save it to disk.
	Dim objThumbnail1File
	Set objThumbnail1File = objUpload.Files("Thumbnail1_1")
	objThumbnail1File.Save Server.MapPath(strGalleryPath & "Thumbnails/" & strFileName & ".jpg")
	
	'Save file info.
	Dim objXmlFile	
	Set objXmlFile = objDescriptions.createElement("file")
	objXmlFile.setAttribute "name", strFileName
	objXmlFile.setAttribute "width", objUpload.Form("Width_1")(1)
	objXmlFile.setAttribute "height", objUpload.Form("Height_1")(1)
	objXmlFile.setAttribute "description", objUpload.Form("Description_1")(1)
	
	objDescriptions.documentElement.appendChild objXmlFile

	objDescriptions.save Server.MapPath(strGalleryPath & "Descriptions.xml")
End Sub		


Sub ProcessDundasUpload
	'Create Dundas Upload object for upload processing.
	Dim objUpload
	Set objUpload = Server.CreateObject("Dundas.Upload.2")
	objUpload.UseVirtualDir = False
	objUpload.MaxUploadSize = 1000000000
	
	'Fetch source file to populate Form collection.
	Dim objSourceFile
	Set objSourceFile = objUpload.GetNextFile

	'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(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

		'Delete thumbnails.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath) & "\Thumbnails")
		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.

	'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.
	Dim objDescriptions
	Set objDescriptions = Server.CreateObject("Msxml2.DOMDocument.4.0")

	'If we receive the first file then 
	'we start from empty file with descriptions
	If intPackageIndex = 0 Then
		objDescriptions.appendChild objDescriptions.createElement("files")
	Else
		objDescriptions.load Server.MapPath(strGalleryPath & "Descriptions.xml")
	End If

	'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 strFileName
	strFileName = GetSafeFileName(objUpload.GetFileName(objSourceFile.OriginalPath))		
	objSourceFile.Save Server.MapPath(strGalleryPath)
	objUpload.FileMove Server.MapPath(strGalleryPath & objSourceFile.FileName), _
		Server.MapPath(strGalleryPath & strFileName)
		
	'Get the thumbnail and save it to disk.
	Dim objThumbnail1File
	Set objThumbnail1File = objUpload.GetNextFile
	objThumbnail1File.Save Server.MapPath(strGalleryPath)
	objUpload.FileMove Server.MapPath(strGalleryPath & objThumbnail1File.FileName), _
		Server.MapPath(strGalleryPath & "Thumbnails/" & strFileName & ".jpg")
				
	'Save file info.
	Dim objXmlFile	
	Set objXmlFile = objDescriptions.createElement("file")
	objXmlFile.setAttribute "name", strFileName
	objXmlFile.setAttribute "width", objUpload.Form("Width_1").Value
	objXmlFile.setAttribute "height", objUpload.Form("Height_1").Value
	If IsEmpty(objUpload.Form("Description_1")) Then
		objXmlFile.setAttribute "description", ""
	Else
		objXmlFile.setAttribute "description", objUpload.Form("Description_1").Value
	End If
	
	objDescriptions.documentElement.appendChild objXmlFile

	objDescriptions.save Server.MapPath(strGalleryPath & "Descriptions.xml")
End Sub


Sub ProcessPersitsAspUpload
	'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

		'Delete thumbnails.
		Set objFolder = objFileSystem.GetFolder(Server.MapPath(strGalleryPath) & "\Thumbnails")
		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.

	'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.
	Dim objDescriptions
	Set objDescriptions = Server.CreateObject("Msxml2.DOMDocument.4.0")

	'If we receive the first file then 
	'we start from empty file with descriptions
	If intPackageIndex = 0 Then
		objDescriptions.appendChild objDescriptions.createElement("files")
	Else
		objDescriptions.load Server.MapPath(strGalleryPath & "Descriptions.xml")
	End If

	'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)

	'Get the thumbnail and save it to disk.
	Dim objThumbnail1File
	Set objThumbnail1File = objUpload.Files("Thumbnail1_1")
	objThumbnail1File.SaveAs Server.MapPath(strGalleryPath & "Thumbnails/" & strFileName & ".jpg")
	
	'Save file info.
	Dim objXmlFile	
	Set objXmlFile = objDescriptions.createElement("file")
	objXmlFile.setAttribute "name", strFileName
	objXmlFile.setAttribute "width", RemoveLeadingCharacters(objUpload.Form("Width_1").Value)
	objXmlFile.setAttribute "height", RemoveLeadingCharacters(objUpload.Form("Height_1").Value)
	objXmlFile.setAttribute "description", RemoveLeadingCharacters(objUpload.Form("Description_1").Value)
	
	objDescriptions.documentElement.appendChild objXmlFile

	objDescriptions.save Server.MapPath(strGalleryPath & "Descriptions.xml")
End Sub


'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


Sub ProcessUpload
	Select Case strUploadComponent
		Case "ABCUpload"
			ProcessABCUpload
		Case "DundasUpload"
			ProcessDundasUpload
		Case "PersitsAspUpload"
			ProcessPersitsAspUpload			
	End Select		
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


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

ProcessUpload
%>