Browse Source

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

master
Teknikode 7 years ago
parent
commit
e918da06be
  1. 3
      Teknik/Areas/API/Controllers/APIv1Controller.cs
  2. 4
      Teknik/Areas/Upload/Controllers/UploadController.cs
  3. 8
      Teknik/Areas/Upload/Scripts/Upload.js
  4. 18
      Teknik/Areas/Upload/Uploader.cs
  5. 4
      Teknik/Helpers/Utility.cs
  6. 5
      Teknik/Scripts/Common.js

3
Teknik/Areas/API/Controllers/APIv1Controller.cs

@ -35,6 +35,7 @@ namespace Teknik.Areas.API.Controllers @@ -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 @@ -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)
{

4
Teknik/Areas/Upload/Controllers/UploadController.cs

@ -42,7 +42,7 @@ namespace Teknik.Areas.Upload.Controllers @@ -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 @@ -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)

8
Teknik/Areas/Upload/Scripts/Upload.js

@ -152,6 +152,7 @@ var dropZone = new Dropzone(document.body, { @@ -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) { @@ -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) { @@ -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) { @@ -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);

18
Teknik/Areas/Upload/Uploader.cs

@ -12,20 +12,24 @@ namespace Teknik.Areas.Upload @@ -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 defaultExtension)
{
return SaveFile(file, contentType, contentLength, defaultExtension, 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, string iv)
{
return SaveFile(file, contentType, contentLength, iv, null, 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)
public static Models.Upload SaveFile(byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key)
{
return SaveFile(file, contentType, contentLength, iv, key, 256, 128);
return SaveFile(file, contentType, contentLength, defaultExtension, iv, key, 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, int keySize, int blockSize)
{
Config config = Config.Load();
TeknikEntities db = new TeknikEntities();
@ -42,7 +46,7 @@ namespace Teknik.Areas.Upload @@ -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)
{

4
Teknik/Helpers/Utility.cs

@ -88,7 +88,7 @@ namespace Teknik @@ -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 @@ -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;
}

5
Teknik/Scripts/Common.js

@ -96,6 +96,11 @@ function randomString(length, chars) { @@ -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;

Loading…
Cancel
Save