<%@ 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 files and data uploaded at previous time.

	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

	'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")
	objDescriptions.appendChild(objDescriptions.createElement("files"))

	'Get total number of uploaded files (all files are uploaded in a single package).
	Dim intFileCount, i
	intFileCount = CInt(objUpload.Form("FileCount")(1))

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 1 To intFileCount
		'Get source file and save it to disk.
		Dim objSourceFile
		Set objSourceFile = objUpload.Files("SourceFile_" & i)
		Dim strFileName
		strFileName = GetSafeFileName(objSourceFile.SafeFileName)		
		objSourceFile.Save Server.MapPath(strGalleryPath & strFileName)		

		'In this demo the following imaging operations are applied:
		'  
		'  - If the user rotates the image in Image Uploader, rotate the original file.
		'  - Add a border around the image.
		Dim objBitmap
		Set objBitmap = Server.CreateObject("GraphicsMill.Bitmap")		
		objBitmap.LoadFromFile Server.MapPath(strGalleryPath & strFileName)
		'Rotate the image if necessary 
		Dim lngAngle
		lngAngle = CLng(objUpload.Form("Angle_" & I))
		'Check if user rotated the image
		If lngAngle<>0 Then
			objBitmap.Transforms.Rotate lngAngle
		End If
		
		'Add extra pixels to the image to be able to draw a border.
		Dim lngWidth, lngHeight
		lngWidth = objBitmap.Data.Width
		lngHeight = objBitmap.Data.Height
		objBitmap.Transforms.Crop -15, -15, lngWidth + 30, lngHeight + 30, &hFFDDDDDD
		
		'Draw double border.
		objBitmap.Graphics.Pen.ForeColor = &hFF0000FF
		objBitmap.Graphics.DrawRectangle 13, 13, lngWidth + 3, lngHeight + 3, False, True
		objBitmap.Graphics.DrawRectangle 10, 10, lngWidth + 9, lngHeight + 9, False, True	
		
		'Save the result file to disk.
		objBitmap.SaveToFile (Server.MapPath(strGalleryPath & strFileName))
		

		'Get first thumbnail (the single thumbnail in this code sample) and save it to disk.
		Dim objThumbnail1File
		Set objThumbnail1File = objUpload.Files("Thumbnail1_" & i)
		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_" & i)(1)
		objXmlFile.setAttribute "height", objUpload.Form("Height_" & i)(1)
		objXmlFile.setAttribute "description", objUpload.Form("Description_" & i)(1)

		objDescriptions.documentElement.appendChild objXmlFile
	Next

	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 first source file to populate Form collection.
	Dim objSourceFile
	Set objSourceFile = objUpload.GetNextFile

	'First of all, clear files and data uploaded at previous time.

	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

	'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")
	objDescriptions.appendChild(objDescriptions.createElement("files"))

	'Get total number of uploaded files (all files are uploaded in a single package).
	Dim intFileCount, i
	intFileCount = CInt(objUpload.Form("FileCount").Value)

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 1 To intFileCount
		'Get source file and save it to disk.
		If i > 1 Then
			Set objSourceFile = objUpload.GetNextFile
		End If
		Dim strFileName
		strFileName = GetSafeFileName(objUpload.GetFileName(objSourceFile.OriginalPath))		
		objSourceFile.Save Server.MapPath(strGalleryPath)
		objUpload.FileMove Server.MapPath(strGalleryPath & objSourceFile.FileName), _
			Server.MapPath(strGalleryPath & strFileName) 

		'In this demo the following imaging operations are applied:
		'  
		'  - If the user rotates the image in Image Uploader, rotate the original file.
		'  - Add a border around the image.
		Dim objBitmap
		Set objBitmap = Server.CreateObject("GraphicsMill.Bitmap")		
		objBitmap.LoadFromFile Server.MapPath(strGalleryPath & strFileName)
		'Rotate the image if necessary 
		Dim lngAngle
		lngAngle = CLng(objUpload.Form("Angle_" & I))
		'Check if user rotated the image
		If lngAngle<>0 Then
			objBitmap.Transforms.Rotate lngAngle
		End If
		
		'Add extra pixels to the image to be able to draw a border.
		Dim lngWidth, lngHeight
		lngWidth = objBitmap.Data.Width
		lngHeight = objBitmap.Data.Height
		objBitmap.Transforms.Crop -15, -15, lngWidth + 30, lngHeight + 30, &hFFDDDDDD
		
		'Draw double border.
		objBitmap.Graphics.Pen.ForeColor = &hFF0000FF
		objBitmap.Graphics.DrawRectangle 13, 13, lngWidth + 3, lngHeight + 3, False, True
		objBitmap.Graphics.DrawRectangle 10, 10, lngWidth + 9, lngHeight + 9, False, True	
		
		'Save the result file to disk.
		objBitmap.SaveToFile (Server.MapPath(strGalleryPath & strFileName))
		

		'Get first thumbnail (the single thumbnail in this code sample) 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_" & i).Value
		objXmlFile.setAttribute "height", objUpload.Form("Height_" & i).Value
		If IsEmpty(objUpload.Form("Description_" & i)) Then
			objXmlFile.setAttribute "description", ""
		Else
			objXmlFile.setAttribute "description", objUpload.Form("Description_" & i).Value
		End If

		objDescriptions.documentElement.appendChild objXmlFile
	Next

	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 files and data uploaded at previous time.

	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

	'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")
	objDescriptions.appendChild(objDescriptions.createElement("files"))

	'Get total number of uploaded files (all files are uploaded in a single package).
	Dim intFileCount, i
	intFileCount = CInt(RemoveLeadingCharacters(objUpload.Form("FileCount").Value))

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 1 To intFileCount
		'Get source file and save it to disk.
		Dim objSourceFile
		Set objSourceFile = objUpload.Files("SourceFile_" & i)
		Dim strFileName
		strFileName = GetSafeFileName(objSourceFile.FileName)		
		objSourceFile.SaveAs Server.MapPath(strGalleryPath & strFileName)		

		'In this demo the following imaging operations are applied:
		'  
		'  - If the user rotates the image in Image Uploader, rotate the original file.
		'  - Add a border around the image.
		Dim objBitmap
		Set objBitmap = Server.CreateObject("GraphicsMill.Bitmap")		
		objBitmap.LoadFromFile Server.MapPath(strGalleryPath & strFileName)
		'Rotate the image if necessary 
		Dim lngAngle
		lngAngle = CLng(objUpload.Form("Angle_" & I))
		'Check if user rotated the image
		If lngAngle<>0 Then
			objBitmap.Transforms.Rotate lngAngle
		End If
		
		'Add extra pixels to the image to be able to draw a border.
		Dim lngWidth, lngHeight
		lngWidth = objBitmap.Data.Width
		lngHeight = objBitmap.Data.Height
		objBitmap.Transforms.Crop -15, -15, lngWidth + 30, lngHeight + 30, &hFFDDDDDD
		
		'Draw double border.
		objBitmap.Graphics.Pen.ForeColor = &hFF0000FF
		objBitmap.Graphics.DrawRectangle 13, 13, lngWidth + 3, lngHeight + 3, False, True
		objBitmap.Graphics.DrawRectangle 10, 10, lngWidth + 9, lngHeight + 9, False, True	
		
		'Save the result file to disk.
		objBitmap.SaveToFile (Server.MapPath(strGalleryPath & strFileName))
		

		'Get first thumbnail (the single thumbnail in this code sample) and save it to disk.
		Dim objThumbnail1File
		Set objThumbnail1File = objUpload.Files("Thumbnail1_" & i)
		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_" & i).Value)
		objXmlFile.setAttribute "height", RemoveLeadingCharacters(objUpload.Form("Height_" & i).Value)
		objXmlFile.setAttribute "description", RemoveLeadingCharacters(objUpload.Form("Description_" & i).Value)

		objDescriptions.documentElement.appendChild objXmlFile
	Next

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