Skip to content

Image write support #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions app/controllers/api/projects/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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])
render '/api/projects/images', formats: [:json]
end
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/projects/remixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 18 additions & 3 deletions lib/concepts/project/operations/create_remix.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'base64'

class Project
class CreateRemix
class << self
Expand All @@ -11,7 +13,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

Expand All @@ -31,8 +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)
params[:image_list].each do |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|
Expand Down Expand Up @@ -61,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
7 changes: 6 additions & 1 deletion spec/concepts/project/create_remix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down