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
|
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 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.ShadowSeries == book.ShadowSeries)
.Where(x => x.ShadowVolume == book.ShadowVolume)
.Select(x => x.ToComic())
.OrderBy(x => x.Number).ToList();
return new BooksList
{
Comics = context.ApplyODataUriFilter(series).Cast<Comic>(),
Name = book.ShadowSeries
};
}
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;
}
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 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();
}
}
}
|