nextcloud/lib/preview.php

625 lines
16 KiB
PHP
Raw Normal View History

<?php
/**
2013-04-25 13:18:45 +04:00
* Copyright (c) 2013 Frank Karlitschek frank@owncloud.org
* Copyright (c) 2013 Georg Ehrke georg@ownCloud.com
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
2013-05-10 01:59:16 +04:00
*
2013-04-25 13:18:45 +04:00
* Thumbnails:
* structure of filename:
* /data/user/thumbnails/pathhash/x-y.png
*
*/
2013-05-29 14:33:24 +04:00
namespace OC;
2013-05-10 01:59:16 +04:00
require_once('preview/images.php');
require_once('preview/movies.php');
require_once('preview/mp3.php');
require_once('preview/pdf.php');
2013-05-21 14:23:31 +04:00
require_once('preview/svg.php');
require_once('preview/txt.php');
2013-05-10 01:59:16 +04:00
require_once('preview/unknown.php');
require_once('preview/office.php');
2013-05-29 14:33:24 +04:00
class Preview {
2013-04-25 13:18:45 +04:00
//the thumbnail folder
const THUMBNAILS_FOLDER = 'thumbnails';
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
//config
private $max_scale_factor;
private $max_x;
private $max_y;
2013-04-25 13:18:45 +04:00
//fileview object
2013-05-10 01:59:16 +04:00
private $fileview = null;
private $userview = null;
//vars
private $file;
private $maxX;
private $maxY;
private $scalingup;
2013-05-17 21:08:16 +04:00
2013-06-13 11:52:39 +04:00
//preview images object
2013-05-10 01:59:16 +04:00
private $preview;
2013-04-25 13:18:45 +04:00
//preview providers
static private $providers = array();
static private $registeredProviders = array();
2013-04-25 13:18:45 +04:00
/**
* @brief check if thumbnail or bigger version of thumbnail of file is cached
2013-05-10 01:59:16 +04:00
* @param $user userid
* @param $root path of root
2013-04-25 13:18:45 +04:00
* @param $file The path to the file where you want a thumbnail from
* @param $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
* @param $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
* @return mixed (bool / string)
* false if thumbnail does not exist
* path to thumbnail if thumbnail exists
*/
2013-05-29 15:03:33 +04:00
public function __construct($user=null, $root='', $file='', $maxX=0, $maxY=0, $scalingup=true, $force=false) {
2013-05-10 01:59:16 +04:00
//set config
2013-05-29 14:33:24 +04:00
$this->max_x = \OC_Config::getValue('preview_max_x', null);
$this->max_y = \OC_Config::getValue('preview_max_y', null);
$this->max_scale_factor = \OC_Config::getValue('preview_max_scale_factor', 10);
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
//save parameters
$this->file = $file;
$this->maxX = $maxX;
$this->maxY = $maxY;
$this->scalingup = $scalingup;
//init fileviews
$this->fileview = new \OC\Files\View('/' . $user . '/' . $root);
$this->userview = new \OC\Files\View('/' . $user);
2013-05-29 15:03:33 +04:00
if($force !== true) {
if(!is_null($this->max_x)) {
if($this->maxX > $this->max_x) {
2013-05-29 14:33:24 +04:00
\OC_Log::write('core', 'maxX reduced from ' . $this->maxX . ' to ' . $this->max_x, \OC_Log::DEBUG);
$this->maxX = $this->max_x;
}
2013-05-10 01:59:16 +04:00
}
2013-06-13 11:52:39 +04:00
2013-05-29 15:03:33 +04:00
if(!is_null($this->max_y)) {
if($this->maxY > $this->max_y) {
2013-05-29 14:33:24 +04:00
\OC_Log::write('core', 'maxY reduced from ' . $this->maxY . ' to ' . $this->max_y, \OC_Log::DEBUG);
$this->maxY = $this->max_y;
}
}
2013-05-30 12:44:23 +04:00
$fileinfo = $this->fileview->getFileInfo($this->file);
if(array_key_exists('size', $fileinfo)){
if((int) $fileinfo['size'] === 0){
\OC_Log::write('core', 'You can\'t generate a preview of a 0 byte file (' . $this->file . ')', \OC_Log::ERROR);
throw new \Exception('0 byte file given');
}
}
//init providers
2013-05-29 15:03:33 +04:00
if(empty(self::$providers)) {
self::initProviders();
}
2013-06-13 11:52:39 +04:00
//check if there are any providers at all
2013-05-29 15:03:33 +04:00
if(empty(self::$providers)) {
2013-05-29 14:33:24 +04:00
\OC_Log::write('core', 'No preview providers exist', \OC_Log::ERROR);
throw new \Exception('No providers');
}
2013-06-13 11:52:39 +04:00
//validate parameters
2013-05-29 15:03:33 +04:00
if($file === '') {
2013-05-29 14:33:24 +04:00
\OC_Log::write('core', 'No filename passed', \OC_Log::ERROR);
throw new \Exception('File not found');
}
2013-06-13 11:52:39 +04:00
//check if file exists
2013-05-29 15:03:33 +04:00
if(!$this->fileview->file_exists($file)) {
2013-05-29 14:33:24 +04:00
\OC_Log::write('core', 'File:"' . $file . '" not found', \OC_Log::ERROR);
throw new \Exception('File not found');
}
2013-06-13 11:52:39 +04:00
//check if given size makes sense
2013-05-29 15:03:33 +04:00
if($maxX === 0 || $maxY === 0) {
2013-05-29 14:33:24 +04:00
\OC_Log::write('core', 'Can not create preview with 0px width or 0px height', \OC_Log::ERROR);
throw new \Exception('Height and/or width set to 0');
2013-05-10 01:59:16 +04:00
}
}
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
/**
* @brief returns the path of the file you want a thumbnail from
* @return string
*/
2013-05-29 15:03:33 +04:00
public function getFile() {
2013-05-10 01:59:16 +04:00
return $this->file;
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
/**
* @brief returns the max width of the preview
* @return integer
*/
2013-05-29 15:03:33 +04:00
public function getMaxX() {
2013-05-10 01:59:16 +04:00
return $this->maxX;
}
/**
* @brief returns the max height of the preview
* @return integer
*/
2013-05-29 15:03:33 +04:00
public function getMaxY() {
2013-05-10 01:59:16 +04:00
return $this->maxY;
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
/**
* @brief returns whether or not scalingup is enabled
* @return bool
*/
2013-05-29 15:03:33 +04:00
public function getScalingup() {
2013-05-10 01:59:16 +04:00
return $this->scalingup;
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
/**
* @brief returns the name of the thumbnailfolder
* @return string
*/
2013-05-29 15:03:33 +04:00
public function getThumbnailsfolder() {
2013-05-10 01:59:16 +04:00
return self::THUMBNAILS_FOLDER;
}
/**
* @brief returns the max scale factor
* @return integer
*/
2013-05-29 15:03:33 +04:00
public function getMaxScaleFactor() {
2013-05-10 01:59:16 +04:00
return $this->max_scale_factor;
}
/**
* @brief returns the max width set in ownCloud's config
* @return integer
*/
2013-05-29 15:03:33 +04:00
public function getConfigMaxX() {
2013-05-10 01:59:16 +04:00
return $this->max_x;
}
/**
* @brief returns the max height set in ownCloud's config
* @return integer
*/
2013-05-29 15:03:33 +04:00
public function getConfigMaxY() {
2013-05-10 01:59:16 +04:00
return $this->max_y;
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
/**
* @brief deletes previews of a file with specific x and y
* @return bool
*/
2013-05-29 15:03:33 +04:00
public function deletePreview() {
2013-05-10 01:59:16 +04:00
$fileinfo = $this->fileview->getFileInfo($this->file);
$fileid = $fileinfo['fileid'];
$this->userview->unlink(self::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $this->maxX . '-' . $this->maxY . '.png');
return;
2013-05-10 01:59:16 +04:00
}
/**
* @brief deletes all previews of a file
* @return bool
*/
2013-05-29 15:03:33 +04:00
public function deleteAllPreviews() {
2013-05-10 01:59:16 +04:00
$fileinfo = $this->fileview->getFileInfo($this->file);
$fileid = $fileinfo['fileid'];
$this->userview->deleteAll(self::THUMBNAILS_FOLDER . '/' . $fileid . '/');
$this->userview->rmdir(self::THUMBNAILS_FOLDER . '/' . $fileid . '/');
return;
2013-05-10 01:59:16 +04:00
}
/**
* @brief check if thumbnail or bigger version of thumbnail of file is cached
* @return mixed (bool / string)
* false if thumbnail does not exist
* path to thumbnail if thumbnail exists
*/
2013-05-29 15:03:33 +04:00
private function isCached() {
2013-05-10 01:59:16 +04:00
$file = $this->file;
$maxX = $this->maxX;
$maxY = $this->maxY;
$scalingup = $this->scalingup;
$fileinfo = $this->fileview->getFileInfo($file);
$fileid = $fileinfo['fileid'];
2013-05-29 15:03:33 +04:00
if(!$this->userview->is_dir(self::THUMBNAILS_FOLDER . '/' . $fileid)) {
2013-05-10 01:59:16 +04:00
return false;
}
2013-04-25 13:18:45 +04:00
//does a preview with the wanted height and width already exist?
2013-05-29 15:03:33 +04:00
if($this->userview->file_exists(self::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $maxX . '-' . $maxY . '.png')) {
2013-05-10 01:59:16 +04:00
return self::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $maxX . '-' . $maxY . '.png';
2013-04-25 13:18:45 +04:00
}
2013-05-10 01:59:16 +04:00
2013-04-25 13:18:45 +04:00
$wantedaspectratio = $maxX / $maxY;
2013-05-10 01:59:16 +04:00
2013-04-25 13:18:45 +04:00
//array for usable cached thumbnails
$possiblethumbnails = array();
2013-05-10 01:59:16 +04:00
$allthumbnails = $this->userview->getDirectoryContent(self::THUMBNAILS_FOLDER . '/' . $fileid);
2013-05-29 15:03:33 +04:00
foreach($allthumbnails as $thumbnail) {
2013-04-25 13:18:45 +04:00
$size = explode('-', $thumbnail['name']);
$x = $size[0];
$y = $size[1];
2013-05-10 01:59:16 +04:00
2013-04-25 13:18:45 +04:00
$aspectratio = $x / $y;
2013-05-29 15:03:33 +04:00
if($aspectratio != $wantedaspectratio) {
2013-04-25 13:18:45 +04:00
continue;
}
2013-05-10 01:59:16 +04:00
2013-05-29 15:03:33 +04:00
if($x < $maxX || $y < $maxY) {
if($scalingup) {
2013-04-25 13:18:45 +04:00
$scalefactor = $maxX / $x;
2013-05-29 15:03:33 +04:00
if($scalefactor > $this->max_scale_factor) {
2013-04-25 13:18:45 +04:00
continue;
}
}else{
continue;
}
}
$possiblethumbnails[$x] = $thumbnail['path'];
}
2013-05-10 01:59:16 +04:00
2013-05-29 15:03:33 +04:00
if(count($possiblethumbnails) === 0) {
2013-04-25 13:18:45 +04:00
return false;
}
2013-05-10 01:59:16 +04:00
2013-05-29 15:03:33 +04:00
if(count($possiblethumbnails) === 1) {
2013-04-25 13:18:45 +04:00
return current($possiblethumbnails);
}
2013-05-10 01:59:16 +04:00
2013-04-25 13:18:45 +04:00
ksort($possiblethumbnails);
2013-05-10 01:59:16 +04:00
2013-05-29 15:03:33 +04:00
if(key(reset($possiblethumbnails)) > $maxX) {
2013-04-25 13:18:45 +04:00
return current(reset($possiblethumbnails));
}
2013-05-10 01:59:16 +04:00
2013-05-29 15:03:33 +04:00
if(key(end($possiblethumbnails)) < $maxX) {
2013-04-25 13:18:45 +04:00
return current(end($possiblethumbnails));
}
2013-05-10 01:59:16 +04:00
2013-05-29 15:03:33 +04:00
foreach($possiblethumbnails as $width => $path) {
if($width < $maxX) {
2013-04-25 13:18:45 +04:00
continue;
}else{
return $path;
}
}
}
2013-04-25 13:18:45 +04:00
/**
2013-05-10 01:59:16 +04:00
* @brief return a preview of a file
* @param $file The path to the file where you want a thumbnail from
* @param $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
* @param $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
* @param $scaleup Scale smaller images up to the thumbnail size or not. Might look ugly
2013-04-25 13:18:45 +04:00
* @return image
*/
2013-05-29 15:03:33 +04:00
public function getPreview() {
2013-05-10 01:59:16 +04:00
$file = $this->file;
$maxX = $this->maxX;
$maxY = $this->maxY;
$scalingup = $this->scalingup;
2013-04-25 13:18:45 +04:00
2013-05-10 01:59:16 +04:00
$fileinfo = $this->fileview->getFileInfo($file);
$fileid = $fileinfo['fileid'];
$cached = self::isCached();
2013-05-29 15:03:33 +04:00
if($cached) {
$image = new \OC_Image($this->userview->file_get_contents($cached, 'r'));
2013-05-10 01:59:16 +04:00
$this->preview = $image;
}else{
$mimetype = $this->fileview->getMimeType($file);
2013-05-17 21:08:16 +04:00
2013-06-26 12:57:37 +04:00
$preview = null;
2013-05-17 21:08:16 +04:00
2013-05-29 15:03:33 +04:00
foreach(self::$providers as $supportedmimetype => $provider) {
if(!preg_match($supportedmimetype, $mimetype)) {
2013-05-10 01:59:16 +04:00
continue;
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
$preview = $provider->getThumbnail($file, $maxX, $maxY, $scalingup, $this->fileview);
2013-05-29 15:03:33 +04:00
if(!$preview) {
2013-05-10 01:59:16 +04:00
continue;
}
2013-05-17 21:08:16 +04:00
//are there any cached thumbnails yet
2013-05-29 15:03:33 +04:00
if($this->userview->is_dir(self::THUMBNAILS_FOLDER . '/') === false) {
$this->userview->mkdir(self::THUMBNAILS_FOLDER . '/');
2013-05-10 01:59:16 +04:00
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
//cache thumbnail
$cachepath = self::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $maxX . '-' . $maxY . '.png';
2013-05-29 15:03:33 +04:00
if($this->userview->is_dir(self::THUMBNAILS_FOLDER . '/' . $fileid . '/') === false) {
$this->userview->mkdir(self::THUMBNAILS_FOLDER . '/' . $fileid . '/');
}
$this->userview->file_put_contents($cachepath, $preview->data());
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
break;
}
2013-06-26 12:57:37 +04:00
if(is_null($preview) || $preview === false) {
$preview = new \OC_Image();
}
2013-05-10 01:59:16 +04:00
$this->preview = $preview;
}
$this->resizeAndCrop();
return $this->preview;
2013-04-25 13:18:45 +04:00
}
/**
* @brief return a preview of a file
* @param $file The path to the file where you want a thumbnail from
* @param $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
* @param $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
* @param $scaleup Scale smaller images up to the thumbnail size or not. Might look ugly
2013-05-10 01:59:16 +04:00
* @return void
*/
2013-05-29 15:03:33 +04:00
public function showPreview() {
2013-05-29 14:33:24 +04:00
\OCP\Response::enableCaching(3600 * 24); // 24 hour
2013-05-10 01:59:16 +04:00
$preview = $this->getPreview();
2013-05-29 15:03:33 +04:00
if($preview) {
2013-05-10 01:59:16 +04:00
$preview->show();
}
}
/**
* @brief resize, crop and fix orientation
* @return image
*/
2013-05-29 15:03:33 +04:00
public function resizeAndCrop() {
$this->preview->fixOrientation();
2013-05-10 01:59:16 +04:00
$image = $this->preview;
$x = $this->maxX;
$y = $this->maxY;
$scalingup = $this->scalingup;
2013-05-29 15:03:33 +04:00
if(!($image instanceof \OC_Image)) {
2013-05-29 14:48:21 +04:00
\OC_Log::write('core', 'Object passed to resizeAndCrop is not an instance of OC_Image', \OC_Log::DEBUG);
2013-05-10 01:59:16 +04:00
return;
}
$realx = (int) $image->width();
$realy = (int) $image->height();
2013-05-29 15:03:33 +04:00
if($x === $realx && $y === $realy) {
2013-05-10 01:59:16 +04:00
return $image;
}
$factorX = $x / $realx;
$factorY = $y / $realy;
2013-04-25 13:18:45 +04:00
2013-05-29 15:03:33 +04:00
if($factorX >= $factorY) {
2013-05-10 01:59:16 +04:00
$factor = $factorX;
}else{
$factor = $factorY;
}
// only scale up if requested
if($scalingup === false) {
if($factor>1) $factor=1;
}
2013-05-29 15:03:33 +04:00
if(!is_null($this->max_scale_factor)) {
if($factor > $this->max_scale_factor) {
2013-05-29 14:33:24 +04:00
\OC_Log::write('core', 'scalefactor reduced from ' . $factor . ' to ' . $this->max_scale_factor, \OC_Log::DEBUG);
2013-05-10 01:59:16 +04:00
$factor = $this->max_scale_factor;
2013-04-25 13:18:45 +04:00
}
}
2013-05-10 01:59:16 +04:00
$newXsize = $realx * $factor;
$newYsize = $realy * $factor;
// resize
$image->preciseResize($newXsize, $newYsize);
2013-05-17 21:08:16 +04:00
2013-05-29 15:03:33 +04:00
if($newXsize === $x && $newYsize === $y) {
2013-05-10 01:59:16 +04:00
$this->preview = $image;
return;
}
2013-05-17 21:08:16 +04:00
2013-05-29 15:03:33 +04:00
if($newXsize >= $x && $newYsize >= $y) {
2013-05-10 01:59:16 +04:00
$cropX = floor(abs($x - $newXsize) * 0.5);
2013-06-11 12:45:50 +04:00
//$cropY = floor(abs($y - $newYsize) * 0.5);
$cropY = 0;
2013-05-10 01:59:16 +04:00
$image->crop($cropX, $cropY, $x, $y);
$this->preview = $image;
return;
}
2013-05-17 21:08:16 +04:00
2013-05-29 15:03:33 +04:00
if($newXsize < $x || $newYsize < $y) {
if($newXsize > $x) {
2013-05-10 01:59:16 +04:00
$cropX = floor(($newXsize - $x) * 0.5);
$image->crop($cropX, 0, $x, $newYsize);
2013-04-25 13:18:45 +04:00
}
2013-05-17 21:08:16 +04:00
2013-05-29 15:03:33 +04:00
if($newYsize > $y) {
2013-05-10 01:59:16 +04:00
$cropY = floor(($newYsize - $y) * 0.5);
$image->crop(0, $cropY, $newXsize, $y);
2013-04-25 13:18:45 +04:00
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
$newXsize = (int) $image->width();
$newYsize = (int) $image->height();
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
//create transparent background layer
$backgroundlayer = imagecreatetruecolor($x, $y);
$white = imagecolorallocate($backgroundlayer, 255, 255, 255);
imagefill($backgroundlayer, 0, 0, $white);
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
$image = $image->resource();
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
$mergeX = floor(abs($x - $newXsize) * 0.5);
$mergeY = floor(abs($y - $newYsize) * 0.5);
2013-05-17 21:08:16 +04:00
imagecopy($backgroundlayer, $image, $mergeX, $mergeY, 0, 0, $newXsize, $newYsize);
2013-05-17 21:08:16 +04:00
//$black = imagecolorallocate(0,0,0);
//imagecolortransparent($transparentlayer, $black);
2013-05-17 21:08:16 +04:00
$image = new \OC_Image($backgroundlayer);
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
$this->preview = $image;
return;
2013-04-25 13:18:45 +04:00
}
}
2013-04-25 13:18:45 +04:00
/**
* @brief register a new preview provider to be used
2013-05-29 14:33:24 +04:00
* @param string $provider class name of a Preview_Provider
2013-04-25 13:18:45 +04:00
* @return void
*/
2013-05-29 15:03:33 +04:00
public static function registerProvider($class, $options=array()) {
2013-04-25 13:18:45 +04:00
self::$registeredProviders[]=array('class'=>$class, 'options'=>$options);
}
2013-04-25 13:18:45 +04:00
/**
* @brief create instances of all the registered preview providers
* @return void
*/
2013-05-29 15:03:33 +04:00
private static function initProviders() {
2013-04-25 13:18:45 +04:00
if(count(self::$providers)>0) {
return;
}
2013-05-17 21:08:16 +04:00
2013-04-25 13:18:45 +04:00
foreach(self::$registeredProviders as $provider) {
$class=$provider['class'];
$options=$provider['options'];
2013-05-17 21:08:16 +04:00
2013-04-25 13:18:45 +04:00
$object = new $class($options);
2013-05-17 21:08:16 +04:00
2013-04-25 13:18:45 +04:00
self::$providers[$object->getMimeType()] = $object;
}
2013-05-17 21:08:16 +04:00
2013-04-25 13:18:45 +04:00
$keys = array_map('strlen', array_keys(self::$providers));
array_multisort($keys, SORT_DESC, self::$providers);
}
2013-05-17 21:08:16 +04:00
2013-04-25 13:18:45 +04:00
/**
2013-05-10 01:59:16 +04:00
* @brief method that handles preview requests from users that are logged in
2013-04-25 13:18:45 +04:00
* @return void
*/
2013-05-29 15:03:33 +04:00
public static function previewRouter($params) {
2013-05-29 14:33:24 +04:00
\OC_Util::checkLoggedIn();
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
$file = '';
$maxX = 0;
$maxY = 0;
/*
* use: ?scalingup=0 / ?scalingup = 1
* do not use ?scalingup=false / ?scalingup = true as these will always be true
*/
2013-05-17 13:30:44 +04:00
$scalingup = true;
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
if(array_key_exists('file', $_GET)) $file = (string) urldecode($_GET['file']);
if(array_key_exists('x', $_GET)) $maxX = (int) $_GET['x'];
if(array_key_exists('y', $_GET)) $maxY = (int) $_GET['y'];
if(array_key_exists('scalingup', $_GET)) $scalingup = (bool) $_GET['scalingup'];
2013-05-17 21:08:16 +04:00
2013-05-29 15:03:33 +04:00
if($file !== '' && $maxX !== 0 && $maxY !== 0) {
try{
2013-05-29 14:33:24 +04:00
$preview = new Preview(\OC_User::getUser(), 'files', $file, $maxX, $maxY, $scalingup);
$preview->showPreview();
2013-05-30 12:44:23 +04:00
}catch(\Exception $e) {
2013-05-29 14:33:24 +04:00
\OC_Response::setStatus(404);
\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
exit;
}
2013-05-10 01:59:16 +04:00
}else{
2013-05-29 14:33:24 +04:00
\OC_Response::setStatus(404);
2013-04-25 14:51:44 +04:00
exit;
}
2013-05-10 01:59:16 +04:00
}
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
/**
* @brief method that handles preview requests from users that are not logged in / view shared folders that are public
* @return void
*/
2013-05-29 15:03:33 +04:00
public static function publicPreviewRouter($params) {
2013-05-10 01:59:16 +04:00
$file = '';
$maxX = 0;
$maxY = 0;
2013-05-17 13:30:44 +04:00
$scalingup = true;
2013-05-10 01:59:16 +04:00
$token = '';
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
$user = null;
$path = null;
2013-05-17 21:08:16 +04:00
2013-05-10 01:59:16 +04:00
if(array_key_exists('file', $_GET)) $file = (string) urldecode($_GET['file']);
if(array_key_exists('x', $_GET)) $maxX = (int) $_GET['x'];
if(array_key_exists('y', $_GET)) $maxY = (int) $_GET['y'];
if(array_key_exists('scalingup', $_GET)) $scalingup = (bool) $_GET['scalingup'];
if(array_key_exists('t', $_GET)) $token = (string) $_GET['t'];
2013-05-29 14:33:24 +04:00
$linkItem = \OCP\Share::getShareByToken($token);
2013-05-10 01:59:16 +04:00
if (is_array($linkItem) && isset($linkItem['uid_owner']) && isset($linkItem['file_source'])) {
$userid = $linkItem['uid_owner'];
2013-05-29 14:33:24 +04:00
\OC_Util::setupFS($userid);
2013-05-22 17:12:25 +04:00
$pathid = $linkItem['file_source'];
$path = \OC\Files\Filesystem::getPath($pathid);
$pathinfo = \OC\Files\Filesystem::getFileInfo($path);
$sharedfile = null;
if($linkItem['item_type'] === 'folder') {
//clean up file parameter
$sharedfile = \OC\Files\Filesystem::normalizePath($file);
if(!\OC\Files\Filesystem::isValidPath($file)) {
\OC_Response::setStatus(403);
exit;
}
} else if($linkItem['item_type'] === 'file') {
$parent = $pathinfo['parent'];
$path = \OC\Files\Filesystem::getPath($parent);
$sharedfile = $pathinfo['name'];
}
2013-05-22 17:12:25 +04:00
$path = \OC\Files\Filesystem::normalizePath($path, false);
if(substr($path, 0, 1) == '/') {
$path = substr($path, 1);
}
2013-05-10 01:59:16 +04:00
}
2013-05-17 21:08:16 +04:00
if($userid !== null && $path !== null && $sharedfile !== null) {
try{
$preview = new Preview($userid, 'files/' . $path, $sharedfile, $maxX, $maxY, $scalingup);
$preview->showPreview();
2013-05-30 12:44:23 +04:00
}catch(\Exception $e) {
2013-05-29 14:33:24 +04:00
\OC_Response::setStatus(404);
\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
exit;
}
2013-05-10 01:59:16 +04:00
}else{
2013-05-29 14:33:24 +04:00
\OC_Response::setStatus(404);
2013-05-10 01:59:16 +04:00
exit;
2013-04-25 14:51:44 +04:00
}
2013-04-25 13:18:45 +04:00
}
2013-05-29 15:03:33 +04:00
public static function post_write($args) {
self::post_delete($args);
}
2013-05-29 15:03:33 +04:00
public static function post_delete($args) {
$path = $args['path'];
2013-05-29 15:03:33 +04:00
if(substr($path, 0, 1) == '/') {
$path = substr($path, 1);
}
2013-05-29 14:33:24 +04:00
$preview = new Preview(\OC_User::getUser(), 'files/', $path, 0, 0, false, true);
$preview->deleteAllPreviews();
}
2013-04-25 13:18:45 +04:00
}