]> Raphaël G. Git Repositories - packbundle/commitdiff
Add download action
authorRaphaël Gertz <git@rapsys.eu>
Mon, 13 Oct 2025 13:30:47 +0000 (15:30 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Mon, 13 Oct 2025 13:30:47 +0000 (15:30 +0200)
Add width and height in thumb path
Cleanup

Controller.php

index 77a3e14e5a10d1c4d1e2c64e9c89f062e96766a1..7728f2e4410327d74bd97cdb1537d7e88dd57deb 100644 (file)
@@ -220,6 +220,57 @@ class Controller extends AbstractController implements ServiceSubscriberInterfac
                return $response;
        }
 
                return $response;
        }
 
+       /**
+        * Return download file
+        *
+        * @param Request $request The Request instance
+        * @param string $hash The hash
+        * @param string $path The image path
+        * @return Response The rendered image
+        */
+       public function download(Request $request, string $hash, string $path/*, string $_format*/): Response {
+               //Without matching hash
+               if ($hash !== $this->slugger->hash($path)) {
+                       //Throw new exception
+                       throw new NotFoundHttpException('Invalid download hash');
+               //Without valid format
+               #} elseif ($_format !== 'jpeg' && $_format !== 'png' && $_format !== 'webp') {
+               #       //Throw new exception
+               #       throw new NotFoundHttpException('Invalid download format');
+               }
+
+               //Unshort path
+               $path = $this->slugger->unshort($short = $path);
+
+               //Without file
+               if (!is_file($path) || !($mtime = stat($path)['mtime'])) {
+                       //Throw new exception
+                       throw new NotFoundHttpException('Unable to get thumb file');
+               }
+
+               //Read thumb from cache
+               $response = new BinaryFileResponse($path);
+
+               //Set file name
+               $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, basename($path));
+
+               //Set etag
+               //TODO: set etag to file content md5 ? cache it ?
+               $response->setEtag(md5($hash));
+
+               //Set last modified
+               $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
+
+               //Set as public
+               $response->setPublic();
+
+               //Return 304 response if not modified
+               $response->isNotModified($request);
+
+               //Return response
+               return $response;
+       }
+
        /**
         * Return facebook image
         *
        /**
         * Return facebook image
         *
@@ -257,6 +308,7 @@ class Controller extends AbstractController implements ServiceSubscriberInterfac
                $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'facebook-'.$hash.'.'.$_format);
 
                //Set etag
                $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'facebook-'.$hash.'.'.$_format);
 
                //Set etag
+               //TODO: set etag to file content md5 ? cache it ?
                $response->setEtag(md5($hash));
 
                //Set last modified
                $response->setEtag(md5($hash));
 
                //Set last modified
@@ -443,6 +495,7 @@ class Controller extends AbstractController implements ServiceSubscriberInterfac
                $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, basename($map));
 
                //Set etag
                $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, basename($map));
 
                //Set etag
+               //TODO: set etag to file content md5 ? cache it ?
                $response->setEtag(md5(serialize([$height, $width, $zoom, $latitude, $longitude])));
 
                //Set last modified
                $response->setEtag(md5(serialize([$height, $width, $zoom, $latitude, $longitude])));
 
                //Set last modified
@@ -708,6 +761,7 @@ class Controller extends AbstractController implements ServiceSubscriberInterfac
                $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, basename($map));
 
                //Set etag
                $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, basename($map));
 
                //Set etag
+               //TODO: set etag to file content md5 ? cache it ?
                $response->setEtag(md5(serialize([$height, $width, $zoom, $coordinate])));
 
                //Set last modified
                $response->setEtag(md5(serialize([$height, $width, $zoom, $coordinate])));
 
                //Set last modified
@@ -751,16 +805,16 @@ class Controller extends AbstractController implements ServiceSubscriberInterfac
                $path = $this->slugger->unshort($short = $path);
 
                //Set thumb
                $path = $this->slugger->unshort($short = $path);
 
                //Set thumb
-               $thumb = $this->config['cache'].'/'.$this->config['prefixes']['thumb'].$path.'.'.$_format;
+               $thumb = $this->config['cache'].'/'.$this->config['prefixes']['thumb'].$path.'.'.$width.'x'.$height.'.'.$_format;
 
                //Without file
 
                //Without file
-               if (!is_file($path) || !($updated = stat($path)['mtime'])) {
+               if (!is_file($path) || !($mtime = stat($path)['mtime'])) {
                        //Throw new exception
                        throw new NotFoundHttpException('Unable to get thumb file');
                }
 
                //Without thumb up to date file
                        //Throw new exception
                        throw new NotFoundHttpException('Unable to get thumb file');
                }
 
                //Without thumb up to date file
-               if (!is_file($thumb) || !($mtime = stat($thumb)['mtime']) || $mtime < $updated) {
+               if (!is_file($thumb) || !($updated = stat($thumb)['mtime']) || $updated < $mtime) {
                        //Without existing thumb path
                        if (!is_dir($dir = dirname($thumb))) {
                                //Create filesystem object
                        //Without existing thumb path
                        if (!is_dir($dir = dirname($thumb))) {
                                //Create filesystem object
@@ -803,8 +857,8 @@ class Controller extends AbstractController implements ServiceSubscriberInterfac
                                throw new \Exception(sprintf('Unable to write image "%s"', $thumb));
                        }
 
                                throw new \Exception(sprintf('Unable to write image "%s"', $thumb));
                        }
 
-                       //Set mtime
-                       $mtime = stat($thumb)['mtime'];
+                       //Set updated
+                       $updated = stat($thumb)['mtime'];
                }
 
                //Read thumb from cache
                }
 
                //Read thumb from cache
@@ -814,10 +868,11 @@ class Controller extends AbstractController implements ServiceSubscriberInterfac
                $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'thumb-'.$hash.'.'.$_format);
 
                //Set etag
                $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'thumb-'.$hash.'.'.$_format);
 
                //Set etag
+               //TODO: set etag to file content md5 ? cache it ?
                $response->setEtag(md5($hash));
 
                //Set last modified
                $response->setEtag(md5($hash));
 
                //Set last modified
-               $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
+               $response->setLastModified(\DateTime::createFromFormat('U', strval($updated)));
 
                //Set as public
                $response->setPublic();
 
                //Set as public
                $response->setPublic();