Skip to content

Instantly share code, notes, and snippets.

@baya
Last active September 11, 2017 13:40
Show Gist options
  • Save baya/6162781 to your computer and use it in GitHub Desktop.
Save baya/6162781 to your computer and use it in GitHub Desktop.
save base64 image
# -*- coding: utf-8 -*-
# entities的数据结构, [{name: name, image: <File:image>}, ...]
class CreateEntitySet < Dun::Activity
data_reader :entities, :contest
def call
entities.map {|e|
entity = Entity.new
entity.sn = SecureRandom.uuid
entity.name = e[:name]
entity.image = create_upload_file e[:image]
entity.contest_id = contest.id
entity.itype = contest.itype
entity.save
entity
}
end
private
def create_upload_file(image)
if image.is_a? String
tempfile = Tempfile.new("fileupload")
tempfile.binmode
tempfile.write(Base64.decode64 image)
ActionDispatch::Http::UploadedFile.new(tempfile: tempfile, filename: SecureRandom.urlsafe_base64)
else
image
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment