/*
**	This is an example script for supporting Flash Streaming Export to a streaming Server.
**	Using this script you can easily integrate any Flash Streaming Server to Flash Streaming Export 
**	workflow in Encore. 
**	Remember to change the extension of this file to "jsx" if you want this script to start appearing in 
**	the Streaming Server list.
*/


/*
For the streaming Flash feature to work you need to set up an FMS server. Files copied to a particular folder will be available for streaming through FMS. You will also need to set up FTP server on that machine to upload flash video files to this folder. The following strings ask for the respective urls for the two servers. There is no way to predict one based on the other so both need to be provided.
*/

// URL of the FMS server. Make sure it has the full path to the file, only filetype:filename will be appended to it to guess the URL of uploaded file.
var streamingSiteURL = "rtmp://<fms-server-name>/vod/"; // Replace <fms-server-name> with name of FMS server.

//URL of the FTP server. This should be set up to upload to the folder which FMS uses to serve media from.
var ftpURL = "ftp://<ftp-server-name>"; // Replace <ftp-server-name> with ftp server

var username = "";
var password = "";

var streamingFilePrefixSeparator = ":";

var local = new ESLocalizer;

// Localised Strings
LoginDialogTitle = local.Localize("/Scripting/FTP/ServerLogin","Server Login");
LoginPanelLabel = local.Localize("/Scripting/FTP/Login","Login");
LoginMessageToUser = local.Localize("/Scripting/FTP/LoginMessage","Please enter login information for uploading file to Streaming Server");
UsernameString = local.Localize("/Scripting/FTP/UserName","Username");
PasswordString = local.Localize("/Scripting/FTP/Password","Password");
ConnectionRetryMessageString = local.Localize("/Scripting/FTP/ConnectionRetryMessage","Could not open FTP connection. Do you want to try again?");
ConnectionRetryTitleString = local.Localize("/Scripting/FTP/ConnectionRetryTitle","Connection Failed");
OKButtonLabel = local.Localize("/Dialogs/OKButton","OK");
UploadFailureMessagePartOne = local.Localize("/Scripting/FTP/UploadFailureMessagePartOne","Failed to upload file ");
UploadFailureMessagePartTwo = local.Localize("/Scripting/FTP/UploadFailureMessagePartTwo"," to the server: ");
UploadFailureTitle = local.Localize("/Scripting/FTP/UploadFailureTitle","Upload Error");

/*
Ask user for Username and Password for the FTP Server.
*/
function GetUserNamePasswordThroughUI()
{
	res = "dialog { 	alignChildren: \"center\",\
							text:\"" + localize(LoginDialogTitle) + "\",\
							LoginInfo: Panel { 	orientation: \"column\", alignChildren:\"left\", \
														text: \"" + localize(LoginPanelLabel) + "\",\
														infoStatic: StaticText { text:\"" + localize(LoginMessageToUser) + "\", creation_properties:{multiline:true} }\
														LoginGroup:Group{orientation: \"column\", alignChildren:\"right\",\
																username: Group { orientation: \"row\", \
																							s: StaticText { text:\"" + localize(UsernameString) + ":\" }, \
																							e: EditText { preferredSize: [200, 20] } \
																							} \
																password: Group { orientation: \"row\", \
																							s: StaticText { text:\"" + localize(PasswordString) + ":\" }, \
																							e: EditText { preferredSize: [200, 20] , properties: {noecho: true} } \
																							} \
																}, \
														}\
							buttons: Group { orientation: \"row\", alignment: \"center\", \
													okBtn: Button { text:\"" + localize(OKButtonLabel) + "\", properties:{name:\"ok\"} }, \
													} \
			}"; 

	var ftp = new FTP;
	
	var success = false;
	
	while (!success)
	{
		win = new Window (res); 
		win.LoginInfo.LoginGroup.username.e.text = username;
		win.LoginInfo.LoginGroup.password.e.text = password;
		
		win.center(); 
		if (win.show() == 2)
		{
			return false;
		}
		username = win.LoginInfo.LoginGroup.username.e.text;
		password = win.LoginInfo.LoginGroup.password.e.text;
		
		// Check that FTP with the provided username and password works.
		success = ftp.open(ftpURL, username, password);
		
		if (!success)
		{
			if (!confirm( localize(ConnectionRetryMessageString), false, localize(ConnectionRetryTitleString) ))
			{
				return false;
			}
		}
	}
	
	ftp.close();
	return true;
}

function GetFileNamePlusTitle(inFileName)
{
		var ar = new Object();
		
		var splitFileArray = inFileName.split(".");
		
		ar["extn"] = new String(splitFileArray.pop());
		ar["title"] = splitFileArray.join(".");
		return ar;
}

function Init()
{
	return GetUserNamePasswordThroughUI();
}

function Call( inFilePath)
{
	var ftp = new FTP;

	var fi = new File(inFilePath)
	var fileTitleExtn  = GetFileNamePlusTitle(fi.displayName);
	
	var newFileTitle = fileTitleExtn["title"] + String(Math.random());

	var newFileName = newFileTitle +"." + fileTitleExtn["extn"];
	
	ftp.open(ftpURL, username, password);
	
	while(ftp.exists(newFileName))
	{
		newFileTitle  = newFileTitle + String(Math.random());
		newFileName = newFileTitle +"." + fileTitleExtn["extn"] ;
	}
	
	
	if (!ftp.put(inFilePath, newFileName))
	{
		alert (UploadFailureMessagePartOne + fi.displayName+ UploadFailureMessagePartTwo + ftpURL , UploadFailureTitle);
		ftp.close();
		return "";
	}

	ftp.close();
	
	var streamingFilePrefix = fileTitleExtn["extn"];
	
	if ( fileTitleExtn["extn"] == "f4v")
	{
		streamingFilePrefix = "mp4";
		newFileTitle = newFileTitle + "." + fileTitleExtn["extn"];
	}
	
	//like:
	//"rtmp://amgupta/vod/" +         "flv"       +               ":"             + "filetitle"
	return streamingSiteURL + streamingFilePrefix + streamingFilePrefixSeparator  + newFileTitle;
}

function Close()
{
	username = "";
	password = "";
}
