class Attachie::FileDriver

Public Class Methods

new(base_path) click to toggle source
# File lib/attachie/file_driver.rb, line 48
def initialize(base_path)
  @base_path = base_path
end

Public Instance Methods

delete(name, bucket) click to toggle source
# File lib/attachie/file_driver.rb, line 100
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) click to toggle source
# File lib/attachie/file_driver.rb, line 92
def download(name, bucket, dest_path)
  path = path_for(name, bucket)

  FileUtils.mkdir_p File.dirname(path)

  FileUtils.cp(path, dest_path)
end
exists?(name, bucket) click to toggle source
# File lib/attachie/file_driver.rb, line 120
def exists?(name, bucket)
  File.exists? path_for(name, bucket)
end
info(name, bucket) click to toggle source
# 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))
  }
end
path_for(name, bucket) click to toggle source
# File lib/attachie/file_driver.rb, line 124
def path_for(name, bucket)
  File.join(@base_path, bucket, name)
end
presigned_post(name, bucket, options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/attachie/file_driver.rb, line 80
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) click to toggle source
# File lib/attachie/file_driver.rb, line 88
def value(name, bucket)
  File.binread path_for(name, bucket)
end