|
| 1 | +require "zip" |
| 2 | + |
| 3 | +class ActiveStorage::Previewer::EpubPreviewer < ActiveStorage::Previewer |
| 4 | + def self.accept?(blob) |
| 5 | + blob.content_type == "application/epub+zip" |
| 6 | + end |
| 7 | + |
| 8 | + def preview(**options) |
| 9 | + download_blob_to_tempfile do |input| |
| 10 | + cover_data = extract_cover(input) |
| 11 | + if cover_data |
| 12 | + # Detect the actual image format |
| 13 | + extension = detect_extension(cover_data) |
| 14 | + content_type = extension == ".png" ? "image/png" : "image/jpeg" |
| 15 | + |
| 16 | + Tempfile.create([ "cover", extension ]) do |output| |
| 17 | + output.binmode |
| 18 | + output.write(cover_data) |
| 19 | + output.rewind |
| 20 | + yield io: output, filename: "#{blob.filename.base}#{extension}", content_type: content_type |
| 21 | + end |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + private |
| 27 | + |
| 28 | + def extract_cover(file) |
| 29 | + Zip::File.open(file.path) do |zip| |
| 30 | + # Try the OPF method first |
| 31 | + cover_data = extract_from_opf(zip) |
| 32 | + return cover_data if cover_data |
| 33 | + |
| 34 | + # Fallback: look for common cover file names |
| 35 | + cover_data = extract_from_common_names(zip) |
| 36 | + return cover_data if cover_data |
| 37 | + |
| 38 | + nil |
| 39 | + end |
| 40 | + rescue |
| 41 | + nil |
| 42 | + end |
| 43 | + |
| 44 | + def extract_from_opf(zip) |
| 45 | + opf_path = find_opf_path(zip) |
| 46 | + return unless opf_path |
| 47 | + |
| 48 | + opf = Nokogiri::XML(zip.read(opf_path)) |
| 49 | + cover_href = find_cover_href(opf) |
| 50 | + return unless cover_href |
| 51 | + |
| 52 | + cover_path = File.join(File.dirname(opf_path), cover_href) |
| 53 | + zip.read(cover_path) |
| 54 | + rescue |
| 55 | + nil |
| 56 | + end |
| 57 | + |
| 58 | + def extract_from_common_names(zip) |
| 59 | + # Common cover file patterns |
| 60 | + patterns = [ |
| 61 | + /cover\.(jpe?g|png|gif)$/i, |
| 62 | + /front\.(jpe?g|png|gif)$/i |
| 63 | + ] |
| 64 | + |
| 65 | + # Look through all zip entries for cover images |
| 66 | + zip.entries.each do |entry| |
| 67 | + filename = File.basename(entry.name) |
| 68 | + if patterns.any? { |pattern| filename.match?(pattern) } |
| 69 | + return zip.read(entry.name) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + nil |
| 74 | + end |
| 75 | + |
| 76 | + def find_opf_path(zip) |
| 77 | + container = Nokogiri::XML(zip.read("META-INF/container.xml")) |
| 78 | + container.at("rootfile")["full-path"] |
| 79 | + rescue |
| 80 | + nil |
| 81 | + end |
| 82 | + |
| 83 | + def find_cover_href(opf) |
| 84 | + # EPUB 3: Use CSS selectors to avoid namespace issues |
| 85 | + cover = opf.css("item[properties='cover-image']").first |
| 86 | + return cover["href"] if cover |
| 87 | + |
| 88 | + # EPUB 3: properties containing "cover-image" (for cases with multiple properties) |
| 89 | + cover = opf.css("item[properties*='cover-image']").first |
| 90 | + return cover["href"] if cover |
| 91 | + |
| 92 | + # EPUB 2: meta name="cover" |
| 93 | + meta = opf.css("meta[name='cover']").first |
| 94 | + if meta |
| 95 | + cover_id = meta["content"] |
| 96 | + cover = opf.css("item[id='#{cover_id}']").first |
| 97 | + return cover["href"] if cover |
| 98 | + end |
| 99 | + |
| 100 | + # Additional fallback: look for items with "cover" in href |
| 101 | + cover = opf.css("item[href*='cover']").first |
| 102 | + return cover["href"] if cover |
| 103 | + |
| 104 | + nil |
| 105 | + end |
| 106 | + |
| 107 | + def detect_extension(data) |
| 108 | + case data[0..3] |
| 109 | + when "\x89PNG" |
| 110 | + ".png" |
| 111 | + when "\xFF\xD8\xFF" |
| 112 | + ".jpg" |
| 113 | + when "GIF8" |
| 114 | + ".gif" |
| 115 | + else |
| 116 | + ".jpg" |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments