Blame view

sources/apps/gallery/lib/thumbnail.php 4.21 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <?php
  /**
   * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  namespace OCA\Gallery;
  
  use OC\Files\Filesystem;
  use OC\Files\View;
  
  class Thumbnail {
d1bafeea1   Kload   [fix] Upgrade to ...
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
  	protected $image;
  	protected $path;
  	protected $user;
  	protected $useOriginal = false;
  
  	/**
  	 * @var \OC\Files\View $view
  	 */
  	protected $view;
  
  	public function __construct($imagePath, $user = null, $square = false) {
  		if (!Filesystem::isValidPath($imagePath)) {
  			return;
  		}
  		if (is_null($user)) {
  			$this->view = Filesystem::getView();
  			$this->user = \OCP\User::getUser();
  		} else {
  			$this->view = new View('/' . $user . '/files');
  			$this->user = $user;
  		}
  		$this->useOriginal = (substr($imagePath, -4) === '.svg' or substr($imagePath, -5) === '.svgz');
  		if ($this->useOriginal) {
  			$this->path = $imagePath;
  		} else {
  			$galleryDir = \OC_User::getHome($this->user) . '/gallery/' . $this->user . '/';
  			if (strrpos($imagePath, '.')) {
  				$extension = substr($imagePath, strrpos($imagePath, '.') + 1);
  				$image = substr($imagePath, 0, strrpos($imagePath, '.'));
  			} else {
  				$extension = '';
  				$image = $imagePath;
  			}
  			if ($square) {
  				$extension = 'square.' . $extension;
  			}
  			$this->path = $galleryDir . $image . '.' . $extension;
  			if (!file_exists($this->path)) {
  				$this->create($imagePath, $square);
  			}
  		}
  	}
  
  	private function create($imagePath, $square) {
  		$galleryDir = \OC_User::getHome($this->user) . '/gallery/' . $this->user . '/';
  		$dir = dirname($imagePath);
6d9380f96   Cédric Dupont   Update sources OC...
61
62
63
  		$fileInfo = $this->view->getFileInfo($imagePath);
  		if (!$fileInfo) {
  			return false;
d1bafeea1   Kload   [fix] Upgrade to ...
64
65
66
67
  		}
  		if (!is_dir($galleryDir . $dir)) {
  			mkdir($galleryDir . $dir, 0755, true);
  		}
6d9380f96   Cédric Dupont   Update sources OC...
68
69
70
71
72
73
74
75
76
  
  		$this->image = new \OCP\Image();
  		// check if file is encrypted
  		if ($fileInfo['encrypted'] === true) {
  			$fileName = $this->view->toTmpFile($imagePath);
  		} else {
  			$fileName = $this->view->getLocalFile($imagePath);
  		}
  		$this->image->loadFromFile($fileName);
d1bafeea1   Kload   [fix] Upgrade to ...
77
78
79
80
81
  		if ($this->image->valid()) {
  			$this->image->fixOrientation();
  			if ($square) {
  				$this->image->centerCrop(200);
  			} else {
6d9380f96   Cédric Dupont   Update sources OC...
82
83
84
85
  				$this->image->fitIn($this->image->width(), 200);
  				if ($this->image->width() > 600) { // max aspect ratio of 3
  					$this->image->crop(($this->image->width() - 600) / 2, 0, 600, 200);
  				}
d1bafeea1   Kload   [fix] Upgrade to ...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  			}
  			$this->image->save($this->path);
  		}
  	}
  
  	public function get() {
  		if (is_null($this->image)) {
  			$this->image = new \OCP\Image($this->path);
  		}
  		return $this->image;
  	}
  
  	public function show() {
  		if ($this->useOriginal) {
  			$fp = @$this->view->fopen($this->path, 'rb');
  			$mtime = $this->view->filemtime($this->path);
  			$size = $this->view->filesize($this->path);
  			$mime = $this->view->getMimetype($this->path);
  		} else {
  			$fp = @fopen($this->path, 'rb');
  			$mtime = filemtime($this->path);
  			$size = filesize($this->path);
  			$mime = \OC_Helper::getMimetype($this->path);
  		}
  		if ($fp) {
  			\OCP\Response::enableCaching();
  			\OCP\Response::setLastModifiedHeader($mtime);
  			header('Content-Length: ' . $size);
  			header('Content-Type: ' . $mime);
6d9380f96   Cédric Dupont   Update sources OC...
115
  			\OCP\Response::setContentDispositionHeader(basename($this->path), 'attachment');
d1bafeea1   Kload   [fix] Upgrade to ...
116
117
118
119
120
121
122
123
124
  
  			fpassthru($fp);
  		} else {
  			\OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
  		}
  	}
  
  	static public function removeHook($params) {
  		$path = $params['path'];
6d9380f96   Cédric Dupont   Update sources OC...
125
  		$user = \OCP\User::getUser();
d1bafeea1   Kload   [fix] Upgrade to ...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  		$galleryDir = \OC_User::getHome($user) . '/gallery/';
  		$thumbPath = $galleryDir . $path;
  		if (is_dir($thumbPath)) {
  			if (file_exists($thumbPath . '.png')) {
  				unlink($thumbPath . '.png');
  			}
  		} else {
  			if (file_exists($thumbPath)) {
  				unlink($thumbPath);
  			}
  
  			if (strrpos($path, '.')) {
  				$extension = substr($path, strrpos($path, '.') + 1);
  				$image = substr($path, 0, strrpos($path, '.'));
  			} else {
  				$extension = '';
  				$image = $path;
  			}
  			$squareThumbPath = $galleryDir . $image . '.square.' . $extension;
  			if (file_exists($squareThumbPath)) {
  				unlink($squareThumbPath);
  			}
  		}
  
  		$parent = dirname($path);
  		if ($parent !== DIRECTORY_SEPARATOR and $parent !== '' and $parent !== $path) {
  			self::removeHook(array('path' => $parent));
  		}
  	}
  
  	static public function writeHook($params) {
  		self::removeHook($params);
d1bafeea1   Kload   [fix] Upgrade to ...
158
159
  	}
  }
d1bafeea1   Kload   [fix] Upgrade to ...
160