forked from fpiraneo/oclife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPreview.php
85 lines (69 loc) · 2.7 KB
/
getPreview.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/*
* Copyright 2014 by Francesco PIRANEO G. ([email protected])
*
* This file is part of oclife.
*
* oclife is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* oclife 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with oclife. If not, see <http://www.gnu.org/licenses/>.
*/
OCP\JSON::checkAppEnabled('oclife');
OCP\JSON::checkLoggedIn();
// Revert parameters from ajax
$filePath = filter_input(INPUT_GET, 'filePath', FILTER_SANITIZE_STRING);
// Get current user
$user = \OCP\User::getUser();
// Build user's view path
$viewPath = '/' . $user . '/files';
// Build the placeholder's path - Image to show in case we don't have the thumbnail
$placeHolderPath = __DIR__ . '/img/noImage.png';
// Build thumb path
if(isset($filePath)) {
$filePathInfo = pathinfo($filePath);
$previewPath = \OC_User::getHome($user) . '/oclife/previews/' . $user;
$previewDir = $previewPath . $filePathInfo['dirname'];
$thumbPath = $previewPath . $filePathInfo['dirname'] . '/' . $filePathInfo['filename'] . '.png';
// Check and eventually prepare preview directory
if(!is_dir($previewDir)) {
mkdir($previewDir, 0755, true);
}
// If preview exist check if it's newer than the file, regenate it otherwise
if(file_exists($thumbPath)) {
$thumbMTime = filemtime($thumbPath);
$fileInfo = \OC\Files\Filesystem::getFileInfo($filePath);
if($thumbMTime < $fileInfo['mtime']) {
unlink($thumbPath);
}
}
// Check if thumbnail exist, create it otherwise
if(!file_exists($thumbPath)) {
$imgHandler = new \OCA\OCLife\ImageHandler();
$imgHandler->setHeight(600);
$imgHandler->setWidth(800);
$imgHandler->setBgColorFromValues(0, 0, 0);
$imgHandler->generateImageThumbnail($viewPath, $filePath, $thumbPath);
}
$previewPath = (is_file($thumbPath)) ? $thumbPath : $placeHolderPath;
} else {
$previewPath = $placeHolderPath;
}
// Output the preview
$mtime = filemtime($previewPath);
$size = filesize($previewPath);
$mime = \OC_Helper::getMimetype($previewPath);
\OCP\Response::enableCaching();
\OCP\Response::setLastModifiedHeader($mtime);
header('Content-Length: ' . $size);
header('Content-Type: ' . $mime);
$fp = @fopen($previewPath, 'rb');
@fpassthru($fp);