Refactor API.

This commit is contained in:
Lars Jung 2015-05-05 01:58:50 +02:00
parent e31028f5a9
commit 7c4f9d574c
5 changed files with 53 additions and 36 deletions

View file

@ -42,10 +42,14 @@ modulejs.define('ext/download', ['_', '$', 'core/event', 'core/location', 'core/
action: 'download',
as: name + '.' + extension,
type: type,
baseHref: location.getAbsHref(),
hrefs: _.pluck(selectedItems, 'absHref').join('|:|')
baseHref: location.getAbsHref()
};
_.each(selectedItems, function (item, idx) {
query['hrefs[' + idx + ']'] = item.absHref;
});
server.formRequest(query);
}

View file

@ -20,14 +20,16 @@ modulejs.define('ext/preview-img', ['_', '$', 'core/event', 'core/server', 'core
}
server.request({
action: 'getThumbHref',
action: 'get',
thumbs: [{
type: 'img',
href: href,
width: settings.size,
height: 0
}]
}, function (json) {
callback(json && json.href ? json.href : null);
callback(json && json.thumbs && json.thumbs[0] ? json.thumbs[0] : null);
});
}

View file

@ -7,21 +7,23 @@ modulejs.define('ext/thumbnails', ['_', 'core/event', 'core/server', 'core/setti
doc: ['x-pdf', 'x-ps'],
delay: 1,
size: 100,
exif: true
exif: false
}, allsettings.thumbnails);
function requestThumb(type, href, ratio, callback) {
server.request({
action: 'getThumbHref',
action: 'get',
thumbs: [{
type: type,
href: href,
width: Math.round(settings.size * ratio),
height: settings.size
}]
}, function (json) {
callback(json && json.href ? json.href : null);
callback(json && json.thumbs && json.thumbs[0] ? json.thumbs[0] : null);
});
}

View file

@ -14,7 +14,7 @@ class Api {
public function apply() {
$action = Util::get_request_param("action");
$supported = array("login", "logout", "get", "getThumbHref", "download");
$supported = array("login", "logout", "get", "download");
Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported));
$methodname = "on_${action}";
@ -71,25 +71,16 @@ class Api {
$response["items"] = $this->app->get_items($href, $what);
}
Util::json_exit($response);
}
private function on_getThumbHref() {
if (Util::get_request_param("thumbs", false)) {
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_href = Util::get_request_param("href");
$width = Util::get_request_param("width");
$height = Util::get_request_param("height");
$thumbs = Util::get_request_param("thumbs");
$response["thumbs"] = $this->app->get_thumbs($thumbs);
}
$thumb = new Thumb($this->app);
$thumb_href = $thumb->thumb($type, $src_href, $width, $height);
Util::json_fail(Util::ERR_FAILED, "thumbnail creation failed", $thumb_href === null);
Util::json_exit(array("href" => $thumb_href));
Util::json_exit($response);
}
@ -104,8 +95,6 @@ class Api {
$archive = new Archive($this->app);
$hrefs = explode("|:|", trim($hrefs));
set_time_limit(0);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$as\"");

View file

@ -383,4 +383,24 @@ class App {
"footerType" => $footer_type
);
}
public function get_thumbs($requests) {
$hrefs = [];
foreach ($requests as $req) {
$type = $req["type"];
$src_href = $req["href"];
$width = $req["width"];
$height = $req["height"];
$thumb = new Thumb($this);
$thumb_href = $thumb->thumb($type, $src_href, $width, $height);
$hrefs[] = $thumb_href;
}
return $hrefs;
}
}