2013-05-27 19:45:33 +00:00
|
|
|
define([
|
2014-08-19 23:25:22 +00:00
|
|
|
"jquery",
|
|
|
|
"constants",
|
|
|
|
"core",
|
|
|
|
"logger",
|
|
|
|
"eventMgr",
|
|
|
|
"settings",
|
|
|
|
"classes/AsyncTask"
|
2013-11-05 23:03:38 +00:00
|
|
|
], function($, constants, core, logger, eventMgr, settings, AsyncTask) {
|
2013-05-19 18:56:15 +00:00
|
|
|
|
2014-08-19 23:25:22 +00:00
|
|
|
var sshHelper = {};
|
2013-05-19 18:56:15 +00:00
|
|
|
|
2014-08-19 23:25:22 +00:00
|
|
|
// Listen to offline status changes
|
|
|
|
var isOffline = false;
|
|
|
|
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
|
|
|
|
isOffline = isOfflineParam;
|
|
|
|
});
|
2013-08-04 00:53:46 +00:00
|
|
|
|
2014-08-19 23:25:22 +00:00
|
|
|
// Only used to check the offline status
|
|
|
|
function connect(task) {
|
|
|
|
task.onRun(function() {
|
|
|
|
if(isOffline === true) {
|
|
|
|
return task.error(new Error("Operation not available in offline mode.|stopPublish"));
|
|
|
|
}
|
|
|
|
task.chain();
|
|
|
|
});
|
|
|
|
}
|
2013-05-19 18:56:15 +00:00
|
|
|
|
2014-08-19 23:25:22 +00:00
|
|
|
sshHelper.upload = function(host, port, username, password, path, title, content, callback) {
|
|
|
|
var task = new AsyncTask();
|
|
|
|
connect(task);
|
|
|
|
task.onRun(function() {
|
|
|
|
var url = constants.SSH_PUBLISH_URL + '?' + $.param({
|
|
|
|
host: host,
|
|
|
|
port: port,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
path: path,
|
|
|
|
title: title
|
|
|
|
});
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
data: content,
|
|
|
|
type: "POST",
|
|
|
|
timeout: constants.AJAX_TIMEOUT
|
|
|
|
}).done(function(response) {
|
|
|
|
if(response.error === undefined) {
|
|
|
|
return task.chain();
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
task.enqueue();
|
|
|
|
};
|
2013-05-19 18:56:15 +00:00
|
|
|
|
2014-08-19 23:25:22 +00:00
|
|
|
function handleError(error, task) {
|
|
|
|
var errorMsg;
|
|
|
|
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));
|
|
|
|
}
|
2013-05-29 19:55:23 +00:00
|
|
|
|
2014-08-19 23:25:22 +00:00
|
|
|
return sshHelper;
|
2013-05-19 18:56:15 +00:00
|
|
|
});
|