File Upload now detects and enforces image files ONLY

This commit is contained in:
Rick Falkvinge
2013-09-18 17:47:50 +00:00
parent 7e2b0740cc
commit c7395dc627
3 changed files with 57 additions and 11 deletions
+41 -4
View File
@@ -1,7 +1,9 @@
<%@ WebHandler Language="C#" Class="Swarmops.Frontend.Automation.FileTransferHandler" %>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
@@ -113,9 +115,44 @@ namespace Swarmops.Frontend.Automation {
private void UploadWholeFile (HttpContext context, List<FilesStatus> statuses) {
for (int i = 0; i < context.Request.Files.Count; i++) {
var file = context.Request.Files[i];
file.SaveAs(StorageRoot + Path.GetFileName(file.FileName));
string fullName = Path.GetFileName(file.FileName);
string fullName = Path.GetFileName(file.FileName);
// Store in MemoryStream in order to try to load as an image
/*
MemoryStream ms = new MemoryStream();
byte[] fileData = new byte[file.ContentLength];
file.InputStream.Read(fileData, 0, file.ContentLength);
ms.Write(fileData, 0, file.ContentLength);
ms.Position = 0;*/
// Try to load as image. If fails, not an acceptable file
try
{
Image image = Image.FromStream(file.InputStream);
}
catch (Exception)
{
FilesStatus errorStatus = new FilesStatus(fullName, file.ContentLength);
errorStatus.error = "ERR_NOT_IMAGE";
errorStatus.url = string.Empty;
errorStatus.delete_url = string.Empty;
errorStatus.thumbnail_url = string.Empty;
statuses.Add(errorStatus);
// -1 for length means the file was NOT saved, and that it could not be parsed as image.
return;
}
// TODO: Store to file name dependent on GUID
// TODO: Store new image in db
file.InputStream.Position = 0;
file.SaveAs(StorageRoot + Path.GetFileName(file.FileName));
statuses.Add(new FilesStatus(fullName, file.ContentLength));
}
}
@@ -193,9 +230,9 @@ namespace Swarmops.Frontend.Automation {
type = "image/png";
size = fileLength;
progress = "1.0";
url = HandlerPath + "FileTransferHandler.ashx?f=" + fileName;
url = HandlerPath + "UploadFileHandler.ashx?f=" + fileName;
thumbnail_url = HandlerPath + "Thumbnail.ashx?f=" + fileName;
delete_url = HandlerPath + "FileTransferHandler.ashx?f=" + fileName;
delete_url = HandlerPath + "UploadFileHandler.ashx?f=" + fileName;
delete_type = "DELETE";
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

+16 -7
View File
@@ -13,10 +13,12 @@
// The below weird construct circumvents a mono-server optimization bug that kills the string
var imageSuccess1 = "\x3Cimg src='";
var imageSuccess2 = "/Images/Icons/iconshock-invoice-greentick-32px.png";
var imageSuccess3 = "' /\x3E"; // needs to be separate var rather than inline because of mono-server optimization bug
var imageSuccess = imageSuccess1 + imageSuccess2 + imageSuccess3;
var imageFileStart = "\x3Cimg src='";
var imageFileSuccess = "/Images/Icons/iconshock-invoice-greentick-32px.png";
var imageFileFail = "/Images/Icons/iconshock-invoice-redcross-32px.png";
var imageFileEnd = "' /\x3E"; // needs to be separate var rather than inline because of mono-server optimization bug
var imageUploadSuccess = imageFileStart + imageFileSuccess + imageFileEnd;
var imageUploadFail = imageFileStart + imageFileFail + imageFileEnd;
$(document).ready(function () {
$('#DropBudgets').combotree({
@@ -42,8 +44,14 @@
url: '/Automation/UploadFileHandler.ashx',
dataType: 'json',
done: function (e, data) {
$.each(data.files, function(index, file) {
$('#DivUploadCount').append(imageSuccess);
$.each(data.result, function(index, file) {
if (file.error == null || file.error.length < 1) {
$('#DivUploadCount').append(imageUploadSuccess);
} else {
$('#DivUploadCount').append(imageUploadFail); // Assume ERR_NOT_IMAGE
alertify.error('File "' + file.name + '" could not be uploaded. Is it really an image?');
}
});
$('#DivProgressUpload').progressbar({ value: 100 });
$('#DivProgressUpload').fadeOut('400', function () { $('#DivUploadCount').fadeIn(); });
@@ -57,6 +65,7 @@
},
add: function (e, data) {
$('#DivUploadCount').css('display', 'none');
$('#DivProgressUpload').progressbar({ value: 0 }); // reset in case of previous uploads on this page
$('#DivProgressUpload').fadeIn();
data.submit();
}
@@ -133,7 +142,7 @@
<!-- file upload begins here -->
<div style="height:36px;float:left"><input id="ButtonUploadVisible" type="button" style="width:36px !important; background-image:url('/Images/Icons/iconshock-diskette-upload-32px.png');background-repeat:no-repeat; background-position:top left"/><input id="ButtonUploadHidden" type="file" name="files[]" multiple style="display:none" /></div><div style="height:36px;width:270px;margin-right:10px;float:right;border:none"><div id="DivUploadCount" style="display:none;overflow:hidden"></div><div id="DivProgressUpload" style="width:100%;margin-top:8px;display:none"></div></div>&nbsp;<br/>
<div style="height:36px;float:left"><input id="ButtonUploadVisible" type="button" style="width:36px !important; background-image:url('/Images/Icons/iconshock-diskette-upload-32px.png');background-repeat:no-repeat; background-position:top left"/><input id="ButtonUploadHidden" type="file" name="files[]" multiple style="display:none" /></div><div style="height:36px;width:270px;margin-right:10px;float:right;border:none"><div id="DivUploadCount" style="display:none;overflow:hidden;height:64px"></div><div id="DivProgressUpload" style="width:100%;margin-top:8px;display:none"></div></div>&nbsp;<br/>
<!-- file upload ends -->