From c7395dc6279b7e97c049a3af38c9311e138bcadd Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Wed, 18 Sep 2013 17:47:50 +0000 Subject: [PATCH] File Upload now detects and enforces image files ONLY --- Site5/Automation/UploadFileHandler.ashx | 45 ++++++++++++++++-- .../Icons/iconshock-invoice-redcross-32px.png | Bin 0 -> 2069 bytes .../Pages/v5/Financial/FileExpenseClaim.aspx | 23 ++++++--- 3 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 Site5/Images/Icons/iconshock-invoice-redcross-32px.png diff --git a/Site5/Automation/UploadFileHandler.ashx b/Site5/Automation/UploadFileHandler.ashx index 5ed5270e..6bbae681 100644 --- a/Site5/Automation/UploadFileHandler.ashx +++ b/Site5/Automation/UploadFileHandler.ashx @@ -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 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"; } } diff --git a/Site5/Images/Icons/iconshock-invoice-redcross-32px.png b/Site5/Images/Icons/iconshock-invoice-redcross-32px.png new file mode 100644 index 0000000000000000000000000000000000000000..6e812dd76e30623e12db781882bda818c96ce15f GIT binary patch literal 2069 zcmV+w2TGBQ^i?#wPQs@W_&S=4*qjaG4G3tm@ zoB>Ap15ud*bwsp+0@cAOFqR@v>;SeUO&j|N#6FTHO_Q`K&0{ya$-eHryZ7ZEE)LF6 znikYMbLPywbI$Mk`TfrMo!>q1e}05>4fxJ4pKEa($E)QE@nW%KGc|GCSX4-h0>Ne+^*$tNWa_*Dl0%z_e_1(_%8C zab`MCUN@LbPOH<&)R1WyJ+^K2$ZPwZZ+&6We@-gRGAtY2w3*84 zj3lxY43l^&ZI6zRkLa3~c;b6sUG}j7HgA5_Syo@cuin~Dbjeb#C<}9O!$m|Y%JF)= z|F+;b4wmguFf5X1#yEDchsMc~?_PP|{TnYBp!>1)kBgT5eF=vt3+_AeE-A&&hKC*@ zKXC?YCdp@Ft++ih;qo$YKpP+9%quU!Ocn%~(bZ?JUH{nH^9JZxx9+KcqI|P58bz+Z zh;)A+sdR>C%R|I&TtQA1cw^T#LT(=oRUyjUJ{G#X%zyVFK~bV=$(5)Rg#ZO z+@#b3zR78>?$|{%R7RjBhA};j4t@C^E52F6H_K3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 --- a/Site5/Pages/v5/Financial/FileExpenseClaim.aspx +++ b/Site5/Pages/v5/Financial/FileExpenseClaim.aspx @@ -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 @@ -
 
+