Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Remove unneeded try block
Browse files Browse the repository at this point in the history
  • Loading branch information
cybre committed Aug 25, 2017
1 parent cecd1fa commit d92b06c
Showing 1 changed file with 68 additions and 72 deletions.
140 changes: 68 additions & 72 deletions src/Backend/Providers/FTP.vala
Original file line number Diff line number Diff line change
Expand Up @@ -77,87 +77,83 @@ private class FTP : IProvider
link = null;
bool status = false;

uint8[] data;

try {
uint8[] data;

try {
GLib.FileUtils.get_data(uri.split("://")[1], out data);
file_size = (int64)data.length;
} catch (GLib.FileError e) {
warning(e.message);
return false;
}
GLib.File file = GLib.File.new_for_uri(uri);
Posix.FILE? posix_file = Posix.FILE.open(file.get_path(), "r");
GLib.FileUtils.get_data(uri.split("://")[1], out data);
file_size = (int64)data.length;
} catch (GLib.FileError e) {
warning(e.message);
return false;
}
GLib.File file = GLib.File.new_for_uri(uri);
Posix.FILE? posix_file = Posix.FILE.open(file.get_path(), "r");

string file_name = file.get_basename();
string file_name = file.get_basename();

if (settings.get_boolean("timestamp-as-name")) {
GLib.DateTime datetime = new GLib.DateTime.now_local();
int64 timestamp = datetime.to_unix();
// Our files are always .png
file_name = timestamp.to_string() + ".png";
}
if (settings.get_boolean("timestamp-as-name")) {
GLib.DateTime datetime = new GLib.DateTime.now_local();
int64 timestamp = datetime.to_unix();
// Our files are always .png
file_name = timestamp.to_string() + ".png";
}

string ftp_uri = settings.get_string("ftp-uri");
string connection_mode = settings.get_string("connection-mode");
string username = settings.get_string("username");
string password = settings.get_string("password");
string website_url = settings.get_string("website-url");

string curl_url = "";
if (ftp_uri.has_suffix("/")) {
curl_url = @"$ftp_uri$file_name";
} else {
curl_url = @"$ftp_uri/$file_name";
}
string ftp_uri = settings.get_string("ftp-uri");
string connection_mode = settings.get_string("connection-mode");
string username = settings.get_string("username");
string password = settings.get_string("password");
string website_url = settings.get_string("website-url");

string curl_url = "";
if (ftp_uri.has_suffix("/")) {
curl_url = @"$ftp_uri$file_name";
} else {
curl_url = @"$ftp_uri/$file_name";
}

string image_link = "";
if (website_url.has_suffix("/")) {
image_link = @"$website_url$file_name";
} else {
image_link = @"$website_url/$file_name";
}
string image_link = "";
if (website_url.has_suffix("/")) {
image_link = @"$website_url$file_name";
} else {
image_link = @"$website_url/$file_name";
}

cancellable = new GLib.Cancellable();

handle.reset();
handle.setopt(Curl.Option.URL, curl_url);
handle.setopt(Curl.Option.UPLOAD, 1L);
handle.setopt(Curl.Option.INFILE, (void*)posix_file);
handle.setopt(Curl.Option.USERNAME, username);
handle.setopt(Curl.Option.PASSWORD, password);
handle.setopt(Curl.Option.FOLLOWLOCATION, true);
handle.setopt(Curl.Option.CONNECTTIMEOUT, 20L);
handle.setopt(Curl.Option.FTP_RESPONSE_TIMEOUT, 20L);
handle.setopt(Curl.Option.VERBOSE, 1L);
handle.setopt(Curl.Option.NOPROGRESS, 0L);
Curl.ProgressCallback d = (user_data, dltotal, dlnow, ultotal, ulnow) => {
if (_instance.cancellable.is_cancelled()) {
return 1;
}
// int64 total = _instance.file_size.abs();
// int64 now = ((int64)ulnow).abs();
// stdout.printf("total: %s\nnow: %s\n", _instance.file_size.to_string(), now.to_string());
// _instance.progress_updated(total, now);
return 0;
};
handle.setopt(Curl.Option.XFERINFOFUNCTION, d);

if (connection_mode == "active") {
handle.setopt(Curl.Option.FTPPORT, "-");
handle.setopt(Curl.Option.FTP_CREATE_MISSING_DIRS, 1);
cancellable = new GLib.Cancellable();

handle.reset();
handle.setopt(Curl.Option.URL, curl_url);
handle.setopt(Curl.Option.UPLOAD, 1L);
handle.setopt(Curl.Option.INFILE, (void*)posix_file);
handle.setopt(Curl.Option.USERNAME, username);
handle.setopt(Curl.Option.PASSWORD, password);
handle.setopt(Curl.Option.FOLLOWLOCATION, true);
handle.setopt(Curl.Option.CONNECTTIMEOUT, 20L);
handle.setopt(Curl.Option.FTP_RESPONSE_TIMEOUT, 20L);
handle.setopt(Curl.Option.VERBOSE, 1L);
handle.setopt(Curl.Option.NOPROGRESS, 0L);
Curl.ProgressCallback d = (user_data, dltotal, dlnow, ultotal, ulnow) => {
if (_instance.cancellable.is_cancelled()) {
return 1;
}
// int64 total = _instance.file_size.abs();
// int64 now = ((int64)ulnow).abs();
// stdout.printf("total: %s\nnow: %s\n", _instance.file_size.to_string(), now.to_string());
// _instance.progress_updated(total, now);
return 0;
};
handle.setopt(Curl.Option.XFERINFOFUNCTION, d);

Curl.Code? res = yield upload_ftp(handle);
if (connection_mode == "active") {
handle.setopt(Curl.Option.FTPPORT, "-");
handle.setopt(Curl.Option.FTP_CREATE_MISSING_DIRS, 1);
}

if (res == Curl.Code.OK) {
stdout.printf("\nYES\n");
link = image_link;
status = true;
}
} catch (GLib.Error e) {
warning(e.message);
Curl.Code? res = yield upload_ftp(handle);

if (res == Curl.Code.OK) {
stdout.printf("\nYES\n");
link = image_link;
status = true;
}

return status;
Expand Down

0 comments on commit d92b06c

Please sign in to comment.