appName = $appName; $this->root = $root; $this->logger = $logger; $this->trans = $trans; $this->config = $config; $this->urlGenerator = $urlGenerator; $this->crypt = $crypt; if (\OC::$server->getAppManager()->isInstalled("files_versions")) { try { $this->versionManager = \OC::$server->query(IVersionManager::class); } catch (QueryException $e) { $this->logger->error("VersionManager init error", ["exception" => $e]); } } $this->fileUtility = new FileUtility($appName, $trans, $logger, $config, $shareManager, $session); } /** * Return mime type */ public static function getMimeTypeRegex() { $mimeTypeRegex = ""; foreach (self::$capabilities as $format) { if (!empty($mimeTypeRegex)) { $mimeTypeRegex = $mimeTypeRegex . "|"; } $mimeTypeRegex = $mimeTypeRegex . str_replace("/", "\/", $format); } $mimeTypeRegex = "/" . $mimeTypeRegex . "/"; return $mimeTypeRegex; } /** * Return mime type */ public function getMimeType() { $m = self::getMimeTypeRegex(); return $m; } /** * The method checks if the file can be converted * * @param FileInfo $fileInfo - File * * @return bool */ public function isAvailable(FileInfo $fileInfo) { if ($this->config->getPreview() !== true) { return false; } if (!$fileInfo || $fileInfo->getSize() === 0 || $fileInfo->getSize() > $this->config->getLimitThumbSize()) { return false; } if (!in_array($fileInfo->getMimetype(), self::$capabilities, true)) { return false; } if ($fileInfo->getStorage()->instanceOfStorage(SharingExternalStorage::class)) { return false; } return true; } /** * The method is generated thumbnail for file and returned image object * * @param string $path - Path of file * @param int $maxX - The maximum X size of the thumbnail * @param int $maxY - The maximum Y size of the thumbnail * @param bool $scalingup - Disable/Enable upscaling of previews * @param View $view - view * * @return Image|bool false if no preview was generated */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $view) { $this->logger->debug("getThumbnail $path $maxX $maxY"); list($fileUrl, $extension, $key) = $this->getFileParam($path, $view); if ($fileUrl === null || $extension === null || $key === null) { return false; } $imageUrl = null; $documentService = new DocumentService($this->trans, $this->config); try { $imageUrl = $documentService->getConvertedUri($fileUrl, $extension, self::THUMBEXTENSION, $key); } catch (\Exception $e) { $this->logger->error("getConvertedUri: from $extension to " . self::THUMBEXTENSION, ["exception" => $e]); return false; } try { $thumbnail = $documentService->request($imageUrl); } catch (\Exception $e) { $this->logger->error("Failed to download thumbnail", ["exception" => $e]); return false; } $image = new Image(); $image->loadFromData($thumbnail); if ($image->valid()) { $image->scaleDownToFit($maxX, $maxY); return $image; } return false; } /** * Generate secure link to download document * * @param File $file - file * @param IUser $user - user with access * @param int $version - file version * @param bool $template - file is template * * @return string */ private function getUrl($file, $user = null, $version = 0, $template = false) { $data = [ "action" => "download", "fileId" => $file->getId() ]; $userId = null; if (!empty($user)) { $userId = $user->getUID(); $data["userId"] = $userId; } if ($version > 0) { $data["version"] = $version; } if ($template) { $data["template"] = true; } $hashUrl = $this->crypt->getHash($data); $fileUrl = $this->urlGenerator->linkToRouteAbsolute($this->appName . ".callback.download", ["doc" => $hashUrl]); if (!$this->config->useDemo() && !empty($this->config->getStorageUrl())) { $fileUrl = str_replace($this->urlGenerator->getAbsoluteURL("/"), $this->config->getStorageUrl(), $fileUrl); } return $fileUrl; } /** * Generate array with file parameters * * @param string $path - Path of file * @param View $view - view * * @return array */ private function getFileParam($path, $view) { $fileInfo = $view->getFileInfo($path); if (!$fileInfo || $fileInfo->getSize() === 0) { return [null, null, null]; } $owner = $fileInfo->getOwner(); $key = null; $versionNum = 0; $template = false; if (FileVersions::splitPathVersion($path) !== false) { if ($this->versionManager === null || $owner === null) { return [null, null, null]; } $versionFolder = new View("/" . $owner->getUID() . "/files_versions"); $absolutePath = $fileInfo->getPath(); $relativePath = $versionFolder->getRelativePath($absolutePath); list($filePath, $fileVersion) = FileVersions::splitPathVersion($relativePath); if ($filePath === null) { return [null, null, null]; } $sourceFile = $this->root->getUserFolder($owner->getUID())->get($filePath); $fileInfo = $sourceFile->getFileInfo(); $versions = FileVersions::processVersionsArray($this->versionManager->getVersionsForFile($owner, $fileInfo)); foreach ($versions as $version) { $versionNum = $versionNum + 1; $versionId = $version->getRevisionId(); if (strcmp($versionId, $fileVersion) === 0) { $key = $this->fileUtility->getVersionKey($version); $key = DocumentService::generateRevisionId($key); break; } } } else { $key = $this->fileUtility->getKey($fileInfo); $key = DocumentService::generateRevisionId($key); } if (TemplateManager::isTemplate($fileInfo->getId())) { $template = true; } $fileUrl = $this->getUrl($fileInfo, $owner, $versionNum, $template); $fileExtension = $fileInfo->getExtension(); return [$fileUrl, $fileExtension, "thumb_$key"]; } }