Class: Attachie::FileDriver
- Inherits:
-
Object
- Object
- Attachie::FileDriver
show all
- Defined in:
- lib/attachie/file_driver.rb
Defined Under Namespace
Classes: FileMultipartUpload
Instance Method Summary
collapse
-
#delete(name, bucket) ⇒ Object
-
#exists?(name, bucket) ⇒ Boolean
-
#initialize(base_path) ⇒ FileDriver
constructor
A new instance of FileDriver.
-
#path_for(name, bucket) ⇒ Object
-
#store(name, data_or_io, bucket, options = {}) ⇒ Object
-
#store_multipart(name, bucket, options = {}, &block) ⇒ Object
-
#value(name, bucket) ⇒ Object
Constructor Details
#initialize(base_path) ⇒ FileDriver
Returns a new instance of FileDriver.
48
49
50
|
# File 'lib/attachie/file_driver.rb', line 48
def initialize(base_path)
@base_path = base_path
end
|
Instance Method Details
#delete(name, bucket) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/attachie/file_driver.rb', line 80
def delete(name, bucket)
path = path_for(name, bucket)
FileUtils.rm_f(path)
begin
dir = File.dirname(File.join(bucket, name))
until dir == bucket
Dir.rmdir File.join(@base_path, dir)
dir = File.dirname(dir)
end
rescue Errno::ENOTEMPTY, Errno::ENOENT
end
true
end
|
#exists?(name, bucket) ⇒ Boolean
100
101
102
|
# File 'lib/attachie/file_driver.rb', line 100
def exists?(name, bucket)
File.exists? path_for(name, bucket)
end
|
#path_for(name, bucket) ⇒ Object
104
105
106
|
# File 'lib/attachie/file_driver.rb', line 104
def path_for(name, bucket)
File.join(@base_path, bucket, name)
end
|
#store(name, data_or_io, bucket, options = {}) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/attachie/file_driver.rb', line 52
def store(name, data_or_io, bucket, options = {})
path = path_for(name, bucket)
FileUtils.mkdir_p File.dirname(path)
open(path, "wb") do |stream|
io = data_or_io.respond_to?(:read) ? data_or_io : StringIO.new(data_or_io)
while chunk = io.read(1024)
stream.write chunk
end
end
true
end
|
#store_multipart(name, bucket, options = {}, &block) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/attachie/file_driver.rb', line 68
def store_multipart(name, bucket, options = {}, &block)
path = path_for(name, bucket)
FileUtils.mkdir_p File.dirname(path)
FileMultipartUpload.new(name, bucket, self, &block)
end
|
#value(name, bucket) ⇒ Object
76
77
78
|
# File 'lib/attachie/file_driver.rb', line 76
def value(name, bucket)
File.binread path_for(name, bucket)
end
|