1
0
Fork 0
mirror of https://github.com/Yetangitu/owncloud-apps.git synced 2025-10-02 14:49:17 +02:00

refactor, internalised serving of cover/thumbnail images to reduce feed size (shorter links)

This commit is contained in:
frankdelange 2014-12-13 18:40:47 +01:00
parent 7d068ec45f
commit 3794606760
7 changed files with 263 additions and 163 deletions

View file

@ -4,7 +4,7 @@
<name>OPDS catalog</name>
<description>Personal OPDS catalog</description>
<licence>AGPL</licence>
<version>0.3</version>
<version>0.3.1</version>
<author>Frank de Lange</author>
<requiremin>7.0</requiremin>
<shipped>true</shipped>

View file

@ -33,6 +33,18 @@ if (Config::get('enable', 'false') === 'false') {
/* id defaults to 'root' (meaning 'serve root feed') */
$id = isset($_GET['id']) ? $_GET['id'] : 'root';
/* if either pid or tid is set, serve preview image for id */
if (isset($_GET['pid'])) {
$id = (int) $_GET['pid'];
$type = 'cover';
}
if (isset($_GET['tid'])) {
$id = (int) $_GET['tid'];
$type = 'thumbnail';
}
$dir = \OC\Files\Filesystem::normalizePath(\OC\Files\Filesystem::getPath($id));
$root = Config::get('root_path', '/Library');
@ -46,10 +58,14 @@ $dirInfo = \OC\Files\Filesystem::getFileInfo($dir);
/* If requested resource is a file, serve it, otherwise produce opds feed */
switch ($dirInfo->getType()) {
case 'file':
Util::serveFile($dir,$id);
if ($type) {
Feed::servePreview($dir,$type);
} else {
Feed::serveFile($dir,$id);
}
break;
case 'dir':
Util::serveFeed($dir, $id);
Feed::serveFeed($dir, $id);
break;
default:
Util::logWarn("I don't know how to handle files of type " . $dirInfo->getType());

114
files_opds/lib/feed.php Normal file
View file

@ -0,0 +1,114 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
/**
* Feed class for OPDS
*/
class Feed
{
/**
* @brief get feed id
*
* @return string feed id
*/
public static function getFeedId() {
return Config::get('id', '');
}
/**
* @brief offer single file for download
*
* @param string $path full path to file
* @param int $id file id
*/
public static function serveFile($path, $id) {
\OCP\User::checkLoggedIn();
\OC::$session->close();
Bookshelf::add($id);
$dirName = dirname($path);
$fileName = basename($path);
\OC_Files::get($dirName, array($fileName), $_SERVER['REQUEST_METHOD'] == 'HEAD');
}
/**
* @brief serve opds feed for given directory
*
* @param string $dir full path to directory
* @param int $id requested id
*/
public static function serveFeed($dir, $id) {
if (isset($_SERVER['HTTP_ACCEPT']) && stristr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
header('Content-Type: application/atom+xml');
} else {
header('Content-Type: text/xml; charset=UTF-8');
}
$sortAttribute = 'name';
$sortDirection = false;
$defaults = new \OC_Defaults();
$tmpl = new \OCP\Template('files_opds', 'feed');
$tmpl->assign('files', Files::formatFileInfos(Files::getFiles($dir, $sortAttribute, $sortDirection)));
$tmpl->assign('bookshelf', Files::formatFileInfos(Bookshelf::get()));
$tmpl->assign('bookshelf-count', Bookshelf::count());
$tmpl->assign('feed_id', self::getFeedId());
$tmpl->assign('id', $id);
$tmpl->assign('dir', $dir);
$tmpl->assign('user', \OCP\User::getDisplayName());
$tmpl->assign('ocname', $defaults->getName());
$tmpl->printPage();
}
/**
* @brief offer preview for download
*
* if no preview exists for this file, send icon instead
*
* @param string $path full path to file
* @param string type type of preview requested
*/
public static function servePreview($path, $type) {
\OCP\User::checkLoggedIn();
\OC::$session->close();
$i = \OC\Files\Filesystem::getFileInfo($path,false);
if (\OC::$server->getPreviewManager()->isMimeSupported($i['mimetype'])) {
$preview = new \OC\Preview(\OC_User::getUser(), 'files');
$preview->setFile($path);
switch ($type) {
case 'cover':
$preview->setMaxX(Config::getApp('cover-x', '200'));
$preview->setMaxY(Config::getApp('cover-y', '200'));
break;
case 'thumbnail':
$preview->setMaxX(Config::getApp('thumb-x', '36'));
$preview->setMaxY(Config::getApp('thumb-y', '36'));
break;
}
$preview->showPreview();
} else {
// no preview, serve icon instead
$scheme = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
header("Location: "
. $scheme
. "://"
. $_SERVER['HTTP_HOST']
. \OC_Helper::mimetypeIcon($i->getMimeType())
);
/* Note: relative URL should be enough (RFC7231) but some OPDS clients
* (especially those in dedicated book readers) might not support them
*
* header("Location: " . \OC_Helper::mimetypeIcon($i->getMimeType()));
*/
}
}
}

View file

@ -32,13 +32,9 @@ class Files extends \OCA\Files\Helper
$entry['type'] = $i['type'];
if ($i['type'] === 'file') {
$entry['mimetype'] = $i['mimetype'];
$entry['preview'] = self::getPreview($i);
$entry['thumbnail'] = self::getThumbnail($i);
$entry['humansize'] = \OC_Helper::humanFileSize($i['size']);
$entry['meta'] = Util::getMeta($i['fileid']);
} else {
$entry['icon'] = self::determineIcon($i);
}
$entry['meta'] = Meta::get($i['fileid']);
}
return $entry;
}
@ -60,33 +56,6 @@ class Files extends \OCA\Files\Helper
return $files;
}
/**
* @brief get preview for file
* @param \OCP\Files\FileInfo $i
* @return string preview URL
*/
public static function getPreview($i) {
if (\OC::$server->getPreviewManager()->isMimeSupported($i['mimetype'])) {
return \OC_Helper::linkToRoute( 'core_ajax_preview', array('x' => Config::getApp('cover-x', '200'), 'y' => Config::getApp('cover-y', '200'), 'file' => \OC\Files\Filesystem::normalizePath(\OC\Files\Filesystem::getPath($i['fileid']))));
} else {
return self::determineIcon($i);
}
}
/**
* @brief get thumbnail for file
* @param \OCP\Files\FileInfo $i
* @return string preview URL
*/
public static function getThumbnail($i) {
if (\OC::$server->getPreviewManager()->isMimeSupported($i['mimetype'])) {
return \OC_Helper::linkToRoute( 'core_ajax_preview', array('x' => Config::getApp('thumb-x', '36'), 'y' => Config::getApp('thumb-y', '36'), 'file' => \OC\Files\Filesystem::normalizePath(\OC\Files\Filesystem::getPath($i['fileid']))));
} else {
return self::determineIcon($i);
}
}
/*
* @brief check if $child is a subdirectory of $parent
*

124
files_opds/lib/meta.php Normal file
View file

@ -0,0 +1,124 @@
<?php
/**
* ownCloud - Files_Opds App
*
* @author Frank de Lange
* @copyright 2014 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Files_Opds;
/**
* Meta (data) class for OPDS
*/
class Meta
{
/**
* @brief create a new metadata array for a given id
*
* @param int $id fileid
* @return array of metadata
*/
public static function create($id) {
$meta = array();
/* start with empty values, except for id. This way, files only get
* scanned once, even if they don't contain valid metadate.
*/
$meta['id'] = $id;
$meta['updated'] = date("Y-m-d\TH:i:sP");
$meta['date'] = '';
$meta['author'] = '';
$meta['title'] = '';
$meta['language'] = '';
$meta['publisher'] = '';
$meta['isbn'] = '';
$meta['copyright'] = '';
$meta['description'] = '';
$meta['subjects'] = '';
return $meta;
}
/**
* @brief load metadata from database
*
* @return array or false
*/
protected static function load($id) {
$sql = 'SELECT * FROM `*PREFIX*opds_metadata` WHERE id = ?';
$args = array($id);
$query = \OCP\DB::prepare($sql);
$result = $query->execute($args);
return ($row = $result->fetchRow()) ? $row : false;
}
/**
* @brief write metadata to database
*
* @param array $meta metadata
* @return OC_DB_StatementWrapper
*/
protected static function save($meta) {
$sql = "INSERT INTO *PREFIX*opds_metadata (`id`, `updated`, `date`, `author`, `title`, `language`, `publisher`, `isbn`, `copyright`, `description`, `subjects`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
$args = array(
$meta['id'],
$meta['updated'],
$meta['date'],
$meta['author'],
$meta['title'],
$meta['language'],
$meta['publisher'],
$meta['isbn'],
$meta['copyright'],
$meta['description'],
$meta['subjects']
);
$query = \OCP\DB::prepare($sql);
return $query->execute($args);
}
/**
* @brief get metadata for fileid
*
* Long function, to be split later
*
* @param int $id fileid
* @return array of metadata
*/
public static function get($id) {
if ($meta = self::load($id)) {
return $meta;
} else {
$meta = self::create($id);
$path = \OC\Files\Filesystem::getLocalFile(\OC\Files\Filesystem::getPath($id));
switch (strtolower(substr(strrchr($path, "."), 1))) {
case 'epub':
$epub = new Epub($path);
$meta['author'] = json_encode($epub->Authors());
$meta['title'] = $epub->Title();
$meta['date'] = $epub->Date();
$meta['publisher'] = $epub->Publisher();
$meta['copyright'] = $epub->Copyright();
$meta['language'] = $epub->Language();
$meta['description'] = strip_tags($epub->Description());
$meta['isbn'] = $epub->ISBN();
$meta['subjects'] = $epub->Subjects();
break;
default:
// set title to filename minus extension
$info = pathinfo($path);
$meta['title'] = basename($path,'.'.$info['extension']);
break;
}
self::save($meta);
return $meta;
}
}
}

View file

@ -74,48 +74,6 @@ class Util
header('HTTP/1.0 ' . $statusCode . ' ' . $message);
}
/**
* @brief offer single file for download
*
* @param string $path full path to file
* @param int $id file id
*/
public static function serveFile($path, $id) {
\OCP\User::checkLoggedIn();
\OC::$session->close();
Bookshelf::add($id);
$dirName = dirname($path);
$fileName = basename($path);
\OC_Files::get($dirName, array($fileName), $_SERVER['REQUEST_METHOD'] == 'HEAD');
}
/**
* @brief serve opds feed for given directory
*
* @param string $dir full path to directory
* @param int $id requested id
*/
public static function serveFeed($dir, $id) {
if (isset($_SERVER['HTTP_ACCEPT']) && stristr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
header('Content-Type: application/atom+xml');
} else {
header('Content-Type: text/xml; charset=UTF-8');
}
$sortAttribute = 'name';
$sortDirection = false;
$defaults = new \OC_Defaults();
$tmpl = new \OCP\Template('files_opds', 'feed');
$tmpl->assign('files', Files::formatFileInfos(Files::getFiles($dir, $sortAttribute, $sortDirection)));
$tmpl->assign('bookshelf', Files::formatFileInfos(Bookshelf::get()));
$tmpl->assign('bookshelf-count', Bookshelf::count());
$tmpl->assign('feed_id', self::getFeedId());
$tmpl->assign('id', $id);
$tmpl->assign('dir', $dir);
$tmpl->assign('user', \OCP\User::getDisplayName());
$tmpl->assign('ocname', $defaults->getName());
$tmpl->printPage();
}
/**
* @brief generate v3 UUID based on display name and site url
*
@ -132,15 +90,6 @@ class Util
return $hash;
}
/**
* @brief get feed id
*
* @return string feed id
*/
public static function getFeedId() {
return Config::get('id', '');
}
/**
* @brief log warning
* @param string message to write to log
@ -148,76 +97,4 @@ class Util
public static function logWarn($msg) {
\OCP\Util::writeLog('files_opds', $msg, \OCP\Util::WARN);
}
/**
* @brief get metadata for fileid
*
* Long function, to be split later
*
* @param int $id fileid
* @return array of metadata
*/
public static function getMeta($id) {
$sql = 'SELECT * FROM `*PREFIX*opds_metadata` WHERE id = ?';
$args = array($id);
$query = \OCP\DB::prepare($sql);
$result = $query->execute($args);
if ($row = $result->fetchRow()) {
return $row;
} else {
/* start with empty values, except for id. This way, files only get
* scanned once, even if they don't contain valid metadate.
*/
$meta = array();
$meta['id'] = $id;
$meta['updated'] = date("Y-m-d\TH:i:sP");
$meta['date'] = '';
$meta['author'] = '';
$meta['title'] = '';
$meta['language'] = '';
$meta['publisher'] = '';
$meta['isbn'] = '';
$meta['copyright'] = '';
$meta['description'] = '';
$meta['subjects'] = '';
$path = \OC\Files\Filesystem::getLocalFile(\OC\Files\Filesystem::getPath($id));
switch (strtolower(substr(strrchr($path, "."), 1))) {
case 'epub':
$epub = new Epub($path);
$meta['author'] = json_encode($epub->Authors());
$meta['title'] = $epub->Title();
$meta['date'] = $epub->Date();
$meta['publisher'] = $epub->Publisher();
$meta['copyright'] = $epub->Copyright();
$meta['language'] = $epub->Language();
$meta['description'] = strip_tags($epub->Description());
$meta['isbn'] = $epub->ISBN();
$meta['subjects'] = $epub->Subjects();
break;
default:
// set title to filename minus extension
$info = pathinfo($path);
$meta['title'] = basename($path,'.'.$info['extension']);
break;
}
$sql = "INSERT INTO *PREFIX*opds_metadata (`id`, `updated`, `date`, `author`, `title`, `language`, `publisher`, `isbn`, `copyright`, `description`, `subjects`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
$args = array(
$meta['id'],
$meta['updated'],
$meta['date'],
$meta['author'],
$meta['title'],
$meta['language'],
$meta['publisher'],
$meta['isbn'],
$meta['copyright'],
$meta['description'],
$meta['subjects']
);
$query = \OCP\DB::prepare($sql);
$result = $query->execute($args);
return $meta;
}
}
}

