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

View File

@ -56,6 +56,17 @@ module CodeOcean
define_method("#{role}?") { self.role == role } define_method("#{role}?") { self.role == role }
end 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 def ancestor_id
file_id || id file_id || id
end end
@ -83,12 +94,7 @@ module CodeOcean
end end
def hash_content def hash_content
self.hashed_content = Digest::MD5.new.hexdigest(if file_type.try(:binary?) self.hashed_content = Digest::MD5.new.hexdigest(read || '')
::File.new(native_file.file.path,
'r').read
else
content
end)
end end
private :hash_content private :hash_content

View File

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

View File

@ -93,14 +93,10 @@ class Runner::Strategy::DockerContainerPool < Runner::Strategy
end end
local_file_path = local_path(file.filepath) local_file_path = local_path(file.filepath)
if file.file_type.binary? begin
FileUtils.cp(file.native_file.path, local_file_path) File.write(local_file_path, file.read)
else rescue IOError => e
begin raise Runner::Error::WorkspaceError.new("Could not create file #{file.filepath}: #{e.inspect}")
File.write(local_file_path, file.content)
rescue IOError => e
raise Runner::Error::WorkspaceError.new("Could not create file #{file.filepath}: #{e.inspect}")
end
end end
end end
FileUtils.chmod_R('+rwtX', local_workspace_path) FileUtils.chmod_R('+rwtX', local_workspace_path)

View File

@ -116,7 +116,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
copy = files.map do |file| copy = files.map do |file|
{ {
path: file.filepath, path: file.filepath,
content: Base64.strict_encode64(file.content.presence || file.native_file.read || ''), content: Base64.strict_encode64(file.read || ''),
} }
end end

View File

@ -131,7 +131,7 @@ describe SubmissionsController do
expect_http_status(:ok) expect_http_status(:ok)
it 'renders the file content' do it 'renders the file content' do
expect(response.body).to eq(file.native_file.read) expect(response.body).to eq(file.read)
end end
end end

View File

@ -160,7 +160,7 @@ describe Runner::Strategy::DockerContainerPool do
let(:files) { [build(:file, :image)] } let(:files) { [build(:file, :image)] }
it 'copies the file inside the workspace' do it 'copies the file inside the workspace' do
expect(FileUtils).to receive(:cp).with(files.first.native_file.path, local_path.join(files.first.filepath)) expect(File).to receive(:write).with(local_path.join(files.first.filepath), files.first.read)
container_pool.copy_files(files) container_pool.copy_files(files)
end end
end end

View File

@ -56,4 +56,24 @@ describe CodeOcean::File do
expect(file.errors[:weight]).to be_present expect(file.errors[:weight]).to be_present
end end
end end
context 'with a native file' do
let(:file) { create(:file, :image) }
after { file.native_file.remove! }
context 'when the path has not been modified' do
it 'reads the native file' do
expect(file.read).to be_present
end
end
context 'when the path has been modified' do
before { file.update(native_file: '../../../../secrets.yml') }
it 'does not read the native file' do
expect(file.read).not_to be_present
end
end
end
end end