@@ -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 ] )
0 commit comments