Class: Attachie::FileDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/attachie/file_driver.rb

Defined Under Namespace

Classes: FileMultipartUpload

Instance Method Summary collapse

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/attachie/file_driver.rb', line 106

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
    # nothing
  end

  true
end

#download(name, bucket, dest_path) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/attachie/file_driver.rb', line 96

def download(name, bucket, dest_path)
  path = path_for(name, bucket)

  FileUtils.mkdir_p File.dirname(path)

  FileUtils.cp(path, dest_path)
rescue Errno::ENOENT => e
  raise ItemNotFound, e.message
end

#exists?(name, bucket) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/attachie/file_driver.rb', line 126

def exists?(name, bucket)
  File.exist? path_for(name, bucket)
end

#info(name, bucket) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/attachie/file_driver.rb', line 72

def info(name, bucket)
  {
    last_modified: File.mtime(path_for(name, bucket)),
    content_type: MIME::Types.of(name).first&.to_s,
    content_length: File.size(path_for(name, bucket))
  }
rescue Errno::ENOENT => e
  raise ItemNotFound, e.message
end

#path_for(name, bucket) ⇒ Object



130
131
132
# File 'lib/attachie/file_driver.rb', line 130

def path_for(name, bucket)
  File.join(@base_path, bucket, name)
end

#presigned_post(name, bucket, options = {}) ⇒ Object

Raises:



68
69
70
# File 'lib/attachie/file_driver.rb', line 68

def presigned_post(name, bucket, options = {})
  raise NotSupported, 'presigned_post is not supported in FileDriver'
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



82
83
84
85
86
87
88
# File 'lib/attachie/file_driver.rb', line 82

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



90
91
92
93
94
# File 'lib/attachie/file_driver.rb', line 90

def value(name, bucket)
  File.binread path_for(name, bucket)
rescue Errno::ENOENT => e
  raise ItemNotFound, e.message
end