Client controller – Custom binaries

I found some time this afternoon to work on the Client Controller. My aim was to allow the uploading of custom dll files for use in the code seen on the page.

There are a few steps involved here. Firstly, some HTML on the main controller page for file uploading. A little tweak to the form tag was required, that was a small headache:

<form method="post" action="index.php" enctype="multipart/form-data">

A nice simple input element for the file, only 1 at a time at the moment!

<input type="file" name="custom_dll" />

Now we need some PHP to handle this on the server. Nice and simple really, I’m just saving the file to a folder called ‘custom_dll’. If this were to be used by many people the next obvious thing would be some more validation!

if(isset($_FILES['custom_dll']))
{
	$uploaddir = 'custom_dll/';
	$uploadfile = $uploaddir . basename($_FILES['custom_dll']['name']);
	
	if (move_uploaded_file($_FILES['custom_dll']['tmp_name'], $uploadfile)) {
		$uploaded_files = $uploaded_files. "<strong>". $_FILES['custom_dll']['name']. "</strong>";
	} else {
		echo "<strong>There was an error uploading the file.</strong>";
	}
}

Now that’s done, I need to actually download it via the console app itself. Before the app can download any files, it needs to ¬†know what files to download. I created a simple PHP page for this with some delimiters on the file names:

$custom_dlls_location = "custom_dll";
$site_root_url = "http://resource.greynet.co.uk/clientController/custom_dll/";

$files = scandir($custom_dlls_location. '/');

foreach($files as $file) {
	if($file != ".." && $file != ".")
	{
		echo $site_root_url. $file. "|";
	}
}

This returns a nice string for us, if we check the source of the page’s URL in a browser, we get this:

http://resource.greynet.co.uk/clientController/custom_dll/Badcustoms.dll|http://resource.greynet.co.uk/clientController/custom_dll/RCWebservice.dll|http://resource.greynet.co.uk/clientController/custom_dll/System.dll|

As you can see, I’m using the pipe (|) symbol to separate my files. This is simply because it’s an easy symbol that you can’t use in a file name in Windows. Now we have a reference it’s time to download the files in the console.

I’ve written all of this in to a nice and simple method:

public static void GetCustomDlls()
{
	string downloadServiceURL = string.Format("{0}/return-file-urls.php", serverCommandURL);
	WebClient wc = new WebClient();

	//download the list of files we need to get as a string
	string filesString = wc.DownloadString(downloadServiceURL);

	string[] filesToGet = filesString.Split('|');

	Console.WriteLine("Custom references retrieved, downloading binaries...");
	//loop through the array and download each file
	foreach (string file in filesToGet)
	{
		if (file != "")
		{
			//split the string in to our url + our file name
			string[] filedata = file.Split('/');
			string fileName = filedata[filedata.Length - 1];

			//Adding our special file names to the assemblies array
			assembliesList.Add(fileName);

			wc.DownloadFile(file, fileName);
		}
	}
}

You can see the idea behind this is quite simple. It didn’t take a huge amount of effort to get each custom dll downloaded ready for use. I’m using the CSharpCodeProvider to compile the downloaded code. Before everything get’s compiled and run we need to add our references:

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();

foreach (string asmbly in assemblies)
{
	parameters.ReferencedAssemblies.Add(asmbly);
}

That was just about it if we’re sticking to the interesting bits. The whole thing is quite simple really. That’s the idea at the end of the day, a simple, dynamic tool one can use for updating applications or anything else they see it fit for.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>