xref: /webtrees/app/Http/RequestHandlers/AddMediaFileAction.php (revision fc34a24d90864b5bbc77967b910fd8bc99a89504)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Services\MediaFileService;
27use Fisharebest\Webtrees\Services\PendingChangesService;
28use Fisharebest\Webtrees\Tree;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function assert;
34use function is_string;
35use function redirect;
36
37/**
38 * Add a new media file to a media object.
39 */
40class AddMediaFileAction implements RequestHandlerInterface
41{
42    private MediaFileService $media_file_service;
43
44    private PendingChangesService $pending_changes_service;
45
46    /**
47     * AddMediaFileAction constructor.
48     *
49     * @param MediaFileService      $media_file_service
50     * @param PendingChangesService $pending_changes_service
51     */
52    public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service)
53    {
54        $this->media_file_service      = $media_file_service;
55        $this->pending_changes_service = $pending_changes_service;
56    }
57
58    /**
59     * Add a media file to an existing media object.
60     *
61     * @param ServerRequestInterface $request
62     *
63     * @return ResponseInterface
64     */
65    public function handle(ServerRequestInterface $request): ResponseInterface
66    {
67        $tree = $request->getAttribute('tree');
68        assert($tree instanceof Tree);
69
70        $xref = $request->getAttribute('xref');
71        assert(is_string($xref));
72
73        $media = Registry::mediaFactory()->make($xref, $tree);
74        $media = Auth::checkMediaAccess($media, true);
75
76        $params = (array) $request->getParsedBody();
77        $title  = $params['title'] ?? '';
78        $type   = $params['type'] ?? '';
79
80        $file = $this->media_file_service->uploadFile($request);
81
82        if ($file === '') {
83            FlashMessages::addMessage(I18N::translate('There was an error uploading your file.'));
84
85            return redirect($media->url());
86        }
87
88        $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, '');
89
90        $media->createFact($gedcom, true);
91
92        // Accept the changes, to keep the filesystem in sync with the GEDCOM data.
93        $this->pending_changes_service->acceptRecord($media);
94
95        return redirect($media->url());
96    }
97}
98