@@ -38,345 +38,345 @@ |
||
| 38 | 38 | |
| 39 | 39 | class Generator { |
| 40 | 40 | |
| 41 | - /** @var IPreview */ |
|
| 42 | - private $previewManager; |
|
| 43 | - /** @var IConfig */ |
|
| 44 | - private $config; |
|
| 45 | - /** @var IAppData */ |
|
| 46 | - private $appData; |
|
| 47 | - /** @var GeneratorHelper */ |
|
| 48 | - private $helper; |
|
| 49 | - /** @var EventDispatcherInterface */ |
|
| 50 | - private $eventDispatcher; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param IConfig $config |
|
| 54 | - * @param IPreview $previewManager |
|
| 55 | - * @param IAppData $appData |
|
| 56 | - * @param GeneratorHelper $helper |
|
| 57 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 58 | - */ |
|
| 59 | - public function __construct( |
|
| 60 | - IConfig $config, |
|
| 61 | - IPreview $previewManager, |
|
| 62 | - IAppData $appData, |
|
| 63 | - GeneratorHelper $helper, |
|
| 64 | - EventDispatcherInterface $eventDispatcher |
|
| 65 | - ) { |
|
| 66 | - $this->config = $config; |
|
| 67 | - $this->previewManager = $previewManager; |
|
| 68 | - $this->appData = $appData; |
|
| 69 | - $this->helper = $helper; |
|
| 70 | - $this->eventDispatcher = $eventDispatcher; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Returns a preview of a file |
|
| 75 | - * |
|
| 76 | - * The cache is searched first and if nothing usable was found then a preview is |
|
| 77 | - * generated by one of the providers |
|
| 78 | - * |
|
| 79 | - * @param File $file |
|
| 80 | - * @param int $width |
|
| 81 | - * @param int $height |
|
| 82 | - * @param bool $crop |
|
| 83 | - * @param string $mode |
|
| 84 | - * @param string $mimeType |
|
| 85 | - * @return ISimpleFile |
|
| 86 | - * @throws NotFoundException |
|
| 87 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 88 | - */ |
|
| 89 | - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
| 90 | - $this->eventDispatcher->dispatch( |
|
| 91 | - IPreview::EVENT, |
|
| 92 | - new GenericEvent($file,[ |
|
| 93 | - 'width' => $width, |
|
| 94 | - 'height' => $height, |
|
| 95 | - 'crop' => $crop, |
|
| 96 | - 'mode' => $mode |
|
| 97 | - ]) |
|
| 98 | - ); |
|
| 99 | - |
|
| 100 | - if ($mimeType === null) { |
|
| 101 | - $mimeType = $file->getMimeType(); |
|
| 102 | - } |
|
| 103 | - if (!$this->previewManager->isMimeSupported($mimeType)) { |
|
| 104 | - throw new NotFoundException(); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $previewFolder = $this->getPreviewFolder($file); |
|
| 108 | - |
|
| 109 | - // Get the max preview and infer the max preview sizes from that |
|
| 110 | - $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType); |
|
| 111 | - list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview); |
|
| 112 | - |
|
| 113 | - // If both width and heigth are -1 we just want the max preview |
|
| 114 | - if ($width === -1 && $height === -1) { |
|
| 115 | - $width = $maxWidth; |
|
| 116 | - $height = $maxHeight; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // Calculate the preview size |
|
| 120 | - list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); |
|
| 121 | - |
|
| 122 | - // No need to generate a preview that is just the max preview |
|
| 123 | - if ($width === $maxWidth && $height === $maxHeight) { |
|
| 124 | - return $maxPreview; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // Try to get a cached preview. Else generate (and store) one |
|
| 128 | - try { |
|
| 129 | - $file = $this->getCachedPreview($previewFolder, $width, $height, $crop); |
|
| 130 | - } catch (NotFoundException $e) { |
|
| 131 | - $file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $file; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param ISimpleFolder $previewFolder |
|
| 139 | - * @param File $file |
|
| 140 | - * @param string $mimeType |
|
| 141 | - * @return ISimpleFile |
|
| 142 | - * @throws NotFoundException |
|
| 143 | - */ |
|
| 144 | - private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) { |
|
| 145 | - $nodes = $previewFolder->getDirectoryListing(); |
|
| 146 | - |
|
| 147 | - foreach ($nodes as $node) { |
|
| 148 | - if (strpos($node->getName(), 'max')) { |
|
| 149 | - return $node; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $previewProviders = $this->previewManager->getProviders(); |
|
| 154 | - foreach ($previewProviders as $supportedMimeType => $providers) { |
|
| 155 | - if (!preg_match($supportedMimeType, $mimeType)) { |
|
| 156 | - continue; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - foreach ($providers as $provider) { |
|
| 160 | - $provider = $this->helper->getProvider($provider); |
|
| 161 | - if (!($provider instanceof IProvider)) { |
|
| 162 | - continue; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048); |
|
| 166 | - $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048); |
|
| 167 | - |
|
| 168 | - $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); |
|
| 169 | - |
|
| 170 | - if (!($preview instanceof IImage)) { |
|
| 171 | - continue; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png'; |
|
| 175 | - try { |
|
| 176 | - $file = $previewFolder->newFile($path); |
|
| 177 | - $file->putContent($preview->data()); |
|
| 178 | - } catch (NotPermittedException $e) { |
|
| 179 | - throw new NotFoundException(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $file; |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - throw new NotFoundException(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @param ISimpleFile $file |
|
| 191 | - * @return int[] |
|
| 192 | - */ |
|
| 193 | - private function getPreviewSize(ISimpleFile $file) { |
|
| 194 | - $size = explode('-', $file->getName()); |
|
| 195 | - return [(int)$size[0], (int)$size[1]]; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @param int $width |
|
| 200 | - * @param int $height |
|
| 201 | - * @param bool $crop |
|
| 202 | - * @return string |
|
| 203 | - */ |
|
| 204 | - private function generatePath($width, $height, $crop) { |
|
| 205 | - $path = (string)$width . '-' . (string)$height; |
|
| 206 | - if ($crop) { |
|
| 207 | - $path .= '-crop'; |
|
| 208 | - } |
|
| 209 | - $path .= '.png'; |
|
| 210 | - return $path; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @param int $width |
|
| 217 | - * @param int $height |
|
| 218 | - * @param bool $crop |
|
| 219 | - * @param string $mode |
|
| 220 | - * @param int $maxWidth |
|
| 221 | - * @param int $maxHeight |
|
| 222 | - * @return int[] |
|
| 223 | - */ |
|
| 224 | - private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) { |
|
| 225 | - |
|
| 226 | - /* |
|
| 41 | + /** @var IPreview */ |
|
| 42 | + private $previewManager; |
|
| 43 | + /** @var IConfig */ |
|
| 44 | + private $config; |
|
| 45 | + /** @var IAppData */ |
|
| 46 | + private $appData; |
|
| 47 | + /** @var GeneratorHelper */ |
|
| 48 | + private $helper; |
|
| 49 | + /** @var EventDispatcherInterface */ |
|
| 50 | + private $eventDispatcher; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param IConfig $config |
|
| 54 | + * @param IPreview $previewManager |
|
| 55 | + * @param IAppData $appData |
|
| 56 | + * @param GeneratorHelper $helper |
|
| 57 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 58 | + */ |
|
| 59 | + public function __construct( |
|
| 60 | + IConfig $config, |
|
| 61 | + IPreview $previewManager, |
|
| 62 | + IAppData $appData, |
|
| 63 | + GeneratorHelper $helper, |
|
| 64 | + EventDispatcherInterface $eventDispatcher |
|
| 65 | + ) { |
|
| 66 | + $this->config = $config; |
|
| 67 | + $this->previewManager = $previewManager; |
|
| 68 | + $this->appData = $appData; |
|
| 69 | + $this->helper = $helper; |
|
| 70 | + $this->eventDispatcher = $eventDispatcher; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Returns a preview of a file |
|
| 75 | + * |
|
| 76 | + * The cache is searched first and if nothing usable was found then a preview is |
|
| 77 | + * generated by one of the providers |
|
| 78 | + * |
|
| 79 | + * @param File $file |
|
| 80 | + * @param int $width |
|
| 81 | + * @param int $height |
|
| 82 | + * @param bool $crop |
|
| 83 | + * @param string $mode |
|
| 84 | + * @param string $mimeType |
|
| 85 | + * @return ISimpleFile |
|
| 86 | + * @throws NotFoundException |
|
| 87 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 88 | + */ |
|
| 89 | + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
| 90 | + $this->eventDispatcher->dispatch( |
|
| 91 | + IPreview::EVENT, |
|
| 92 | + new GenericEvent($file,[ |
|
| 93 | + 'width' => $width, |
|
| 94 | + 'height' => $height, |
|
| 95 | + 'crop' => $crop, |
|
| 96 | + 'mode' => $mode |
|
| 97 | + ]) |
|
| 98 | + ); |
|
| 99 | + |
|
| 100 | + if ($mimeType === null) { |
|
| 101 | + $mimeType = $file->getMimeType(); |
|
| 102 | + } |
|
| 103 | + if (!$this->previewManager->isMimeSupported($mimeType)) { |
|
| 104 | + throw new NotFoundException(); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $previewFolder = $this->getPreviewFolder($file); |
|
| 108 | + |
|
| 109 | + // Get the max preview and infer the max preview sizes from that |
|
| 110 | + $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType); |
|
| 111 | + list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview); |
|
| 112 | + |
|
| 113 | + // If both width and heigth are -1 we just want the max preview |
|
| 114 | + if ($width === -1 && $height === -1) { |
|
| 115 | + $width = $maxWidth; |
|
| 116 | + $height = $maxHeight; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // Calculate the preview size |
|
| 120 | + list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); |
|
| 121 | + |
|
| 122 | + // No need to generate a preview that is just the max preview |
|
| 123 | + if ($width === $maxWidth && $height === $maxHeight) { |
|
| 124 | + return $maxPreview; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // Try to get a cached preview. Else generate (and store) one |
|
| 128 | + try { |
|
| 129 | + $file = $this->getCachedPreview($previewFolder, $width, $height, $crop); |
|
| 130 | + } catch (NotFoundException $e) { |
|
| 131 | + $file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $file; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param ISimpleFolder $previewFolder |
|
| 139 | + * @param File $file |
|
| 140 | + * @param string $mimeType |
|
| 141 | + * @return ISimpleFile |
|
| 142 | + * @throws NotFoundException |
|
| 143 | + */ |
|
| 144 | + private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) { |
|
| 145 | + $nodes = $previewFolder->getDirectoryListing(); |
|
| 146 | + |
|
| 147 | + foreach ($nodes as $node) { |
|
| 148 | + if (strpos($node->getName(), 'max')) { |
|
| 149 | + return $node; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $previewProviders = $this->previewManager->getProviders(); |
|
| 154 | + foreach ($previewProviders as $supportedMimeType => $providers) { |
|
| 155 | + if (!preg_match($supportedMimeType, $mimeType)) { |
|
| 156 | + continue; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + foreach ($providers as $provider) { |
|
| 160 | + $provider = $this->helper->getProvider($provider); |
|
| 161 | + if (!($provider instanceof IProvider)) { |
|
| 162 | + continue; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048); |
|
| 166 | + $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048); |
|
| 167 | + |
|
| 168 | + $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); |
|
| 169 | + |
|
| 170 | + if (!($preview instanceof IImage)) { |
|
| 171 | + continue; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png'; |
|
| 175 | + try { |
|
| 176 | + $file = $previewFolder->newFile($path); |
|
| 177 | + $file->putContent($preview->data()); |
|
| 178 | + } catch (NotPermittedException $e) { |
|
| 179 | + throw new NotFoundException(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $file; |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + throw new NotFoundException(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @param ISimpleFile $file |
|
| 191 | + * @return int[] |
|
| 192 | + */ |
|
| 193 | + private function getPreviewSize(ISimpleFile $file) { |
|
| 194 | + $size = explode('-', $file->getName()); |
|
| 195 | + return [(int)$size[0], (int)$size[1]]; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @param int $width |
|
| 200 | + * @param int $height |
|
| 201 | + * @param bool $crop |
|
| 202 | + * @return string |
|
| 203 | + */ |
|
| 204 | + private function generatePath($width, $height, $crop) { |
|
| 205 | + $path = (string)$width . '-' . (string)$height; |
|
| 206 | + if ($crop) { |
|
| 207 | + $path .= '-crop'; |
|
| 208 | + } |
|
| 209 | + $path .= '.png'; |
|
| 210 | + return $path; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @param int $width |
|
| 217 | + * @param int $height |
|
| 218 | + * @param bool $crop |
|
| 219 | + * @param string $mode |
|
| 220 | + * @param int $maxWidth |
|
| 221 | + * @param int $maxHeight |
|
| 222 | + * @return int[] |
|
| 223 | + */ |
|
| 224 | + private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) { |
|
| 225 | + |
|
| 226 | + /* |
|
| 227 | 227 | * If we are not cropping we have to make sure the requested image |
| 228 | 228 | * respects the aspect ratio of the original. |
| 229 | 229 | */ |
| 230 | - if (!$crop) { |
|
| 231 | - $ratio = $maxHeight / $maxWidth; |
|
| 230 | + if (!$crop) { |
|
| 231 | + $ratio = $maxHeight / $maxWidth; |
|
| 232 | 232 | |
| 233 | - if ($width === -1) { |
|
| 234 | - $width = $height / $ratio; |
|
| 235 | - } |
|
| 236 | - if ($height === -1) { |
|
| 237 | - $height = $width * $ratio; |
|
| 238 | - } |
|
| 233 | + if ($width === -1) { |
|
| 234 | + $width = $height / $ratio; |
|
| 235 | + } |
|
| 236 | + if ($height === -1) { |
|
| 237 | + $height = $width * $ratio; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - $ratioH = $height / $maxHeight; |
|
| 241 | - $ratioW = $width / $maxWidth; |
|
| 240 | + $ratioH = $height / $maxHeight; |
|
| 241 | + $ratioW = $width / $maxWidth; |
|
| 242 | 242 | |
| 243 | - /* |
|
| 243 | + /* |
|
| 244 | 244 | * Fill means that the $height and $width are the max |
| 245 | 245 | * Cover means min. |
| 246 | 246 | */ |
| 247 | - if ($mode === IPreview::MODE_FILL) { |
|
| 248 | - if ($ratioH > $ratioW) { |
|
| 249 | - $height = $width * $ratio; |
|
| 250 | - } else { |
|
| 251 | - $width = $height / $ratio; |
|
| 252 | - } |
|
| 253 | - } else if ($mode === IPreview::MODE_COVER) { |
|
| 254 | - if ($ratioH > $ratioW) { |
|
| 255 | - $width = $height / $ratio; |
|
| 256 | - } else { |
|
| 257 | - $height = $width * $ratio; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - if ($height !== $maxHeight && $width !== $maxWidth) { |
|
| 263 | - /* |
|
| 247 | + if ($mode === IPreview::MODE_FILL) { |
|
| 248 | + if ($ratioH > $ratioW) { |
|
| 249 | + $height = $width * $ratio; |
|
| 250 | + } else { |
|
| 251 | + $width = $height / $ratio; |
|
| 252 | + } |
|
| 253 | + } else if ($mode === IPreview::MODE_COVER) { |
|
| 254 | + if ($ratioH > $ratioW) { |
|
| 255 | + $width = $height / $ratio; |
|
| 256 | + } else { |
|
| 257 | + $height = $width * $ratio; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + if ($height !== $maxHeight && $width !== $maxWidth) { |
|
| 263 | + /* |
|
| 264 | 264 | * Scale to the nearest power of two |
| 265 | 265 | */ |
| 266 | - $pow2height = 2 ** ceil(log($height) / log(2)); |
|
| 267 | - $pow2width = 2 ** ceil(log($width) / log(2)); |
|
| 268 | - |
|
| 269 | - $ratioH = $height / $pow2height; |
|
| 270 | - $ratioW = $width / $pow2width; |
|
| 271 | - |
|
| 272 | - if ($ratioH < $ratioW) { |
|
| 273 | - $width = $pow2width; |
|
| 274 | - $height /= $ratioW; |
|
| 275 | - } else { |
|
| 276 | - $height = $pow2height; |
|
| 277 | - $width /= $ratioH; |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /* |
|
| 266 | + $pow2height = 2 ** ceil(log($height) / log(2)); |
|
| 267 | + $pow2width = 2 ** ceil(log($width) / log(2)); |
|
| 268 | + |
|
| 269 | + $ratioH = $height / $pow2height; |
|
| 270 | + $ratioW = $width / $pow2width; |
|
| 271 | + |
|
| 272 | + if ($ratioH < $ratioW) { |
|
| 273 | + $width = $pow2width; |
|
| 274 | + $height /= $ratioW; |
|
| 275 | + } else { |
|
| 276 | + $height = $pow2height; |
|
| 277 | + $width /= $ratioH; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /* |
|
| 282 | 282 | * Make sure the requested height and width fall within the max |
| 283 | 283 | * of the preview. |
| 284 | 284 | */ |
| 285 | - if ($height > $maxHeight) { |
|
| 286 | - $ratio = $height / $maxHeight; |
|
| 287 | - $height = $maxHeight; |
|
| 288 | - $width /= $ratio; |
|
| 289 | - } |
|
| 290 | - if ($width > $maxWidth) { |
|
| 291 | - $ratio = $width / $maxWidth; |
|
| 292 | - $width = $maxWidth; |
|
| 293 | - $height /= $ratio; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - return [(int)round($width), (int)round($height)]; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * @param ISimpleFolder $previewFolder |
|
| 301 | - * @param ISimpleFile $maxPreview |
|
| 302 | - * @param int $width |
|
| 303 | - * @param int $height |
|
| 304 | - * @param bool $crop |
|
| 305 | - * @param int $maxWidth |
|
| 306 | - * @param int $maxHeight |
|
| 307 | - * @return ISimpleFile |
|
| 308 | - * @throws NotFoundException |
|
| 309 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 310 | - */ |
|
| 311 | - private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) { |
|
| 312 | - $preview = $this->helper->getImage($maxPreview); |
|
| 313 | - |
|
| 314 | - if (!$preview->valid()) { |
|
| 315 | - throw new \InvalidArgumentException('Failed to generate preview, failed to load image'); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - if ($crop) { |
|
| 319 | - if ($height !== $preview->height() && $width !== $preview->width()) { |
|
| 320 | - //Resize |
|
| 321 | - $widthR = $preview->width() / $width; |
|
| 322 | - $heightR = $preview->height() / $height; |
|
| 323 | - |
|
| 324 | - if ($widthR > $heightR) { |
|
| 325 | - $scaleH = $height; |
|
| 326 | - $scaleW = $maxWidth / $heightR; |
|
| 327 | - } else { |
|
| 328 | - $scaleH = $maxHeight / $widthR; |
|
| 329 | - $scaleW = $width; |
|
| 330 | - } |
|
| 331 | - $preview->preciseResize(round($scaleW), round($scaleH)); |
|
| 332 | - } |
|
| 333 | - $cropX = floor(abs($width - $preview->width()) * 0.5); |
|
| 334 | - $cropY = 0; |
|
| 335 | - $preview->crop($cropX, $cropY, $width, $height); |
|
| 336 | - } else { |
|
| 337 | - $preview->resize(max($width, $height)); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - $path = $this->generatePath($width, $height, $crop); |
|
| 342 | - try { |
|
| 343 | - $file = $previewFolder->newFile($path); |
|
| 344 | - $file->putContent($preview->data()); |
|
| 345 | - } catch (NotPermittedException $e) { |
|
| 346 | - throw new NotFoundException(); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - return $file; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * @param ISimpleFolder $previewFolder |
|
| 354 | - * @param int $width |
|
| 355 | - * @param int $height |
|
| 356 | - * @param bool $crop |
|
| 357 | - * @return ISimpleFile |
|
| 358 | - * |
|
| 359 | - * @throws NotFoundException |
|
| 360 | - */ |
|
| 361 | - private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) { |
|
| 362 | - $path = $this->generatePath($width, $height, $crop); |
|
| 363 | - |
|
| 364 | - return $previewFolder->getFile($path); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Get the specific preview folder for this file |
|
| 369 | - * |
|
| 370 | - * @param File $file |
|
| 371 | - * @return ISimpleFolder |
|
| 372 | - */ |
|
| 373 | - private function getPreviewFolder(File $file) { |
|
| 374 | - try { |
|
| 375 | - $folder = $this->appData->getFolder($file->getId()); |
|
| 376 | - } catch (NotFoundException $e) { |
|
| 377 | - $folder = $this->appData->newFolder($file->getId()); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - return $folder; |
|
| 381 | - } |
|
| 285 | + if ($height > $maxHeight) { |
|
| 286 | + $ratio = $height / $maxHeight; |
|
| 287 | + $height = $maxHeight; |
|
| 288 | + $width /= $ratio; |
|
| 289 | + } |
|
| 290 | + if ($width > $maxWidth) { |
|
| 291 | + $ratio = $width / $maxWidth; |
|
| 292 | + $width = $maxWidth; |
|
| 293 | + $height /= $ratio; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + return [(int)round($width), (int)round($height)]; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * @param ISimpleFolder $previewFolder |
|
| 301 | + * @param ISimpleFile $maxPreview |
|
| 302 | + * @param int $width |
|
| 303 | + * @param int $height |
|
| 304 | + * @param bool $crop |
|
| 305 | + * @param int $maxWidth |
|
| 306 | + * @param int $maxHeight |
|
| 307 | + * @return ISimpleFile |
|
| 308 | + * @throws NotFoundException |
|
| 309 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 310 | + */ |
|
| 311 | + private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) { |
|
| 312 | + $preview = $this->helper->getImage($maxPreview); |
|
| 313 | + |
|
| 314 | + if (!$preview->valid()) { |
|
| 315 | + throw new \InvalidArgumentException('Failed to generate preview, failed to load image'); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + if ($crop) { |
|
| 319 | + if ($height !== $preview->height() && $width !== $preview->width()) { |
|
| 320 | + //Resize |
|
| 321 | + $widthR = $preview->width() / $width; |
|
| 322 | + $heightR = $preview->height() / $height; |
|
| 323 | + |
|
| 324 | + if ($widthR > $heightR) { |
|
| 325 | + $scaleH = $height; |
|
| 326 | + $scaleW = $maxWidth / $heightR; |
|
| 327 | + } else { |
|
| 328 | + $scaleH = $maxHeight / $widthR; |
|
| 329 | + $scaleW = $width; |
|
| 330 | + } |
|
| 331 | + $preview->preciseResize(round($scaleW), round($scaleH)); |
|
| 332 | + } |
|
| 333 | + $cropX = floor(abs($width - $preview->width()) * 0.5); |
|
| 334 | + $cropY = 0; |
|
| 335 | + $preview->crop($cropX, $cropY, $width, $height); |
|
| 336 | + } else { |
|
| 337 | + $preview->resize(max($width, $height)); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + $path = $this->generatePath($width, $height, $crop); |
|
| 342 | + try { |
|
| 343 | + $file = $previewFolder->newFile($path); |
|
| 344 | + $file->putContent($preview->data()); |
|
| 345 | + } catch (NotPermittedException $e) { |
|
| 346 | + throw new NotFoundException(); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + return $file; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * @param ISimpleFolder $previewFolder |
|
| 354 | + * @param int $width |
|
| 355 | + * @param int $height |
|
| 356 | + * @param bool $crop |
|
| 357 | + * @return ISimpleFile |
|
| 358 | + * |
|
| 359 | + * @throws NotFoundException |
|
| 360 | + */ |
|
| 361 | + private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) { |
|
| 362 | + $path = $this->generatePath($width, $height, $crop); |
|
| 363 | + |
|
| 364 | + return $previewFolder->getFile($path); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Get the specific preview folder for this file |
|
| 369 | + * |
|
| 370 | + * @param File $file |
|
| 371 | + * @return ISimpleFolder |
|
| 372 | + */ |
|
| 373 | + private function getPreviewFolder(File $file) { |
|
| 374 | + try { |
|
| 375 | + $folder = $this->appData->getFolder($file->getId()); |
|
| 376 | + } catch (NotFoundException $e) { |
|
| 377 | + $folder = $this->appData->newFolder($file->getId()); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + return $folder; |
|
| 381 | + } |
|
| 382 | 382 | } |
@@ -35,40 +35,40 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | $application = new Application(); |
| 37 | 37 | $application->registerRoutes($this, [ |
| 38 | - 'routes' => [ |
|
| 39 | - ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'], |
|
| 40 | - ['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'], |
|
| 41 | - ['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'], |
|
| 42 | - ['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'], |
|
| 43 | - ['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'], |
|
| 44 | - ['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'], |
|
| 45 | - ['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'], |
|
| 46 | - ['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'], |
|
| 47 | - ['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'], |
|
| 48 | - ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'], |
|
| 49 | - ['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'], |
|
| 50 | - ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'], |
|
| 51 | - ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'], |
|
| 52 | - ['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'], |
|
| 53 | - ['name' => 'ClientFlowLogin#redirectPage', 'url' => '/login/flow/redirect', 'verb' => 'GET'], |
|
| 54 | - ['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'], |
|
| 55 | - ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'], |
|
| 56 | - ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'], |
|
| 57 | - ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'], |
|
| 58 | - ['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'], |
|
| 59 | - ['name' => 'Preview#getPreview', 'url' => '/core/preview', 'verb' => 'GET'], |
|
| 60 | - ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'], |
|
| 61 | - ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'], |
|
| 62 | - ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'], |
|
| 63 | - ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'], |
|
| 64 | - ['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'], |
|
| 65 | - ], |
|
| 66 | - 'ocs' => [ |
|
| 67 | - ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'], |
|
| 68 | - ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'], |
|
| 69 | - ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'], |
|
| 70 | - ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'], |
|
| 71 | - ], |
|
| 38 | + 'routes' => [ |
|
| 39 | + ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'], |
|
| 40 | + ['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'], |
|
| 41 | + ['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'], |
|
| 42 | + ['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'], |
|
| 43 | + ['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'], |
|
| 44 | + ['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'], |
|
| 45 | + ['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'], |
|
| 46 | + ['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'], |
|
| 47 | + ['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'], |
|
| 48 | + ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'], |
|
| 49 | + ['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'], |
|
| 50 | + ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'], |
|
| 51 | + ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'], |
|
| 52 | + ['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'], |
|
| 53 | + ['name' => 'ClientFlowLogin#redirectPage', 'url' => '/login/flow/redirect', 'verb' => 'GET'], |
|
| 54 | + ['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'], |
|
| 55 | + ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'], |
|
| 56 | + ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'], |
|
| 57 | + ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'], |
|
| 58 | + ['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'], |
|
| 59 | + ['name' => 'Preview#getPreview', 'url' => '/core/preview', 'verb' => 'GET'], |
|
| 60 | + ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'], |
|
| 61 | + ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'], |
|
| 62 | + ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'], |
|
| 63 | + ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'], |
|
| 64 | + ['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'], |
|
| 65 | + ], |
|
| 66 | + 'ocs' => [ |
|
| 67 | + ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'], |
|
| 68 | + ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'], |
|
| 69 | + ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'], |
|
| 70 | + ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'], |
|
| 71 | + ], |
|
| 72 | 72 | ]); |
| 73 | 73 | |
| 74 | 74 | // Post installation check |
@@ -77,15 +77,15 @@ discard block |
||
| 77 | 77 | // Core ajax actions |
| 78 | 78 | // Search |
| 79 | 79 | $this->create('search_ajax_search', '/core/search') |
| 80 | - ->actionInclude('core/search/ajax/search.php'); |
|
| 80 | + ->actionInclude('core/search/ajax/search.php'); |
|
| 81 | 81 | // Routing |
| 82 | 82 | $this->create('core_ajax_update', '/core/ajax/update.php') |
| 83 | - ->actionInclude('core/ajax/update.php'); |
|
| 83 | + ->actionInclude('core/ajax/update.php'); |
|
| 84 | 84 | |
| 85 | 85 | // File routes |
| 86 | 86 | $this->create('files.viewcontroller.showFile', '/f/{fileid}')->action(function($urlParams) { |
| 87 | - $app = new \OCA\Files\AppInfo\Application($urlParams); |
|
| 88 | - $app->dispatch('ViewController', 'index'); |
|
| 87 | + $app = new \OCA\Files\AppInfo\Application($urlParams); |
|
| 88 | + $app->dispatch('ViewController', 'index'); |
|
| 89 | 89 | }); |
| 90 | 90 | |
| 91 | 91 | // Call routes |
@@ -94,57 +94,57 @@ discard block |
||
| 94 | 94 | * @suppress PhanUndeclaredClassMethod |
| 95 | 95 | */ |
| 96 | 96 | $this->create('spreed.pagecontroller.showCall', '/call/{token}')->action(function($urlParams) { |
| 97 | - if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) { |
|
| 98 | - $app = new \OCA\Spreed\AppInfo\Application($urlParams); |
|
| 99 | - $app->dispatch('PageController', 'index'); |
|
| 100 | - } else { |
|
| 101 | - throw new \OC\HintException('App spreed is not enabled'); |
|
| 102 | - } |
|
| 97 | + if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) { |
|
| 98 | + $app = new \OCA\Spreed\AppInfo\Application($urlParams); |
|
| 99 | + $app->dispatch('PageController', 'index'); |
|
| 100 | + } else { |
|
| 101 | + throw new \OC\HintException('App spreed is not enabled'); |
|
| 102 | + } |
|
| 103 | 103 | }); |
| 104 | 104 | |
| 105 | 105 | // Sharing routes |
| 106 | 106 | $this->create('files_sharing.sharecontroller.showShare', '/s/{token}')->action(function($urlParams) { |
| 107 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 108 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 109 | - $app->dispatch('ShareController', 'showShare'); |
|
| 110 | - } else { |
|
| 111 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 112 | - } |
|
| 107 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 108 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 109 | + $app->dispatch('ShareController', 'showShare'); |
|
| 110 | + } else { |
|
| 111 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 112 | + } |
|
| 113 | 113 | }); |
| 114 | 114 | $this->create('files_sharing.sharecontroller.authenticate', '/s/{token}/authenticate')->post()->action(function($urlParams) { |
| 115 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 116 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 117 | - $app->dispatch('ShareController', 'authenticate'); |
|
| 118 | - } else { |
|
| 119 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 120 | - } |
|
| 115 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 116 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 117 | + $app->dispatch('ShareController', 'authenticate'); |
|
| 118 | + } else { |
|
| 119 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 120 | + } |
|
| 121 | 121 | }); |
| 122 | 122 | $this->create('files_sharing.sharecontroller.showAuthenticate', '/s/{token}/authenticate')->get()->action(function($urlParams) { |
| 123 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 124 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 125 | - $app->dispatch('ShareController', 'showAuthenticate'); |
|
| 126 | - } else { |
|
| 127 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 128 | - } |
|
| 123 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 124 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 125 | + $app->dispatch('ShareController', 'showAuthenticate'); |
|
| 126 | + } else { |
|
| 127 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 128 | + } |
|
| 129 | 129 | }); |
| 130 | 130 | $this->create('files_sharing.sharecontroller.downloadShare', '/s/{token}/download')->get()->action(function($urlParams) { |
| 131 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 132 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 133 | - $app->dispatch('ShareController', 'downloadShare'); |
|
| 134 | - } else { |
|
| 135 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 136 | - } |
|
| 131 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 132 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 133 | + $app->dispatch('ShareController', 'downloadShare'); |
|
| 134 | + } else { |
|
| 135 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 136 | + } |
|
| 137 | 137 | }); |
| 138 | 138 | $this->create('files_sharing.publicpreview.directLink', '/s/{token}/preview')->get()->action(function($urlParams) { |
| 139 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 140 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 141 | - $app->dispatch('PublicPreviewController', 'directLink'); |
|
| 142 | - } else { |
|
| 143 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 144 | - } |
|
| 139 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 140 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 141 | + $app->dispatch('PublicPreviewController', 'directLink'); |
|
| 142 | + } else { |
|
| 143 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 144 | + } |
|
| 145 | 145 | }); |
| 146 | 146 | |
| 147 | 147 | // used for heartbeat |
| 148 | 148 | $this->create('heartbeat', '/heartbeat')->action(function(){ |
| 149 | - // do nothing |
|
| 149 | + // do nothing |
|
| 150 | 150 | }); |
@@ -36,116 +36,116 @@ |
||
| 36 | 36 | |
| 37 | 37 | class PublicPreviewController extends Controller { |
| 38 | 38 | |
| 39 | - /** @var ShareManager */ |
|
| 40 | - private $shareManager; |
|
| 41 | - |
|
| 42 | - /** @var IPreview */ |
|
| 43 | - private $previewManager; |
|
| 44 | - |
|
| 45 | - public function __construct($appName, |
|
| 46 | - IRequest $request, |
|
| 47 | - ShareManager $shareManger, |
|
| 48 | - IPreview $previewManager) { |
|
| 49 | - parent::__construct($appName, $request); |
|
| 50 | - |
|
| 51 | - $this->shareManager = $shareManger; |
|
| 52 | - $this->previewManager = $previewManager; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @PublicPage |
|
| 57 | - * @NoCSRFRequired |
|
| 58 | - * |
|
| 59 | - * @param string $file |
|
| 60 | - * @param int $x |
|
| 61 | - * @param int $y |
|
| 62 | - * @param string $t |
|
| 63 | - * @param bool $a |
|
| 64 | - * @return DataResponse|FileDisplayResponse |
|
| 65 | - */ |
|
| 66 | - public function getPreview( |
|
| 67 | - $file = '', |
|
| 68 | - $x = 32, |
|
| 69 | - $y = 32, |
|
| 70 | - $t = '', |
|
| 71 | - $a = false |
|
| 72 | - ) { |
|
| 73 | - |
|
| 74 | - if ($t === '' || $x === 0 || $y === 0) { |
|
| 75 | - return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - try { |
|
| 79 | - $share = $this->shareManager->getShareByToken($t); |
|
| 80 | - } catch (ShareNotFound $e) { |
|
| 81 | - return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) { |
|
| 85 | - return new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - try { |
|
| 89 | - $node = $share->getNode(); |
|
| 90 | - if ($node instanceof Folder) { |
|
| 91 | - $file = $node->get($file); |
|
| 92 | - } else { |
|
| 93 | - $file = $node; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - $f = $this->previewManager->getPreview($file, $x, $y, !$a); |
|
| 97 | - return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); |
|
| 98 | - } catch (NotFoundException $e) { |
|
| 99 | - return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
| 100 | - } catch (\InvalidArgumentException $e) { |
|
| 101 | - return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @PublicPage |
|
| 107 | - * @NoCSRFRequired |
|
| 108 | - * @NoSameSiteCookieRequired |
|
| 109 | - * |
|
| 110 | - * @param $token |
|
| 111 | - * @return DataResponse|FileDisplayResponse |
|
| 112 | - */ |
|
| 113 | - public function directLink($token) { |
|
| 114 | - // No token no image |
|
| 115 | - if ($token === '') { |
|
| 116 | - return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // No share no image |
|
| 120 | - try { |
|
| 121 | - $share = $this->shareManager->getShareByToken($token); |
|
| 122 | - } catch (ShareNotFound $e) { |
|
| 123 | - return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - // No permissions no image |
|
| 127 | - if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) { |
|
| 128 | - return new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // Password protected shares have no direct link! |
|
| 132 | - if ($share->getPassword() !== null) { |
|
| 133 | - return new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - try { |
|
| 137 | - $node = $share->getNode(); |
|
| 138 | - if ($node instanceof Folder) { |
|
| 139 | - // Direct link only works for single files |
|
| 140 | - return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $f = $this->previewManager->getPreview($node, -1, -1, false); |
|
| 144 | - return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); |
|
| 145 | - } catch (NotFoundException $e) { |
|
| 146 | - return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
| 147 | - } catch (\InvalidArgumentException $e) { |
|
| 148 | - return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 149 | - } |
|
| 150 | - } |
|
| 39 | + /** @var ShareManager */ |
|
| 40 | + private $shareManager; |
|
| 41 | + |
|
| 42 | + /** @var IPreview */ |
|
| 43 | + private $previewManager; |
|
| 44 | + |
|
| 45 | + public function __construct($appName, |
|
| 46 | + IRequest $request, |
|
| 47 | + ShareManager $shareManger, |
|
| 48 | + IPreview $previewManager) { |
|
| 49 | + parent::__construct($appName, $request); |
|
| 50 | + |
|
| 51 | + $this->shareManager = $shareManger; |
|
| 52 | + $this->previewManager = $previewManager; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @PublicPage |
|
| 57 | + * @NoCSRFRequired |
|
| 58 | + * |
|
| 59 | + * @param string $file |
|
| 60 | + * @param int $x |
|
| 61 | + * @param int $y |
|
| 62 | + * @param string $t |
|
| 63 | + * @param bool $a |
|
| 64 | + * @return DataResponse|FileDisplayResponse |
|
| 65 | + */ |
|
| 66 | + public function getPreview( |
|
| 67 | + $file = '', |
|
| 68 | + $x = 32, |
|
| 69 | + $y = 32, |
|
| 70 | + $t = '', |
|
| 71 | + $a = false |
|
| 72 | + ) { |
|
| 73 | + |
|
| 74 | + if ($t === '' || $x === 0 || $y === 0) { |
|
| 75 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + try { |
|
| 79 | + $share = $this->shareManager->getShareByToken($t); |
|
| 80 | + } catch (ShareNotFound $e) { |
|
| 81 | + return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) { |
|
| 85 | + return new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + try { |
|
| 89 | + $node = $share->getNode(); |
|
| 90 | + if ($node instanceof Folder) { |
|
| 91 | + $file = $node->get($file); |
|
| 92 | + } else { |
|
| 93 | + $file = $node; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + $f = $this->previewManager->getPreview($file, $x, $y, !$a); |
|
| 97 | + return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); |
|
| 98 | + } catch (NotFoundException $e) { |
|
| 99 | + return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
| 100 | + } catch (\InvalidArgumentException $e) { |
|
| 101 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @PublicPage |
|
| 107 | + * @NoCSRFRequired |
|
| 108 | + * @NoSameSiteCookieRequired |
|
| 109 | + * |
|
| 110 | + * @param $token |
|
| 111 | + * @return DataResponse|FileDisplayResponse |
|
| 112 | + */ |
|
| 113 | + public function directLink($token) { |
|
| 114 | + // No token no image |
|
| 115 | + if ($token === '') { |
|
| 116 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // No share no image |
|
| 120 | + try { |
|
| 121 | + $share = $this->shareManager->getShareByToken($token); |
|
| 122 | + } catch (ShareNotFound $e) { |
|
| 123 | + return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + // No permissions no image |
|
| 127 | + if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) { |
|
| 128 | + return new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // Password protected shares have no direct link! |
|
| 132 | + if ($share->getPassword() !== null) { |
|
| 133 | + return new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + try { |
|
| 137 | + $node = $share->getNode(); |
|
| 138 | + if ($node instanceof Folder) { |
|
| 139 | + // Direct link only works for single files |
|
| 140 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $f = $this->previewManager->getPreview($node, -1, -1, false); |
|
| 144 | + return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); |
|
| 145 | + } catch (NotFoundException $e) { |
|
| 146 | + return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
| 147 | + } catch (\InvalidArgumentException $e) { |
|
| 148 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | 151 | } |
@@ -64,563 +64,563 @@ |
||
| 64 | 64 | */ |
| 65 | 65 | class ShareController extends Controller { |
| 66 | 66 | |
| 67 | - /** @var IConfig */ |
|
| 68 | - protected $config; |
|
| 69 | - /** @var IURLGenerator */ |
|
| 70 | - protected $urlGenerator; |
|
| 71 | - /** @var IUserManager */ |
|
| 72 | - protected $userManager; |
|
| 73 | - /** @var ILogger */ |
|
| 74 | - protected $logger; |
|
| 75 | - /** @var \OCP\Activity\IManager */ |
|
| 76 | - protected $activityManager; |
|
| 77 | - /** @var \OCP\Share\IManager */ |
|
| 78 | - protected $shareManager; |
|
| 79 | - /** @var ISession */ |
|
| 80 | - protected $session; |
|
| 81 | - /** @var IPreview */ |
|
| 82 | - protected $previewManager; |
|
| 83 | - /** @var IRootFolder */ |
|
| 84 | - protected $rootFolder; |
|
| 85 | - /** @var FederatedShareProvider */ |
|
| 86 | - protected $federatedShareProvider; |
|
| 87 | - /** @var EventDispatcherInterface */ |
|
| 88 | - protected $eventDispatcher; |
|
| 89 | - /** @var IL10N */ |
|
| 90 | - protected $l10n; |
|
| 91 | - /** @var Defaults */ |
|
| 92 | - protected $defaults; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param string $appName |
|
| 96 | - * @param IRequest $request |
|
| 97 | - * @param IConfig $config |
|
| 98 | - * @param IURLGenerator $urlGenerator |
|
| 99 | - * @param IUserManager $userManager |
|
| 100 | - * @param ILogger $logger |
|
| 101 | - * @param \OCP\Activity\IManager $activityManager |
|
| 102 | - * @param \OCP\Share\IManager $shareManager |
|
| 103 | - * @param ISession $session |
|
| 104 | - * @param IPreview $previewManager |
|
| 105 | - * @param IRootFolder $rootFolder |
|
| 106 | - * @param FederatedShareProvider $federatedShareProvider |
|
| 107 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 108 | - * @param IL10N $l10n |
|
| 109 | - * @param Defaults $defaults |
|
| 110 | - */ |
|
| 111 | - public function __construct($appName, |
|
| 112 | - IRequest $request, |
|
| 113 | - IConfig $config, |
|
| 114 | - IURLGenerator $urlGenerator, |
|
| 115 | - IUserManager $userManager, |
|
| 116 | - ILogger $logger, |
|
| 117 | - \OCP\Activity\IManager $activityManager, |
|
| 118 | - \OCP\Share\IManager $shareManager, |
|
| 119 | - ISession $session, |
|
| 120 | - IPreview $previewManager, |
|
| 121 | - IRootFolder $rootFolder, |
|
| 122 | - FederatedShareProvider $federatedShareProvider, |
|
| 123 | - EventDispatcherInterface $eventDispatcher, |
|
| 124 | - IL10N $l10n, |
|
| 125 | - Defaults $defaults) { |
|
| 126 | - parent::__construct($appName, $request); |
|
| 127 | - |
|
| 128 | - $this->config = $config; |
|
| 129 | - $this->urlGenerator = $urlGenerator; |
|
| 130 | - $this->userManager = $userManager; |
|
| 131 | - $this->logger = $logger; |
|
| 132 | - $this->activityManager = $activityManager; |
|
| 133 | - $this->shareManager = $shareManager; |
|
| 134 | - $this->session = $session; |
|
| 135 | - $this->previewManager = $previewManager; |
|
| 136 | - $this->rootFolder = $rootFolder; |
|
| 137 | - $this->federatedShareProvider = $federatedShareProvider; |
|
| 138 | - $this->eventDispatcher = $eventDispatcher; |
|
| 139 | - $this->l10n = $l10n; |
|
| 140 | - $this->defaults = $defaults; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @PublicPage |
|
| 145 | - * @NoCSRFRequired |
|
| 146 | - * |
|
| 147 | - * @param string $token |
|
| 148 | - * @return TemplateResponse|RedirectResponse |
|
| 149 | - */ |
|
| 150 | - public function showAuthenticate($token) { |
|
| 151 | - $share = $this->shareManager->getShareByToken($token); |
|
| 152 | - |
|
| 153 | - if($this->linkShareAuth($share)) { |
|
| 154 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return new TemplateResponse($this->appName, 'authenticate', array(), 'guest'); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @PublicPage |
|
| 162 | - * @UseSession |
|
| 163 | - * @BruteForceProtection(action=publicLinkAuth) |
|
| 164 | - * |
|
| 165 | - * Authenticates against password-protected shares |
|
| 166 | - * @param string $token |
|
| 167 | - * @param string $password |
|
| 168 | - * @return RedirectResponse|TemplateResponse|NotFoundResponse |
|
| 169 | - */ |
|
| 170 | - public function authenticate($token, $password = '') { |
|
| 171 | - |
|
| 172 | - // Check whether share exists |
|
| 173 | - try { |
|
| 174 | - $share = $this->shareManager->getShareByToken($token); |
|
| 175 | - } catch (ShareNotFound $e) { |
|
| 176 | - return new NotFoundResponse(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $authenticate = $this->linkShareAuth($share, $password); |
|
| 180 | - |
|
| 181 | - if($authenticate === true) { |
|
| 182 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $response = new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest'); |
|
| 186 | - $response->throttle(); |
|
| 187 | - return $response; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Authenticate a link item with the given password. |
|
| 192 | - * Or use the session if no password is provided. |
|
| 193 | - * |
|
| 194 | - * This is a modified version of Helper::authenticate |
|
| 195 | - * TODO: Try to merge back eventually with Helper::authenticate |
|
| 196 | - * |
|
| 197 | - * @param \OCP\Share\IShare $share |
|
| 198 | - * @param string|null $password |
|
| 199 | - * @return bool |
|
| 200 | - */ |
|
| 201 | - private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { |
|
| 202 | - if ($password !== null) { |
|
| 203 | - if ($this->shareManager->checkPassword($share, $password)) { |
|
| 204 | - $this->session->set('public_link_authenticated', (string)$share->getId()); |
|
| 205 | - } else { |
|
| 206 | - $this->emitAccessShareHook($share, 403, 'Wrong password'); |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - } else { |
|
| 210 | - // not authenticated ? |
|
| 211 | - if ( ! $this->session->exists('public_link_authenticated') |
|
| 212 | - || $this->session->get('public_link_authenticated') !== (string)$share->getId()) { |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - return true; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * throws hooks when a share is attempted to be accessed |
|
| 221 | - * |
|
| 222 | - * @param \OCP\Share\IShare|string $share the Share instance if available, |
|
| 223 | - * otherwise token |
|
| 224 | - * @param int $errorCode |
|
| 225 | - * @param string $errorMessage |
|
| 226 | - * @throws \OC\HintException |
|
| 227 | - * @throws \OC\ServerNotAvailableException |
|
| 228 | - */ |
|
| 229 | - protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { |
|
| 230 | - $itemType = $itemSource = $uidOwner = ''; |
|
| 231 | - $token = $share; |
|
| 232 | - $exception = null; |
|
| 233 | - if($share instanceof \OCP\Share\IShare) { |
|
| 234 | - try { |
|
| 235 | - $token = $share->getToken(); |
|
| 236 | - $uidOwner = $share->getSharedBy(); |
|
| 237 | - $itemType = $share->getNodeType(); |
|
| 238 | - $itemSource = $share->getNodeId(); |
|
| 239 | - } catch (\Exception $e) { |
|
| 240 | - // we log what we know and pass on the exception afterwards |
|
| 241 | - $exception = $e; |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - \OC_Hook::emit('OCP\Share', 'share_link_access', [ |
|
| 245 | - 'itemType' => $itemType, |
|
| 246 | - 'itemSource' => $itemSource, |
|
| 247 | - 'uidOwner' => $uidOwner, |
|
| 248 | - 'token' => $token, |
|
| 249 | - 'errorCode' => $errorCode, |
|
| 250 | - 'errorMessage' => $errorMessage, |
|
| 251 | - ]); |
|
| 252 | - if(!is_null($exception)) { |
|
| 253 | - throw $exception; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Validate the permissions of the share |
|
| 259 | - * |
|
| 260 | - * @param Share\IShare $share |
|
| 261 | - * @return bool |
|
| 262 | - */ |
|
| 263 | - private function validateShare(\OCP\Share\IShare $share) { |
|
| 264 | - return $share->getNode()->isReadable() && $share->getNode()->isShareable(); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @PublicPage |
|
| 269 | - * @NoCSRFRequired |
|
| 270 | - * |
|
| 271 | - * @param string $token |
|
| 272 | - * @param string $path |
|
| 273 | - * @return TemplateResponse|RedirectResponse|NotFoundResponse |
|
| 274 | - * @throws NotFoundException |
|
| 275 | - * @throws \Exception |
|
| 276 | - */ |
|
| 277 | - public function showShare($token, $path = '') { |
|
| 278 | - \OC_User::setIncognitoMode(true); |
|
| 279 | - |
|
| 280 | - // Check whether share exists |
|
| 281 | - try { |
|
| 282 | - $share = $this->shareManager->getShareByToken($token); |
|
| 283 | - } catch (ShareNotFound $e) { |
|
| 284 | - $this->emitAccessShareHook($token, 404, 'Share not found'); |
|
| 285 | - return new NotFoundResponse(); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - // Share is password protected - check whether the user is permitted to access the share |
|
| 289 | - if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 290 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 291 | - array('token' => $token))); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - if (!$this->validateShare($share)) { |
|
| 295 | - throw new NotFoundException(); |
|
| 296 | - } |
|
| 297 | - // We can't get the path of a file share |
|
| 298 | - try { |
|
| 299 | - if ($share->getNode() instanceof \OCP\Files\File && $path !== '') { |
|
| 300 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 301 | - throw new NotFoundException(); |
|
| 302 | - } |
|
| 303 | - } catch (\Exception $e) { |
|
| 304 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 305 | - throw $e; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - $shareTmpl = []; |
|
| 309 | - $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 310 | - $shareTmpl['owner'] = $share->getShareOwner(); |
|
| 311 | - $shareTmpl['filename'] = $share->getNode()->getName(); |
|
| 312 | - $shareTmpl['directory_path'] = $share->getTarget(); |
|
| 313 | - $shareTmpl['mimetype'] = $share->getNode()->getMimetype(); |
|
| 314 | - $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($share->getNode()->getMimetype()); |
|
| 315 | - $shareTmpl['dirToken'] = $token; |
|
| 316 | - $shareTmpl['sharingToken'] = $token; |
|
| 317 | - $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
| 318 | - $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
|
| 319 | - $shareTmpl['dir'] = ''; |
|
| 320 | - $shareTmpl['nonHumanFileSize'] = $share->getNode()->getSize(); |
|
| 321 | - $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($share->getNode()->getSize()); |
|
| 322 | - |
|
| 323 | - // Show file list |
|
| 324 | - $hideFileList = false; |
|
| 325 | - if ($share->getNode() instanceof \OCP\Files\Folder) { |
|
| 326 | - /** @var \OCP\Files\Folder $rootFolder */ |
|
| 327 | - $rootFolder = $share->getNode(); |
|
| 328 | - |
|
| 329 | - try { |
|
| 330 | - $folderNode = $rootFolder->get($path); |
|
| 331 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 332 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 333 | - throw new NotFoundException(); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $shareTmpl['dir'] = $rootFolder->getRelativePath($folderNode->getPath()); |
|
| 337 | - |
|
| 338 | - /* |
|
| 67 | + /** @var IConfig */ |
|
| 68 | + protected $config; |
|
| 69 | + /** @var IURLGenerator */ |
|
| 70 | + protected $urlGenerator; |
|
| 71 | + /** @var IUserManager */ |
|
| 72 | + protected $userManager; |
|
| 73 | + /** @var ILogger */ |
|
| 74 | + protected $logger; |
|
| 75 | + /** @var \OCP\Activity\IManager */ |
|
| 76 | + protected $activityManager; |
|
| 77 | + /** @var \OCP\Share\IManager */ |
|
| 78 | + protected $shareManager; |
|
| 79 | + /** @var ISession */ |
|
| 80 | + protected $session; |
|
| 81 | + /** @var IPreview */ |
|
| 82 | + protected $previewManager; |
|
| 83 | + /** @var IRootFolder */ |
|
| 84 | + protected $rootFolder; |
|
| 85 | + /** @var FederatedShareProvider */ |
|
| 86 | + protected $federatedShareProvider; |
|
| 87 | + /** @var EventDispatcherInterface */ |
|
| 88 | + protected $eventDispatcher; |
|
| 89 | + /** @var IL10N */ |
|
| 90 | + protected $l10n; |
|
| 91 | + /** @var Defaults */ |
|
| 92 | + protected $defaults; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param string $appName |
|
| 96 | + * @param IRequest $request |
|
| 97 | + * @param IConfig $config |
|
| 98 | + * @param IURLGenerator $urlGenerator |
|
| 99 | + * @param IUserManager $userManager |
|
| 100 | + * @param ILogger $logger |
|
| 101 | + * @param \OCP\Activity\IManager $activityManager |
|
| 102 | + * @param \OCP\Share\IManager $shareManager |
|
| 103 | + * @param ISession $session |
|
| 104 | + * @param IPreview $previewManager |
|
| 105 | + * @param IRootFolder $rootFolder |
|
| 106 | + * @param FederatedShareProvider $federatedShareProvider |
|
| 107 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 108 | + * @param IL10N $l10n |
|
| 109 | + * @param Defaults $defaults |
|
| 110 | + */ |
|
| 111 | + public function __construct($appName, |
|
| 112 | + IRequest $request, |
|
| 113 | + IConfig $config, |
|
| 114 | + IURLGenerator $urlGenerator, |
|
| 115 | + IUserManager $userManager, |
|
| 116 | + ILogger $logger, |
|
| 117 | + \OCP\Activity\IManager $activityManager, |
|
| 118 | + \OCP\Share\IManager $shareManager, |
|
| 119 | + ISession $session, |
|
| 120 | + IPreview $previewManager, |
|
| 121 | + IRootFolder $rootFolder, |
|
| 122 | + FederatedShareProvider $federatedShareProvider, |
|
| 123 | + EventDispatcherInterface $eventDispatcher, |
|
| 124 | + IL10N $l10n, |
|
| 125 | + Defaults $defaults) { |
|
| 126 | + parent::__construct($appName, $request); |
|
| 127 | + |
|
| 128 | + $this->config = $config; |
|
| 129 | + $this->urlGenerator = $urlGenerator; |
|
| 130 | + $this->userManager = $userManager; |
|
| 131 | + $this->logger = $logger; |
|
| 132 | + $this->activityManager = $activityManager; |
|
| 133 | + $this->shareManager = $shareManager; |
|
| 134 | + $this->session = $session; |
|
| 135 | + $this->previewManager = $previewManager; |
|
| 136 | + $this->rootFolder = $rootFolder; |
|
| 137 | + $this->federatedShareProvider = $federatedShareProvider; |
|
| 138 | + $this->eventDispatcher = $eventDispatcher; |
|
| 139 | + $this->l10n = $l10n; |
|
| 140 | + $this->defaults = $defaults; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @PublicPage |
|
| 145 | + * @NoCSRFRequired |
|
| 146 | + * |
|
| 147 | + * @param string $token |
|
| 148 | + * @return TemplateResponse|RedirectResponse |
|
| 149 | + */ |
|
| 150 | + public function showAuthenticate($token) { |
|
| 151 | + $share = $this->shareManager->getShareByToken($token); |
|
| 152 | + |
|
| 153 | + if($this->linkShareAuth($share)) { |
|
| 154 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return new TemplateResponse($this->appName, 'authenticate', array(), 'guest'); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @PublicPage |
|
| 162 | + * @UseSession |
|
| 163 | + * @BruteForceProtection(action=publicLinkAuth) |
|
| 164 | + * |
|
| 165 | + * Authenticates against password-protected shares |
|
| 166 | + * @param string $token |
|
| 167 | + * @param string $password |
|
| 168 | + * @return RedirectResponse|TemplateResponse|NotFoundResponse |
|
| 169 | + */ |
|
| 170 | + public function authenticate($token, $password = '') { |
|
| 171 | + |
|
| 172 | + // Check whether share exists |
|
| 173 | + try { |
|
| 174 | + $share = $this->shareManager->getShareByToken($token); |
|
| 175 | + } catch (ShareNotFound $e) { |
|
| 176 | + return new NotFoundResponse(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $authenticate = $this->linkShareAuth($share, $password); |
|
| 180 | + |
|
| 181 | + if($authenticate === true) { |
|
| 182 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $response = new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest'); |
|
| 186 | + $response->throttle(); |
|
| 187 | + return $response; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Authenticate a link item with the given password. |
|
| 192 | + * Or use the session if no password is provided. |
|
| 193 | + * |
|
| 194 | + * This is a modified version of Helper::authenticate |
|
| 195 | + * TODO: Try to merge back eventually with Helper::authenticate |
|
| 196 | + * |
|
| 197 | + * @param \OCP\Share\IShare $share |
|
| 198 | + * @param string|null $password |
|
| 199 | + * @return bool |
|
| 200 | + */ |
|
| 201 | + private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { |
|
| 202 | + if ($password !== null) { |
|
| 203 | + if ($this->shareManager->checkPassword($share, $password)) { |
|
| 204 | + $this->session->set('public_link_authenticated', (string)$share->getId()); |
|
| 205 | + } else { |
|
| 206 | + $this->emitAccessShareHook($share, 403, 'Wrong password'); |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + } else { |
|
| 210 | + // not authenticated ? |
|
| 211 | + if ( ! $this->session->exists('public_link_authenticated') |
|
| 212 | + || $this->session->get('public_link_authenticated') !== (string)$share->getId()) { |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + return true; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * throws hooks when a share is attempted to be accessed |
|
| 221 | + * |
|
| 222 | + * @param \OCP\Share\IShare|string $share the Share instance if available, |
|
| 223 | + * otherwise token |
|
| 224 | + * @param int $errorCode |
|
| 225 | + * @param string $errorMessage |
|
| 226 | + * @throws \OC\HintException |
|
| 227 | + * @throws \OC\ServerNotAvailableException |
|
| 228 | + */ |
|
| 229 | + protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { |
|
| 230 | + $itemType = $itemSource = $uidOwner = ''; |
|
| 231 | + $token = $share; |
|
| 232 | + $exception = null; |
|
| 233 | + if($share instanceof \OCP\Share\IShare) { |
|
| 234 | + try { |
|
| 235 | + $token = $share->getToken(); |
|
| 236 | + $uidOwner = $share->getSharedBy(); |
|
| 237 | + $itemType = $share->getNodeType(); |
|
| 238 | + $itemSource = $share->getNodeId(); |
|
| 239 | + } catch (\Exception $e) { |
|
| 240 | + // we log what we know and pass on the exception afterwards |
|
| 241 | + $exception = $e; |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + \OC_Hook::emit('OCP\Share', 'share_link_access', [ |
|
| 245 | + 'itemType' => $itemType, |
|
| 246 | + 'itemSource' => $itemSource, |
|
| 247 | + 'uidOwner' => $uidOwner, |
|
| 248 | + 'token' => $token, |
|
| 249 | + 'errorCode' => $errorCode, |
|
| 250 | + 'errorMessage' => $errorMessage, |
|
| 251 | + ]); |
|
| 252 | + if(!is_null($exception)) { |
|
| 253 | + throw $exception; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Validate the permissions of the share |
|
| 259 | + * |
|
| 260 | + * @param Share\IShare $share |
|
| 261 | + * @return bool |
|
| 262 | + */ |
|
| 263 | + private function validateShare(\OCP\Share\IShare $share) { |
|
| 264 | + return $share->getNode()->isReadable() && $share->getNode()->isShareable(); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @PublicPage |
|
| 269 | + * @NoCSRFRequired |
|
| 270 | + * |
|
| 271 | + * @param string $token |
|
| 272 | + * @param string $path |
|
| 273 | + * @return TemplateResponse|RedirectResponse|NotFoundResponse |
|
| 274 | + * @throws NotFoundException |
|
| 275 | + * @throws \Exception |
|
| 276 | + */ |
|
| 277 | + public function showShare($token, $path = '') { |
|
| 278 | + \OC_User::setIncognitoMode(true); |
|
| 279 | + |
|
| 280 | + // Check whether share exists |
|
| 281 | + try { |
|
| 282 | + $share = $this->shareManager->getShareByToken($token); |
|
| 283 | + } catch (ShareNotFound $e) { |
|
| 284 | + $this->emitAccessShareHook($token, 404, 'Share not found'); |
|
| 285 | + return new NotFoundResponse(); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + // Share is password protected - check whether the user is permitted to access the share |
|
| 289 | + if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 290 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 291 | + array('token' => $token))); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + if (!$this->validateShare($share)) { |
|
| 295 | + throw new NotFoundException(); |
|
| 296 | + } |
|
| 297 | + // We can't get the path of a file share |
|
| 298 | + try { |
|
| 299 | + if ($share->getNode() instanceof \OCP\Files\File && $path !== '') { |
|
| 300 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 301 | + throw new NotFoundException(); |
|
| 302 | + } |
|
| 303 | + } catch (\Exception $e) { |
|
| 304 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 305 | + throw $e; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + $shareTmpl = []; |
|
| 309 | + $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 310 | + $shareTmpl['owner'] = $share->getShareOwner(); |
|
| 311 | + $shareTmpl['filename'] = $share->getNode()->getName(); |
|
| 312 | + $shareTmpl['directory_path'] = $share->getTarget(); |
|
| 313 | + $shareTmpl['mimetype'] = $share->getNode()->getMimetype(); |
|
| 314 | + $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($share->getNode()->getMimetype()); |
|
| 315 | + $shareTmpl['dirToken'] = $token; |
|
| 316 | + $shareTmpl['sharingToken'] = $token; |
|
| 317 | + $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
| 318 | + $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
|
| 319 | + $shareTmpl['dir'] = ''; |
|
| 320 | + $shareTmpl['nonHumanFileSize'] = $share->getNode()->getSize(); |
|
| 321 | + $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($share->getNode()->getSize()); |
|
| 322 | + |
|
| 323 | + // Show file list |
|
| 324 | + $hideFileList = false; |
|
| 325 | + if ($share->getNode() instanceof \OCP\Files\Folder) { |
|
| 326 | + /** @var \OCP\Files\Folder $rootFolder */ |
|
| 327 | + $rootFolder = $share->getNode(); |
|
| 328 | + |
|
| 329 | + try { |
|
| 330 | + $folderNode = $rootFolder->get($path); |
|
| 331 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 332 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 333 | + throw new NotFoundException(); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $shareTmpl['dir'] = $rootFolder->getRelativePath($folderNode->getPath()); |
|
| 337 | + |
|
| 338 | + /* |
|
| 339 | 339 | * The OC_Util methods require a view. This just uses the node API |
| 340 | 340 | */ |
| 341 | - $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
|
| 342 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 343 | - $freeSpace = max($freeSpace, 0); |
|
| 344 | - } else { |
|
| 345 | - $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $hideFileList = $share->getPermissions() & \OCP\Constants::PERMISSION_READ ? false : true; |
|
| 349 | - $maxUploadFilesize = $freeSpace; |
|
| 350 | - |
|
| 351 | - $folder = new Template('files', 'list', ''); |
|
| 352 | - $folder->assign('dir', $rootFolder->getRelativePath($folderNode->getPath())); |
|
| 353 | - $folder->assign('dirToken', $token); |
|
| 354 | - $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
|
| 355 | - $folder->assign('isPublic', true); |
|
| 356 | - $folder->assign('hideFileList', $hideFileList); |
|
| 357 | - $folder->assign('publicUploadEnabled', 'no'); |
|
| 358 | - $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
|
| 359 | - $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
|
| 360 | - $folder->assign('freeSpace', $freeSpace); |
|
| 361 | - $folder->assign('usedSpacePercent', 0); |
|
| 362 | - $folder->assign('trash', false); |
|
| 363 | - $shareTmpl['folder'] = $folder->fetchPage(); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - $shareTmpl['hideFileList'] = $hideFileList; |
|
| 367 | - $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 368 | - $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $token]); |
|
| 369 | - $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 370 | - $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
|
| 371 | - $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
|
| 372 | - $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
|
| 373 | - $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
|
| 374 | - $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
|
| 375 | - $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
|
| 376 | - if ($shareTmpl['previewSupported']) { |
|
| 377 | - $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 378 | - ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); |
|
| 379 | - // We just have direct previews for image files |
|
| 380 | - if ($share->getNode()->getMimePart() === 'image') { |
|
| 381 | - $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); |
|
| 382 | - } |
|
| 383 | - } else { |
|
| 384 | - $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - // Load files we need |
|
| 388 | - \OCP\Util::addScript('files', 'file-upload'); |
|
| 389 | - \OCP\Util::addStyle('files_sharing', 'publicView'); |
|
| 390 | - \OCP\Util::addScript('files_sharing', 'public'); |
|
| 391 | - \OCP\Util::addScript('files', 'fileactions'); |
|
| 392 | - \OCP\Util::addScript('files', 'fileactionsmenu'); |
|
| 393 | - \OCP\Util::addScript('files', 'jquery.fileupload'); |
|
| 394 | - \OCP\Util::addScript('files_sharing', 'files_drop'); |
|
| 395 | - |
|
| 396 | - if (isset($shareTmpl['folder'])) { |
|
| 397 | - // JS required for folders |
|
| 398 | - \OCP\Util::addStyle('files', 'merged'); |
|
| 399 | - \OCP\Util::addScript('files', 'filesummary'); |
|
| 400 | - \OCP\Util::addScript('files', 'breadcrumb'); |
|
| 401 | - \OCP\Util::addScript('files', 'fileinfomodel'); |
|
| 402 | - \OCP\Util::addScript('files', 'newfilemenu'); |
|
| 403 | - \OCP\Util::addScript('files', 'files'); |
|
| 404 | - \OCP\Util::addScript('files', 'filelist'); |
|
| 405 | - \OCP\Util::addScript('files', 'keyboardshortcuts'); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - // OpenGraph Support: http://ogp.me/ |
|
| 409 | - \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
|
| 410 | - \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->l10n->t('%s is publicly shared', [$shareTmpl['filename']])]); |
|
| 411 | - \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
|
| 412 | - \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
|
| 413 | - \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
|
| 414 | - \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $shareTmpl['previewImage']]); |
|
| 415 | - |
|
| 416 | - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts'); |
|
| 417 | - |
|
| 418 | - $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
|
| 419 | - $csp->addAllowedFrameDomain('\'self\''); |
|
| 420 | - $response = new TemplateResponse($this->appName, 'public', $shareTmpl, 'base'); |
|
| 421 | - $response->setContentSecurityPolicy($csp); |
|
| 422 | - |
|
| 423 | - $this->emitAccessShareHook($share); |
|
| 424 | - |
|
| 425 | - return $response; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * @PublicPage |
|
| 430 | - * @NoCSRFRequired |
|
| 431 | - * |
|
| 432 | - * @param string $token |
|
| 433 | - * @param string $files |
|
| 434 | - * @param string $path |
|
| 435 | - * @param string $downloadStartSecret |
|
| 436 | - * @return void|\OCP\AppFramework\Http\Response |
|
| 437 | - * @throws NotFoundException |
|
| 438 | - */ |
|
| 439 | - public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { |
|
| 440 | - \OC_User::setIncognitoMode(true); |
|
| 441 | - |
|
| 442 | - $share = $this->shareManager->getShareByToken($token); |
|
| 443 | - |
|
| 444 | - if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 445 | - return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - // Share is password protected - check whether the user is permitted to access the share |
|
| 449 | - if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 450 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 451 | - ['token' => $token])); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - $files_list = null; |
|
| 455 | - if (!is_null($files)) { // download selected files |
|
| 456 | - $files_list = json_decode($files); |
|
| 457 | - // in case we get only a single file |
|
| 458 | - if ($files_list === null) { |
|
| 459 | - $files_list = [$files]; |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 464 | - $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
| 465 | - |
|
| 466 | - if (!$this->validateShare($share)) { |
|
| 467 | - throw new NotFoundException(); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - // Single file share |
|
| 471 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 472 | - // Single file download |
|
| 473 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 474 | - } |
|
| 475 | - // Directory share |
|
| 476 | - else { |
|
| 477 | - /** @var \OCP\Files\Folder $node */ |
|
| 478 | - $node = $share->getNode(); |
|
| 479 | - |
|
| 480 | - // Try to get the path |
|
| 481 | - if ($path !== '') { |
|
| 482 | - try { |
|
| 483 | - $node = $node->get($path); |
|
| 484 | - } catch (NotFoundException $e) { |
|
| 485 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 486 | - return new NotFoundResponse(); |
|
| 487 | - } |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - $originalSharePath = $userFolder->getRelativePath($node->getPath()); |
|
| 491 | - |
|
| 492 | - if ($node instanceof \OCP\Files\File) { |
|
| 493 | - // Single file download |
|
| 494 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 495 | - } else if (!empty($files_list)) { |
|
| 496 | - $this->fileListDownloaded($share, $files_list, $node); |
|
| 497 | - } else { |
|
| 498 | - // The folder is downloaded |
|
| 499 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 500 | - } |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /* FIXME: We should do this all nicely in OCP */ |
|
| 504 | - OC_Util::tearDownFS(); |
|
| 505 | - OC_Util::setupFS($share->getShareOwner()); |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * this sets a cookie to be able to recognize the start of the download |
|
| 509 | - * the content must not be longer than 32 characters and must only contain |
|
| 510 | - * alphanumeric characters |
|
| 511 | - */ |
|
| 512 | - if (!empty($downloadStartSecret) |
|
| 513 | - && !isset($downloadStartSecret[32]) |
|
| 514 | - && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { |
|
| 515 | - |
|
| 516 | - // FIXME: set on the response once we use an actual app framework response |
|
| 517 | - setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - $this->emitAccessShareHook($share); |
|
| 521 | - |
|
| 522 | - $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' ); |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * Http range requests support |
|
| 526 | - */ |
|
| 527 | - if (isset($_SERVER['HTTP_RANGE'])) { |
|
| 528 | - $server_params['range'] = $this->request->getHeader('Range'); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - // download selected files |
|
| 532 | - if (!is_null($files) && $files !== '') { |
|
| 533 | - // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 534 | - // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 535 | - OC_Files::get($originalSharePath, $files_list, $server_params); |
|
| 536 | - exit(); |
|
| 537 | - } else { |
|
| 538 | - // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 539 | - // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 540 | - OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); |
|
| 541 | - exit(); |
|
| 542 | - } |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * create activity for every downloaded file |
|
| 547 | - * |
|
| 548 | - * @param Share\IShare $share |
|
| 549 | - * @param array $files_list |
|
| 550 | - * @param \OCP\Files\Folder $node |
|
| 551 | - */ |
|
| 552 | - protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) { |
|
| 553 | - foreach ($files_list as $file) { |
|
| 554 | - $subNode = $node->get($file); |
|
| 555 | - $this->singleFileDownloaded($share, $subNode); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * create activity if a single file was downloaded from a link share |
|
| 562 | - * |
|
| 563 | - * @param Share\IShare $share |
|
| 564 | - */ |
|
| 565 | - protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) { |
|
| 566 | - |
|
| 567 | - $fileId = $node->getId(); |
|
| 568 | - |
|
| 569 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 570 | - $userNodeList = $userFolder->getById($fileId); |
|
| 571 | - $userNode = $userNodeList[0]; |
|
| 572 | - $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 573 | - $userPath = $userFolder->getRelativePath($userNode->getPath()); |
|
| 574 | - $ownerPath = $ownerFolder->getRelativePath($node->getPath()); |
|
| 575 | - |
|
| 576 | - $parameters = [$userPath]; |
|
| 577 | - |
|
| 578 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 579 | - if ($node instanceof \OCP\Files\File) { |
|
| 580 | - $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
|
| 581 | - } else { |
|
| 582 | - $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
|
| 583 | - } |
|
| 584 | - $parameters[] = $share->getSharedWith(); |
|
| 585 | - } else { |
|
| 586 | - if ($node instanceof \OCP\Files\File) { |
|
| 587 | - $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
|
| 588 | - } else { |
|
| 589 | - $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
|
| 590 | - } |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
|
| 594 | - |
|
| 595 | - if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 596 | - $parameters[0] = $ownerPath; |
|
| 597 | - $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
|
| 598 | - } |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * publish activity |
|
| 603 | - * |
|
| 604 | - * @param string $subject |
|
| 605 | - * @param array $parameters |
|
| 606 | - * @param string $affectedUser |
|
| 607 | - * @param int $fileId |
|
| 608 | - * @param string $filePath |
|
| 609 | - */ |
|
| 610 | - protected function publishActivity($subject, |
|
| 611 | - array $parameters, |
|
| 612 | - $affectedUser, |
|
| 613 | - $fileId, |
|
| 614 | - $filePath) { |
|
| 615 | - |
|
| 616 | - $event = $this->activityManager->generateEvent(); |
|
| 617 | - $event->setApp('files_sharing') |
|
| 618 | - ->setType('public_links') |
|
| 619 | - ->setSubject($subject, $parameters) |
|
| 620 | - ->setAffectedUser($affectedUser) |
|
| 621 | - ->setObject('files', $fileId, $filePath); |
|
| 622 | - $this->activityManager->publish($event); |
|
| 623 | - } |
|
| 341 | + $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
|
| 342 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 343 | + $freeSpace = max($freeSpace, 0); |
|
| 344 | + } else { |
|
| 345 | + $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + $hideFileList = $share->getPermissions() & \OCP\Constants::PERMISSION_READ ? false : true; |
|
| 349 | + $maxUploadFilesize = $freeSpace; |
|
| 350 | + |
|
| 351 | + $folder = new Template('files', 'list', ''); |
|
| 352 | + $folder->assign('dir', $rootFolder->getRelativePath($folderNode->getPath())); |
|
| 353 | + $folder->assign('dirToken', $token); |
|
| 354 | + $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
|
| 355 | + $folder->assign('isPublic', true); |
|
| 356 | + $folder->assign('hideFileList', $hideFileList); |
|
| 357 | + $folder->assign('publicUploadEnabled', 'no'); |
|
| 358 | + $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
|
| 359 | + $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
|
| 360 | + $folder->assign('freeSpace', $freeSpace); |
|
| 361 | + $folder->assign('usedSpacePercent', 0); |
|
| 362 | + $folder->assign('trash', false); |
|
| 363 | + $shareTmpl['folder'] = $folder->fetchPage(); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + $shareTmpl['hideFileList'] = $hideFileList; |
|
| 367 | + $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 368 | + $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $token]); |
|
| 369 | + $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 370 | + $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
|
| 371 | + $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
|
| 372 | + $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
|
| 373 | + $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
|
| 374 | + $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
|
| 375 | + $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
|
| 376 | + if ($shareTmpl['previewSupported']) { |
|
| 377 | + $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 378 | + ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); |
|
| 379 | + // We just have direct previews for image files |
|
| 380 | + if ($share->getNode()->getMimePart() === 'image') { |
|
| 381 | + $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); |
|
| 382 | + } |
|
| 383 | + } else { |
|
| 384 | + $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + // Load files we need |
|
| 388 | + \OCP\Util::addScript('files', 'file-upload'); |
|
| 389 | + \OCP\Util::addStyle('files_sharing', 'publicView'); |
|
| 390 | + \OCP\Util::addScript('files_sharing', 'public'); |
|
| 391 | + \OCP\Util::addScript('files', 'fileactions'); |
|
| 392 | + \OCP\Util::addScript('files', 'fileactionsmenu'); |
|
| 393 | + \OCP\Util::addScript('files', 'jquery.fileupload'); |
|
| 394 | + \OCP\Util::addScript('files_sharing', 'files_drop'); |
|
| 395 | + |
|
| 396 | + if (isset($shareTmpl['folder'])) { |
|
| 397 | + // JS required for folders |
|
| 398 | + \OCP\Util::addStyle('files', 'merged'); |
|
| 399 | + \OCP\Util::addScript('files', 'filesummary'); |
|
| 400 | + \OCP\Util::addScript('files', 'breadcrumb'); |
|
| 401 | + \OCP\Util::addScript('files', 'fileinfomodel'); |
|
| 402 | + \OCP\Util::addScript('files', 'newfilemenu'); |
|
| 403 | + \OCP\Util::addScript('files', 'files'); |
|
| 404 | + \OCP\Util::addScript('files', 'filelist'); |
|
| 405 | + \OCP\Util::addScript('files', 'keyboardshortcuts'); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + // OpenGraph Support: http://ogp.me/ |
|
| 409 | + \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
|
| 410 | + \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->l10n->t('%s is publicly shared', [$shareTmpl['filename']])]); |
|
| 411 | + \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
|
| 412 | + \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
|
| 413 | + \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
|
| 414 | + \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $shareTmpl['previewImage']]); |
|
| 415 | + |
|
| 416 | + $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts'); |
|
| 417 | + |
|
| 418 | + $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
|
| 419 | + $csp->addAllowedFrameDomain('\'self\''); |
|
| 420 | + $response = new TemplateResponse($this->appName, 'public', $shareTmpl, 'base'); |
|
| 421 | + $response->setContentSecurityPolicy($csp); |
|
| 422 | + |
|
| 423 | + $this->emitAccessShareHook($share); |
|
| 424 | + |
|
| 425 | + return $response; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * @PublicPage |
|
| 430 | + * @NoCSRFRequired |
|
| 431 | + * |
|
| 432 | + * @param string $token |
|
| 433 | + * @param string $files |
|
| 434 | + * @param string $path |
|
| 435 | + * @param string $downloadStartSecret |
|
| 436 | + * @return void|\OCP\AppFramework\Http\Response |
|
| 437 | + * @throws NotFoundException |
|
| 438 | + */ |
|
| 439 | + public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { |
|
| 440 | + \OC_User::setIncognitoMode(true); |
|
| 441 | + |
|
| 442 | + $share = $this->shareManager->getShareByToken($token); |
|
| 443 | + |
|
| 444 | + if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 445 | + return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + // Share is password protected - check whether the user is permitted to access the share |
|
| 449 | + if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 450 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 451 | + ['token' => $token])); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + $files_list = null; |
|
| 455 | + if (!is_null($files)) { // download selected files |
|
| 456 | + $files_list = json_decode($files); |
|
| 457 | + // in case we get only a single file |
|
| 458 | + if ($files_list === null) { |
|
| 459 | + $files_list = [$files]; |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 464 | + $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
| 465 | + |
|
| 466 | + if (!$this->validateShare($share)) { |
|
| 467 | + throw new NotFoundException(); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + // Single file share |
|
| 471 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 472 | + // Single file download |
|
| 473 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 474 | + } |
|
| 475 | + // Directory share |
|
| 476 | + else { |
|
| 477 | + /** @var \OCP\Files\Folder $node */ |
|
| 478 | + $node = $share->getNode(); |
|
| 479 | + |
|
| 480 | + // Try to get the path |
|
| 481 | + if ($path !== '') { |
|
| 482 | + try { |
|
| 483 | + $node = $node->get($path); |
|
| 484 | + } catch (NotFoundException $e) { |
|
| 485 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 486 | + return new NotFoundResponse(); |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + $originalSharePath = $userFolder->getRelativePath($node->getPath()); |
|
| 491 | + |
|
| 492 | + if ($node instanceof \OCP\Files\File) { |
|
| 493 | + // Single file download |
|
| 494 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 495 | + } else if (!empty($files_list)) { |
|
| 496 | + $this->fileListDownloaded($share, $files_list, $node); |
|
| 497 | + } else { |
|
| 498 | + // The folder is downloaded |
|
| 499 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 500 | + } |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /* FIXME: We should do this all nicely in OCP */ |
|
| 504 | + OC_Util::tearDownFS(); |
|
| 505 | + OC_Util::setupFS($share->getShareOwner()); |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * this sets a cookie to be able to recognize the start of the download |
|
| 509 | + * the content must not be longer than 32 characters and must only contain |
|
| 510 | + * alphanumeric characters |
|
| 511 | + */ |
|
| 512 | + if (!empty($downloadStartSecret) |
|
| 513 | + && !isset($downloadStartSecret[32]) |
|
| 514 | + && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { |
|
| 515 | + |
|
| 516 | + // FIXME: set on the response once we use an actual app framework response |
|
| 517 | + setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + $this->emitAccessShareHook($share); |
|
| 521 | + |
|
| 522 | + $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' ); |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * Http range requests support |
|
| 526 | + */ |
|
| 527 | + if (isset($_SERVER['HTTP_RANGE'])) { |
|
| 528 | + $server_params['range'] = $this->request->getHeader('Range'); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + // download selected files |
|
| 532 | + if (!is_null($files) && $files !== '') { |
|
| 533 | + // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 534 | + // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 535 | + OC_Files::get($originalSharePath, $files_list, $server_params); |
|
| 536 | + exit(); |
|
| 537 | + } else { |
|
| 538 | + // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 539 | + // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 540 | + OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); |
|
| 541 | + exit(); |
|
| 542 | + } |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * create activity for every downloaded file |
|
| 547 | + * |
|
| 548 | + * @param Share\IShare $share |
|
| 549 | + * @param array $files_list |
|
| 550 | + * @param \OCP\Files\Folder $node |
|
| 551 | + */ |
|
| 552 | + protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) { |
|
| 553 | + foreach ($files_list as $file) { |
|
| 554 | + $subNode = $node->get($file); |
|
| 555 | + $this->singleFileDownloaded($share, $subNode); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * create activity if a single file was downloaded from a link share |
|
| 562 | + * |
|
| 563 | + * @param Share\IShare $share |
|
| 564 | + */ |
|
| 565 | + protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) { |
|
| 566 | + |
|
| 567 | + $fileId = $node->getId(); |
|
| 568 | + |
|
| 569 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 570 | + $userNodeList = $userFolder->getById($fileId); |
|
| 571 | + $userNode = $userNodeList[0]; |
|
| 572 | + $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 573 | + $userPath = $userFolder->getRelativePath($userNode->getPath()); |
|
| 574 | + $ownerPath = $ownerFolder->getRelativePath($node->getPath()); |
|
| 575 | + |
|
| 576 | + $parameters = [$userPath]; |
|
| 577 | + |
|
| 578 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 579 | + if ($node instanceof \OCP\Files\File) { |
|
| 580 | + $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
|
| 581 | + } else { |
|
| 582 | + $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
|
| 583 | + } |
|
| 584 | + $parameters[] = $share->getSharedWith(); |
|
| 585 | + } else { |
|
| 586 | + if ($node instanceof \OCP\Files\File) { |
|
| 587 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
|
| 588 | + } else { |
|
| 589 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
|
| 590 | + } |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
|
| 594 | + |
|
| 595 | + if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 596 | + $parameters[0] = $ownerPath; |
|
| 597 | + $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
|
| 598 | + } |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * publish activity |
|
| 603 | + * |
|
| 604 | + * @param string $subject |
|
| 605 | + * @param array $parameters |
|
| 606 | + * @param string $affectedUser |
|
| 607 | + * @param int $fileId |
|
| 608 | + * @param string $filePath |
|
| 609 | + */ |
|
| 610 | + protected function publishActivity($subject, |
|
| 611 | + array $parameters, |
|
| 612 | + $affectedUser, |
|
| 613 | + $fileId, |
|
| 614 | + $filePath) { |
|
| 615 | + |
|
| 616 | + $event = $this->activityManager->generateEvent(); |
|
| 617 | + $event->setApp('files_sharing') |
|
| 618 | + ->setType('public_links') |
|
| 619 | + ->setSubject($subject, $parameters) |
|
| 620 | + ->setAffectedUser($affectedUser) |
|
| 621 | + ->setObject('files', $fileId, $filePath); |
|
| 622 | + $this->activityManager->publish($event); |
|
| 623 | + } |
|
| 624 | 624 | |
| 625 | 625 | |
| 626 | 626 | } |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | public function showAuthenticate($token) { |
| 151 | 151 | $share = $this->shareManager->getShareByToken($token); |
| 152 | 152 | |
| 153 | - if($this->linkShareAuth($share)) { |
|
| 153 | + if ($this->linkShareAuth($share)) { |
|
| 154 | 154 | return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
| 155 | 155 | } |
| 156 | 156 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | |
| 179 | 179 | $authenticate = $this->linkShareAuth($share, $password); |
| 180 | 180 | |
| 181 | - if($authenticate === true) { |
|
| 181 | + if ($authenticate === true) { |
|
| 182 | 182 | return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -201,15 +201,15 @@ discard block |
||
| 201 | 201 | private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { |
| 202 | 202 | if ($password !== null) { |
| 203 | 203 | if ($this->shareManager->checkPassword($share, $password)) { |
| 204 | - $this->session->set('public_link_authenticated', (string)$share->getId()); |
|
| 204 | + $this->session->set('public_link_authenticated', (string) $share->getId()); |
|
| 205 | 205 | } else { |
| 206 | 206 | $this->emitAccessShareHook($share, 403, 'Wrong password'); |
| 207 | 207 | return false; |
| 208 | 208 | } |
| 209 | 209 | } else { |
| 210 | 210 | // not authenticated ? |
| 211 | - if ( ! $this->session->exists('public_link_authenticated') |
|
| 212 | - || $this->session->get('public_link_authenticated') !== (string)$share->getId()) { |
|
| 211 | + if (!$this->session->exists('public_link_authenticated') |
|
| 212 | + || $this->session->get('public_link_authenticated') !== (string) $share->getId()) { |
|
| 213 | 213 | return false; |
| 214 | 214 | } |
| 215 | 215 | } |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | $itemType = $itemSource = $uidOwner = ''; |
| 231 | 231 | $token = $share; |
| 232 | 232 | $exception = null; |
| 233 | - if($share instanceof \OCP\Share\IShare) { |
|
| 233 | + if ($share instanceof \OCP\Share\IShare) { |
|
| 234 | 234 | try { |
| 235 | 235 | $token = $share->getToken(); |
| 236 | 236 | $uidOwner = $share->getSharedBy(); |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | 'errorCode' => $errorCode, |
| 250 | 250 | 'errorMessage' => $errorMessage, |
| 251 | 251 | ]); |
| 252 | - if(!is_null($exception)) { |
|
| 252 | + if (!is_null($exception)) { |
|
| 253 | 253 | throw $exception; |
| 254 | 254 | } |
| 255 | 255 | } |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
| 375 | 375 | $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
| 376 | 376 | if ($shareTmpl['previewSupported']) { |
| 377 | - $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 377 | + $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', |
|
| 378 | 378 | ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); |
| 379 | 379 | // We just have direct previews for image files |
| 380 | 380 | if ($share->getNode()->getMimePart() === 'image') { |
@@ -406,7 +406,7 @@ discard block |
||
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | // OpenGraph Support: http://ogp.me/ |
| 409 | - \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
|
| 409 | + \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $this->defaults->getName().($this->defaults->getSlogan() !== '' ? ' - '.$this->defaults->getSlogan() : '')]); |
|
| 410 | 410 | \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->l10n->t('%s is publicly shared', [$shareTmpl['filename']])]); |
| 411 | 411 | \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
| 412 | 412 | \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
@@ -441,7 +441,7 @@ discard block |
||
| 441 | 441 | |
| 442 | 442 | $share = $this->shareManager->getShareByToken($token); |
| 443 | 443 | |
| 444 | - if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 444 | + if (!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 445 | 445 | return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
| 446 | 446 | } |
| 447 | 447 | |
@@ -519,7 +519,7 @@ discard block |
||
| 519 | 519 | |
| 520 | 520 | $this->emitAccessShareHook($share); |
| 521 | 521 | |
| 522 | - $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' ); |
|
| 522 | + $server_params = array('head' => $this->request->getMethod() === 'HEAD'); |
|
| 523 | 523 | |
| 524 | 524 | /** |
| 525 | 525 | * Http range requests support |