xref: /webtrees/app/Http/RequestHandlers/AdminMediaFileThumbnail.php (revision a1bd562d15d6654c7f635d5f296b83f5d866e1cb)
16577bfc3SGreg Roach<?php
26577bfc3SGreg Roach
36577bfc3SGreg Roach/**
46577bfc3SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
66577bfc3SGreg Roach * This program is free software: you can redistribute it and/or modify
76577bfc3SGreg Roach * it under the terms of the GNU General Public License as published by
86577bfc3SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96577bfc3SGreg Roach * (at your option) any later version.
106577bfc3SGreg Roach * This program is distributed in the hope that it will be useful,
116577bfc3SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126577bfc3SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136577bfc3SGreg Roach * GNU General Public License for more details.
146577bfc3SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
166577bfc3SGreg Roach */
176577bfc3SGreg Roach
186577bfc3SGreg Roachdeclare(strict_types=1);
196577bfc3SGreg Roach
206577bfc3SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216577bfc3SGreg Roach
22*a1bd562dSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
23*a1bd562dSGreg Roachuse Fisharebest\Webtrees\I18N;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
25*a1bd562dSGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
26748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
276577bfc3SGreg Roachuse Psr\Http\Message\ResponseInterface;
286577bfc3SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
296577bfc3SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
306577bfc3SGreg Roach
316577bfc3SGreg Roach/**
326577bfc3SGreg Roach * Create a thumbnail of a file, for use on the admin page.
336577bfc3SGreg Roach */
346577bfc3SGreg Roachclass AdminMediaFileThumbnail implements RequestHandlerInterface
356577bfc3SGreg Roach{
36*a1bd562dSGreg Roach    private MediaFileService $media_file_service;
37*a1bd562dSGreg Roach
38*a1bd562dSGreg Roach    /**
39*a1bd562dSGreg Roach     * @param MediaFileService $media_file_service
40*a1bd562dSGreg Roach     */
41*a1bd562dSGreg Roach    public function __construct(MediaFileService $media_file_service)
42*a1bd562dSGreg Roach    {
43*a1bd562dSGreg Roach        $this->media_file_service = $media_file_service;
44*a1bd562dSGreg Roach    }
45*a1bd562dSGreg Roach
466577bfc3SGreg Roach    /**
476577bfc3SGreg Roach     * Show an image/thumbnail, with/without a watermark.
486577bfc3SGreg Roach     *
496577bfc3SGreg Roach     * @param ServerRequestInterface $request
506577bfc3SGreg Roach     *
516577bfc3SGreg Roach     * @return ResponseInterface
526577bfc3SGreg Roach     */
536577bfc3SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
546577bfc3SGreg Roach    {
556b9cb339SGreg Roach        $filesystem = Registry::filesystem()->data();
56748dbe15SGreg Roach        $path       = Validator::queryParams($request)->string('path');
576577bfc3SGreg Roach
58*a1bd562dSGreg Roach        $media_folders = $this->media_file_service->allMediaFolders($filesystem)->all();
59*a1bd562dSGreg Roach
60*a1bd562dSGreg Roach        foreach ($media_folders as $media_folder) {
61*a1bd562dSGreg Roach            if (str_starts_with($path, $media_folder)) {
626b9cb339SGreg Roach                return Registry::imageFactory()->thumbnailResponse($filesystem, $path, 120, 120, 'contain');
636577bfc3SGreg Roach            }
646577bfc3SGreg Roach        }
65*a1bd562dSGreg Roach
66*a1bd562dSGreg Roach        throw new HttpBadRequestException(I18N::translate('The parameter “path” is invalid.'));
67*a1bd562dSGreg Roach    }
68*a1bd562dSGreg Roach}
69