/
ShokoServiceImplementationImage.cs
413 lines (364 loc) · 12.4 KB
/
ShokoServiceImplementationImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog;
using Shoko.Models.Enums;
using Shoko.Models.Interfaces;
using Shoko.Server.Extensions;
using Shoko.Server.Properties;
using Shoko.Server.Repositories;
using Mime = MimeMapping.MimeUtility;
namespace Shoko.Server;
[ApiController]
[Route("/api/Image")]
[ApiVersionNeutral]
[ApiExplorerSettings(IgnoreApi = true)]
public class ShokoServiceImplementationImage : Controller, IShokoServerImage, IHttpContextAccessor
{
public HttpContext HttpContext { get; set; }
private static Logger logger = LogManager.GetCurrentClassLogger();
[HttpGet("{imageid}/{imageType}/{thumnbnailOnly?}")]
public object GetImage(int imageid, int imageType, bool? thumnbnailOnly = false)
{
var path = GetImagePath(imageid, imageType, thumnbnailOnly);
if (string.IsNullOrEmpty(path) || !System.IO.File.Exists(path))
{
return NotFound();
}
var mime = Mime.GetMimeMapping(path);
Response.ContentType = mime;
return System.IO.File.OpenRead(path);
}
[HttpGet("WithPath/{serverImagePath}")]
public object GetImageUsingPath(string serverImagePath)
{
if (!System.IO.File.Exists(serverImagePath))
{
logger.Trace("Could not find AniDB_Cover image: {0}", serverImagePath);
return NotFound();
}
Response.ContentType = Mime.GetMimeMapping(serverImagePath);
return System.IO.File.OpenRead(serverImagePath);
}
[HttpGet("Blank")]
public object BlankImage()
{
var dta = Resources.blank;
var ms = new MemoryStream(dta);
ms.Seek(0, SeekOrigin.Begin);
Response.ContentType = "image/jpeg";
return ms;
}
[NonAction]
internal static Bitmap ReSize(Bitmap im, int width, int height)
{
var dest = new Bitmap(width, height);
using (var g = Graphics.FromImage(dest))
{
g.InterpolationMode = width >= im.Width
? InterpolationMode.HighQualityBilinear
: InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(im, 0, 0, width, height);
}
return dest;
}
[NonAction]
public Stream ResizeToRatio(Image im, double newratio)
{
double calcwidth = im.Width;
double calcheight = im.Height;
if (Math.Abs(newratio) < 0.001D)
{
var stream = new MemoryStream();
im.Save(stream, ImageFormat.Jpeg);
stream.Seek(0, SeekOrigin.Begin);
Response.ContentType = "image/jpeg";
return stream;
}
double nheight = 0;
do
{
nheight = calcwidth / newratio;
if (nheight > im.Height + 0.5F)
{
calcwidth = calcwidth * (im.Height / nheight);
}
else
{
calcheight = nheight;
}
} while (nheight > im.Height + 0.5F);
var newwidth = (int)Math.Round(calcwidth);
var newheight = (int)Math.Round(calcheight);
var x = 0;
var y = 0;
if (newwidth < im.Width)
{
x = (im.Width - newwidth) / 2;
}
if (newheight < im.Height)
{
y = (im.Height - newheight) / 2;
}
Image im2 = ReSize(new Bitmap(im), newwidth, newheight);
var g = Graphics.FromImage(im2);
g.DrawImage(im, new Rectangle(0, 0, im2.Width, im2.Height),
new Rectangle(x, y, im2.Width, im2.Height), GraphicsUnit.Pixel);
var ms = new MemoryStream();
im2.Save(ms, ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
Response.ContentType = "image/jpeg";
return ms;
}
[HttpGet("Support/{name}/{ratio}")]
public object GetSupportImage(string name, float? ratio)
{
if (string.IsNullOrEmpty(name))
{
return NotFound();
}
name = Path.GetFileNameWithoutExtension(name);
var man = Resources.ResourceManager;
var dta = (byte[])man.GetObject(name);
if (dta == null || dta.Length == 0)
{
return NotFound();
}
//Little hack
var ms = new MemoryStream(dta);
ms.Seek(0, SeekOrigin.Begin);
if (!name.Contains("404") || ratio == null || Math.Abs(ratio.Value) < 0.001D)
{
Response.ContentType = "image/png";
return ms;
}
var im = Image.FromStream(ms);
float w = im.Width;
float h = im.Height;
float nw;
float nh;
if (w <= h)
{
nw = h * ratio.Value;
if (nw < w)
{
nw = w;
nh = w / ratio.Value;
}
else
{
nh = h;
}
}
else
{
nh = w / ratio.Value;
if (nh < h)
{
nh = h;
nw = w * ratio.Value;
}
else
{
nw = w;
}
}
nw = (float)Math.Round(nw);
nh = (float)Math.Round(nh);
Image im2 = new Bitmap((int)nw, (int)nh, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(im2))
{
g.InterpolationMode = nw >= im.Width
? InterpolationMode.HighQualityBilinear
: InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
var src = new Rectangle(0, 0, im.Width, im.Height);
var dst = new Rectangle((int)((nw - w) / 2), (int)((nh - h) / 2), im.Width, im.Height);
g.DrawImage(im, dst, src, GraphicsUnit.Pixel);
}
var ms2 = new MemoryStream();
im2.Save(ms2, ImageFormat.Png);
ms2.Seek(0, SeekOrigin.Begin);
ms.Dispose();
Response.ContentType = "image/png";
return ms2;
}
[HttpGet("Thumb/{imageId}/{imageType}/{ratio}")]
public object GetThumb(int imageId, int imageType, float ratio)
{
var m = GetImage(imageId, imageType);
if (m == NotFound())
{
return m;
}
if (!(m is Stream image))
{
return NotFound();
}
using (var im = Image.FromStream(image))
{
return ResizeToRatio(im, ratio);
}
}
[HttpGet("Path/{imageId}/{imageType}/{thumnbnailOnly?}")]
public string GetImagePath(int imageId, int imageType, bool? thumnbnailOnly)
{
var it = (ImageEntityType)imageType;
switch (it)
{
case ImageEntityType.AniDB_Cover:
var anime = RepoFactory.AniDB_Anime.GetByAnimeID(imageId);
if (anime == null)
{
return null;
}
if (System.IO.File.Exists(anime.PosterPath))
{
return anime.PosterPath;
}
else
{
logger.Trace("Could not find AniDB_Cover image: {0}", anime.PosterPath);
return string.Empty;
}
case ImageEntityType.AniDB_Character:
var chr = RepoFactory.AniDB_Character.GetByID(imageId);
if (chr == null)
{
return null;
}
if (System.IO.File.Exists(chr.GetPosterPath()))
{
return chr.GetPosterPath();
}
else
{
logger.Trace("Could not find AniDB_Character image: {0}", chr.GetPosterPath());
return string.Empty;
}
case ImageEntityType.AniDB_Creator:
var creator = RepoFactory.AniDB_Seiyuu.GetByID(imageId);
if (creator == null)
{
return string.Empty;
}
if (System.IO.File.Exists(creator.GetPosterPath()))
{
return creator.GetPosterPath();
}
else
{
logger.Trace("Could not find AniDB_Creator image: {0}", creator.GetPosterPath());
return string.Empty;
}
case ImageEntityType.TvDB_Cover:
var poster = RepoFactory.TvDB_ImagePoster.GetByID(imageId);
if (poster == null)
{
return null;
}
if (System.IO.File.Exists(poster.GetFullImagePath()))
{
return poster.GetFullImagePath();
}
else
{
logger.Trace("Could not find TvDB_Cover image: {0}", poster.GetFullImagePath());
return string.Empty;
}
case ImageEntityType.TvDB_Banner:
var wideBanner = RepoFactory.TvDB_ImageWideBanner.GetByID(imageId);
if (wideBanner == null)
{
return null;
}
if (System.IO.File.Exists(wideBanner.GetFullImagePath()))
{
return wideBanner.GetFullImagePath();
}
else
{
logger.Trace("Could not find TvDB_Banner image: {0}", wideBanner.GetFullImagePath());
return string.Empty;
}
case ImageEntityType.TvDB_Episode:
var ep = RepoFactory.TvDB_Episode.GetByID(imageId);
if (ep == null)
{
return null;
}
if (System.IO.File.Exists(ep.GetFullImagePath()))
{
return ep.GetFullImagePath();
}
else
{
logger.Trace("Could not find TvDB_Episode image: {0}", ep.GetFullImagePath());
return string.Empty;
}
case ImageEntityType.TvDB_FanArt:
var fanart = RepoFactory.TvDB_ImageFanart.GetByID(imageId);
if (fanart == null)
{
return null;
}
if (System.IO.File.Exists(fanart.GetFullImagePath()))
{
return fanart.GetFullImagePath();
}
logger.Trace("Could not find TvDB_FanArt image: {0}", fanart.GetFullImagePath());
return string.Empty;
case ImageEntityType.MovieDB_Poster:
var mPoster = RepoFactory.MovieDB_Poster.GetByID(imageId);
if (mPoster == null)
{
return null;
}
// now find only the original size
mPoster = RepoFactory.MovieDB_Poster.GetByOnlineID(mPoster.URL);
if (mPoster == null)
{
return null;
}
if (System.IO.File.Exists(mPoster.GetFullImagePath()))
{
return mPoster.GetFullImagePath();
}
else
{
logger.Trace("Could not find MovieDB_Poster image: {0}", mPoster.GetFullImagePath());
return string.Empty;
}
case ImageEntityType.MovieDB_FanArt:
var mFanart = RepoFactory.MovieDB_Fanart.GetByID(imageId);
if (mFanart == null)
{
return null;
}
mFanart = RepoFactory.MovieDB_Fanart.GetByOnlineID(mFanart.URL);
if (mFanart == null)
{
return null;
}
if (System.IO.File.Exists(mFanart.GetFullImagePath()))
{
return mFanart.GetFullImagePath();
}
else
{
logger.Trace("Could not find MovieDB_FanArt image: {0}", mFanart.GetFullImagePath());
return string.Empty;
}
default:
return string.Empty;
}
}
}