Class: Uploader::FtpService
Instance Method Summary
collapse
Methods inherited from Service
#initialize, is_registered?, register, services
Instance Method Details
#attempt_upload!(server_path, file, binary) ⇒ Object
64
65
66
67
68
69
70
71
72
|
# File 'lib/uploader/ftp_service.rb', line 64
def attempt_upload!(server_path, file, binary)
$stderr.puts "Path on server: #{server_path}"
if binary
file.binmode
@ftp.storbinary("STOR #{server_path}", file, Net::FTP::DEFAULT_BLOCKSIZE)
else
@ftp.storlines("STOR #{server_path}", file)
end
end
|
20
21
22
|
# File 'lib/uploader/ftp_service.rb', line 20
def close!
@ftp.close
end
|
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/uploader/ftp_service.rb', line 9
def connect!
$stderr.puts "Opening a persistent FTP connection to #{@config.host}..."
@ftp = Net::FTP.new
@ftp.passive = true
@ftp.connect(@config.host)
@ftp.login(@config.username, @config.password)
rescue
$strerr.puts "Something went wrong with the FTP login."
end
|
#create_remote_folder!(server_path) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/uploader/ftp_service.rb', line 74
def create_remote_folder!(server_path)
server_path.descend do |path|
begin
@ftp.mkdir(path)
rescue Net::FTPPermError
$stderr.puts "Remote folder already exists."
rescue
end
end
end
|
#initialized? ⇒ Boolean
24
25
26
|
# File 'lib/uploader/ftp_service.rb', line 24
def initialized?
not @ftp.nil?
end
|
#upload(remote_path, contents) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/uploader/ftp_service.rb', line 28
def upload(remote_path, contents)
raise UploadArgumentError if not remote_path.is_a?(Path)
if contents.is_a?(File)
upload_via_ftp(remote_path, contents)
elsif contents.is_a?(String)
upload_contents_via_ftp(remote_path, contents)
else
raise UnrecognizedContentType
end
end
|
#upload_contents_via_ftp(remote_path, contents) ⇒ Object
43
44
45
46
|
# File 'lib/uploader/ftp_service.rb', line 43
def upload_contents_via_ftp(remote_path, contents)
file = StringIO.new(contents)
upload_via_ftp(remote_path, file, false)
end
|
#upload_via_ftp(remote_path, file, binary = true) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/uploader/ftp_service.rb', line 48
def upload_via_ftp(remote_path, file, binary=true)
server_path = Path.new(@config.remote_root_path) + remote_path.relative
$stderr.puts "Opening an FTP connection for #{remote_path}"
begin
attempt_upload!(server_path, file, binary)
rescue => e
$stderr.puts "FTP Error"
$stderr.puts e.message
create_remote_folder!(server_path)
attempt_upload!(server_path, file, binary)
end
end
|