|
|
|
@ -26,98 +26,97 @@ namespace Teknik.Areas.API.Controllers
@@ -26,98 +26,97 @@ namespace Teknik.Areas.API.Controllers
|
|
|
|
|
[AllowAnonymous] |
|
|
|
|
public ActionResult Upload(HttpPostedFileWrapper file, string contentType = null, bool encrypt = false, bool saveKey = false, string key = null, int keySize = 0, string iv = null, int blockSize = 0, bool genDeletionKey = false) |
|
|
|
|
{ |
|
|
|
|
if (file != null) |
|
|
|
|
{ |
|
|
|
|
if (file.ContentLength <= Config.UploadConfig.MaxUploadSize) |
|
|
|
|
try { |
|
|
|
|
if (file != null) |
|
|
|
|
{ |
|
|
|
|
// convert file to bytes
|
|
|
|
|
byte[] fileData = null; |
|
|
|
|
int contentLength = file.ContentLength; |
|
|
|
|
using (var binaryReader = new BinaryReader(file.InputStream)) |
|
|
|
|
{ |
|
|
|
|
fileData = binaryReader.ReadBytes(file.ContentLength); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Need to grab the contentType if it's empty
|
|
|
|
|
if (string.IsNullOrEmpty(contentType)) |
|
|
|
|
if (file.ContentLength <= Config.UploadConfig.MaxUploadSize) |
|
|
|
|
{ |
|
|
|
|
contentType = (string.IsNullOrEmpty(file.ContentType)) ? "application/octet-stream" : file.ContentType; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Initialize the key size and block size if empty
|
|
|
|
|
if (keySize <= 0) |
|
|
|
|
keySize = Config.UploadConfig.KeySize; |
|
|
|
|
if (blockSize <= 0) |
|
|
|
|
blockSize = Config.UploadConfig.BlockSize; |
|
|
|
|
|
|
|
|
|
byte[] data = null; |
|
|
|
|
// If they want us to encrypt the file first, do that here
|
|
|
|
|
if (encrypt) |
|
|
|
|
{ |
|
|
|
|
// Generate key and iv if empty
|
|
|
|
|
if (string.IsNullOrEmpty(key)) |
|
|
|
|
{ |
|
|
|
|
key = Utility.RandomString(keySize); |
|
|
|
|
} |
|
|
|
|
if (string.IsNullOrEmpty(iv)) |
|
|
|
|
// convert file to bytes
|
|
|
|
|
byte[] fileData = null; |
|
|
|
|
int contentLength = file.ContentLength; |
|
|
|
|
using (var binaryReader = new BinaryReader(file.InputStream)) |
|
|
|
|
{ |
|
|
|
|
iv = Utility.RandomString(blockSize); |
|
|
|
|
fileData = binaryReader.ReadBytes(file.ContentLength); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data = AES.Encrypt(fileData, key, iv); |
|
|
|
|
if (data == null || data.Length <= 0) |
|
|
|
|
// Need to grab the contentType if it's empty
|
|
|
|
|
if (string.IsNullOrEmpty(contentType)) |
|
|
|
|
{ |
|
|
|
|
return Json(new { error = "Unable to encrypt file" }); |
|
|
|
|
contentType = (string.IsNullOrEmpty(file.ContentType)) ? "application/octet-stream" : file.ContentType; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Save the file data
|
|
|
|
|
Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, iv, key, keySize, blockSize); |
|
|
|
|
// Initialize the key size and block size if empty
|
|
|
|
|
if (keySize <= 0) |
|
|
|
|
keySize = Config.UploadConfig.KeySize; |
|
|
|
|
if (blockSize <= 0) |
|
|
|
|
blockSize = Config.UploadConfig.BlockSize; |
|
|
|
|
|
|
|
|
|
if (upload != null) |
|
|
|
|
{ |
|
|
|
|
// Save the key to the DB if they wanted it to be
|
|
|
|
|
if (saveKey) |
|
|
|
|
byte[] data = null; |
|
|
|
|
// If they want us to encrypt the file first, do that here
|
|
|
|
|
if (encrypt) |
|
|
|
|
{ |
|
|
|
|
upload.Key = key; |
|
|
|
|
db.Entry(upload).State = EntityState.Modified; |
|
|
|
|
db.SaveChanges(); |
|
|
|
|
} |
|
|
|
|
// Generate key and iv if empty
|
|
|
|
|
if (string.IsNullOrEmpty(key)) |
|
|
|
|
{ |
|
|
|
|
key = Utility.RandomString(keySize / 8); |
|
|
|
|
} |
|
|
|
|
if (string.IsNullOrEmpty(iv)) |
|
|
|
|
{ |
|
|
|
|
iv = Utility.RandomString(blockSize / 8); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Generate delete key if asked to
|
|
|
|
|
if (genDeletionKey) |
|
|
|
|
{ |
|
|
|
|
string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength); |
|
|
|
|
upload.DeleteKey = delKey; |
|
|
|
|
db.Entry(upload).State = EntityState.Modified; |
|
|
|
|
db.SaveChanges(); |
|
|
|
|
data = AES.Encrypt(fileData, key, iv); |
|
|
|
|
if (data == null || data.Length <= 0) |
|
|
|
|
{ |
|
|
|
|
return Json(new { error = new { message = "Unable to encrypt file" } }); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Pull all the information together
|
|
|
|
|
var returnData = new |
|
|
|
|
// Save the file data
|
|
|
|
|
Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, iv, (saveKey) ? key : null, keySize, blockSize); |
|
|
|
|
|
|
|
|
|
if (upload != null) |
|
|
|
|
{ |
|
|
|
|
url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }), |
|
|
|
|
fileName = upload.Url, |
|
|
|
|
contentType = contentType, |
|
|
|
|
contentLength = contentLength, |
|
|
|
|
key = key, |
|
|
|
|
keySize = keySize, |
|
|
|
|
iv = iv, |
|
|
|
|
blockSize = blockSize, |
|
|
|
|
deletionKey = upload.DeleteKey |
|
|
|
|
// Generate delete key if asked to
|
|
|
|
|
if (genDeletionKey) |
|
|
|
|
{ |
|
|
|
|
string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength); |
|
|
|
|
upload.DeleteKey = delKey; |
|
|
|
|
db.Entry(upload).State = EntityState.Modified; |
|
|
|
|
db.SaveChanges(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Pull all the information together
|
|
|
|
|
string fullUrl = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }); |
|
|
|
|
var returnData = new |
|
|
|
|
{ |
|
|
|
|
url = (saveKey) ? fullUrl : fullUrl + "#" + key, |
|
|
|
|
fileName = upload.Url, |
|
|
|
|
contentType = contentType, |
|
|
|
|
contentLength = contentLength, |
|
|
|
|
key = key, |
|
|
|
|
keySize = keySize, |
|
|
|
|
iv = iv, |
|
|
|
|
blockSize = blockSize, |
|
|
|
|
deletionKey = upload.DeleteKey |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
return Json(new { result = returnData }); |
|
|
|
|
}; |
|
|
|
|
return Json(new { result = returnData }); |
|
|
|
|
} |
|
|
|
|
return Json(new { error = new { message = "Unable to save file" } }); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
return Json(new { error = new { message = "File Too Large" } }); |
|
|
|
|
} |
|
|
|
|
return Json(new { error = "Unable to save file" }); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
return Json(new { error = "File Too Large" }); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Json(new { error = "Invalid Upload Request" }); |
|
|
|
|
return Json(new { error = new { message = "Invalid Upload Request" } }); |
|
|
|
|
} |
|
|
|
|
catch(Exception ex) |
|
|
|
|
{ |
|
|
|
|
return Json(new { error = new { message = "Exception: " + ex.Message } }); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |