diff --git a/app/services/proforma_service/convert_exercise_to_task.rb b/app/services/proforma_service/convert_exercise_to_task.rb index 7a44c5aa..ce861120 100644 --- a/app/services/proforma_service/convert_exercise_to_task.rb +++ b/app/services/proforma_service/convert_exercise_to_task.rb @@ -86,27 +86,29 @@ module ProformaService end def task_file(file) - Proforma::TaskFile.new( - { - id: file.id, - filename: file.path.present? && file.path != '.' ? ::File.join(file.path, file.name_with_extension) : file.name_with_extension, - usage_by_lms: file.read_only ? 'display' : 'edit', - visible: file.hidden ? 'no' : 'yes', - internal_description: file.role || 'regular_file' - }.tap do |params| - if file.native_file.present? - file = ::File.new(file.native_file.file.path, 'r') - params[:content] = file.read - params[:used_by_grader] = false - params[:binary] = true - params[:mimetype] = MimeMagic.by_magic(file).type - else - params[:content] = file.content - params[:used_by_grader] = true - params[:binary] = false - end - end + task_file = Proforma::TaskFile.new( + id: file.id, + filename: file.path.present? && file.path != '.' ? ::File.join(file.path, file.name_with_extension) : file.name_with_extension, + usage_by_lms: file.read_only ? 'display' : 'edit', + visible: file.hidden ? 'no' : 'yes', + internal_description: file.role || 'regular_file' ) + add_content_to_task_file(file, task_file) + task_file + end + + 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 + task_file.used_by_grader = false + task_file.binary = true + task_file.mimetype = MimeMagic.by_magic(file).type + else + task_file.content = file.content + task_file.used_by_grader = true + task_file.binary = false + end end end end diff --git a/app/services/proforma_service/convert_task_to_exercise.rb b/app/services/proforma_service/convert_task_to_exercise.rb index f87a9354..6cb89a2c 100644 --- a/app/services/proforma_service/convert_task_to_exercise.rb +++ b/app/services/proforma_service/convert_task_to_exercise.rb @@ -48,7 +48,7 @@ module ProformaService end def codeocean_file_from_task_file(file) - CodeOcean::File.new({ + codeocean_file = CodeOcean::File.new( context: @exercise, file_type: FileType.find_by(file_extension: File.extname(file.filename)), hidden: file.visible == 'no', @@ -56,13 +56,13 @@ module ProformaService read_only: file.usage_by_lms != 'edit', role: file.internal_description, path: File.dirname(file.filename).in?(['.', '']) ? nil : File.dirname(file.filename) - }.tap do |params| - if file.binary - params[:native_file] = FileIO.new(file.content.dup.force_encoding('UTF-8'), File.basename(file.filename)) - else - params[:content] = file.content - end - end) + ) + if file.binary + codeocean_file.native_file = FileIO.new(file.content.dup.force_encoding('UTF-8'), File.basename(file.filename)) + else + codeocean_file.content = file.content + end + codeocean_file end end end