From 2013237150010afd00404840ad7235ce76826089 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 22 Jan 2025 17:37:30 +0000 Subject: [PATCH 1/8] add route to allow images to be updated --- app/controllers/api/projects/images_controller.rb | 12 ++++++++++++ config/routes.rb | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/projects/images_controller.rb b/app/controllers/api/projects/images_controller.rb index bb55f4ecb..7833ea24d 100644 --- a/app/controllers/api/projects/images_controller.rb +++ b/app/controllers/api/projects/images_controller.rb @@ -17,6 +17,18 @@ def create @project.images.attach(params[:images]) render '/api/projects/images', formats: [:json] end + + def update + @project = Project.find_by!(identifier: params[:project_id]) + authorize! :update, @project + + puts params[:image] + puts 'the filename is ' + params[:image].original_filename + existing_image = @project.images.find { |i| i.blob.filename == params[:image].original_filename } + existing_image.purge + @project.images.attach(params[:image]) + render '/api/projects/images', formats: [:json] + end end end end diff --git a/config/routes.rb b/config/routes.rb index ddf306f27..3f14e74e2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,7 +36,7 @@ put :toggle_finished, on: :member, to: 'projects#toggle_finished' resource :remix, only: %i[show create], controller: 'projects/remixes' resources :remixes, only: %i[index], controller: 'projects/remixes' - resource :images, only: %i[show create], controller: 'projects/images' + resource :images, only: %i[show create update], controller: 'projects/images' end resource :project_errors, only: %i[create] From d9cd0e79f83409328724b3fbe53b9719dfbaf978 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 11 Jun 2025 16:42:16 +0100 Subject: [PATCH 2/8] allowing image list data to be passed through --- app/controllers/api/projects/remixes_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/projects/remixes_controller.rb b/app/controllers/api/projects/remixes_controller.rb index 0eb1ba6e2..ce2bb03f5 100644 --- a/app/controllers/api/projects/remixes_controller.rb +++ b/app/controllers/api/projects/remixes_controller.rb @@ -54,7 +54,7 @@ def remix_params :videos, :audio, :instructions, - image_list: [], + image_list: %i[filename url content], components: %i[id name extension content index]) end end From c17a16d3616e30a2ad6093ad5b3d6e073e43385d Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 11 Jun 2025 17:21:03 +0100 Subject: [PATCH 3/8] initial stab at getting remixes to accept new or updated images --- app/controllers/api/projects/remixes_controller.rb | 2 +- lib/concepts/project/operations/create_remix.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/projects/remixes_controller.rb b/app/controllers/api/projects/remixes_controller.rb index ce2bb03f5..6168108c4 100644 --- a/app/controllers/api/projects/remixes_controller.rb +++ b/app/controllers/api/projects/remixes_controller.rb @@ -54,7 +54,7 @@ def remix_params :videos, :audio, :instructions, - image_list: %i[filename url content], + image_list: [:filename, :url, {content: [] }], components: %i[id name extension content index]) end end diff --git a/lib/concepts/project/operations/create_remix.rb b/lib/concepts/project/operations/create_remix.rb index ef42defc3..caed486e0 100644 --- a/lib/concepts/project/operations/create_remix.rb +++ b/lib/concepts/project/operations/create_remix.rb @@ -34,6 +34,16 @@ def create_remix(original_project, params, user_id, remix_origin) original_project.images.each do |image| remix.images.attach(image.blob) end + params[:image_list].each do |image| + if image[:content].present? + remix.images.attach(io: StringIO.new(image[:content].pack('C*')), filename: image[:filename]) + end + end + + original_project.images.each do |image| + existing_image = remix.images.find { |img| img.filename.to_s == image.filename.to_s } + remix.images.attach(image.blob) unless existing_image + end original_project.videos.each do |video| remix.videos.attach(video.blob) From a3b52e258a6a200bd862df74583abb67e09f1506 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Thu, 3 Jul 2025 14:50:34 +0100 Subject: [PATCH 4/8] switching to base64 instead of uint8array --- lib/concepts/project/operations/create_remix.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/concepts/project/operations/create_remix.rb b/lib/concepts/project/operations/create_remix.rb index caed486e0..8ddd5c920 100644 --- a/lib/concepts/project/operations/create_remix.rb +++ b/lib/concepts/project/operations/create_remix.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'base64' class Project class CreateRemix @@ -35,8 +36,11 @@ def create_remix(original_project, params, user_id, remix_origin) remix.images.attach(image.blob) end params[:image_list].each do |image| + pp 'checking images' + pp image if image[:content].present? - remix.images.attach(io: StringIO.new(image[:content].pack('C*')), filename: image[:filename]) + pp 'updating image' + remix.images.attach(io: StringIO.new(image[:content]), filename: image[:filename]) end end From 14a1de053aeb6784febeae1ae8c5d4345977bc95 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 9 Jul 2025 11:56:31 +0100 Subject: [PATCH 5/8] bug fixing images when remixing --- app/controllers/api/projects/remixes_controller.rb | 2 +- lib/concepts/project/operations/create_remix.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/projects/remixes_controller.rb b/app/controllers/api/projects/remixes_controller.rb index 6168108c4..657a8c959 100644 --- a/app/controllers/api/projects/remixes_controller.rb +++ b/app/controllers/api/projects/remixes_controller.rb @@ -54,7 +54,7 @@ def remix_params :videos, :audio, :instructions, - image_list: [:filename, :url, {content: [] }], + image_list: [:filename, :url, :content], components: %i[id name extension content index]) end end diff --git a/lib/concepts/project/operations/create_remix.rb b/lib/concepts/project/operations/create_remix.rb index 8ddd5c920..e67060e5f 100644 --- a/lib/concepts/project/operations/create_remix.rb +++ b/lib/concepts/project/operations/create_remix.rb @@ -12,7 +12,7 @@ def call(params:, user_id:, original_project:, remix_origin:) response rescue StandardError => e Sentry.capture_exception(e) - response[:error] = I18n.t('errors.project.remixing.cannot_save') + response[:error] = "#{I18n.t('errors.project.remixing.cannot_save')}: #{e.message}" response end @@ -40,7 +40,7 @@ def create_remix(original_project, params, user_id, remix_origin) pp image if image[:content].present? pp 'updating image' - remix.images.attach(io: StringIO.new(image[:content]), filename: image[:filename]) + remix.images.attach(io: StringIO.new(Base64.decode64(image[:content])), filename: image[:filename]) end end From 6856ea348a23c0f7378c49da3a52240e82749d71 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 9 Jul 2025 11:58:00 +0100 Subject: [PATCH 6/8] rubocop fixes --- app/controllers/api/projects/images_controller.rb | 4 ++-- app/controllers/api/projects/remixes_controller.rb | 2 +- lib/concepts/project/operations/create_remix.rb | 8 ++------ 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/projects/images_controller.rb b/app/controllers/api/projects/images_controller.rb index 7833ea24d..7ee51e8d8 100644 --- a/app/controllers/api/projects/images_controller.rb +++ b/app/controllers/api/projects/images_controller.rb @@ -22,8 +22,8 @@ def update @project = Project.find_by!(identifier: params[:project_id]) authorize! :update, @project - puts params[:image] - puts 'the filename is ' + params[:image].original_filename + Rails.logger.debug params[:image] + Rails.logger.debug { "the filename is #{params[:image].original_filename}" } existing_image = @project.images.find { |i| i.blob.filename == params[:image].original_filename } existing_image.purge @project.images.attach(params[:image]) diff --git a/app/controllers/api/projects/remixes_controller.rb b/app/controllers/api/projects/remixes_controller.rb index 657a8c959..ce2bb03f5 100644 --- a/app/controllers/api/projects/remixes_controller.rb +++ b/app/controllers/api/projects/remixes_controller.rb @@ -54,7 +54,7 @@ def remix_params :videos, :audio, :instructions, - image_list: [:filename, :url, :content], + image_list: %i[filename url content], components: %i[id name extension content index]) end end diff --git a/lib/concepts/project/operations/create_remix.rb b/lib/concepts/project/operations/create_remix.rb index e67060e5f..f9d6f20a2 100644 --- a/lib/concepts/project/operations/create_remix.rb +++ b/lib/concepts/project/operations/create_remix.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'base64' class Project @@ -36,12 +37,7 @@ def create_remix(original_project, params, user_id, remix_origin) remix.images.attach(image.blob) end params[:image_list].each do |image| - pp 'checking images' - pp image - if image[:content].present? - pp 'updating image' - remix.images.attach(io: StringIO.new(Base64.decode64(image[:content])), filename: image[:filename]) - end + remix.images.attach(io: StringIO.new(Base64.decode64(image[:content])), filename: image[:filename]) if image[:content].present? end original_project.images.each do |image| From 29057de03d5c00a306947750141d16401c926e9c Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 9 Jul 2025 12:34:00 +0100 Subject: [PATCH 7/8] refactoring to please rubocop --- .../project/operations/create_remix.rb | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/concepts/project/operations/create_remix.rb b/lib/concepts/project/operations/create_remix.rb index f9d6f20a2..70efcef82 100644 --- a/lib/concepts/project/operations/create_remix.rb +++ b/lib/concepts/project/operations/create_remix.rb @@ -33,16 +33,13 @@ def remix_project(response, params, user_id, original_project, remix_origin) def create_remix(original_project, params, user_id, remix_origin) remix = format_project(original_project, params, user_id, remix_origin) - original_project.images.each do |image| - remix.images.attach(image.blob) - end params[:image_list].each do |image| - remix.images.attach(io: StringIO.new(Base64.decode64(image[:content])), filename: image[:filename]) if image[:content].present? - end - - original_project.images.each do |image| - existing_image = remix.images.find { |img| img.filename.to_s == image.filename.to_s } - remix.images.attach(image.blob) unless existing_image + if image[:content].present? + remix.images.attach(io: extract_image_io(image), filename: image[:filename]) + else + existing_image = find_existing_image(image, original_project) + remix.images.attach(existing_image.blob) if existing_image + end end original_project.videos.each do |video| @@ -71,6 +68,14 @@ def format_project(original_project, params, user_id, remix_origin) proj.lesson_id = nil # Only the original can have a lesson id end end + + def extract_image_io(image) + StringIO.new(Base64.decode64(image[:content])) + end + + def find_existing_image(image, original_project) + original_project.images.find { |img| img.filename.to_s == image[:filename].to_s } + end end end end From cd0f5107bdcb0ceeaf027c97e970f86ac60c58c5 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Thu, 10 Jul 2025 09:04:26 +0100 Subject: [PATCH 8/8] attempt to fix some tests --- spec/concepts/project/create_remix_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/concepts/project/create_remix_spec.rb b/spec/concepts/project/create_remix_spec.rb index 3978c47bc..b1c0e0822 100644 --- a/spec/concepts/project/create_remix_spec.rb +++ b/spec/concepts/project/create_remix_spec.rb @@ -20,7 +20,12 @@ extension: component.extension, content: 'some updated component content' } - ] + ], + image_list: original_project.images.map do |image| + { + filename: image.filename.to_s + } + end } end