Skip to content

Commit

Permalink
Rewrite file/directory existence and upload mechanisms.
Browse files Browse the repository at this point in the history
The creation of intermediate directories was removed in commit 32df530 as some ftp systems
will allow you to create a directory without the intermediate directories.

This commit is more conservative and assumes you must create each intermediate directory one at a time.

file_exists? now encapsulates issuing ftp 'NLST' with exception handling on missing file/directories.

create_directory_structure will now traverse the desired directory path and create missing intermediate directories.

Move the actual uploading into a private method.
net/ftp methods don't support Pathnames so convert them to Strings.

https://bugzilla.redhat.com/show_bug.cgi?id=1184465
  • Loading branch information
jrafanie committed Feb 6, 2015
1 parent 39bf19c commit 3f7aaec
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions vmdb/app/models/file_depot_ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ def self.validate_settings(settings)

def upload_file(file)
super
with_connection do |ftp|
with_connection do
begin
return if destination_file_exists?(ftp, destination_file)
return if file_exists?(destination_file)

create_directory_structure(ftp)
ftp.putbinaryfile(file.local_file, destination_file)
upload(file.local_file, destination_file)
rescue => err
msg = "Error '#{err.message.chomp}', writing to FTP: [#{uri}], Username: [#{authentication_userid}]"
$log.error("#{log_header(__method__)} #{msg}")
Expand All @@ -33,7 +32,6 @@ def upload_file(file)
:state => "available",
:log_uri => destination_file
)
$log.info("#{log_header(__method__)} Uploading file: #{destination_file}... Complete")
file.post_upload_tasks
end
end
Expand All @@ -43,7 +41,7 @@ def remove_file(file)
@file = file
$log.info("#{log_header(__method__)} Removing log file [#{destination_file}]...")
with_connection do |ftp|
ftp.delete(destination_file)
ftp.delete(destination_file.to_s)
end
$log.info("#{log_header(__method__)} Removing log file [#{destination_file}]...complete")
end
Expand Down Expand Up @@ -91,19 +89,26 @@ def connect(cred_hash = nil)

private

def create_directory_structure(ftp)
$log.info("MIQ(#{self.class.name}##{__method__}) Creating directory structure on server...")
ftp.mkdir(destination_path)
rescue Net::FTPPermError => err
return if err.message.to_s.strip.start_with?("521") # path already exists.
raise
def create_directory_structure(directory_path)
Pathname.new(directory_path).descend do |path|
next if file_exists?(path)

$log.info("#{log_header(__method__)} creating #{path}")
ftp.mkdir(path.to_s)
end
end

def file_exists?(file_or_directory)
!!ftp.nlst(file_or_directory.to_s)
rescue Net::FTPPermError
false
end

def destination_file_exists?(ftp, file)
$log.info("MIQ(#{self.class.name}##{__method__}) Checking for log file #{file} on server...")
result = ftp.ls(file).present?
$log.info("MIQ(#{self.class.name}##{__method__}) Found file: #{file} on server... skipping") if result
result
def upload(source, destination)
create_directory_structure(destination_path)
$log.info("#{log_header(__method__)} Uploading file: #{destination} to File Depot: #{name}...")
ftp.putbinaryfile(source, destination.to_s)
$log.info("#{log_header(__method__)} Uploading file: #{destination_file}... Complete")
end

def destination_file
Expand All @@ -116,9 +121,8 @@ def destination_path

def base_path
# uri: "ftp://ftp.example.com/incoming" => #<Pathname:incoming>
path = URI.split(URI.encode(uri))[5]
path = Pathname.new(path)
path.relative_path_from(Pathname.new("/"))
path = URI(URI.encode(uri)).path
Pathname.new(path)
end

def login_credentials
Expand Down

0 comments on commit 3f7aaec

Please sign in to comment.