xref: /webtrees/app/Http/RequestHandlers/EditMediaFileAction.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Html;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\MediaFile;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\MediaFileService;
29use Fisharebest\Webtrees\Services\PendingChangesService;
30use Fisharebest\Webtrees\Validator;
31use League\Flysystem\FilesystemException;
32use League\Flysystem\UnableToMoveFile;
33use League\Flysystem\UnableToRetrieveMetadata;
34use Psr\Http\Message\ResponseInterface;
35use Psr\Http\Message\ServerRequestInterface;
36use Psr\Http\Server\RequestHandlerInterface;
37
38use function preg_replace;
39use function redirect;
40use function route;
41use function str_replace;
42use function trim;
43
44/**
45 * Edit a media file.
46 */
47class EditMediaFileAction implements RequestHandlerInterface
48{
49    private MediaFileService $media_file_service;
50
51    private PendingChangesService $pending_changes_service;
52
53    /**
54     * @param MediaFileService      $media_file_service
55     * @param PendingChangesService $pending_changes_service
56     */
57    public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service)
58    {
59        $this->media_file_service      = $media_file_service;
60        $this->pending_changes_service = $pending_changes_service;
61    }
62
63    /**
64     * Save an edited media file.
65     *
66     * @param ServerRequestInterface $request
67     *
68     * @return ResponseInterface
69     */
70    public function handle(ServerRequestInterface $request): ResponseInterface
71    {
72        $tree     = Validator::attributes($request)->tree();
73        $xref     = Validator::attributes($request)->isXref()->string('xref');
74        $fact_id  = Validator::attributes($request)->string('fact_id');
75        $folder   = Validator::parsedBody($request)->string('folder');
76        $new_file = Validator::parsedBody($request)->string('new_file');
77        $remote   = Validator::parsedBody($request)->string('remote');
78        $title    = Validator::parsedBody($request)->string('title');
79        $type     = Validator::parsedBody($request)->string('type');
80        $media    = Registry::mediaFactory()->make($xref, $tree);
81        $media    = Auth::checkMediaAccess($media, true);
82
83        $type  = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->canonical($type);
84        $title = Registry::elementFactory()->make('OBJE:FILE:TITL')->canonical($title);
85
86        // Find the fact to edit
87        $media_file = $media->mediaFiles()
88            ->first(static function (MediaFile $media_file) use ($fact_id): bool {
89                return $media_file->factId() === $fact_id;
90            });
91
92        // Media file does not exist?
93        if ($media_file === null) {
94            return redirect(route(TreePage::class, ['tree' => $tree->name()]));
95        }
96
97        // We can edit the file as either a URL or a folder/file
98        if ($remote !== '') {
99            $file = $remote;
100        } else {
101            $new_file = str_replace('\\', '/', $new_file);
102            $folder   = str_replace('\\', '/', $folder);
103            $folder   = trim($folder, '/');
104
105            if ($folder === '') {
106                $file = $new_file;
107            } else {
108                $file = $folder . '/' . $new_file;
109            }
110        }
111
112        // Invalid filename?  Do not change it.
113        if ($new_file === '') {
114            $file = $media_file->filename();
115        }
116
117        $filesystem = $media->tree()->mediaFilesystem();
118        $old        = $media_file->filename();
119        $new        = $file;
120
121        // Update the filesystem, if we can.
122        if ($old !== $new && !$media_file->isExternal() && $filesystem->fileExists($old)) {
123            try {
124                $file_exists = $filesystem->fileExists($old);
125
126                if ($file_exists) {
127                    try {
128                        $filesystem->move($old, $new);
129                        FlashMessages::addMessage(I18N::translate('The media file %1$s has been renamed to %2$s.', Html::filename($media_file->filename()), Html::filename($file)), 'info');
130                    } catch (FilesystemException | UnableToMoveFile) {
131                        // Don't overwrite existing file
132                        FlashMessages::addMessage(I18N::translate('The media file %1$s could not be renamed to %2$s.', Html::filename($media_file->filename()), Html::filename($file)), 'info');
133                        $file = $old;
134                    }
135                }
136            } catch (FilesystemException | UnableToRetrieveMetadata) {
137                // File does not exist?
138            }
139        }
140
141        $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, '');
142
143        $media->updateFact($fact_id, $gedcom, true);
144
145        // Accept the changes, to keep the filesystem in sync with the GEDCOM data.
146        if ($old !== $new && !$media_file->isExternal()) {
147            $this->pending_changes_service->acceptRecord($media);
148        }
149
150        return redirect($media->url());
151    }
152}
153