Commit c1aa912c authored by Jerome Chauveau's avatar Jerome Chauveau
Browse files

img download fix with an imageutils helper

parent ddf6c8b4
Loading
Loading
Loading
Loading
+2 −21
Changes for ArticleImageDataHandler.inc.php: 2 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -17,26 +17,7 @@ class ArticleImageDataHandler extends ArticleHandler
    public function __construct()
    {
        parent::__construct();
        $umask = Config::getVar('files', 'umask', 0022);
        $adapter = new LocalFilesystemAdapter(
            Config::getVar('files', 'files_dir'),
            PortableVisibilityConverter::fromArray([
                'file' => [
                    'public' => FileManager::FILE_MODE_MASK & ~$umask,
                    'private' => FileManager::FILE_MODE_MASK & ~$umask,
                ],
                'dir' => [
                    'public' => FileManager::DIRECTORY_MODE_MASK & ~$umask,
                    'private' => FileManager::DIRECTORY_MODE_MASK & ~$umask,
                ]
            ]),
            LOCK_EX,
            LocalFilesystemAdapter::DISALLOW_LINKS
        );

        Hook::call('File::adapter', [&$adapter, $this]);

        $this->fs = new Filesystem($adapter);
        $this->fs = ImageUtils::buildFileSystem($this);
    }

    /**
@@ -64,7 +45,7 @@ class ArticleImageDataHandler extends ArticleHandler
             $imagePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . DIRECTORY_SEPARATOR . $imgName;
             $mimetype = 'application/octet-stream';
             $filesize = $this->fs->fileSize($imagePath);
             $encodedFilename = urlencode($filename);
             $encodedFilename = urlencode($imgName);
             header("Content-Type: $mimetype");
             header("Content-Length: $filesize");
             header('Accept-Ranges: none');
+36 −7
Changes for BookImageDataHandler.inc.php: 36 added lines, 7 removed lines.
Original line number Diff line number Diff line
<?php


use APP\core\Application;
use APP\facades\Repo;
use APP\pages\catalog\CatalogBookHandler;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use PKP\config\Config;
use PKP\file\FileManager;
use PKP\plugins\Hook;

class BookImageDataHandler extends CatalogBookHandler
{

    private $fs;

    public function __construct()
    {
        parent::__construct();
        $this->fs = ImageUtils::buildFileSystem($this);
    }

    /**
     * Image from zip archive submission file
     * @param array $args
@@ -16,12 +31,14 @@ class BookImageDataHandler extends CatalogBookHandler
    {
        $pubId= $args[1];
        $imgName = $args[2];
        $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
        $submission = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION);


        $pubFormatFiles = Services::get('submissionFile')->getMany([
			'submissionIds' => [$submission->getId()],
			'assocTypes' => [ASSOC_TYPE_PUBLICATION_FORMAT],
		]);
        $pubFormatFiles = Repo::submissionFile()
            ->getCollector()
            ->filterBySubmissionIds([$submission->getId()])
            ->filterByAssoc(Application::ASSOC_TYPE_PUBLICATION_FORMAT)
            ->getMany();

        $publicationFile = NULL;
        foreach ($pubFormatFiles as $file){
@@ -29,9 +46,21 @@ class BookImageDataHandler extends CatalogBookHandler
                $publicationFile = $file;
            }
        }

        $pathInfo = pathinfo($publicationFile->getData('path'));
        $imagePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . DIRECTORY_SEPARATOR . $imgName;
        return Services::get('file')->download($imagePath, $imgName);
        $fs = Services::get('file')->fs;
        $mimetype = $file->mimetype ?? 'application/octet-stream';
        $filesize = $fs->fileSize($imagePath);
        $encodedFilename = urlencode($imgName);
        header("Content-Type: {$mimetype}");
        header("Content-Length: {$filesize}");
        header('Accept-Ranges: none');
        header('Content-Disposition: inline' . ";filename=\"{$encodedFilename}\";filename*=UTF-8''{$encodedFilename}");
        header('Cache-Control: private'); // Workarounds for IE weirdness
        header('Pragma: public');
        fpassthru($fs->readStream($imagePath));
        exit;


    }

ImageUtils.php

0 → 100644
+33 −0
Changes for ImageUtils.php: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use PKP\config\Config;
use PKP\file\FileManager;
use PKP\plugins\Hook;
use APP\handler\Handler;
class ImageUtils
{
    public static function buildFileSystem(Handler $handler) : Filesystem{
        $umask = Config::getVar('files', 'umask', 0022);
        $adapter = new LocalFilesystemAdapter(
            Config::getVar('files', 'files_dir'),
            PortableVisibilityConverter::fromArray([
                'file' => [
                    'public' => FileManager::FILE_MODE_MASK & ~$umask,
                    'private' => FileManager::FILE_MODE_MASK & ~$umask,
                ],
                'dir' => [
                    'public' => FileManager::DIRECTORY_MODE_MASK & ~$umask,
                    'private' => FileManager::DIRECTORY_MODE_MASK & ~$umask,
                ]
            ]),
            LOCK_EX,
            LocalFilesystemAdapter::DISALLOW_LINKS
        );

        Hook::call('File::adapter', [&$adapter, $handler]);

        return new Filesystem($adapter);
    }
}
 No newline at end of file
+2 −1
Changes for XML2HTMLPlugin.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ class XML2HTMLPlugin extends GenericPlugin
        $galley =& $params[2];
        $submissionFile =& $params[3];
        $zipMode = $submissionFile->getData('mimetype') === "application/zip";
        var_dump($submissionFile);
        if ($zipMode) {
            $this->archiveView($submissionFile->getData('path'), $submission->getId(), $galley->getId(), 'catalog');
            return true;
@@ -78,7 +79,7 @@ class XML2HTMLPlugin extends GenericPlugin
            );

            if ($format == NULL || $format == '')
                $format = 'JATS';//default value is JATS
                $format = 'TEI';//default value is JATS

            $xmlFilePath = XML2HTMLPlugin::unzipXMLArchive($filePath);
            $html = $this->resolveZipImagePaths(
+1 −1
Changes for index.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -5,6 +5,6 @@
 * Date: 04/10/22
 * Time: 17:38
 */

require_once('ImageUtils.php');
require_once('XML2HTMLPlugin.php');
return new XML2HTMLPlugin();