Support webp (#69, nice)

This commit is contained in:
Alex Ling 2020-06-15 12:35:44 +00:00
parent 4c2cf64f53
commit dc084aff7c
3 changed files with 6 additions and 4 deletions

View File

@ -4,6 +4,8 @@ require "uri"
require "./util" require "./util"
require "./archive" require "./archive"
SUPPORTED_IMG_TYPES = ["image/jpeg", "image/png", "image/webp"]
struct Image struct Image
property data : Bytes property data : Bytes
property mime : String property mime : String
@ -27,7 +29,7 @@ class Entry
@size = (File.size path).humanize_bytes @size = (File.size path).humanize_bytes
file = ArchiveFile.new path file = ArchiveFile.new path
@pages = file.entries.count do |e| @pages = file.entries.count do |e|
["image/jpeg", "image/png"].includes? \ SUPPORTED_IMG_TYPES.includes? \
MIME.from_filename? e.filename MIME.from_filename? e.filename
end end
file.close file.close
@ -72,7 +74,7 @@ class Entry
ArchiveFile.open @zip_path do |file| ArchiveFile.open @zip_path do |file|
page = file.entries page = file.entries
.select { |e| .select { |e|
["image/jpeg", "image/png"].includes? \ SUPPORTED_IMG_TYPES.includes? \
MIME.from_filename? e.filename MIME.from_filename? e.filename
} }
.sort { |a, b| .sort { |a, b|

View File

@ -224,7 +224,7 @@ class APIRouter < Router
entry_id = env.params.query["entry"]? entry_id = env.params.query["entry"]?
title = @context.library.get_title(title_id).not_nil! title = @context.library.get_title(title_id).not_nil!
unless ["image/jpeg", "image/png"].includes? \ unless SUPPORTED_IMG_TYPES.includes? \
MIME.from_filename? filename MIME.from_filename? filename
raise "The uploaded image must be either JPEG or PNG" raise "The uploaded image must be either JPEG or PNG"
end end

View File

@ -72,7 +72,7 @@
<span uk-icon="icon: cloud-upload"></span> <span uk-icon="icon: cloud-upload"></span>
<span class="uk-text-middle">Upload a cover image by dropping it here or</span> <span class="uk-text-middle">Upload a cover image by dropping it here or</span>
<div uk-form-custom> <div uk-form-custom>
<input type="file" accept="image/jpeg, image/png"> <input type="file" accept="<%= SUPPORTED_IMG_TYPES.join ", " %>">
<span class="uk-link">selecting one</span> <span class="uk-link">selecting one</span>
</div> </div>
</div> </div>