Browse Source

Added MIME magic number filetype finder

pull/111/head
Teknikode 5 years ago
parent
commit
64a48d2ddd
  1. 16
      Teknik/Areas/API/Controllers/APIv1Controller.cs
  2. 16
      Teknik/Areas/Upload/Uploader.cs
  3. 6
      Teknik/Teknik.csproj
  4. 2
      Teknik/packages.config
  5. 10
      Utilities/Utilities/FileHelper.cs
  6. 3
      Utilities/Utilities/Utilities.csproj
  7. 2
      Utilities/Utilities/packages.config
  8. 1
      UtilitiesTests/UtilitiesTests.csproj
  9. 19
      UtilitiesTests/app.config

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

@ -11,6 +11,8 @@ using Teknik.Controllers; @@ -11,6 +11,8 @@ using Teknik.Controllers;
using Teknik.Utilities;
using Teknik.Models;
using System.Text;
using MimeDetective;
using MimeDetective.Extensions;
using Teknik.Areas.Shortener.Models;
using nClam;
using Teknik.Filters;
@ -70,7 +72,19 @@ namespace Teknik.Areas.API.Controllers @@ -70,7 +72,19 @@ namespace Teknik.Areas.API.Controllers
// Need to grab the contentType if it's empty
if (string.IsNullOrEmpty(model.contentType))
{
model.contentType = (string.IsNullOrEmpty(model.file.ContentType)) ? "application/octet-stream" : model.file.ContentType;
model.contentType = model.file.ContentType;
if (string.IsNullOrEmpty(model.contentType))
{
model.file.InputStream.Seek(0, SeekOrigin.Begin);
FileType fileType = model.file.InputStream.GetFileType();
if (fileType != null)
model.contentType = fileType.Mime;
if (string.IsNullOrEmpty(model.contentType))
{
model.contentType = "application/octet-stream";
}
}
}
// Initialize the key size and block size if empty

16
Teknik/Areas/Upload/Uploader.cs

