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

	'This value will keep a total size of actually uploaded images. It will be displayed
	'so that you could compare how many bytes you save. 
	Dim lngTotalUploadedFileSize
	lngTotalUploadedFileSize = 0

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 1 To intFileCount
		'Get the first thumbnail and save it to disk. It stores 1200x1200 optimized image.
		Dim objThumbnail1File
		Set objThumbnail1File = objUpload.Files("Thumbnail1_" & i)
		Dim strFileName
		strFileName = GetSafeFileName(objFileSystem.GetFileName(objUpload.Form("FileName_" & i)(1)))		
		objThumbnail1File.Save Server.MapPath(strGalleryPath & strFileName)
		
		lngTotalUploadedFileSize = lngTotalUploadedFileSize + _
			objFileSystem.GetFile(Server.MapPath(strGalleryPath & strFileName)).Size
			
		'Get the second thumbnail and save it to disk. It stores 120x120 preview image. 
		Dim objThumbnail2File
		Set objThumbnail2File = objUpload.Files("Thumbnail2_" & i)
		objThumbnail2File.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)

		objDescriptions.documentElement.appendChild objXmlFile
	Next

	'Write both optimized and source file sizes for comparison.
	objDescriptions.documentElement.setAttribute "totalSourceFileSize", objUpload.Form("TotalSourceFileSize")(1)
	objDescriptions.documentElement.setAttribute "totalUploadedFileSize", Round(lngTotalUploadedFileSize / 1024)

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


Sub ProcessDundasUpload
	'Create aspSmartUpload object for upload processing.
	'Create Dundas Upload object for upload processing.
	Dim objUpload
	Set objUpload = Server.CreateObject("Dundas.Upload.2")
	objUpload.UseVirtualDir = False
	objUpload.MaxUploadSize = 1000000000
	
	'Fetch first thumbnail to populate Form collection.
	Dim objThumbnail1File
	Set objThumbnail1File = 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)

	'This value will keep a total size of actually uploaded images. It will be displayed
	'so that you could compare how many bytes you save. 
	Dim lngTotalUploadedFileSize
	lngTotalUploadedFileSize = 0

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 1 To intFileCount
		'Get the first thumbnail and save it to disk. It stores 1200x1200 optimized image.
		If i > 1 Then
			Set objThumbnail1File = objUpload.GetNextFile
		End If
		Dim strFileName
		strFileName = GetSafeFileName(objFileSystem.GetFileName(objUpload.Form("FileName_" & i).Value))		
		objThumbnail1File.Save Server.MapPath(strGalleryPath)
		objUpload.FileMove Server.MapPath(strGalleryPath & objThumbnail1File.FileName), _
			Server.MapPath(strGalleryPath & strFileName)
		
		lngTotalUploadedFileSize = lngTotalUploadedFileSize + _
			objFileSystem.GetFile(Server.MapPath(strGalleryPath & strFileName)).Size
			
		'Get the second thumbnail and save it to disk. It stores 120x120 preview image. 
		Dim objThumbnail2File
		Set objThumbnail2File = objUpload.GetNextFile
		objThumbnail2File.Save Server.MapPath(strGalleryPath)
		objUpload.FileMove Server.MapPath(strGalleryPath & objThumbnail2File.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

		objDescriptions.documentElement.appendChild objXmlFile
	Next

	'Write both optimized and source file sizes for comparison.
	objDescriptions.documentElement.setAttribute "totalSourceFileSize", objUpload.Form("TotalSourceFileSize").Value
	objDescriptions.documentElement.setAttribute "totalUploadedFileSize", Round(lngTotalUploadedFileSize / 1024)

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

	'This value will keep a total size of actually uploaded images. It will be displayed
	'so that you could compare how many bytes you save. 
	Dim lngTotalUploadedFileSize
	lngTotalUploadedFileSize = 0

	'Iterate through uploaded data and save the original file, thumbnail, and description.
	For i = 1 To intFileCount
		'Get the first thumbnail and save it to disk. It stores 1200x1200 optimized image.
		Dim objThumbnail1File
		Set objThumbnail1File = objUpload.Files("Thumbnail1_" & i)
		Dim strFileName
		strFileName = GetSafeFileName(objFileSystem.GetFileName(RemoveLeadingCharacters(objUpload.Form("FileName_" & i).Value)))		
		objThumbnail1File.SaveAs Server.MapPath(strGalleryPath & strFileName)
		
		lngTotalUploadedFileSize = lngTotalUploadedFileSize + _
			objFileSystem.GetFile(Server.MapPath(strGalleryPath & strFileName)).Size
			
		'Get the second thumbnail and save it to disk. It stores 120x120 preview image. 
		Dim objThumbnail2File
		Set objThumbnail2File = objUpload.Files("Thumbnail2_" & i)
		objThumbnail2File.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)

		objDescriptions.documentElement.appendChild objXmlFile
	Next

	'Write both optimized and source file sizes for comparison.
	objDescriptions.documentElement.setAttribute "totalSourceFileSize", RemoveLeadingCharacters(objUpload.Form("TotalSourceFileSize").Value)
	objDescriptions.documentElement.setAttribute "totalUploadedFileSize", Round(lngTotalUploadedFileSize / 1024)

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