View file

@ -78,10 +78,10 @@ echo '<?xml version="1.0" encoding="UTF-8"?>';
<link type="<?php p($file['mimetype']); ?>"
rel="http://opds-spec.org/acquisition/open-access"
href="?id=<?php p($file['id']); ?>"/>
<link href="<?php p($file['preview']); ?>"
<link href="?pid=<?php p($file['id']); ?>"
rel="http://opds-spec.org/image"
type="image/jpeg" />
<link href="<?php p($file['thumbnail']); ?>"
<link href="?tid=<?php p($file['id']); ?>"
rel="http://opds-spec.org/image/thumbnail"
type="image/jpeg" />
<summary type="text"><?php p(formatMetadata($file['humansize'],$file['mimetype'],$file['name'])); ?></summary>
@ -124,10 +124,10 @@ echo '<?xml version="1.0" encoding="UTF-8"?>';
<link type="<?php p($file['mimetype']); ?>"
rel="http://opds-spec.org/acquisition/open-access"
href="?id=<?php p($file['id']); ?>"/>
<link href="<?php p($file['preview']); ?>"
<link href="?pid=<?php p($file['id']); ?>"
rel="http://opds-spec.org/image"
type="image/jpeg" />
<link href="<?php p($file['thumbnail']); ?>"
<link href="?tid=<?php p($file['id']); ?>"
rel="http://opds-spec.org/image/thumbnail"
type="image/jpeg" />
<summary type="text"><?php p(formatMetadata($file['humansize'],$file['mimetype'],$file['name'])); ?></summary>