diff --git a/app/controllers/api/projects/images_controller.rb b/app/controllers/api/projects/images_controller.rb index bb55f4ec..7833ea24 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/app/controllers/api/projects/remixes_controller.rb b/app/controllers/api/projects/remixes_controller.rb index 0eb1ba6e..6168108c 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: [:filename, :url, {content: [] }], components: %i[id name extension content index]) end end diff --git a/config/routes.rb b/config/routes.rb index f48edf7f..ac704d57 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,7 +38,7 @@ put :finished, on: :member, to: 'school_projects#set_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] diff --git a/lib/concepts/project/operations/create_remix.rb b/lib/concepts/project/operations/create_remix.rb index ef42defc..8ddd5c92 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 @@ -34,6 +35,19 @@ 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| + pp 'checking images' + pp image + if image[:content].present? + pp 'updating image' + remix.images.attach(io: StringIO.new(image[:content]), 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)