xref: /webtrees/app/Http/RequestHandlers/MediaFileDownload.php (revision d11be7027e34e3121be11cc025421873364403f9)
146b03695SGreg Roach<?php
246b03695SGreg Roach
346b03695SGreg Roach/**
446b03695SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
646b03695SGreg Roach * This program is free software: you can redistribute it and/or modify
746b03695SGreg Roach * it under the terms of the GNU General Public License as published by
846b03695SGreg Roach * the Free Software Foundation, either version 3 of the License, or
946b03695SGreg Roach * (at your option) any later version.
1046b03695SGreg Roach * This program is distributed in the hope that it will be useful,
1146b03695SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1246b03695SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1346b03695SGreg Roach * GNU General Public License for more details.
1446b03695SGreg 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/>.
1646b03695SGreg Roach */
1746b03695SGreg Roach
1846b03695SGreg Roachdeclare(strict_types=1);
1946b03695SGreg Roach
2046b03695SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2146b03695SGreg Roach
2246b03695SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2346b03695SGreg Roachuse Fisharebest\Webtrees\Auth;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2646b03695SGreg Roachuse Psr\Http\Message\ResponseInterface;
2746b03695SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2846b03695SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
2946b03695SGreg Roach
3046b03695SGreg Roachuse function redirect;
3146b03695SGreg Roach
3246b03695SGreg Roach/**
3346b03695SGreg Roach * Download a media file.
3446b03695SGreg Roach */
3546b03695SGreg Roachclass MediaFileDownload implements RequestHandlerInterface
3646b03695SGreg Roach{
3746b03695SGreg Roach    /**
3846b03695SGreg Roach     * Download a non-image media file.
3946b03695SGreg Roach     *
4046b03695SGreg Roach     * @param ServerRequestInterface $request
4146b03695SGreg Roach     *
4246b03695SGreg Roach     * @return ResponseInterface
4346b03695SGreg Roach     */
4446b03695SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
4546b03695SGreg Roach    {
46b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
47b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
486577bfc3SGreg Roach
496b9cb339SGreg Roach        $image_factory = Registry::imageFactory();
5046b03695SGreg Roach
51748dbe15SGreg Roach        $disposition = Validator::queryParams($request)->isInArray(['inline', 'attachment'])->string('disposition');
52748dbe15SGreg Roach        $xref        = Validator::queryParams($request)->isXref()->string('xref');
53748dbe15SGreg Roach        $fact_id     = Validator::queryParams($request)->string('fact_id');
546b9cb339SGreg Roach        $media       = Registry::mediaFactory()->make($xref, $tree);
5546b03695SGreg Roach        $media       = Auth::checkMediaAccess($media);
5646b03695SGreg Roach
5746b03695SGreg Roach        foreach ($media->mediaFiles() as $media_file) {
5846b03695SGreg Roach            if ($media_file->factId() === $fact_id) {
5946b03695SGreg Roach                if ($media_file->isExternal()) {
6046b03695SGreg Roach                    return redirect($media_file->filename());
6146b03695SGreg Roach                }
6246b03695SGreg Roach
636577bfc3SGreg Roach                $watermark = $media_file->isImage() && $image_factory->fileNeedsWatermark($media_file, $user);
646577bfc3SGreg Roach                $download  = $disposition === 'attachment';
6546b03695SGreg Roach
6671e22769SGreg Roach                $response = $image_factory->mediaFileResponse($media_file, $watermark, $download);
6771e22769SGreg Roach
686172e7f6SGreg Roach                return $response->withHeader('cache-control', 'public,max-age=31536000');
6946b03695SGreg Roach            }
7046b03695SGreg Roach        }
7146b03695SGreg Roach
726577bfc3SGreg Roach        return $image_factory->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND);
7346b03695SGreg Roach    }
7446b03695SGreg Roach}
75