nextcloud/apps/gallery/ajax/sharing.php

118 lines
3.2 KiB
PHP
Raw Normal View History

2012-03-24 21:35:16 +04:00
<?php
/**
* ownCloud - gallery application
*
* @author Bartek Przybylski
* @copyright 2012 Bartek Przybylski bartek@alefzero.eu
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
2012-03-24 21:35:16 +04:00
if (!isset($_GET['token']) || !isset($_GET['operation'])) {
2012-05-03 14:23:29 +04:00
OCP\JSON::error(array('cause' => 'Not enought arguments'));
2012-03-24 21:35:16 +04:00
exit;
}
$operation = $_GET['operation'];
$token = $_GET['token'];
if (!OC_Gallery_Sharing::isTokenValid($token)) {
2012-05-03 14:23:29 +04:00
OCP\JSON::error(array('cause' => 'Given token is not valid'));
2012-03-24 21:35:16 +04:00
exit;
}
function handleGetGallery($token, $path) {
$owner = OC_Gallery_Sharing::getTokenOwner($token);
$apath = OC_Gallery_Sharing::getPath($token);
if ($path == false)
$root = $apath;
else
$root = rtrim($apath,'/').$path;
$r = OC_Gallery_Album::find($owner, null, $root);
$albums = array();
$photos = array();
$albumId = -1;
if ($row = $r->fetchRow()) {
$albumId = $row['album_id'];
}
if ($albumId != -1) {
if (OC_Gallery_Sharing::isRecursive($token)) {
$r = OC_Gallery_Album::find($owner, null, null, $root);
while ($row = $r->fetchRow())
$albums[] = $row['album_name'];
}
$r = OC_Gallery_Photo::find($albumId);
while ($row = $r->fetchRow())
$photos[] = $row['file_path'];
}
2012-05-03 14:23:29 +04:00
OCP\JSON::success(array('albums' => $albums, 'photos' => $photos));
2012-03-24 21:35:16 +04:00
}
function handleGetThumbnail($token, $imgpath) {
$owner = OC_Gallery_Sharing::getTokenOwner($token);
$image = OC_Gallery_Photo::getThumbnail($imgpath, $owner);
if ($image) {
2012-05-03 12:46:27 +04:00
OCP\Response::enableCaching(3600 * 24); // 24 hour
2012-03-24 21:35:16 +04:00
$image->show();
}
}
function handleGetAlbumThumbnail($token, $albumname)
{
$owner = OC_Gallery_Sharing::getTokenOwner($token);
2012-06-06 12:40:22 +04:00
$view = OCP\Files::getStorage('gallery');
$file = $view->fopen($albumname.'.png', 'r');
2012-03-24 21:35:16 +04:00
$image = new OC_Image($file);
if ($image->valid()) {
$image->centerCrop();
$image->resize(200);
$image->fixOrientation();
2012-05-03 12:46:27 +04:00
OCP\Response::enableCaching(3600 * 24); // 24 hour
2012-03-24 21:35:16 +04:00
$image->show();
}
}
function handleGetPhoto($token, $photo) {
$owner = OC_Gallery_Sharing::getTokenOwner($token);
2012-06-06 12:40:22 +04:00
$view = OCP\Files::getStorage('files');
$file = $view->fopen(urldecode($photo), 'r');
2012-03-24 21:35:16 +04:00
header('Content-Type: '.OC_Image::getMimeTypeForFile($file));
2012-05-03 12:46:27 +04:00
OCP\Response::sendFile($file);
2012-03-24 21:35:16 +04:00
}
switch ($operation) {
case 'get_gallery':
handleGetGallery($token, isset($_GET['path'])? $_GET['path'] : false);
break;
case 'get_thumbnail':
handleGetThumbnail($token, urldecode($_GET['img']));
break;
case 'get_album_thumbnail':
handleGetAlbumThumbnail($token, urldecode($_GET['albumname']));
break;
case 'get_photo':
handleGetPhoto($token, urldecode($_GET['photo']));
break;
}