diff --git a/Teknik.sln b/Teknik.sln index 7fd3350..6d2d8f4 100644 --- a/Teknik.sln +++ b/Teknik.sln @@ -14,13 +14,20 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Debug|x64.ActiveCfg = Debug|x64 + {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Debug|x64.Build.0 = Debug|x64 + {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Debug|x64.Deploy.0 = Debug|x64 {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Release|Any CPU.ActiveCfg = Release|Any CPU {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Release|Any CPU.Build.0 = Release|Any CPU + {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Release|x64.ActiveCfg = Release|x64 + {B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Teknik/Areas/Upload/Controllers/UploadController.cs b/Teknik/Areas/Upload/Controllers/UploadController.cs index fef11bd..4d4029d 100644 --- a/Teknik/Areas/Upload/Controllers/UploadController.cs +++ b/Teknik/Areas/Upload/Controllers/UploadController.cs @@ -46,75 +46,83 @@ namespace Teknik.Areas.Upload.Controllers [AllowAnonymous] public ActionResult Upload(string fileType, string fileExt, string iv, int keySize, int blockSize, bool encrypt, bool saveKey, HttpPostedFileWrapper data, string key = null) { - if (Config.UploadConfig.UploadEnabled) + try { - if (data.ContentLength <= Config.UploadConfig.MaxUploadSize) + if (Config.UploadConfig.UploadEnabled) { - // convert file to bytes - byte[] fileData = null; - int contentLength = data.ContentLength; - using (var binaryReader = new BinaryReader(data.InputStream)) + if (data.ContentLength <= Config.UploadConfig.MaxUploadSize) { - fileData = binaryReader.ReadBytes(data.ContentLength); - } - - // Scan the file to detect a virus - if (Config.UploadConfig.VirusScanEnable) - { - ClamClient clam = new ClamClient(Config.UploadConfig.ClamServer, Config.UploadConfig.ClamPort); - ClamScanResult scanResult = clam.SendAndScanFile(fileData); - - switch (scanResult.Result) + // convert file to bytes + byte[] fileData = null; + int contentLength = data.ContentLength; + using (var binaryReader = new BinaryReader(data.InputStream)) { - case ClamScanResults.Clean: - break; - case ClamScanResults.VirusDetected: - return Json(new { error = new { message = string.Format("Virus Detected: {0}. As per our Terms of Service, Viruses are not permited.", scanResult.InfectedFiles.First().VirusName, Url.SubRouteUrl("tos", "TOS.Index")) } }); - case ClamScanResults.Error: - break; - case ClamScanResults.Unknown: - break; - } - } - - // if they want us to encrypt it, we do so here - if (encrypt) - { - // Generate key and iv if empty - if (string.IsNullOrEmpty(key)) - { - key = Utility.RandomString(keySize / 8); + fileData = binaryReader.ReadBytes(data.ContentLength); } - fileData = AES.Encrypt(fileData, key, iv); - if (fileData == null || fileData.Length <= 0) + // Scan the file to detect a virus + if (Config.UploadConfig.VirusScanEnable) { - return Json(new { error = new { message = "Unable to encrypt file" } }); - } - } - Models.Upload upload = Uploader.SaveFile(fileData, fileType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize); - if (upload != null) - { - if (User.Identity.IsAuthenticated) - { - Profile.Models.User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault(); - if (user != null) + ClamClient clam = new ClamClient(Config.UploadConfig.ClamServer, Config.UploadConfig.ClamPort); + clam.MaxStreamSize = Config.UploadConfig.MaxUploadSize; + ClamScanResult scanResult = clam.SendAndScanFile(fileData); + + switch (scanResult.Result) { - upload.UserId = user.UserId; - db.Entry(upload).State = EntityState.Modified; - db.SaveChanges(); + case ClamScanResults.Clean: + break; + case ClamScanResults.VirusDetected: + return Json(new { error = new { message = string.Format("Virus Detected: {0}. As per our Terms of Service, Viruses are not permited.", scanResult.InfectedFiles.First().VirusName, Url.SubRouteUrl("tos", "TOS.Index")) } }); + case ClamScanResults.Error: + return Json(new { error = new { message = string.Format("Error scanning the file upload for viruses. {0}", scanResult.RawResult) } }); + case ClamScanResults.Unknown: + return Json(new { error = new { message = string.Format("Unknown result while scanning the file upload for viruses. {0}", scanResult.RawResult) } }); } } - return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url }), key = key } }, "text/plain"); + + // if they want us to encrypt it, we do so here + if (encrypt) + { + // Generate key and iv if empty + if (string.IsNullOrEmpty(key)) + { + key = Utility.RandomString(keySize / 8); + } + + fileData = AES.Encrypt(fileData, key, iv); + if (fileData == null || fileData.Length <= 0) + { + return Json(new { error = new { message = "Unable to encrypt file" } }); + } + } + Models.Upload upload = Uploader.SaveFile(fileData, fileType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize); + if (upload != null) + { + if (User.Identity.IsAuthenticated) + { + Profile.Models.User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault(); + if (user != null) + { + upload.UserId = user.UserId; + db.Entry(upload).State = EntityState.Modified; + db.SaveChanges(); + } + } + return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url }), key = key } }, "text/plain"); + } + return Json(new { error = new { message = "Unable to upload file" } }); + } + else + { + return Json(new { error = new { message = "File Too Large" } }); } - return Json(new { error = new { message = "Unable to upload file" } }); - } - else - { - return Json(new { error = new { message = "File Too Large" } }); } + return Json(new { error = new { message = "Uploads are disabled" } }); + } + catch (Exception ex) + { + return Json(new { error = new { message = "Exception while uploading file: " + ex.Message } }); } - return Json(new { error = new { message = "Uploads are disabled" } }); } // User did not supply key diff --git a/Teknik/Areas/Upload/Scripts/Upload.js b/Teknik/Areas/Upload/Scripts/Upload.js index b313632..c88c12f 100644 --- a/Teknik/Areas/Upload/Scripts/Upload.js +++ b/Teknik/Areas/Upload/Scripts/Upload.js @@ -138,7 +138,7 @@ var dropZone = new Dropzone(document.body, {
\
\
\ -
0%
\ +
0%
\
\
\
\ @@ -165,6 +165,8 @@ var dropZone = new Dropzone(document.body, { // An error occured $("#progress-" + fileID).children('.progress-bar').css('width', '100%'); $("#progress-" + fileID).children('.progress-bar').removeClass('progress-bar-success'); + $("#progress-" + fileID).children('.progress-bar').removeClass('progress-bar-striped'); + $("#progress-" + fileID).children('.progress-bar').removeClass('active'); $("#progress-" + fileID).children('.progress-bar').addClass('progress-bar-danger'); $("#progress-" + fileID).children('.progress-bar').html('File Too Large'); } @@ -219,6 +221,8 @@ function encryptFile(file, callback) { // An error occured $("#progress-" + fileID).children('.progress-bar').css('width', '100%'); $("#progress-" + fileID).children('.progress-bar').removeClass('progress-bar-success'); + $("#progress-" + fileID).children('.progress-bar').removeClass('progress-bar-striped'); + $("#progress-" + fileID).children('.progress-bar').removeClass('active'); $("#progress-" + fileID).children('.progress-bar').addClass('progress-bar-danger'); $("#progress-" + fileID).children('.progress-bar').html('Error Occured'); } @@ -281,10 +285,17 @@ function uploadFile(data, key, iv, filetype, fileExt, fileID, saveKey, serverSid } function uploadProgress(fileID, evt) { + var serverSideEncrypt = $('#serverSideEncrypt').is(':checked'); if (evt.lengthComputable) { var percentComplete = Math.round(evt.loaded * 100 / evt.total); - $('#progress-' + fileID).children('.progress-bar').css('width', (percentComplete * (2 / 5)) + 60 + '%'); - $('#progress-' + fileID).children('.progress-bar').html(percentComplete + '% Uploaded'); + if (serverSideEncrypt && percentComplete == 100) { + $('#progress-' + fileID).children('.progress-bar').css('width', '100%'); + $('#progress-' + fileID).children('.progress-bar').html('Encrypting'); + } + else { + $('#progress-' + fileID).children('.progress-bar').css('width', (percentComplete * (2 / 5)) + 60 + '%'); + $('#progress-' + fileID).children('.progress-bar').html(percentComplete + '% Uploaded'); + } } } @@ -299,6 +310,8 @@ function uploadComplete(fileID, key, saveKey, serverSideEncrypt, evt) { fullName = fullName + '#' + key; } $('#progress-' + fileID).children('.progress-bar').css('width', '100%'); + $("#progress-" + fileID).children('.progress-bar').removeClass('progress-bar-striped'); + $("#progress-" + fileID).children('.progress-bar').removeClass('active'); $('#progress-' + fileID).children('.progress-bar').html('Complete'); $('#upload-link-' + fileID).html('

' + fullName + '

'); var keyBtn = '