Skip to content

Commit

Permalink
Support all image types (resolves #192)
Browse files Browse the repository at this point in the history
  • Loading branch information
hkalexling committed Sep 6, 2021
1 parent da8a485 commit f67e4e6
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/library/entry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class Entry

file = ArchiveFile.new @zip_path
@pages = file.entries.count do |e|
SUPPORTED_IMG_TYPES.includes? \
MIME.from_filename? e.filename
filename_is_image e.filename
end
file.close
end
Expand Down Expand Up @@ -103,8 +102,7 @@ class Entry
ArchiveFile.open @zip_path do |file|
entries = file.entries
.select { |e|
SUPPORTED_IMG_TYPES.includes? \
MIME.from_filename? e.filename
filename_is_image e.filename
}
.sort! { |a, b|
compare_numerically a.filename, b.filename
Expand Down
2 changes: 0 additions & 2 deletions src/library/types.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
SUPPORTED_IMG_TYPES = ["image/jpeg", "image/png", "image/webp"]

enum SortMethod
Auto
Title
Expand Down
5 changes: 2 additions & 3 deletions src/routes/api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,8 @@ struct APIRouter
entry_id = env.params.query["eid"]?
title = Library.default.get_title(title_id).not_nil!

unless SUPPORTED_IMG_TYPES.includes? \
MIME.from_filename? filename
raise "The uploaded image must be either JPEG or PNG"
unless filename_is_image filename
raise "The uploaded file must be an image"
end

ext = File.extname filename
Expand Down
4 changes: 4 additions & 0 deletions src/util/util.cr
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ class String
match / s.size
end
end

def filename_is_image(fn : String) : Bool
MIME.from_filename?(fn).try(&.starts_with?("image/")) || false
end
2 changes: 1 addition & 1 deletion src/views/title.html.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<span uk-icon="icon: cloud-upload"></span>
<span class="uk-text-middle">Upload a cover image by dropping it here or</span>
<div uk-form-custom>
<input type="file" accept="<%= SUPPORTED_IMG_TYPES.join ", " %>">
<input type="file" accept="image/*">
<span class="uk-link">selecting one</span>
</div>
</div>
Expand Down

0 comments on commit f67e4e6

Please sign in to comment.