public static Response GetSyncWebpStream(Comic comic, Guid id, IResponseFormatter response)
{
string tmpPath = System.IO.Path.GetTempPath();
var zipPath = comic.FilePath;
string extractPath = tmpPath + "\\" + comic.Id + "\\";
extractPath = Path.GetFullPath(extractPath);
// Check if original image is in the cache.
string fileName = Path.GetFileName(zipPath);
MemoryStream cbz_stream = null;
cbz_stream = ImageCache.Instance.LoadFromCache(fileName, false, true);
var comic_xml = new MemoryStream();
var encoder = new SimpleEncoder();
if (cbz_stream == null)
{
//Opening ComicArchive and getting ComicInfo.xml for use in new cbz file
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (var entry in archive.Entries)
{
if (entry.FullName.Equals("ComicInfo.xml", StringComparison.OrdinalIgnoreCase))
{
using (var entry_stream = entry.Open())
{
entry_stream.CopyTo(comic_xml);
}
}
}
}
comic_xml.Seek(0, SeekOrigin.Begin);
var ms = new MemoryStream();
using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var demoFile = zipArchive.CreateEntry("ComicInfo.xml");
using (var entreyStream = demoFile.Open())
{
comic_xml.CopyTo(entreyStream);
}
for (int i = 0; i <= comic.PageCount - 1; i++)
{
MemoryStream stream = null;
string org_filename = string.Format("{0}-p{1}.jpg", id, i);
stream = ImageCache.Instance.LoadFromCache(org_filename, false, false);
if (stream == null)
{
// Image is not in the cache, get it via ComicRack.
var bytes = BCR.GetPageImageBytes(id, i);
if (bytes == null)
{
return(HttpStatusCode.NotFound);
}
stream = new MemoryStream(bytes);
// Always save the original page to the cache
ImageCache.Instance.SaveToCache(org_filename, stream, false, false);
}
stream.Seek(0, SeekOrigin.Begin);
Bitmap image = new Bitmap(stream);
var result = i.ToString().PadLeft(5, '0');
string webpFileName = string.Format("P{0}.webp", result);
string combined = Path.Combine(extractPath, webpFileName);
var entry = zipArchive.CreateEntry(webpFileName, CompressionLevel.Fastest);
Int32 webpquality = Database.Instance.GlobalSettings.webp_quality;
using (var entryStream = entry.Open())
{
try {
encoder.Encode(image, entryStream, webpquality);
}
catch //(Exception e)
{
// MessageBox.Show(e.ToString());
}
}
stream.Dispose();
}
}
ImageCache.Instance.SaveToCache(fileName, ms, false, true);
ms.Seek(0, SeekOrigin.Begin);
StreamResponse resp = new StreamResponse(() => ms, "application/zip");
return(resp
.WithHeader("Content-Disposition", "attachment; filename=" + fileName)
.AsAttachment(fileName, "application/zip"));
}
else
{
StreamResponse resp = new StreamResponse(() => cbz_stream, "application/zip");
return(resp
.WithHeader("Content-Disposition", "attachment; filename=" + fileName)
.AsAttachment(fileName, "application/zip"));
}
}