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

		'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

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