Skip to content

Commit f57bfec

Browse files
authored
Rework BinaryItem to support System.Drawing (#12)
1 parent 561c56b commit f57bfec

File tree

4 files changed

+80
-57
lines changed

4 files changed

+80
-57
lines changed

FB2Library/Elements/BinaryItem.cs

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ public class BinaryItem
1616
private const string ContentTypeAttributeName = "content-type";
1717
private const string IdAttributeName = "id";
1818

19-
public ContentTypeEnum ContentType{get;set;}
20-
public Byte[] BinaryData { get; set; }
21-
public string Id { get; set; }
22-
19+
internal const string Fb2BinaryItemName = "binary";
2320

21+
public static Func<byte[], ContentTypeEnum> DetectContentType { get; set; }
2422

25-
internal const string Fb2BinaryItemName = "binary";
23+
public ContentTypeEnum ContentType { get; set; }
24+
public byte[] BinaryData { get; set; }
25+
public string Id { get; set; }
2626

2727
internal void Load(XElement binarye)
2828
{
2929
if (binarye == null)
3030
{
31-
throw new ArgumentNullException("binarye");
31+
throw new ArgumentNullException(nameof(binarye));
3232
}
3333

3434
if (binarye.Name.LocalName != Fb2BinaryItemName)
3535
{
36-
throw new ArgumentException("Element of wrong type passed", "binarye");
36+
throw new ArgumentException("Element of wrong type passed", nameof(binarye));
3737
}
3838

3939
XAttribute xContentType = binarye.Attribute(ContentTypeAttributeName);
40-
if ((xContentType == null) || (xContentType.Value == null))
40+
if (xContentType?.Value == null)
4141
{
4242
throw new NullReferenceException("content type not defined/present");
4343
}
@@ -58,59 +58,35 @@ internal void Load(XElement binarye)
5858
}
5959

6060
XAttribute idAttribute = binarye.Attribute(IdAttributeName);
61-
if ((idAttribute == null) || (idAttribute.Value == null))
62-
{
63-
throw new NullReferenceException("ID not defined/present");
64-
}
65-
Id = idAttribute.Value;
61+
62+
Id = idAttribute?.Value ?? throw new NullReferenceException("ID not defined/present");
6663

6764
if (BinaryData != null)
6865
{
69-
BinaryData= null;
66+
BinaryData = null;
7067
}
7168
BinaryData = Convert.FromBase64String(binarye.Value);
72-
//ContentTypeEnum content;
73-
// try to detect type , this will detect for unknown and fix for wrongly set
74-
//DetectContentType(out content, BinaryData);
75-
// if we were not able to detect type and type was not set
76-
//if (content == ContentTypeEnum.ContentTypeUnknown && ContentType == ContentTypeEnum.ContentTypeUnknown)
77-
//{
78-
// // then we throw exception
79-
// throw new Exception("Unknown image content type passed");
80-
//}
81-
ContentType = ContentTypeEnum.ContentTypeUnknown; // TODO
69+
70+
// try to detect type, this will detect for unknown and fix for wrongly set
71+
if (DetectContentType == null)
72+
{
73+
ContentType = ContentTypeEnum.ContentTypeUnknown;
74+
}
75+
else
76+
{
77+
var contentType = DetectContentType(BinaryData);
78+
79+
// if we were not able to detect type and type was not set
80+
if (contentType == ContentTypeEnum.ContentTypeUnknown
81+
&& ContentType == ContentTypeEnum.ContentTypeUnknown)
82+
{
83+
// then we throw exception
84+
throw new Exception("Unknown image content type passed");
85+
}
86+
87+
ContentType = contentType;
88+
}
8289
}
83-
84-
// private void DetectContentType(out ContentTypeEnum contentType, byte[] binaryData)
85-
// {
86-
// contentType = ContentTypeEnum.ContentTypeUnknown;
87-
// try
88-
// {
89-
// using (MemoryStream imgStream = new MemoryStream(binaryData))
90-
// {
91-
// using (Bitmap bitmap = new Bitmap(imgStream))
92-
// {
93-
// if (bitmap.RawFormat.Equals(ImageFormat.Jpeg))
94-
// {
95-
// contentType = ContentTypeEnum.ContentTypeJpeg;
96-
// }
97-
// else if (bitmap.RawFormat.Equals(ImageFormat.Png))
98-
// {
99-
// contentType = ContentTypeEnum.ContentTypePng;
100-
// }
101-
// else if (bitmap.RawFormat.Equals(ImageFormat.Gif))
102-
// {
103-
// contentType = ContentTypeEnum.ContentTypeGif;
104-
// }
105-
// }
106-
//}
107-
// }
108-
// catch (Exception ex)
109-
// {
110-
// throw new Exception(string.Format("Error during image type detection: {0}",ex),ex);
111-
// }
112-
113-
// }
11490

