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

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

	'Get file info and save it to XML file.
	'NOTE: In this demo this file is used by different upload threads asynchronously. That's why 
	'it is necessary to synchronize access to this file. 	
	Application.Lock() 'TODO: Use synchronization in more narrow scope

	Dim strDescriptionsPath
	strDescriptionsPath = Server.MapPath(strGalleryPath & "Descriptions.xml")

	'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 objFileSystem.FileExists(strDescriptionsPath) Then
		objDescriptions.load strDescriptionsPath
	Else
		objDescriptions.appendChild objDescriptions.createElement("files")
	End If

	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 "handlerUrl", Request.ServerVariables("SCRIPT_NAME") & Request.ServerVariables("QUERY_STRING")

	objDescriptions.documentElement.appendChild objXmlFile

	objDescriptions.save strDescriptionsPath
		
	Application.UnLock()		
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
%>