Method: IsoDoc::Function::Utils#save_dataimage

Defined in:
lib/isodoc/function/utils.rb

#save_dataimage(uri, _relative_dir = true) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/isodoc/function/utils.rb', line 177

def save_dataimage(uri, _relative_dir = true)
  %r{^data:(?<imgclass>image|application)/(?<imgtype>[^;]+);(?:charset=[^;]+;)?base64,(?<imgdata>.+)$} =~ uri
  imgtype = "emf" if emf?("#{imgclass}/#{imgtype}")
  imgtype = imgtype.sub(/\+[a-z0-9]+$/, "") # svg+xml
  imgtype = "png" unless /^[a-z0-9]+$/.match? imgtype
  imgtype == "postscript" and imgtype = "eps"
  Tempfile.open(["image", ".#{imgtype}"],
                mode: File::BINARY | File::SHARE_DELETE) do |f|
    f.binmode
    f.write(Base64.strict_decode64(imgdata))
    @tempfile_cache << f # persist to the end
    f.path
  end
end