-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathimages_controller.rb
251 lines (194 loc) · 7.71 KB
/
images_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# frozen_string_literal: true
class ImagesController < ApplicationController
rescue_from GdsApi::BaseError do |e|
GovukError.notify(e)
redirect_to images_path, alert_with_description: t("images.index.flashes.api_error")
end
def index
@document = Document.with_current_edition.find_by_param(params[:document_id])
end
def create
Document.transaction do
@document = Document.with_current_edition.lock.find_by_param(params[:document_id])
@issues = ::Requirements::ImageUploadChecker.new(params[:image]).issues
if @issues.any?
flash.now["alert_with_items"] = {
"title" => I18n.t!("images.index.flashes.upload_requirements"),
"items" => @issues.items,
}
render :index
return
end
current_edition = @document.current_edition
current_revision = current_edition.revision
image_revision = ImageUploadService.new(params[:image], current_revision).call(current_user)
next_revision = current_revision.build_revision_update_for_image_upsert(
image_revision,
current_user,
)
current_edition.assign_revision(next_revision, current_user).save!
PreviewService.new(current_edition).try_create_preview
redirect_to crop_image_path(params[:document_id],
image_revision.image_id,
wizard: params[:wizard])
end
end
def crop
@document, @image_revision = find_document_and_image_revision(
params[:document_id],
params[:image_id],
)
end
def update_crop
Document.transaction do # rubocop:disable Metrics/BlockLength
document, previous_image_revision = find_locked_document_and_image_revision(
params[:document_id],
params[:image_id],
)
image_revision = previous_image_revision.build_revision_update(
update_crop_params,
current_user,
)
if image_revision != previous_image_revision
current_edition = document.current_edition
current_revision = current_edition.revision
next_revision = current_revision.build_revision_update_for_image_upsert(
image_revision,
current_user,
)
current_edition.assign_revision(next_revision, current_user).save!
lead = next_revision.lead_image_revision == image_revision
TimelineEntry.create_for_revision(
entry_type: lead ? :lead_image_updated : :image_updated,
edition: current_edition,
)
# TODO remove old images from asset manager
PreviewService.new(document.current_edition).try_create_preview
end
if params[:wizard].present?
redirect_to edit_image_path(document,
image_revision.image_id,
wizard: params[:wizard])
return
end
redirect_to images_path(document),
notice: t("images.index.flashes.cropped", file: image_revision.filename)
end
end
def edit
@document, @image_revision = find_document_and_image_revision(
params[:document_id],
params[:image_id],
)
end
def update
Document.transaction do # rubocop:disable Metrics/BlockLength
@document, previous_image_revision = find_locked_document_and_image_revision(
params[:document_id],
params[:image_id],
)
@image_revision = previous_image_revision.build_revision_update(
update_params,
current_user,
)
@issues = Requirements::ImageRevisionChecker.new(@image_revision)
.pre_preview_metadata_issues
if @issues.any?
flash.now["alert_with_items"] = {
"title" => I18n.t!("images.edit.flashes.requirements"),
"items" => @issues.items,
}
render :edit
return
end
if @image_revision != previous_image_revision
current_edition = @document.current_edition
current_revision = current_edition.revision
next_revision = current_revision.build_revision_update_for_image_upsert(
@image_revision,
current_user,
)
next_revision.lead_image_revision = @image_revision if params[:wizard] == "lead_image"
current_edition.assign_revision(next_revision, current_user).save!
if params[:wizard] == "lead_image"
TimelineEntry.create_for_revision(entry_type: :lead_image_updated,
edition: current_edition)
else
TimelineEntry.create_for_revision(entry_type: :image_updated,
edition: current_edition)
end
PreviewService.new(current_edition).try_create_preview
end
if params[:wizard] == "lead_image"
redirect_to document_path(@document),
notice: t("documents.show.flashes.lead_image.added",
file: @image_revision.filename)
else
redirect_to images_path(@document),
notice: t("images.index.flashes.details_edited",
file: @image_revision.filename)
end
end
end
def destroy
Document.transaction do
document, image_revision = find_locked_document_and_image_revision(
params[:document_id],
params[:image_id],
)
current_edition = document.current_edition
current_revision = current_edition.revision
lead = image_revision == current_revision.lead_image_revision
next_revision = current_revision.build_revision_update_for_image_removed(
image_revision,
current_user,
)
current_edition.assign_revision(next_revision, current_user).save!
TimelineEntry.create_for_revision(
entry_type: lead ? :lead_image_removed : :image_removed,
edition: current_edition,
)
# TODO remove images from asset manager
PreviewService.new(current_edition).try_create_preview
if params[:wizard] == "lead_image"
redirect_to document_path(document), notice: t("documents.show.flashes.lead_image.deleted", file: image_revision.filename)
else
redirect_to images_path(document), notice: t("images.index.flashes.deleted", file: image_revision.filename)
end
end
end
def download
_, image_revision = find_document_and_image_revision(params[:document_id],
params[:image_id])
variant = image_revision.crop_variant("960x640!").processed
send_data(
image_revision.blob.service.download(variant.key),
filename: image_revision.filename,
type: image_revision.content_type,
)
end
private
def find_document_and_image_revision(document_id, image_id)
document = Document.with_current_edition.find_by_param(document_id)
image_revision = document.current_edition
.image_revisions
.find_by!(image_id: image_id)
[document, image_revision]
end
def find_locked_document_and_image_revision(document_id, image_id)
document = Document.with_current_edition.lock.find_by_param(document_id)
image_revision = document.current_edition
.image_revisions
.find_by!(image_id: image_id)
[document, image_revision]
end
def update_params
params.require(:image_revision).permit(:caption, :alt_text, :credit)
end
def update_crop_params
image_aspect_ratio = Image::HEIGHT.to_f / Image::WIDTH
crop_height = params[:crop_width].to_i * image_aspect_ratio
# FIXME: this will raise a warning because of unpermitted paramaters
params.permit(:crop_x, :crop_y, :crop_width).merge(crop_height: crop_height.to_i)
end
end