11591
protected string GetXContentType()
11692
{
@@ -122,9 +98,9 @@ protected string GetXContentType()
12298
return "image/png";
12399
case ContentTypeEnum.ContentTypeGif:
124100
return "image/gif";
101+
case ContentTypeEnum.ContentTypeUnknown:
125102
default:
126103
return "";
127-
128104
}
129105
}
130106

@@ -136,7 +112,6 @@ public XElement ToXML()
136112
xBinary.Value=Convert.ToBase64String(BinaryData);
137113

138114
return xBinary;
139-
140115
}
141116
}
142117
}

FBLibrary.Sample.ConsoleApp/FBLibrary.Sample.ConsoleApp.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@
1111
<ItemGroup>
1212
<ProjectReference Include="..\FB2Library\FB2Library.csproj" />
1313
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
17+
<PackageReference Include="runtime.osx.10.10-x64.CoreCompat.System.Drawing" Version="6.0.5.128" />
18+
</ItemGroup>
1419

1520
</Project>

FBLibrary.Sample.ConsoleApp/Program.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System.Drawing;
2+
using System.Drawing.Imaging;
13
using System.Xml;
24
using FB2Library;
5+
using FB2Library.Elements;
36

47
var filePath = Path.Combine("..", "..", "..", "..", "files", "test.fb2");
58
await using var fileStream = new FileStream(filePath, FileMode.Open);
@@ -9,7 +12,42 @@
912
DtdProcessing = DtdProcessing.Ignore
1013
};
1114
var loadSettings = new XmlLoadSettings(readerSettings);
15+
16+
BinaryItem.DetectContentType = binaryData =>
17+
{
18+
try
19+
{
20+
using var imgStream = new MemoryStream(binaryData);
21+
22+
// For MacOS need native libgdiplus, more:
23+
// https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only#recommended-action
24+
#pragma warning disable CA1416
25+
using var bitmap = new Bitmap(imgStream);
26+
27+
var rawFormat = bitmap.RawFormat;
28+
29+
if (rawFormat.Equals(ImageFormat.Jpeg))
30+
return ContentTypeEnum.ContentTypeJpeg;
31+
32+
if (rawFormat.Equals(ImageFormat.Png))
33+
return ContentTypeEnum.ContentTypePng;
34+
35+
if (rawFormat.Equals(ImageFormat.Gif))
36+
return ContentTypeEnum.ContentTypeGif;
37+
38+
return ContentTypeEnum.ContentTypeUnknown;
39+
40+
#pragma warning restore CA1416
41+
}
42+
catch (Exception ex)
43+
{
44+
throw new Exception($"Error during image type detection: {ex}", ex);
45+
}
46+
};
47+
48+
1249
var reader = new FB2Reader();
1350
var file = await reader.ReadAsync(fileStream, loadSettings);
1451

52+
1553
Console.WriteLine("Done");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"configProperties": {
3+
"System.Drawing.EnableUnixSupport": true
4+
}
5+
}

0 commit comments

Comments
 (0)