Add new CodeOcean::File#read method

* With a new method, we can simplify our code to handle differences between file.content and file.native_file.read
This commit is contained in:
Sebastian Serth
2022-08-19 22:34:24 +02:00
parent a9aab612b6
commit d762f976a8
8 changed files with 45 additions and 27 deletions

View File

@@ -28,7 +28,7 @@ class SubmissionsController < ApplicationController
stringio = Zip::OutputStream.write_buffer do |zio|
@files.each do |file|
zio.put_next_entry(file.filepath.delete_prefix('/'))
zio.write(file.content.presence || file.native_file.read)
zio.write(file.read)
end
# zip exercise description
@@ -56,11 +56,7 @@ class SubmissionsController < ApplicationController
def download_file
raise Pundit::NotAuthorizedError if @embed_options[:disable_download]
if @file.native_file?
send_file(@file.native_file.path)
else
send_data(@file.content, filename: @file.name_with_extension)
end
send_data(@file.read, filename: @file.name_with_extension)
end
def index
@@ -71,7 +67,7 @@ class SubmissionsController < ApplicationController
def render_file
if @file.native_file?
send_file(@file.native_file.path, disposition: 'inline')
send_data(@file.read, filename: @file.name_with_extension, disposition: 'inline')
else
render(plain: @file.content)
end

View File

@@ -56,6 +56,17 @@ module CodeOcean
define_method("#{role}?") { self.role == role }
end
def read
if native_file?
valid = Pathname(native_file.current_path).fnmatch? ::File.join(native_file.root, '**')
return nil unless valid
native_file.read
else
content
end
end
def ancestor_id
file_id || id
end
@@ -83,12 +94,7 @@ module CodeOcean
end
def hash_content
self.hashed_content = Digest::MD5.new.hexdigest(if file_type.try(:binary?)
::File.new(native_file.file.path,
'r').read
else
content
end)
self.hashed_content = Digest::MD5.new.hexdigest(read || '')
end
private :hash_content

View File

@@ -112,11 +112,11 @@ module ProformaService
def add_content_to_task_file(file, task_file)
if file.native_file.present?
file = ::File.new(file.native_file.file.path, 'r')
task_file.content = file.read
file_content = file.read
task_file.content = file_content
task_file.used_by_grader = false
task_file.binary = true
task_file.mimetype = MimeMagic.by_magic(file).type
task_file.mimetype = MimeMagic.by_magic(file_content).type
else
task_file.content = file.content
task_file.used_by_grader = true