diff options
Diffstat (limited to 'lib/Service/CacheDocumentService.php')
-rw-r--r-- | lib/Service/CacheDocumentService.php | 70 |
1 files changed, 53 insertions, 17 deletions
diff --git a/lib/Service/CacheDocumentService.php b/lib/Service/CacheDocumentService.php index 84aa32a2..b147c8f6 100644 --- a/lib/Service/CacheDocumentService.php +++ b/lib/Service/CacheDocumentService.php @@ -35,6 +35,8 @@ use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TStringTools; use Exception; +use Gumlet\ImageResize; +use Gumlet\ImageResizeException; use OCA\Social\Exceptions\CacheContentException; use OCA\Social\Exceptions\CacheContentMimeTypeException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; @@ -45,6 +47,7 @@ use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; +use OCA\Social\Model\ActivityPub\Object\Document; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; @@ -58,6 +61,9 @@ class CacheDocumentService { use TStringTools; + const RESIZED_WIDTH = 280; + const RESIZED_HEIGHT = 180; + /** @var IAppData */ private $appData; @@ -91,11 +97,9 @@ class CacheDocumentService { /** - * @param string $url - * + * @param Document $document * @param string $mime * - * @return string * @throws CacheContentMimeTypeException * @throws MalformedArrayException * @throws NotFoundException @@ -108,10 +112,36 @@ class CacheDocumentService { * @throws SocialAppConfigException * @throws UnauthorizedFediverseException */ - public function saveRemoteFileToCache(string $url, &$mime = '') { + public function saveRemoteFileToCache(Document $document, &$mime = '') { + $content = $this->retrieveContent($document->getUrl()); - $filename = $this->uuid(); + // To get the mime type, we create a temp file + $tmpFile = tmpfile(); + $tmpPath = stream_get_meta_data($tmpFile)['uri']; + fwrite($tmpFile, $content); + $mime = mime_content_type($tmpPath); + fclose($tmpFile); + $this->filterMimeTypes($mime); + + $filename = $this->saveContentToCache($content); + $document->setLocalCopy($filename); + $this->resizeImage($content); + $resized = $this->saveContentToCache($content); + $document->setResizedCopy($resized); + } + + + /** + * @param string $content + * + * @return string + * @throws NotPermittedException + * @throws NotFoundException + */ + private function saveContentToCache(string $content): string { + + $filename = $this->uuid(); // creating a path aa/bb/cc/dd/ from the filename aabbccdd-0123-[...] $path = chunk_split(substr($filename, 0, 8), 2, '/'); @@ -121,17 +151,6 @@ class CacheDocumentService { $folder = $this->appData->newFolder($path); } - $content = $this->retrieveContent($url); - - // To get the mime type, we create a temp file - $tmpFile = tmpfile(); - $tmpPath = stream_get_meta_data($tmpFile)['uri']; - fwrite($tmpFile, $content); - $mime = mime_content_type($tmpPath); - fclose($tmpFile); - - $this->filterMimeTypes($mime); - $cache = $folder->newFile($filename); $cache->putContent($content); @@ -160,6 +179,23 @@ class CacheDocumentService { throw new CacheContentMimeTypeException(); } + + /** + * @param $content + */ + private function resizeImage(&$content) { + try { + $image = ImageResize::createFromString($content); + $image->quality_jpg = 100; + $image->quality_png = 9; + + $image->resizeToBestFit(self::RESIZED_WIDTH, self::RESIZED_HEIGHT); + $content = $image->getImageAsString(); + } catch (ImageResizeException $e) { + } + } + + /** * @param string $path * @@ -167,7 +203,7 @@ class CacheDocumentService { * @throws CacheContentException * @throws CacheDocumentDoesNotExistException */ - public function getContentFromCache(string $path) { + public function getContentFromCache(string $path): ISimpleFile { if ($path === '') { throw new CacheDocumentDoesNotExistException(); } |