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

Added the uploaded files extension as teh default extension in case one could not be determined from the content-type.

This commit is contained in:
Uncled1023 2016-01-27 14:18:31 -08:00
parent 083553a9b2
commit e918da06be
6 changed files with 28 additions and 16 deletions

View File

@ -35,6 +35,7 @@ namespace Teknik.Areas.API.Controllers
{
// convert file to bytes
byte[] fileData = null;
string fileExt = Path.GetExtension(file.FileName);
int contentLength = file.ContentLength;
using (var binaryReader = new BinaryReader(file.InputStream))
{
@ -75,7 +76,7 @@ namespace Teknik.Areas.API.Controllers
}
// Save the file data
Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, iv, (saveKey) ? key : null, keySize, blockSize);
Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);
if (upload != null)
{

View File

@ -42,7 +42,7 @@ namespace Teknik.Areas.Upload.Controllers
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Upload(string fileType, string iv, int keySize, int blockSize, bool encrypt, HttpPostedFileWrapper data, string key = null)
public ActionResult Upload(string fileType, string fileExt, string iv, int keySize, int blockSize, bool encrypt, HttpPostedFileWrapper data, string key = null)
{
if (data.ContentLength <= Config.UploadConfig.MaxUploadSize)
{
@ -68,7 +68,7 @@ namespace Teknik.Areas.Upload.Controllers
return Json(new { error = new { message = "Unable to encrypt file" } });
}
}
Models.Upload upload = Uploader.SaveFile(fileData, fileType, contentLength, iv, key, keySize, blockSize);
Models.Upload upload = Uploader.SaveFile(fileData, fileType, contentLength, fileExt, iv, key, keySize, blockSize);
if (upload != null)
{
if (User.Identity.IsAuthenticated)

View File

@ -152,6 +152,7 @@ var dropZone = new Dropzone(document.body, {
function encryptFile(file, callback) {
var filetype = file.type;
var fileID = file.ID;
var fileExt = getFileExtension(file.name);
// Get session settings
var saveKey = $('#saveKey').is(':checked');
@ -169,7 +170,7 @@ function encryptFile(file, callback) {
// Encrypt on the server side if they ask for it
if (serverSideEncrypt) {
callback(e.target.result, keyStr, ivStr, filetype, fileID, saveKey, serverSideEncrypt);
callback(e.target.result, keyStr, ivStr, filetype, fileExt, fileID, saveKey, serverSideEncrypt);
}
else {
var worker = new Worker(encScriptSrc);
@ -184,7 +185,7 @@ function encryptFile(file, callback) {
case 'finish':
if (callback != null) {
// Finish
callback(e.data.buffer, keyStr, ivStr, filetype, fileID, saveKey, serverSideEncrypt);
callback(e.data.buffer, keyStr, ivStr, filetype, fileExt, fileID, saveKey, serverSideEncrypt);
}
break;
}
@ -226,13 +227,14 @@ function encryptFile(file, callback) {
reader.readAsArrayBuffer(blob);
}
function uploadFile(data, key, iv, filetype, fileID, saveKey, serverSideEncrypt)
function uploadFile(data, key, iv, filetype, fileExt, fileID, saveKey, serverSideEncrypt)
{
$('#key-' + fileID).val(key);
var blob = new Blob([data]);
// Now we need to upload the file
var fd = new FormData();
fd.append('fileType', filetype);
fd.append('fileExt', fileExt);
if (saveKey)
{
fd.append('key', key);

View File

@ -12,20 +12,24 @@ namespace Teknik.Areas.Upload
{
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength)
{
return SaveFile(file, contentType, contentLength, null, null, 256, 128);
return SaveFile(file, contentType, contentLength, string.Empty, null, null, 256, 128);
}
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string iv)
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension)
{
return SaveFile(file, contentType, contentLength, iv, null, 256, 128);
return SaveFile(file, contentType, contentLength, defaultExtension, null, null, 256, 128);
}
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string iv, string key)
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension, string iv)
{
return SaveFile(file, contentType, contentLength, iv, key, 256, 128);
return SaveFile(file, contentType, contentLength, defaultExtension, iv, null, 256, 128);
}
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string iv, string key, int keySize, int blockSize)
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key)
{
return SaveFile(file, contentType, contentLength, defaultExtension, iv, key, 256, 128);
}
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key, int keySize, int blockSize)
{
Config config = Config.Load();
TeknikEntities db = new TeknikEntities();
@ -42,7 +46,7 @@ namespace Teknik.Areas.Upload
File.WriteAllBytes(fileName, file);
// Generate a unique url
string extension = (config.UploadConfig.IncludeExtension) ? Utility.GetDefaultExtension(contentType) : string.Empty;
string extension = (config.UploadConfig.IncludeExtension) ? Utility.GetDefaultExtension(contentType, defaultExtension) : string.Empty;
string url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
{

View File

@ -88,7 +88,7 @@ namespace Teknik
return string.Empty;
}
public static string GetDefaultExtension(string mimeType)
public static string GetDefaultExtension(string mimeType, string defaultExtension = "")
{
string result;
RegistryKey key;
@ -96,7 +96,7 @@ namespace Teknik
key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
value = key != null ? key.GetValue("Extension", null) : null;
result = value != null ? value.ToString() : string.Empty;
result = value != null ? value.ToString() : defaultExtension;
return result;
}

View File

@ -96,6 +96,11 @@ function randomString(length, chars) {
return result;
}
function getFileExtension(fileName) {
var index = fileName.lastIndexOf('.');
return '.' + fileName.substr(index + 1);
}
/***************************** TIMER Page Load *******************************/
var loopTime;