-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimages_controller.rb
More file actions
53 lines (43 loc) · 1.42 KB
/
images_controller.rb
File metadata and controls
53 lines (43 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class ImagesController < ApplicationController
include ActionController::MimeResponds
include JSONAPI::Pagination
before_action :require_galc_admin!, only: %i[create destroy]
before_action :set_image, except: %i[create]
# TODO: do we really want/need to be sending images from the Rails app?
# GET /images/1
def show
# TODO: store checksums, send ETags
respond_to do |format|
format.jsonapi { render jsonapi: @image } # TODO: add serializer
format.jpeg { send_file @image.file_path, type: 'image/jpeg', disposition: 'inline' }
end
end
def thumbnail
# TODO: store checksums, send ETags
respond_to do |format|
format.jpeg { send_file @image.thumbnail_path, type: 'image/jpeg', disposition: 'inline' }
end
end
# POST /images
#
# NOTE: In an ideal world we wouldn't process images synchronously,
# but it's safe enough given only admins can do it
def create
file = params.require(:file)
@image = Image.from_uploaded_file(file)
render plain: @image.id.to_s
end
# DELETE /images/1
def destroy
@image.destroy
rescue ActiveRecord::DeleteRestrictionError => e
item_details = @image.items.pluck(:artist, :title).map { |a, t| "#{a}, #{t.inspect}" }.join('; ')
e.message << ": #{item_details}"
raise
end
# TODO: add test to enforce there's no route for PUT/PATCH
private
def set_image
@image = Image.find(params[:id])
end
end