Skip to content

Commit

Permalink
manually merging new media detectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed Oct 2, 2018
1 parent 549d8d8 commit 7349b54
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/Umbraco.Web/Media/Exif/ImageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Drawing;
using System.IO;
using System.Text;
using Umbraco.Web.Media.TypeDetector;

namespace Umbraco.Web.Media.Exif
{
Expand Down Expand Up @@ -118,22 +119,25 @@ public static ImageFile FromStream(Stream stream)
/// <returns>The <see cref="ImageFile"/> created from the file.</returns>
public static ImageFile FromStream(Stream stream, Encoding encoding)
{
stream.Seek (0, SeekOrigin.Begin);
byte[] header = new byte[8];
stream.Seek (0, SeekOrigin.Begin);
if (stream.Read (header, 0, header.Length) != header.Length)
throw new NotValidImageFileException ();

// JPEG
if (header[0] == 0xFF && header[1] == 0xD8)
return new JPEGFile (stream, encoding);
if (JpegDetector.IsOfType(stream))
{
return new JPEGFile(stream, encoding);
}

// TIFF
string tiffHeader = Encoding.ASCII.GetString (header, 0, 4);
if (tiffHeader == "MM\x00\x2a" || tiffHeader == "II\x2a\x00")
return new TIFFFile (stream, encoding);
if (TIFFDetector.IsOfType(stream))
{
return new TIFFFile(stream, encoding);
}

// SVG
if (SvgDetector.IsOfType(stream))
{
return new SvgFile(stream);
}

throw new NotValidImageFileException ();
throw new NotValidImageFileException();
}
#endregion
}
Expand Down
37 changes: 37 additions & 0 deletions src/Umbraco.Web/Media/Exif/SvgFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace Umbraco.Web.Media.Exif
{
internal class SvgFile : ImageFile
{
public SvgFile(Stream fileStream)
{
fileStream.Position = 0;

var document = XDocument.Load(fileStream); //if it throws an exception the ugly try catch in MediaFileSystem will catch it

var width = document.Root?.Attributes().Where(x => x.Name == "width").Select(x => x.Value).FirstOrDefault();
var height = document.Root?.Attributes().Where(x => x.Name == "height").Select(x => x.Value).FirstOrDefault();

Properties.Add(new ExifSInt(ExifTag.PixelYDimension,
height == null ? Core.Constants.Conventions.Media.DefaultSize : int.Parse(height)));
Properties.Add(new ExifSInt(ExifTag.PixelXDimension,
width == null ? Core.Constants.Conventions.Media.DefaultSize : int.Parse(width)));

Format = ImageFileFormat.SVG;
}

public override void Save(Stream stream)
{
}

public override Image ToImage()
{
throw new NotImplementedException();
}
}
}
14 changes: 14 additions & 0 deletions src/Umbraco.Web/Media/TypeDetector/JpegDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.IO;

namespace Umbraco.Web.Media.TypeDetector
{
public class JpegDetector : RasterizedTypeDetector
{
public static bool IsOfType(Stream fileStream)
{
var header = GetFileHeader(fileStream);

return header[0] == 0xff && header[1] == 0xD8;
}
}
}
16 changes: 16 additions & 0 deletions src/Umbraco.Web/Media/TypeDetector/RasterizedTypeDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.IO;

namespace Umbraco.Web.Media.TypeDetector
{
public abstract class RasterizedTypeDetector
{
public static byte[] GetFileHeader(Stream fileStream)
{
fileStream.Seek(0, SeekOrigin.Begin);
byte[] header = new byte[8];
fileStream.Seek(0, SeekOrigin.Begin);

return header;
}
}
}
24 changes: 24 additions & 0 deletions src/Umbraco.Web/Media/TypeDetector/SvgDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.IO;
using System.Xml.Linq;

namespace Umbraco.Web.Media.TypeDetector
{
public class SvgDetector
{
public static bool IsOfType(Stream fileStream)
{
var document = new XDocument();

try
{
document = XDocument.Load(fileStream);
}
catch (System.Exception ex)
{
return false;
}

return document.Root?.Name.LocalName == "svg";
}
}
}
24 changes: 24 additions & 0 deletions src/Umbraco.Web/Media/TypeDetector/TIFFDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.IO;
using System.Text;

namespace Umbraco.Web.Media.TypeDetector
{
public class TIFFDetector
{
public static bool IsOfType(Stream fileStream)
{
string tiffHeader = GetFileHeader(fileStream);

return tiffHeader == "MM\x00\x2a" || tiffHeader == "II\x2a\x00";
}

public static string GetFileHeader(Stream fileStream)
{
var header = RasterizedTypeDetector.GetFileHeader(fileStream);

string tiffHeader = Encoding.ASCII.GetString(header, 0, 4);

return tiffHeader;
}
}
}
5 changes: 5 additions & 0 deletions src/Umbraco.Web/Umbraco.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,16 @@
<Compile Include="Media\Exif\JPEGMarker.cs" />
<Compile Include="Media\Exif\JPEGSection.cs" />
<Compile Include="Media\Exif\MathEx.cs" />
<Compile Include="Media\Exif\SvgFile.cs" />
<Compile Include="Media\Exif\TIFFFile.cs" />
<Compile Include="Media\Exif\TIFFHeader.cs" />
<Compile Include="Media\Exif\TIFFStrip.cs" />
<Compile Include="Media\Exif\Utility.cs" />
<Compile Include="Media\ImageHelper.cs" />
<Compile Include="Media\TypeDetector\JpegDetector.cs" />
<Compile Include="Media\TypeDetector\RasterizedTypeDetector.cs" />
<Compile Include="Media\TypeDetector\SvgDetector.cs" />
<Compile Include="Media\TypeDetector\TIFFDetector.cs" />
<Compile Include="Media\UploadAutoFillProperties.cs" />
<Compile Include="Models\Mapping\MappingOperationOptionsExtensions.cs" />
<Compile Include="ContentApps\ContentAppDefinitionCollection.cs" />
Expand Down

0 comments on commit 7349b54

Please sign in to comment.