FIX: import/export theme should work with uploads

This commit is contained in:
Sam
2017-11-14 16:30:23 +11:00
parent 075a458489
commit 47e4c9bb46
3 changed files with 85 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
require_dependency 'upload_creator'
require 'base64'
class Admin::ThemesController < Admin::AdminController
@@ -31,7 +32,28 @@ class Admin::ThemesController < Admin::AdminController
@theme = Theme.new(name: theme["name"], user_id: current_user.id)
theme["theme_fields"]&.each do |field|
@theme.set_field(target: field["target"], name: field["name"], value: field["value"])
if field["raw_upload"]
begin
tmp = Tempfile.new
tmp.binmode
file = Base64.decode64(field["raw_upload"])
tmp.write(file)
tmp.rewind
upload = UploadCreator.new(tmp, field["filename"]).create_for(current_user.id)
field["upload_id"] = upload.id
ensure
tmp.unlink
end
end
@theme.set_field(
target: field["target"],
name: field["name"],
value: field["value"],
type_id: field["type_id"],
upload_id: field["upload_id"]
)
end
if @theme.save
@@ -168,7 +190,7 @@ class Admin::ThemesController < Admin::AdminController
response.headers['Content-Disposition'] = "attachment; filename=#{@theme.name.parameterize}.dcstyle.json"
response.sending_file = true
render json: ThemeSerializer.new(@theme)
render json: ThemeWithEmbeddedUploadsSerializer.new(@theme, root: 'theme')
end
end