Last active
September 11, 2017 13:40
-
-
Save baya/6162781 to your computer and use it in GitHub Desktop.
save base64 image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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