vendor/terminal42/contao-fineuploader/src/ChunkUploader.php line 77

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Terminal42\FineUploaderBundle;
  4. use Contao\File;
  5. use Contao\System;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Session\Session;
  8. use Terminal42\FineUploaderBundle\Widget\BaseWidget;
  9. class ChunkUploader
  10. {
  11.     /**
  12.      * @var Filesystem
  13.      */
  14.     private $fs;
  15.     /**
  16.      * @var Session
  17.      */
  18.     private $session;
  19.     /**
  20.      * ChunkUploader constructor.
  21.      */
  22.     public function __construct(Filesystem $fsSession $session)
  23.     {
  24.         $this->fs $fs;
  25.         $this->session $session;
  26.     }
  27.     /**
  28.      * Handle the chunk by storing it in the session for further merge.
  29.      *
  30.      * @param string $filePath
  31.      *
  32.      * @return string
  33.      */
  34.     public function handleChunk(Request $requestBaseWidget $widget$filePath)
  35.     {
  36.         $fileName $request->request->get('qqfilename');
  37.         $sessionKey $this->getSessionKey($widget);
  38.         $chunks $this->session->get($sessionKey);
  39.         $chunks[$fileName][] = $filePath;
  40.         // This is the last chunking request, merge the chunks and create the final file
  41.         if ($this->isLastChunk($request)) {
  42.             $filePath $this->mergeChunks($widget$chunks[$fileName], $fileName);
  43.             // Unset the file session after merging the chunks
  44.             unset($chunks[$fileName]);
  45.         }
  46.         // Update the session
  47.         $this->session->set($sessionKey$chunks);
  48.         return $filePath;
  49.     }
  50.     /**
  51.      * Return true if this is the last chunk.
  52.      *
  53.      * @return bool
  54.      */
  55.     public function isLastChunk(Request $request)
  56.     {
  57.         return $request->request->getInt('qqpartindex') === $request->request->getInt('qqtotalparts') - 1;
  58.     }
  59.     /**
  60.      * Clear the session from chunks.
  61.      */
  62.     public function clearSession(BaseWidget $widget): void
  63.     {
  64.         $this->session->remove($this->getSessionKey($widget));
  65.     }
  66.     /**
  67.      * Merge the chunks.
  68.      *
  69.      * @param string $fileName
  70.      *
  71.      * @return string
  72.      */
  73.     private function mergeChunks(BaseWidget $widget, array $chunks$fileName)
  74.     {
  75.         // Get the new file name if temporary file already exists
  76.         if ($this->fs->tmpFileExists($fileName)) {
  77.             $fileName $this->fs->getTmpFileName($fileName);
  78.         }
  79.         $file $this->fs->mergeTmpFiles($chunks$fileName);
  80.         // Validate the file
  81.         $this->validateFile($file$widget);
  82.         return $file->path;
  83.     }
  84.     /**
  85.      * Validate the file.
  86.      */
  87.     private function validateFile(File $fileBaseWidget $widget): void
  88.     {
  89.         $config $widget->getUploaderConfig();
  90.         $minSizeLimit $config->getMinSizeLimit();
  91.         // Validate the minimum size limit
  92.         if ($minSizeLimit && $file->size $minSizeLimit) {
  93.             $widget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['minFileSize'], System::getReadableSize($minSizeLimit)));
  94.         }
  95.         $maxSizeLimit $config->getMaxSizeLimit();
  96.         // Validate the maximum size limit
  97.         if ($maxSizeLimit && $file->size $maxSizeLimit) {
  98.             $widget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['maxFileSize'], System::getReadableSize($maxSizeLimit)));
  99.         }
  100.     }
  101.     /**
  102.      * Get the session key.
  103.      *
  104.      * @return string
  105.      */
  106.     private function getSessionKey(BaseWidget $widget)
  107.     {
  108.         return $widget->name.'_FINEUPLOADER_CHUNKS';
  109.     }
  110. }