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")); } }
public static Response GetSyncWebp(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); var encoder = new SimpleEncoder(); // 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); if (cbz_stream == null) { // If directory does not exist, create it. if (!Directory.Exists(extractPath)) { Directory.CreateDirectory(extractPath); } using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { foreach (var entry in archive.Entries) { if (entry.FullName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) { string combined = Path.Combine(extractPath, entry.FullName); entry.ExtractToFile(combined, true); } } } //System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath); // Check if original image is in the cache. 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); Int32 webpquality = Database.Instance.GlobalSettings.webp_quality; using (var saveImageStream = System.IO.File.Open(combined, FileMode.Create)) { try { encoder.Encode(image, saveImageStream, webpquality); } catch //(Exception e) { // MessageBox.Show(e.ToString()); } } stream.Dispose(); } string zipName = tmpPath + "\\" + comic.Id + ".cbz"; //check if zipfile exists if so delete it. try { if (File.Exists(zipName)) { File.Delete(zipName); } //Creates a new, blank zip file to work with - the file will be //finalized when the using statement completes using (ZipArchive newFile = ZipFile.Open(zipName, ZipArchiveMode.Create)) { foreach (string file in Directory.GetFiles(extractPath)) { newFile.CreateEntryFromFile(file, System.IO.Path.GetFileName(file)); } } } catch (Exception) { return(HttpStatusCode.NotFound); } // Always save the original page to the cache var resp_stream = new MemoryStream(File.ReadAllBytes(zipName)); ImageCache.Instance.SaveToCache(fileName, resp_stream, false, true); StreamResponse resp = new StreamResponse(() => resp_stream, "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")); } }