Stackedit/res/helpers/sshHelper.js

92 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
"core",
2013-08-04 00:53:46 +00:00
"eventMgr",
2013-06-22 23:48:57 +00:00
"settings",
2013-06-16 10:47:35 +00:00
"classes/AsyncTask"
2013-08-04 00:53:46 +00:00
], function($, core, eventMgr, 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-08-04 00:53:46 +00:00
// Listen to offline status changes
var isOffline = false;
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
isOffline = isOfflineParam;
});
2013-05-29 19:55:23 +00:00
// Only used to check the offline status
function connect(task) {
task.onRun(function() {
2013-08-04 00:53:46 +00:00
if(isOffline === true) {
2013-05-29 19:55:23 +00:00
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
});