summaryrefslogtreecommitdiffstats
path: root/ComicRackWebViewer/API.cs
blob: ec31013c84016c62bbc8a80db357284050fd6c8c (plain)
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
using System;
using System.Windows;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using cYo.Projects.ComicRack.Engine;
using cYo.Projects.ComicRack.Engine.IO.Provider;
using cYo.Projects.ComicRack.Viewer;
using Nancy;
using Nancy.OData;

namespace ComicRackWebViewer
{
    public static class API
    {
        public static BooksList GetIssuesOfList(string name, NancyContext context)
        {
            var list = Program.Database.ComicLists.FirstOrDefault(x => x.Name == name);
            if (list == null)
            {
                return new BooksList
                {
                    Comics = Enumerable.Empty<Comic>(),
                    Name = name
                };
            }
            return new BooksList
            {
                Comics = context.ApplyODataUriFilter(list.GetBooks().Select(x => x.ToComic())).Cast<Comic>(),
                Name = name
            };
        }
        
        public static BooksList GetIssuesOfListFromId(Guid id, NancyContext context)
        {
            var list = Program.Database.ComicLists.FindItem(id);
            if (list == null)
            {
                return new BooksList
                {
                    Comics = Enumerable.Empty<Comic>(),
                    Id = id
                };
            }
            return new BooksList
            {
                Comics = list.GetBooks().Select(x => x.ToComic()),
                Id = id
            };
        }

        public static IEnumerable<Series> GetSeries()
        {
            return Plugin.Application.GetLibraryBooks().AsSeries();
        }

        public static BooksList GetSeries(Guid id, NancyContext context)
        {
            var books = Plugin.Application.GetLibraryBooks();
            var book = books.Where(x => x.Id == id).First();
            var series = books.Where(x => x.Series == book.Series)
                .Where(x => x.Volume == book.Volume)
                .Select(x => x.ToComic())
                .OrderBy(x => x.Number).ToList();
            return new BooksList
            {
                Comics = context.ApplyODataUriFilter(series).Cast<Comic>(),
                Name = book.Series
            };
        }
        
        public static IEnumerable<Comic> GetComicsFromSeries(Guid id)
        {
            var books = Plugin.Application.GetLibraryBooks();
            var book = books.Where(x => x.Id == id).First();
            var series = books.Where(x => x.Series == book.Series)
                .Where(x => x.Volume == book.Volume)
                .Select(x => x.ToComic())
                .OrderBy(x => x.Number).ToList();
            
            return series;
        }

        public static Response GetThumbnailImage(Guid id, int page, IResponseFormatter response)
        {
            var bitmap = Image.FromStream(new MemoryStream(GetPageImageBytes(id, page)), false, false);
            double ratio = 200D / (double)bitmap.Height;
            int width = (int)(bitmap.Width * ratio);
            var callback = new Image.GetThumbnailImageAbort(() => true);
            var thumbnail = bitmap.GetThumbnailImage(width, 200, callback, IntPtr.Zero);
            MemoryStream stream = GetBytesFromImage(thumbnail);
            return response.FromStream(stream, MimeTypes.GetMimeType(".jpg"));
        }

        public static MemoryStream GetBytesFromImage(Image image)
        {
            var bitmap = new Bitmap(image);
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Jpeg);
            stream.Position = 0;
            return stream;
        }

        private static byte[] GetPageImageBytes(Guid id, int page)
        {
            try
            {
              var comic = GetComics().First(x => x.Id == id);
              var index = comic.TranslatePageToImageIndex(page);
              var provider = GetProvider(comic);
              if (provider == null)
              {
                  return null;
              }
              return provider.GetByteImage(index); // ComicRack returns the page converted to a jpeg image.....
            }
            catch (Exception e)
            {
                //MessageBox.Show(e.ToString());
                return null;
            }
        }

        public static Response GetPageImage(Guid id, int page, IResponseFormatter response)
        {
            var bytes = GetPageImageBytes(id, page);
            if (bytes == null)
            {
                return response.AsRedirect("/original/Views/spacer.png");
            }
            return response.FromStream(new MemoryStream(bytes), MimeTypes.GetMimeType(".jpg"));
        }

        
        public static Image Resize(Image img, int width, int height)
        {
            //create a new Bitmap the size of the new image
            Bitmap bmp = new Bitmap(width, height);
            //create a new graphic from the Bitmap
            Graphics graphic = Graphics.FromImage((Image)bmp);
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //draw the newly resized image
            graphic.DrawImage(img, 0, 0, width, height);
            //dispose and free up the resources
            graphic.Dispose();
            //return the image
            return (Image)bmp;
        }
        
        public static Response GetPageImage(Guid id, int page, int width, int height, BCRSettings settings, IResponseFormatter response)
        {
            string filename = string.Format("{0}-p{1}-w{2}-h{3}.jpg", id, page, width, height);
            try
            {
              MemoryStream stream = settings.LoadFromCache(filename, !(width == -1 && height == -1));
              if (stream == null)
                return response.FromStream(stream, MimeTypes.GetMimeType(".jpg"));
            }
            catch (Exception e)
            {
              // Image is not in the cache.
            }
            
            var bytes = GetPageImageBytes(id, page);
            if (bytes == null)
            {
              return HttpStatusCode.NotFound;
            }
            
            if (width == -1 && height == -1)
            {
              // Return original image.
              MemoryStream mem = new MemoryStream(bytes);
              settings.SaveToCache(filename, mem, false);
              
              return response.FromStream(mem, MimeTypes.GetMimeType(".jpg"));
            }
            else
            {
              var bitmap = Image.FromStream(new MemoryStream(bytes), false, false);
              if (width == -1)
              {
                double ratio = height / (double)bitmap.Height;
                width = (int)(bitmap.Width * ratio);
              }
              else
              if (height == -1)
              {
                double ratio = width / (double)bitmap.Width;
                height = (int)(bitmap.Height * ratio);
              }
                  
              // Use high quality resize.
              var thumbnail = Resize(bitmap, width, height);
              bitmap.Dispose();
              MemoryStream stream = GetBytesFromImage(thumbnail);
              thumbnail.Dispose();
              
              settings.SaveToCache(filename, stream, true);
                            
              return response.FromStream(stream, MimeTypes.GetMimeType(".jpg"));
            }
        }
        
        private static ImageProvider GetProvider(ComicBook comic)
        {
            var provider = comic.CreateImageProvider();
            if (provider == null)
            {
                return null;
            }
            if (provider.Status != ImageProviderStatus.Completed)
            {
                provider.Open(false);
            }
            return provider;
        }

        public static Comic GetComic(Guid id)
        {
          try
          {
            var comic = GetComics().First(x => x.Id == id);
            return comic.ToComic();
          }
          catch(Exception e)
          {
            //MessageBox.Show(e.ToString());
            return null;
          }
        }
        
        public static ComicBook GetComicBook(Guid id)
        {
          try
          {
            var comic = GetComics().First(x => x.Id == id);
            return comic;
          }
          catch(Exception e)
          {
            //MessageBox.Show(e.ToString());
            return null;
          }
        }

        public static IQueryable<ComicBook> GetComics()
        {
            return Plugin.Application.GetLibraryBooks().AsQueryable();
        }

        public static Response GetIcon(string key, IResponseFormatter response)
        {
            var image = ComicBook.PublisherIcons.GetImage(key);
            if (image == null)
            {
                return response.AsRedirect("/original/Views/spacer.png");
            }
            return response.FromStream(API.GetBytesFromImage(image), MimeTypes.GetMimeType(".jpg"));
        }

        public static IEnumerable<Publisher> GetPublishers()
        {
            return Plugin.Application.GetLibraryBooks().GroupBy(x => x.Publisher).Select(x =>
            {
                return x.GroupBy(y => y.Imprint).Select(y => new Publisher { Name = x.Key, Imprint = y.Key });
            }).SelectMany(x => x).OrderBy(x => x.Imprint).OrderBy(x => x.Name);
        }

        public static IEnumerable<Series> GetSeries(string publisher, string imprint)
        {
            IEnumerable<ComicBook> comics;
            if (string.Compare(publisher, "no publisher", true) == 0)
            {
                comics = Plugin.Application.GetLibraryBooks().Where(x => string.IsNullOrEmpty(x.Publisher));
            }
            else
            {
                comics = Plugin.Application.GetLibraryBooks().Where(x => string.Compare(publisher, x.Publisher, true) == 0);
                if (string.IsNullOrEmpty(imprint))
                {
                    comics = comics.Where(x => string.IsNullOrEmpty(x.Imprint));
                }
                comics = comics.Where(x => string.Compare(imprint, x.Imprint, true) == 0);
            }
            return comics.AsSeries();
        }
    }
}