Class: ArchiveStorage::Adapters::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_storage/adapters/filesystem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ FileSystem

Returns a new instance of FileSystem.



13
14
15
# File 'lib/archive_storage/adapters/filesystem.rb', line 13

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/archive_storage/adapters/filesystem.rb', line 11

def config
  @config
end

Instance Method Details

#copy_from(source_adapter, source_key, target_key) ⇒ Object



42
43
44
45
# File 'lib/archive_storage/adapters/filesystem.rb', line 42

def copy_from(source_adapter, source_key, target_key)
   = source_adapter.head(source_key)
  write(target_key, source_adapter.read(source_key), content_type: .content_type, metadata: . || {})
end

#delete(key) ⇒ Object



69
70
71
72
73
# File 'lib/archive_storage/adapters/filesystem.rb', line 69

def delete(key)
  ::FileUtils.rm_f(path_for(key))
  ::FileUtils.rm_f((key))
  true
end

#download_to(key, path) ⇒ Object



35
36
37
38
39
40
# File 'lib/archive_storage/adapters/filesystem.rb', line 35

def download_to(key, path)
  raise_not_found(key) unless exists?(key)

  ::FileUtils.mkdir_p(::File.dirname(path))
  ::FileUtils.cp(path_for(key), path)
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/archive_storage/adapters/filesystem.rb', line 65

def exists?(key)
  ::File.file?(path_for(key))
end

#head(key) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/archive_storage/adapters/filesystem.rb', line 47

def head(key)
  raise_not_found(key) unless exists?(key)

  body = ::File.binread(path_for(key))
   = (key)

  checksum = Digest::MD5.hexdigest(body)

  Metadata.new(
    byte_size: body.bytesize,
    content_type: [:content_type],
    etag: nil,
    checksum: checksum,
    checksum_algorithm: "md5",
    metadata: [:metadata] || {}
  )
end

#read(key) ⇒ Object



29
30
31
32
33
# File 'lib/archive_storage/adapters/filesystem.rb', line 29

def read(key)
  raise_not_found(key) unless exists?(key)

  ::File.binread(path_for(key))
end

#upload(key, file, content_type: nil) ⇒ Object



17
18
19
# File 'lib/archive_storage/adapters/filesystem.rb', line 17

def upload(key, file, content_type: nil)
  write(key, read_upload_body(file), content_type: content_type || detect_content_type(file))
end

#url(key, **_options) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/archive_storage/adapters/filesystem.rb', line 75

def url(key, **_options)
  raise_not_found(key) unless exists?(key)

  if config.base_url
    "#{config.base_url.to_s.delete_suffix("/")}/#{key}"
  else
    path_for(key)
  end
end

#write(key, body, content_type: nil, metadata: {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/archive_storage/adapters/filesystem.rb', line 21

def write(key, body, content_type: nil, metadata: {})
  path = path_for(key)
  ::FileUtils.mkdir_p(::File.dirname(path))
  ::File.binwrite(path, body.to_s.b)
  (key, content_type: content_type, metadata: )
  true
end