Class: Shrine::Storage::Scp

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/storage/scp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory:, ssh_host: nil, host: nil, prefix: nil, options: %w[-q], permissions: 0600) ⇒ Scp

Returns a new instance of Scp.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shrine/storage/scp.rb', line 11

def initialize(directory:, ssh_host: nil, host: nil, prefix: nil, options: %w[-q], permissions: 0600)
  # Initializes a storage for uploading via scp.
  #
  # :directory
  # :  the path where files will be transferred to
  #
  # :ssh_host
  # :  optional user@hostname for remote scp transfers over ssh
  #
  # :host
  # :  URLs will by default be relative if `:prefix` is set, and you
  #    can use this option to set a CDN host (e.g. `//abc123.cloudfront.net`).
  #
  # :prefix
  # :  The directory relative to `directory` to which files will be stored,
  #    and it is included in the URL.
  #
  # :options
  # :  Additional arguments specific to scp
  #    https://linux.die.net/man/1/scp
  #
  # :permissions
  # :  bit pattern for permissions to set on uploaded files
  #
  @directory   = directory.chomp(File::SEPARATOR)
  @ssh_host    = ssh_host
  @host        = host.chomp(File::SEPARATOR) if host
  @prefix      = prefix.chomp(File::SEPARATOR) if prefix
  @options     = options
  @permissions = permissions
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



9
10
11
# File 'lib/shrine/storage/scp.rb', line 9

def directory
  @directory
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/shrine/storage/scp.rb', line 9

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/shrine/storage/scp.rb', line 9

def options
  @options
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



9
10
11
# File 'lib/shrine/storage/scp.rb', line 9

def permissions
  @permissions
end

#prefixObject (readonly)

Returns the value of attribute prefix.



9
10
11
# File 'lib/shrine/storage/scp.rb', line 9

def prefix
  @prefix
end

#ssh_hostObject (readonly)

Returns the value of attribute ssh_host.



9
10
11
# File 'lib/shrine/storage/scp.rb', line 9

def ssh_host
  @ssh_host
end

Instance Method Details

#clear!Object



73
74
75
76
# File 'lib/shrine/storage/scp.rb', line 73

def clear!
  file_path = path("*")
  bash "rm -rf #{file_path}"
end

#delete(id) ⇒ Object



68
69
70
71
# File 'lib/shrine/storage/scp.rb', line 68

def delete(id)
  file_path = path(id)
  bash "rm -rf #{file_path}"
end

#download(id) ⇒ Object



49
50
51
52
# File 'lib/shrine/storage/scp.rb', line 49

def download(id)
  file = scp_down(id)
  file
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/shrine/storage/scp.rb', line 59

def exists?(id)
  file_path = File.join(directory, id)
  bash "ls -la #{file_path}"
end

#open(id) ⇒ Object



54
55
56
57
# File 'lib/shrine/storage/scp.rb', line 54

def open(id)
  file = scp_down(id)
  file.tap(&:open)
end

#upload(io, id) ⇒ Object



43
44
45
46
47
# File 'lib/shrine/storage/scp.rb', line 43

def upload(io, id, **)
  file = write_io(io, id)
  scp_up(id, file.path)
  file
end

#url(id, **_options) ⇒ Object



64
65
66
# File 'lib/shrine/storage/scp.rb', line 64

def url(id, **_options)
  File.join([host, prefix, id].compact)
end