Module: SequelImage::SingletonMethods

Defined in:
lib/ramaze/contrib/sequel/image.rb

Instance Method Summary collapse

Instance Method Details

#store(file, uid, hash = {}) ⇒ Object

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ramaze/contrib/sequel/image.rb', line 103

def store(file, uid, hash = {})
  image = new(hash)

  type     = file[:type]
  filename = file[:filename]
  tempfile = file[:tempfile]
  raise ArgumentError, 'Empty tempfile' if tempfile.size == 0

  ext         = Rack::Mime::MIME_TYPES.invert[type]
  image.mime  = type
  target_name = image.next_name(File.basename(filename, File.extname(filename)), ext)
  target_path = File.join(image.public_root, image.path, target_name)

  FileUtils.mkdir_p(File.dirname(target_path))
  FileUtils.cp(tempfile.path, target_path)

  image.original = target_path
  image.save
end