1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added config options to the Uploads.

This commit is contained in:
Uncled1023 2016-01-26 17:14:03 -08:00
parent a68e7cca02
commit 407da9243f
4 changed files with 105 additions and 44 deletions

View File

@ -24,13 +24,25 @@ namespace Teknik.Areas.Upload.Controllers
public ActionResult Index()
{
ViewBag.Title = "Teknik Upload - End to End Encryption";
return View(new UploadViewModel());
UploadViewModel model = new UploadViewModel();
Areas.Profile.Models.User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
if (user != null)
{
model.SaveKey = user.UploadSettings.SaveKey;
model.ServerSideEncrypt = user.UploadSettings.ServerSideEncrypt;
}
else
{
model.SaveKey = false;
model.ServerSideEncrypt = false;
}
return View(model);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Upload(string fileType, string iv, int keySize, int blockSize, HttpPostedFileWrapper data)
public ActionResult Upload(string fileType, string iv, int keySize, int blockSize, bool encrypt, HttpPostedFileWrapper data, string key = null)
{
if (data.ContentLength <= Config.UploadConfig.MaxUploadSize)
{
@ -41,7 +53,22 @@ namespace Teknik.Areas.Upload.Controllers
{
fileData = binaryReader.ReadBytes(data.ContentLength);
}
Models.Upload upload = Uploader.SaveFile(fileData, fileType, contentLength, iv, null, keySize, blockSize);
// 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, iv, key, keySize, blockSize);
if (upload != null)
{
return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }) } }, "text/plain");

View File

@ -160,44 +160,49 @@ function encryptFile(file, callback) {
var keyStr = randomString((keySize / 8), '#aA');
var ivStr = randomString((blockSize / 8), '#aA');
var worker = new Worker(encScriptSrc);
worker.addEventListener('message', function (e) {
switch (e.data.cmd)
{
case 'progress':
var percentComplete = Math.round(e.data.processed * 100 / e.data.total);
$("#progress-" + fileID).children('.progress-bar').css('width', (percentComplete * (2 / 5)) + 20 + '%');
$("#progress-" + fileID).children('.progress-bar').html(percentComplete + '% Encrypted');
break;
case 'finish':
if (callback != null) {
// Finish
callback(e.data.buffer, keyStr, ivStr, filetype, fileID);
}
break;
}
});
worker.onerror = function (err) {
// 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').addClass('progress-bar-danger');
$("#progress-" + fileID).children('.progress-bar').html('Error Occured');
// Encrypt on the server side if they ask for it
if (serverSideEncrypt) {
callback(e.target.result, keyStr, ivStr, filetype, fileID);
}
else {
var worker = new Worker(encScriptSrc);
// Execute worker with data
var objData =
{
cmd: 'encrypt',
script: aesScriptSrc,
key: keyStr,
iv: ivStr,
chunkSize: chunkSize,
file: e.target.result
};
worker.postMessage(objData, [objData.file]);
worker.addEventListener('message', function (e) {
switch (e.data.cmd) {
case 'progress':
var percentComplete = Math.round(e.data.processed * 100 / e.data.total);
$("#progress-" + fileID).children('.progress-bar').css('width', (percentComplete * (2 / 5)) + 20 + '%');
$("#progress-" + fileID).children('.progress-bar').html(percentComplete + '% Encrypted');
break;
case 'finish':
if (callback != null) {
// Finish
callback(e.data.buffer, keyStr, ivStr, filetype, fileID);
}
break;
}
});
worker.onerror = function (err) {
// 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').addClass('progress-bar-danger');
$("#progress-" + fileID).children('.progress-bar').html('Error Occured');
}
// Execute worker with data
var objData =
{
cmd: 'encrypt',
script: aesScriptSrc,
key: keyStr,
iv: ivStr,
chunkSize: chunkSize,
file: e.target.result
};
worker.postMessage(objData, [objData.file]);
}
};
})(callback);
@ -221,10 +226,15 @@ function uploadFile(data, key, iv, filetype, fileID)
// Now we need to upload the file
var fd = new FormData();
fd.append('fileType', filetype);
if (saveKey)
{
fd.append('key', key);
}
fd.append('iv', iv);
fd.append('keySize', keySize);
fd.append('blockSize', blockSize);
fd.append('data', blob);
fd.append('encrypt', serverSideEncrypt);
fd.append('__RequestVerificationToken', $('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val());
var xhr = new XMLHttpRequest();
@ -248,14 +258,26 @@ function uploadComplete(fileID, key, evt) {
obj = JSON.parse(evt.target.responseText);
var name = obj.result.name;
var fullName = obj.result.url;
if (!saveKey && !serverSideEncrypt) {
fullName = fullName + '#' + key;
}
$('#progress-' + fileID).children('.progress-bar').css('width', '100%');
$('#progress-' + fileID).children('.progress-bar').html('Complete');
$('#upload-link-' + fileID).html('<p><a href="' + fullName + '#' + key + '" target="_blank" class="alert-link">' + fullName + '#' + key + '</a></p>');
$('#upload-link-' + fileID).html('<p><a href="' + fullName + '" target="_blank" class="alert-link">' + fullName + '</a></p>');
var keyBtn = '<div class="col-sm-4 text-center" id="key-link-' + fileID + '"> \
<button type="button" class="btn btn-default btn-sm" id="save-key-link-' + fileID + '">Save Key On Server</button> \
</div>';
if (saveKey) {
keyBtn = '<div class="col-sm-4 text-center" id="key-link-' + fileID + '"> \
<button type="button" class="btn btn-default btn-sm" id="remove-key-link-' + fileID + '">Remove Key From Server</button> \
</div>';
}
if (!saveKey && serverSideEncrypt) {
keyBtn = '';
}
$('#link-footer-' + fileID).html(' \
<div class="row"> \
<div class="col-sm-4 text-center" id="key-link-' + fileID + '"> \
<button type="button" class="btn btn-default btn-sm" id="save-key-link-' + fileID + '">Save Key On Server</button> \
</div> \
' + keyBtn + ' \
<div class="col-sm-4 text-center"> \
<button type="button" class="btn btn-default btn-sm" id="generate-delete-link-' + fileID + '">Generate Deletion URL</button> \
</div> \
@ -264,7 +286,14 @@ function uploadComplete(fileID, key, evt) {
</div> \
</div> \
');
linkSaveKey('#save-key-link-' + fileID + '', name, key, fileID);
if (saveKey) {
linkRemoveKey('#remove-key-link-' + fileID + '', name, key, fileID);
}
else {
if (!serverSideEncrypt) {
linkSaveKey('#save-key-link-' + fileID + '', name, key, fileID);
}
}
linkUploadDelete('#generate-delete-link-' + fileID + '', name);
linkRemove('#remove-link-' + fileID + '', fileID);
}

View File

@ -8,5 +8,8 @@ namespace Teknik.Areas.Upload.ViewModels
{
public class UploadViewModel : ViewModelBase
{
public bool SaveKey { get; set; }
public bool ServerSideEncrypt { get; set; }
}
}

View File

@ -1,6 +1,8 @@
@model Teknik.Areas.Upload.ViewModels.UploadViewModel
<script>
var saveKey = @(Model.SaveKey ? "true" : "false");
var serverSideEncrypt = @(Model.ServerSideEncrypt ? "true" : "false");
var encScriptSrc = '@Scripts.Url("~/bundles/cryptoWorker")';
var aesScriptSrc = '@Scripts.Url("~/bundles/crypto")';
var generateDeleteKeyURL = '@Url.SubRouteUrl("upload", "Upload.Action", new { action= "GenerateDeleteKey" })';