Stackedit/js/helpers/sshHelper.js

85 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
"core",
2013-06-22 23:48:57 +00:00
"settings",
2013-06-16 10:47:35 +00:00
"classes/AsyncTask"
2013-06-22 23:48:57 +00:00
], function($, core, settings, AsyncTask) {
2013-05-19 18:56:15 +00:00
2013-05-29 19:55:23 +00:00
var sshHelper = {};
2013-05-19 18:56:15 +00:00
2013-05-29 19:55:23 +00:00
// Only used to check the offline status
function connect(task) {
task.onRun(function() {
if(core.isOffline === true) {
task.error(new Error("Operation not available in offline mode.|stopPublish"));
return;
}
task.chain();
});
}
2013-05-19 18:56:15 +00:00
2013-05-29 19:55:23 +00:00
sshHelper.upload = function(host, port, username, password, path, title, content, callback) {
2013-06-16 10:47:35 +00:00
var task = new AsyncTask();
2013-05-29 19:55:23 +00:00
connect(task);
task.onRun(function() {
2013-06-22 23:48:57 +00:00
var url = settings.sshProxy + "upload";
2013-05-29 19:55:23 +00:00
var data = {
host: host,
port: port,
username: username,
password: password,
path: path,
title: title,
content: content
};
$.ajax({
url: url,
data: data,
type: "POST",
dataType: "json",
timeout: AJAX_TIMEOUT
}).done(function(response, textStatus, jqXHR) {
if(response.error === undefined) {
task.chain();
return;
}
handleError(response.error, task);
}).fail(function(jqXHR) {
var error = {
code: jqXHR.status,
message: jqXHR.statusText
};
handleError(error, task);
});
});
task.onSuccess(function() {
callback();
});
task.onError(function(error) {
callback(error);
});
2013-06-16 10:47:35 +00:00
task.enqueue();
2013-05-29 19:55:23 +00:00
};
2013-05-19 18:56:15 +00:00
2013-05-29 19:55:23 +00:00
function handleError(error, task) {
var errorMsg = undefined;
if(error) {
logger.error(error);
// Try to analyze the error
if(typeof error === "string") {
errorMsg = "SSH error: " + error + ".";
}
else {
errorMsg = "Could not publish on SSH server.";
if(error.code <= 0) {
core.setOffline();
errorMsg = "|stopPublish";
}
}
}
task.error(new Error(errorMsg));
}
return sshHelper;
2013-05-19 18:56:15 +00:00
});