Class: CarrierWave::Storage::SFTP::File

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/sftp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ File

Returns a new instance of File.



20
21
22
# File 'lib/carrierwave/storage/sftp.rb', line 20

def initialize(uploader, base, path)
  @uploader, @base, @path = uploader, base, path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/carrierwave/storage/sftp.rb', line 18

def path
  @path
end

Instance Method Details

#content_typeObject



71
72
73
# File 'lib/carrierwave/storage/sftp.rb', line 71

def content_type
  @content_type || inferred_content_type
end

#content_type=(new_content_type) ⇒ Object



75
76
77
# File 'lib/carrierwave/storage/sftp.rb', line 75

def content_type=(new_content_type)
  @content_type = new_content_type
end

#deleteObject



79
80
81
82
83
84
# File 'lib/carrierwave/storage/sftp.rb', line 79

def delete
  connection do |sftp|
    sftp.remove!(full_path)
  end
rescue
end

#exists?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/carrierwave/storage/sftp.rb', line 60

def exists?
  size ? true : false
end

#filename(options = {}) ⇒ Object



35
36
37
# File 'lib/carrierwave/storage/sftp.rb', line 35

def filename(options = {})
  url.gsub(/.*\/(.*?$)/, '\1')
end

#readObject



64
65
66
67
68
69
# File 'lib/carrierwave/storage/sftp.rb', line 64

def read
  file = to_file
  content = file.read
  file.close
  content
end

#sizeObject



50
51
52
53
54
55
56
57
58
# File 'lib/carrierwave/storage/sftp.rb', line 50

def size
  size = nil

  connection do |sftp|
    size = sftp.stat!(full_path).size
  end

  size
end

#store(file) ⇒ Object



24
25
26
27
28
29
# File 'lib/carrierwave/storage/sftp.rb', line 24

def store(file)
  connection do |sftp|
    sftp.mkdir_p!(::File.dirname full_path)
    sftp.upload!(file.path, full_path)
  end
end

#to_fileObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/carrierwave/storage/sftp.rb', line 39

def to_file
  temp_file = Tempfile.new(filename)
  temp_file.binmode
  connection do |sftp|
    sftp.download!(full_path, temp_file)
  end
  temp_file.open
  temp_file.rewind
  temp_file
end

#urlObject



31
32
33
# File 'lib/carrierwave/storage/sftp.rb', line 31

def url
  "#{@uploader.sftp_url}/#{path}"
end