Skip to content

Commit 78e6b5b

Browse files
committed
migrate exercise show and update to ActiveStorage
1 parent e37cbc8 commit 78e6b5b

File tree

7 files changed

+45
-44
lines changed

7 files changed

+45
-44
lines changed

app/controllers/code_ocean/files_controller.rb

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class FilesController < ApplicationController
66
include FileParameters
77

88
before_action :set_content_type_nosniff
9-
# Overwrite the CSP header and some default actions for the :render_protected_upload action
109
content_security_policy false, only: :render_protected_upload
1110
skip_before_action :deny_access_from_render_host, only: :render_protected_upload
1211
skip_before_action :verify_authenticity_token, only: :render_protected_upload
@@ -25,54 +24,54 @@ def show_protected_upload
2524
@file = CodeOcean::File.find(params[:id])
2625
authorize!
2726
# The `@file.name_with_extension` is assembled based on the user-selected file type, not on the actual file name stored on disk.
28-
raise Pundit::NotAuthorizedError if @embed_options[:disable_download] || @file.filepath != params[:filename] || @file.native_file.blank?
27+
raise Pundit::NotAuthorizedError if @embed_options[:disable_download] || @file.filepath != params[:filename] || @file.attachment.blank?
2928

30-
real_location = Pathname(@file.native_file.current_path).realpath
31-
send_file(real_location, type: 'application/octet-stream', filename: @file.name_with_extension, disposition: 'attachment')
29+
url = rails_blob_path(@file.attachment, disposition: 'attachment', expires_in: 5.minutes)
30+
redirect_to url, allow_other_host: true
3231
end
3332

3433
def render_protected_upload
3534
# Set @current_user with a new *learner* for Pundit checks
3635
@current_user = ExternalUser.new
3736

3837
@file = authorize AuthenticatedUrlHelper.retrieve!(CodeOcean::File, request)
39-
4038
# The `@file.name_with_extension` is assembled based on the user-selected file type, not on the actual file name stored on disk.
41-
raise Pundit::NotAuthorizedError unless @file.filepath == params[:filename] || @file.native_file.present?
39+
raise Pundit::NotAuthorizedError unless @file.filepath == params[:filename] || @file.attachment.present?
4240

43-
real_location = Pathname(@file.native_file.current_path).realpath
44-
send_file(real_location, type: @file.native_file.content_type, filename: @file.name_with_extension)
45-
end
41+
url = rails_blob_path(@file.attachment, disposition: 'inline', expires_in: 5.minutes)
4642

47-
def create
48-
@file = CodeOcean::File.new(file_params)
49-
if @file.file_template_id
50-
content = FileTemplate.find(@file.file_template_id).content
51-
content.sub! '{{file_name}}', @file.name
52-
@file.content = content
53-
end
54-
authorize!
55-
create_and_respond(object: @file, path: proc { implement_exercise_path(@file.context.exercise) })
43+
redirect_to url, allow_other_host: true
5644
end
5745

58-
def create_and_respond(options = {})
59-
@object = options[:object]
60-
respond_to do |format|
61-
if @object.save
62-
yield if block_given?
63-
path = options[:path].try(:call) || @object
64-
respond_with_valid_object(format, notice: t('shared.object_created', model: @object.class.model_name.human),
65-
path:, status: :created)
66-
else
67-
filename = "#{@object.path || ''}/#{@object.name || ''}#{@object.file_type.try(:file_extension) || ''}"
68-
format.html do
69-
flash[:danger] = t('code_ocean/files.error.filename', name: filename)
70-
redirect_to options[:path], status: :see_other
71-
end
72-
format.json { render json: @object.errors, status: :unprocessable_content }
73-
end
74-
end
75-
end
46+
# def create
47+
# @file = CodeOcean::File.new(file_params)
48+
# if @file.file_template_id
49+
# content = FileTemplate.find(@file.file_template_id).content
50+
# content.sub! '{{file_name}}', @file.name
51+
# @file.content = content
52+
# end
53+
# authorize!
54+
# create_and_respond(object: @file, path: proc { implement_exercise_path(@file.context.exercise) })
55+
# end
56+
#
57+
# def create_and_respond(options = {})
58+
# @object = options[:object]
59+
# respond_to do |format|
60+
# if @object.save
61+
# yield if block_given?
62+
# path = options[:path].try(:call) || @object
63+
# respond_with_valid_object(format, notice: t('shared.object_created', model: @object.class.model_name.human),
64+
# path:, status: :created)
65+
# else
66+
# filename = "#{@object.path || ''}/#{@object.name || ''}#{@object.file_type.try(:file_extension) || ''}"
67+
# format.html do
68+
# flash[:danger] = t('code_ocean/files.error.filename', name: filename)
69+
# redirect_to options[:path], status: :see_other
70+
# end
71+
# format.json { render json: @object.errors, status: :unprocessable_content }
72+
# end
73+
# end
74+
# end
7675

