Browse Source

Issue #54 - Added additional cleaning and logic to file extension parsing.

core
Teknikode 3 years ago
parent
commit
883318f871
  1. 4
      Teknik/Areas/API/V1/Controllers/UploadAPIv1Controller.cs
  2. 21
      Teknik/Scripts/common.js
  3. 29
      Utilities/FileHelper.cs

4
Teknik/Areas/API/V1/Controllers/UploadAPIv1Controller.cs

@ -56,7 +56,7 @@ namespace Teknik.Areas.API.V1.Controllers @@ -56,7 +56,7 @@ namespace Teknik.Areas.API.V1.Controllers
if (model.file.Length <= maxUploadSize)
{
// convert file to bytes
string fileExt = Path.GetExtension(model.file.FileName);
string fileExt = FileHelper.GetFileExtension(model.file.FileName);
long contentLength = model.file.Length;
// Scan the file to detect a virus
@ -86,7 +86,7 @@ namespace Teknik.Areas.API.V1.Controllers @@ -86,7 +86,7 @@ namespace Teknik.Areas.API.V1.Controllers
if (string.IsNullOrEmpty(model.contentType))
{
using (System.IO.Stream fileStream = model.file.OpenReadStream())
using (Stream fileStream = model.file.OpenReadStream())
{
fileStream.Seek(0, SeekOrigin.Begin);
FileType fileType = fileStream.GetFileType();

21
Teknik/Scripts/common.js

@ -158,11 +158,24 @@ function randomString(length, chars) { @@ -158,11 +158,24 @@ function randomString(length, chars) {
}
function getFileExtension(fileName) {
var index = fileName.lastIndexOf('.');
if (index >= 0 && fileName.length > 0) {
return '.' + fileName.substr(index + 1);
var ext;
var a = fileName.split(".");
if (a.length === 1 || (a[0] === "" && a.length === 2)) {
ext = "";
} else if (a.length > 2) {
ext = "." + a.pop();
// Special case for G-Zip files
if (ext === ".gz") {
ext = "." + a.pop() + ext;
}
} else {
ext = a.pop();
}
return '';
// Clear the # and ? characters
ext = ext.split('#')[0];
ext = ext.split('?')[0];
return ext;
}
function SelectAll(id) {

29
Utilities/FileHelper.cs

@ -101,5 +101,34 @@ namespace Teknik.Utilities @@ -101,5 +101,34 @@ namespace Teknik.Utilities
var webRoot = env.WebRootPath;
return Path.Combine(webRoot, file);
}
public static string GetFileExtension(string fileName)
{
string ext;
var parts = fileName.Split('.');
if (parts.Length == 1 || (parts[0] == "" && parts.Length == 2))
{
ext = string.Empty;
}
else if (parts.Length > 2)
{
ext = "." + parts[parts.Length - 1];
// Special case for G-Zip files
if (ext == ".gz")
{
ext = "." + parts[parts.Length - 2] + ext;
}
}
else
{
ext = parts[1];
}
// Strip out invalid parts
ext = ext.Split('#')[0];
ext = ext.Split('?')[0];
return ext;
}
}
}

Loading…
Cancel
Save