Refactor code.

This commit is contained in:
Lars Jung 2015-05-02 23:38:47 +02:00
parent 9bdf86fc24
commit 46b335eb9f
7 changed files with 21 additions and 36 deletions

View file

@ -95,7 +95,7 @@ modulejs.define('core/location', ['_', 'modernizr', 'core/event', 'core/settings
_.each(json.items, function (jsonItem) {
var e = Item.get(jsonItem.absHref, jsonItem.time, jsonItem.size, jsonItem.is_managed, jsonItem.content, jsonItem.md5, jsonItem.sha1);
var e = Item.get(jsonItem.absHref, jsonItem.time, jsonItem.size, jsonItem.isManaged, jsonItem.content);
found[e.absHref] = true;
});

View file

@ -27,7 +27,7 @@ modulejs.define('ext/preview-img', ['_', '$', 'core/event', 'core/server', 'core
height: 0
}, function (json) {
callback(json && json.code === 'RC_SUCCESS' ? json.absHref : null);
callback(json && json.absHref ? json.absHref : null);
});
}

View file

@ -21,7 +21,7 @@ modulejs.define('ext/thumbnails', ['_', 'core/event', 'core/server', 'core/setti
height: settings.size
}, function (json) {
callback(json && json.code === 'RC_SUCCESS' ? json.absHref : null);
callback(json && json.absHref ? json.absHref : null);
});
}

View file

@ -46,7 +46,7 @@ modulejs.define('model/item', ['_', 'core/event', 'core/location', 'core/server'
}
}
function getItem(absHref, time, size, isManaged, isContentFetched, md5, sha1) {
function getItem(absHref, time, size, isManaged, isContentFetched) {
absHref = location.forceEncoding(absHref);
@ -68,12 +68,6 @@ modulejs.define('model/item', ['_', 'core/event', 'core/location', 'core/server'
if (isContentFetched) {
self.isContentFetched = true;
}
if (md5) {
self.md5 = md5;
}
if (sha1) {
self.sha1 = sha1;
}
return self;
}
@ -111,7 +105,7 @@ modulejs.define('model/item', ['_', 'core/event', 'core/location', 'core/server'
if (response.items) {
_.each(response.items, function (item) {
getItem(item.absHref, item.time, item.size, item.is_managed, item.content, item.md5, item.sha1);
getItem(item.absHref, item.time, item.size, item.isManaged, item.content);
});
}

View file

@ -15,7 +15,7 @@ class Api {
$action = Util::get_request_param("action");
$supported = array("login", "logout", "get", "getThumbHref", "download");
Util::json_fail(Util::RC_UNSUPPORTED, "unsupported action", !in_array($action, $supported));
Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported));
$methodname = "on_${action}";
$this->$methodname();
@ -26,14 +26,14 @@ class Api {
$pass = Util::get_request_param("pass");
$_SESSION[AS_ADMIN_SESSION_KEY] = sha1($pass) === PASSHASH;
Util::json_exit(array("as_admin" => $_SESSION[AS_ADMIN_SESSION_KEY]));
Util::json_exit(array("asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]));
}
private function on_logout() {
$_SESSION[AS_ADMIN_SESSION_KEY] = false;
Util::json_exit(array("as_admin" => $_SESSION[AS_ADMIN_SESSION_KEY]));
Util::json_exit(array("asAdmin" => $_SESSION[AS_ADMIN_SESSION_KEY]));
}
@ -76,8 +76,8 @@ class Api {
private function on_getThumbHref() {
Util::json_fail(Util::RC_DISABLED, "thumbnails disabled", !$this->app->get_option("thumbnails.enabled", false));
Util::json_fail(Util::RC_UNSUPPORTED, "thumbnails not supported", !HAS_PHP_JPEG);
Util::json_fail(Util::ERR_DISABLED, "thumbnails disabled", !$this->app->get_option("thumbnails.enabled", false));
Util::json_fail(Util::ERR_UNSUPPORTED, "thumbnails not supported", !HAS_PHP_JPEG);
$type = Util::get_request_param("type");
$src_url = Util::get_request_param("href");
@ -86,7 +86,7 @@ class Api {
$thumb = new Thumb($this->app);
$thumb_url = $thumb->thumb($type, $src_url, $width, $height);
Util::json_fail(Util::RC_FAILED, "thumbnail creation failed", $thumb_url === null);
Util::json_fail(Util::ERR_FAILED, "thumbnail creation failed", $thumb_url === null);
Util::json_exit(array("absHref" => $thumb_url));
}
@ -94,7 +94,7 @@ class Api {
private function on_download() {
Util::json_fail(Util::RC_DISABLED, "downloads disabled", !$this->app->get_option("download.enabled", false));
Util::json_fail(Util::ERR_DISABLED, "downloads disabled", !$this->app->get_option("download.enabled", false));
$as = Util::get_request_param("as");
$type = Util::get_request_param("type");
@ -110,7 +110,7 @@ class Api {
header("Connection: close");
$rc = $archive->output($type, $hrefs);
Util::json_fail(Util::RC_FAILED, "packaging failed", $rc !== 0);
Util::json_fail(Util::ERR_FAILED, "packaging failed", $rc !== 0);
exit;
}
}

View file

@ -37,7 +37,6 @@ class Item {
public $app;
public $path, $url, $date, $size;
public $is_folder, $is_content_fetched;
public $md5, $sha1;
private function __construct($app, $path) {
@ -62,11 +61,8 @@ class Item {
);
if ($this->is_folder) {
$obj["is_managed"] = $this->app->is_managed_url($this->url);
$obj["isManaged"] = $this->app->is_managed_url($this->url);
$obj["content"] = $this->is_content_fetched;
} else {
$obj["md5"] = $this->md5;
$obj["sha1"] = $this->sha1;
}
return $obj;

View file

@ -3,11 +3,10 @@
class Util {
const RC_SUCCESS = "RC_SUCCESS";
const RC_MISSING_PARAM = "RC_MISSING_PARAM";
const RC_FAILED = "RC_FAILED";
const RC_DISABLED = "RC_DISABLED";
const RC_UNSUPPORTED = "RC_UNSUPPORTED";
const ERR_MISSING_PARAM = "ERR_MISSING_PARAM";
const ERR_FAILED = "ERR_FAILED";
const ERR_DISABLED = "ERR_DISABLED";
const ERR_UNSUPPORTED = "ERR_UNSUPPORTED";
public static function normalize_path($path, $trailing_slash = false) {
@ -19,20 +18,16 @@ class Util {
public static function json_exit($obj = array()) {
if (!isset($obj["code"])) {
$obj["code"] = Util::RC_SUCCESS;
}
header("Content-type: application/json;charset=utf-8");
echo json_encode($obj);
exit;
}
public static function json_fail($code, $msg = "", $cond = true) {
public static function json_fail($err, $msg = "", $cond = true) {
if ($cond) {
Util::json_exit(array("code" => $code, "msg" => $msg));
Util::json_exit(array("err" => $err, "msg" => $msg));
}
}
@ -46,7 +41,7 @@ class Util {
public static function get_request_param($key, $default = null) {
if (!array_key_exists($key, $_POST)) {
Util::json_fail(Util::RC_MISSING_PARAM, "parameter '$key' is missing", $default === null);
Util::json_fail(Util::ERR_MISSING_PARAM, "parameter '$key' is missing", $default === null);
return $default;
}