Class: Attachs::Storages::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/attachs/storages/local.rb

Instance Method Summary collapse

Constructor Details

#initialize(default) ⇒ Local

Returns a new instance of Local.



5
6
7
# File 'lib/attachs/storages/local.rb', line 5

def initialize(default)
  @tmp = (Rails.env.test? and !default)
end

Instance Method Details

#delete(path) ⇒ Object



35
36
37
# File 'lib/attachs/storages/local.rb', line 35

def delete(path)
  ::File.delete realpath(path)
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/attachs/storages/local.rb', line 9

def exists?(path)
  ::File.exists? realpath(path)
end

#magick(source, output, upload) {|Attachs::Magick::Image.new(realpath(source), realpath(output))| ... } ⇒ Object

Yields:



39
40
41
42
# File 'lib/attachs/storages/local.rb', line 39

def magick(source, output, upload)
  create_dir realpath(output)
  yield Attachs::Magick::Image.new(realpath(source), realpath(output))
end

#realpath(path) ⇒ Object



17
18
19
# File 'lib/attachs/storages/local.rb', line 17

def realpath(path)
  base_path.join path
end

#size(path) ⇒ Object



13
14
15
# File 'lib/attachs/storages/local.rb', line 13

def size(path)
  ::File.size realpath(path)
end

#store(upload, path) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/attachs/storages/local.rb', line 25

def store(upload, path)
  create_dir realpath(path)
  upload.rewind # Hack to avoid empty files
  ::File.open(realpath(path), 'wb') do |file|
    while chunk = upload.read(16 * 1024)
      file.write(chunk)
    end
  end
end

#url(path) ⇒ Object



21
22
23
# File 'lib/attachs/storages/local.rb', line 21

def url(path)
  ::File.join Rails.application.config.attachs.base_url, path
end