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', action: 'download',
as: name + '.' + extension, as: name + '.' + extension,
type: type, type: type,
baseHref: location.getAbsHref(), baseHref: location.getAbsHref()
hrefs: _.pluck(selectedItems, 'absHref').join('|:|')
}; };
_.each(selectedItems, function (item, idx) {
query['hrefs[' + idx + ']'] = item.absHref;
});
server.formRequest(query); server.formRequest(query);
} }

View file

@ -20,14 +20,16 @@ modulejs.define('ext/preview-img', ['_', '$', 'core/event', 'core/server', 'core
} }
server.request({ server.request({
action: 'getThumbHref', action: 'get',
type: 'img', thumbs: [{
href: href, type: 'img',
width: settings.size, href: href,
height: 0 width: settings.size,
height: 0
}]
}, function (json) { }, 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'], doc: ['x-pdf', 'x-ps'],
delay: 1, delay: 1,
size: 100, size: 100,
exif: true exif: false
}, allsettings.thumbnails); }, allsettings.thumbnails);
function requestThumb(type, href, ratio, callback) { function requestThumb(type, href, ratio, callback) {
server.request({ server.request({
action: 'getThumbHref', action: 'get',
type: type, thumbs: [{
href: href, type: type,
width: Math.round(settings.size * ratio), href: href,
height: settings.size width: Math.round(settings.size * ratio),
height: settings.size
}]
}, function (json) { }, 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() { public function apply() {
$action = Util::get_request_param("action"); $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)); Util::json_fail(Util::ERR_UNSUPPORTED, "unsupported action", !in_array($action, $supported));
$methodname = "on_${action}"; $methodname = "on_${action}";
@ -71,28 +71,19 @@ class Api {
$response["items"] = $this->app->get_items($href, $what); $response["items"] = $this->app->get_items($href, $what);
} }
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);
$thumbs = Util::get_request_param("thumbs");
$response["thumbs"] = $this->app->get_thumbs($thumbs);
}
Util::json_exit($response); Util::json_exit($response);
} }
private function on_getThumbHref() {
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");
$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));
}
private function on_download() { private function on_download() {
Util::json_fail(Util::ERR_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));
@ -104,8 +95,6 @@ class Api {
$archive = new Archive($this->app); $archive = new Archive($this->app);
$hrefs = explode("|:|", trim($hrefs));
set_time_limit(0); set_time_limit(0);
header("Content-Type: application/octet-stream"); header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$as\""); header("Content-Disposition: attachment; filename=\"$as\"");

View file

@ -383,4 +383,24 @@ class App {
"footerType" => $footer_type "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;
}
} }