7776
def destroy
7877
@file = CodeOcean::File.find(params[:id])

app/controllers/concerns/file_parameters.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def reject_illegal_file_attributes(exercise, params)
3434

3535
def file_attributes
3636
%w[content context_id feedback_message file_id file_type_id hidden id name native_file path read_only role weight
37-
file_template_id hidden_feedback]
37+
file_template_id hidden_feedback attachment]
3838
end
3939
private :file_attributes
4040
end

app/controllers/exercises_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,11 @@ def exercise_params_with_tags
304304
def handle_file_uploads
305305
if exercise_params
306306
exercise_params[:files_attributes].try(:each) do |_index, file_attributes|
307-
if file_attributes[:content].respond_to?(:read)
307+
if file_attributes[:attachment].respond_to?(:read)
308308
if FileType.find_by(id: file_attributes[:file_type_id]).try(:binary?)
309-
file_attributes[:native_file] = file_attributes[:content]
310309
file_attributes[:content] = nil
311310
else
312-
file_attributes[:content] = file_attributes[:content].read.detect_encoding!.encode.delete("\x00")
311+
file_attributes[:content] = file_attributes[:attachment].read.detect_encoding!.encode.delete("\x00")
313312
end
314313
end
315314
end

app/models/code_ocean/file.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class File < ApplicationRecord
3636
has_many :events_synchronized_editor, class_name: 'Event::SynchronizedEditor'
3737
alias descendants files
3838

39+
has_one_attached :attachment
40+
3941
mount_uploader :native_file, FileUploader
4042

4143
scope :editable, -> { where(read_only: false) }

app/views/exercises/_code_field.html.slim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.mb-3
2-
= form.label(attribute, class: 'form-label')
3-
= form.text_area(attribute, class: 'code-field form-control', rows: 16, style: 'display:none;')
2+
= form.label('content', class: 'form-label')
3+
= form.text_area('content', class: 'code-field form-control', rows: 16, style: 'display:none;')
44
= render(partial: 'editor_edit', locals: {exercise: @exercise})
55
.card.border-warning.p-2.my-2
66
= form.file_field(attribute, class: 'form-control', style: 'display: inline-block;')

app/views/exercises/_file_form.html.slim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ li.card.mt-2
4848
.mb-3
4949
= f.label(:weight, class: 'form-label')
5050
= f.number_field(:weight, class: 'form-control', min: 0, step: 'any')
51-
= render('code_field', attribute: :content, form: f)
51+
= render('code_field', attribute: :attachment, form: f)
52+
/ = render('code_field', attribute: :content, form: f)

app/views/shared/_file.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
= row(label: 'code_ocean/file.hidden_feedback', value: file.hidden_feedback)
99
= row(label: 'code_ocean/file.feedback_message', value: render_markdown(file.feedback_message), class: 'm-0')
1010
= row(label: 'code_ocean/file.weight', value: file.weight)
11-
= row(label: 'code_ocean/file.content', value: file.native_file? ? link_to_if(policy(file).show?, file.native_file.file.filename, protected_upload_path(id: file.id, filename: file.filepath)) : code_tag(file.content, file.file_type.programming_language))
11+
= row(label: 'code_ocean/file.content', value: file.attachment.attached? ? link_to_if(policy(file).show?, file.attachment.filename, protected_upload_path(id: file.id, filename: file.filepath)) : code_tag(file.content, file.file_type.programming_language))

0 commit comments

Comments
 (0)