@@ -16,28 +16,28 @@ public class BinaryItem
16
16
private const string ContentTypeAttributeName = "content-type" ;
17
17
private const string IdAttributeName = "id" ;
18
18
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" ;
23
20
21
+ public static Func < byte [ ] , ContentTypeEnum > DetectContentType { get ; set ; }
24
22
25
- internal const string Fb2BinaryItemName = "binary" ;
23
+ public ContentTypeEnum ContentType { get ; set ; }
24
+ public byte [ ] BinaryData { get ; set ; }
25
+ public string Id { get ; set ; }
26
26
27
27
internal void Load ( XElement binarye )
28
28
{
29
29
if ( binarye == null )
30
30
{
31
- throw new ArgumentNullException ( " binarye" ) ;
31
+ throw new ArgumentNullException ( nameof ( binarye ) ) ;
32
32
}
33
33
34
34
if ( binarye . Name . LocalName != Fb2BinaryItemName )
35
35
{
36
- throw new ArgumentException ( "Element of wrong type passed" , " binarye" ) ;
36
+ throw new ArgumentException ( "Element of wrong type passed" , nameof ( binarye ) ) ;
37
37
}
38
38
39
39
XAttribute xContentType = binarye . Attribute ( ContentTypeAttributeName ) ;
40
- if ( ( xContentType == null ) || ( xContentType . Value == null ) )
40
+ if ( xContentType ? . Value == null )
41
41
{
42
42
throw new NullReferenceException ( "content type not defined/present" ) ;
43
43
}
@@ -58,59 +58,35 @@ internal void Load(XElement binarye)
58
58
}
59
59
60
60
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" ) ;
66
63
67
64
if ( BinaryData != null )
68
65
{
69
- BinaryData = null ;
66
+ BinaryData = null ;
70
67
}
71
68
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
+ }
82
89
}
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
- // }
114
90
115
91
protected string GetXContentType ( )
116
92
{
@@ -122,9 +98,9 @@ protected string GetXContentType()
122
98
return "image/png" ;
123
99
case ContentTypeEnum . ContentTypeGif :
124
100
return "image/gif" ;
101
+ case ContentTypeEnum . ContentTypeUnknown :
125
102
default :
126
103
return "" ;
127
-
128
104
}
129
105
}
130
106
@@ -136,7 +112,6 @@ public XElement ToXML()
136
112
xBinary . Value = Convert . ToBase64String ( BinaryData ) ;
137
113
138
114
return xBinary ;
139
-
140
115
}
141
116
}
142
117
}
0 commit comments