-
Notifications
You must be signed in to change notification settings - Fork 194
/
attachments_controller.rb
116 lines (95 loc) · 2.88 KB
/
attachments_controller.rb
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class AttachmentsController < ApplicationController
include PublicDocumentRoutesHelper
def show
if attachment_visible?
expires_headers
send_file_for_mime_type
else
fail
end
link_rel_headers
end
private
def attachment_visible?
upload_exists?(upload_path) && attachment_visibility.visible?
end
def fail
if (edition = attachment_visibility.unpublished_edition)
redirect_to edition.unpublishing.document_path
elsif (replacement = attachment_data.replaced_by)
expires_headers
redirect_to replacement.url, status: 301
elsif image? upload_path
redirect_to view_context.path_to_image('thumbnail-placeholder.png')
elsif incoming_upload_exists? upload_path
redirect_to_placeholder
else
render plain: "Not found", status: :not_found
end
end
def link_rel_headers
if (edition = attachment_visibility.visible_edition)
response.headers['Link'] = "<#{public_document_url(edition)}>; rel=\"up\""
end
end
def analytics_format
@edition.type.underscore.to_sym
end
def set_slimmer_template
slimmer_template 'chromeless'
end
def attachment_data
@attachment_data ||= AttachmentData.find(params[:id])
end
def expires_headers
if current_user.nil?
expires_in(Whitehall.uploads_cache_max_age, public: true)
else
expires_now
end
end
def upload_path
File.join(Whitehall.clean_uploads_root, path_to_attachment_or_thumbnail)
end
def file_with_extensions
[params[:file], params[:extension], params[:format]].compact.join('.')
end
def path_to_attachment_or_thumbnail
attachment_data.file.store_path(file_with_extensions)
end
def attachment_visibility
@attachment_visibility ||= AttachmentVisibility.new(attachment_data, current_user)
end
def file_is_clean?(path)
path.starts_with?(Whitehall.clean_uploads_root)
end
def image?(path)
['.jpg', '.jpeg', '.png', '.gif'].include?(File.extname(path))
end
def incoming_upload_exists?(path)
path = path.sub(Whitehall.clean_uploads_root, Whitehall.incoming_uploads_root)
File.exist?(path)
end
def mime_type_for(path)
Mime::Type.lookup_by_extension(File.extname(path).from(1).downcase)
end
def real_path_for_x_accel_mapping(potentially_symlinked_path)
File.realpath(potentially_symlinked_path)
end
def redirect_to_placeholder
# Cache is explicitly 1 minute to prevent the virus redirect beng
# cached by CDNs.
expires_in(1.minute, public: true)
redirect_to placeholder_url
end
def send_file_for_mime_type
if (mime_type = mime_type_for(upload_path))
send_file real_path_for_x_accel_mapping(upload_path), type: mime_type, disposition: 'inline'
else
send_file real_path_for_x_accel_mapping(upload_path), disposition: 'inline'
end
end
def upload_exists?(path)
File.exist?(path) && file_is_clean?(path)
end
end