Browse Source

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

tags/3.0.0^2
Teknikode 9 months ago
parent
commit
883318f871

+ 2
- 2
Teknik/Areas/API/V1/Controllers/UploadAPIv1Controller.cs View File

@@ -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

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();

+ 17
- 4
Teknik/Scripts/common.js View File

@@ -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
- 0
Utilities/FileHelper.cs View File

@@ -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