@ -17,22 +17,22 @@ namespace Teknik.Areas.Upload @@ -17,22 +17,22 @@ namespace Teknik.Areas.Upload
{
return SaveFile(db, config, file, contentType, contentLength, encrypt, string.Empty, null, null, 256, 128);
}
public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string defaultExtension)
public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string fileExt)
{
return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, null, null, 256, 128);
return SaveFile(db, config, file, contentType, contentLength, encrypt, fileExt, null, null, 256, 128);
}
public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string defaultExtension, string iv)
public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string fileExt, string iv)
{
return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, iv, null, 256, 128);
return SaveFile(db, config, file, contentType, contentLength, encrypt, fileExt, iv, null, 256, 128);
}
public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string defaultExtension, string iv, string key)
public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string fileExt, string iv, string key)
{
return SaveFile(db, config, file, contentType, contentLength, encrypt, defaultExtension, iv, key, 256, 128);
return SaveFile(db, config, file, contentType, contentLength, encrypt, fileExt, iv, key, 256, 128);
}
public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string defaultExtension, string iv, string key, int keySize, int blockSize)
public static Models.Upload SaveFile(TeknikEntities db, Config config, System.IO.Stream file, string contentType, int contentLength, bool encrypt, string fileExt, string iv, string key, int keySize, int blockSize)
{
if (!Directory.Exists(config.UploadConfig.UploadDirectory))
{
@ -67,7 +67,7 @@ namespace Teknik.Areas.Upload @@ -67,7 +67,7 @@ namespace Teknik.Areas.Upload
}
// Generate a unique url
string extension = (config.UploadConfig.IncludeExtension) ? FileHelper.GetDefaultExtension(contentType, defaultExtension) : string.Empty;
string extension = (config.UploadConfig.IncludeExtension) ? fileExt : string.Empty;
string url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
{

6
Teknik/Teknik.csproj

@ -104,6 +104,9 @@ @@ -104,6 +104,9 @@
<HintPath>..\packages\Microsoft.Owin.Security.3.0.1\lib\net45\Microsoft.Owin.Security.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mime-Detective, Version=0.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Mime-Detective.0.0.5\lib\net45\Mime-Detective.dll</HintPath>
</Reference>
<Reference Include="MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\packages\MySql.Data.6.9.9\lib\net45\MySql.Data.dll</HintPath>
<Private>True</Private>
@ -377,6 +380,8 @@ @@ -377,6 +380,8 @@
<Compile Include="BaseViewPage.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="App_Data\libgnurx-0.dll" />
<Content Include="App_Data\libmagic-1.dll" />
<Content Include="App_Data\reservedUsernames.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
@ -659,6 +664,7 @@ @@ -659,6 +664,7 @@
<Content Include="Areas\IRC\Views\web.config" />
<Content Include="Areas\IRC\Views\IRC\Client.cshtml" />
<Content Include="Areas\IRC\Views\_ViewStart.cshtml" />
<Content Include="App_Data\magic.mgc" />
<None Include="Properties\PublishProfiles\Teknik Dev.pubxml" />
<None Include="Properties\PublishProfiles\Teknik Production.pubxml" />
<None Include="Scripts\jquery-2.1.4.intellisense.js" />

2
Teknik/packages.config

@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net462" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net462" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" userInstalled="true" />
<package id="Mime-Detective" version="0.0.5" targetFramework="net462" />
<package id="Modernizr" version="2.8.3" targetFramework="net452" userInstalled="true" />
<package id="MySql.Data" version="6.9.9" targetFramework="net452" />
<package id="NBitcoin" version="3.0.2.10" targetFramework="net462" />
@ -40,6 +41,7 @@ @@ -40,6 +41,7 @@
<package id="Piwik.Tracker" version="2.16.0.0" targetFramework="net452" />
<package id="QRCoder" version="1.2.3" targetFramework="net462" />
<package id="Respond" version="1.4.2" targetFramework="net452" userInstalled="true" />
<package id="System.Xml.XmlSerializer" version="4.3.0" targetFramework="net462" />
<package id="Teknik.Pygments" version="2.0.2.3" targetFramework="net462" />
<package id="TwoStepsAuthenticator" version="1.2.0" targetFramework="net452" />
<package id="WebGrease" version="1.6.0" targetFramework="net46" userInstalled="true" />

10
Utilities/Utilities/FileHelper.cs

@ -1,4 +1,6 @@ @@ -1,4 +1,6 @@
using Microsoft.Win32;
using MimeDetective;
using MimeDetective.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
@ -88,5 +90,13 @@ namespace Teknik.Utilities @@ -88,5 +90,13 @@ namespace Teknik.Utilities
return result;
}
public static string GetDefaultExtension(Stream fileStream, string defaultExtension)
{
FileType fileType = fileStream.GetFileType();
if (!string.IsNullOrEmpty(fileType.Extension))
return fileType.Extension;
return defaultExtension;
}
}
}

3
Utilities/Utilities/Utilities.csproj

@ -48,6 +48,9 @@ @@ -48,6 +48,9 @@
<HintPath>..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mime-Detective, Version=0.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Mime-Detective.0.0.5\lib\net45\Mime-Detective.dll</HintPath>
</Reference>
<Reference Include="MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\..\packages\MySql.Data.6.9.9\lib\net45\MySql.Data.dll</HintPath>
<Private>True</Private>

2
Utilities/Utilities/packages.config

@ -9,7 +9,9 @@ @@ -9,7 +9,9 @@
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net462" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net462" />
<package id="Mime-Detective" version="0.0.5" targetFramework="net462" />
<package id="MySql.Data" version="6.9.9" targetFramework="net462" />
<package id="Newtonsoft.Json" version="10.0.1" targetFramework="net462" />
<package id="System.Xml.XmlSerializer" version="4.3.0" targetFramework="net462" />
<package id="WebGrease" version="1.6.0" targetFramework="net462" />
</packages>

1
UtilitiesTests/UtilitiesTests.csproj

@ -64,6 +64,7 @@ @@ -64,6 +64,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>

19
UtilitiesTests/app.config

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Loading…
Cancel
Save