xref: /webtrees/app/Http/RequestHandlers/EditMediaFileAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
1ddb44b4cSGreg Roach<?php
2ddb44b4cSGreg Roach
3ddb44b4cSGreg Roach/**
4ddb44b4cSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6ddb44b4cSGreg Roach * This program is free software: you can redistribute it and/or modify
7ddb44b4cSGreg Roach * it under the terms of the GNU General Public License as published by
8ddb44b4cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ddb44b4cSGreg Roach * (at your option) any later version.
10ddb44b4cSGreg Roach * This program is distributed in the hope that it will be useful,
11ddb44b4cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ddb44b4cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ddb44b4cSGreg Roach * GNU General Public License for more details.
14ddb44b4cSGreg 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/>.
16ddb44b4cSGreg Roach */
17ddb44b4cSGreg Roach
18ddb44b4cSGreg Roachdeclare(strict_types=1);
19ddb44b4cSGreg Roach
20ddb44b4cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21ddb44b4cSGreg Roach
2252ed2878SGreg Roachuse Fisharebest\Webtrees\Auth;
23ddb44b4cSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Html;
25ddb44b4cSGreg Roachuse Fisharebest\Webtrees\I18N;
26ddb44b4cSGreg Roachuse Fisharebest\Webtrees\MediaFile;
27ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Registry;
28ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
29ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService;
30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
31f7cf8a15SGreg Roachuse League\Flysystem\FilesystemException;
32f7cf8a15SGreg Roachuse League\Flysystem\UnableToMoveFile;
33f0448b68SGreg Roachuse League\Flysystem\UnableToRetrieveMetadata;
34ddb44b4cSGreg Roachuse Psr\Http\Message\ResponseInterface;
35ddb44b4cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
36ddb44b4cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
37ddb44b4cSGreg Roach
38ddb44b4cSGreg Roachuse function preg_replace;
39ddb44b4cSGreg Roachuse function redirect;
40ddb44b4cSGreg Roachuse function route;
41ddb44b4cSGreg Roachuse function str_replace;
42ddb44b4cSGreg Roachuse function trim;
43ddb44b4cSGreg Roach
44ddb44b4cSGreg Roach/**
45ddb44b4cSGreg Roach * Edit a media file.
46ddb44b4cSGreg Roach */
47ddb44b4cSGreg Roachclass EditMediaFileAction implements RequestHandlerInterface
48ddb44b4cSGreg Roach{
49c4943cffSGreg Roach    private MediaFileService $media_file_service;
50ddb44b4cSGreg Roach
51c4943cffSGreg Roach    private PendingChangesService $pending_changes_service;
52ddb44b4cSGreg Roach
53ddb44b4cSGreg Roach    /**
54ddb44b4cSGreg Roach     * EditMediaFileAction constructor.
55ddb44b4cSGreg Roach     *
56ddb44b4cSGreg Roach     * @param MediaFileService      $media_file_service
57ddb44b4cSGreg Roach     * @param PendingChangesService $pending_changes_service
58ddb44b4cSGreg Roach     */
59ddb44b4cSGreg Roach    public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service)
60ddb44b4cSGreg Roach    {
61ddb44b4cSGreg Roach        $this->media_file_service      = $media_file_service;
62ddb44b4cSGreg Roach        $this->pending_changes_service = $pending_changes_service;
63ddb44b4cSGreg Roach    }
64ddb44b4cSGreg Roach
65ddb44b4cSGreg Roach    /**
66ddb44b4cSGreg Roach     * Save an edited media file.
67ddb44b4cSGreg Roach     *
68ddb44b4cSGreg Roach     * @param ServerRequestInterface $request
69ddb44b4cSGreg Roach     *
70ddb44b4cSGreg Roach     * @return ResponseInterface
71ddb44b4cSGreg Roach     */
72ddb44b4cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
73ddb44b4cSGreg Roach    {
74b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
75b55cbc6bSGreg Roach        $xref     = Validator::attributes($request)->isXref()->string('xref');
76b55cbc6bSGreg Roach        $fact_id  = Validator::attributes($request)->string('fact_id');
77748dbe15SGreg Roach        $folder   = Validator::parsedBody($request)->string('folder');
78748dbe15SGreg Roach        $new_file = Validator::parsedBody($request)->string('new_file');
79748dbe15SGreg Roach        $remote   = Validator::parsedBody($request)->string('remote');
80748dbe15SGreg Roach        $title    = Validator::parsedBody($request)->string('title');
81748dbe15SGreg Roach        $type     = Validator::parsedBody($request)->string('type');
82ddb44b4cSGreg Roach        $media    = Registry::mediaFactory()->make($xref, $tree);
8352ed2878SGreg Roach        $media    = Auth::checkMediaAccess($media, true);
84ddb44b4cSGreg Roach
8503f0aef8SGreg Roach        $type  = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->canonical($type);
8603f0aef8SGreg Roach        $title = Registry::elementFactory()->make('OBJE:FILE:TITL')->canonical($title);
87ddb44b4cSGreg Roach
88ddb44b4cSGreg Roach        // Find the fact to edit
89ddb44b4cSGreg Roach        $media_file = $media->mediaFiles()
90ddb44b4cSGreg Roach            ->first(static function (MediaFile $media_file) use ($fact_id): bool {
91ddb44b4cSGreg Roach                return $media_file->factId() === $fact_id;
92ddb44b4cSGreg Roach            });
93ddb44b4cSGreg Roach
94ddb44b4cSGreg Roach        // Media file does not exist?
95ddb44b4cSGreg Roach        if ($media_file === null) {
96ddb44b4cSGreg Roach            return redirect(route(TreePage::class, ['tree' => $tree->name()]));
97ddb44b4cSGreg Roach        }
98ddb44b4cSGreg Roach
99ddb44b4cSGreg Roach        // We can edit the file as either a URL or a folder/file
100ddb44b4cSGreg Roach        if ($remote !== '') {
101ddb44b4cSGreg Roach            $file = $remote;
102ddb44b4cSGreg Roach        } else {
103ddb44b4cSGreg Roach            $new_file = str_replace('\\', '/', $new_file);
104ddb44b4cSGreg Roach            $folder   = str_replace('\\', '/', $folder);
105ddb44b4cSGreg Roach            $folder   = trim($folder, '/');
106ddb44b4cSGreg Roach
107ddb44b4cSGreg Roach            if ($folder === '') {
108ddb44b4cSGreg Roach                $file = $new_file;
109ddb44b4cSGreg Roach            } else {
110ddb44b4cSGreg Roach                $file = $folder . '/' . $new_file;
111ddb44b4cSGreg Roach            }
112ddb44b4cSGreg Roach        }
113ddb44b4cSGreg Roach
114ddb44b4cSGreg Roach        // Invalid filename?  Do not change it.
115ddb44b4cSGreg Roach        if ($new_file === '') {
116ddb44b4cSGreg Roach            $file = $media_file->filename();
117ddb44b4cSGreg Roach        }
118ddb44b4cSGreg Roach
1199458f20aSGreg Roach        $filesystem = $media->tree()->mediaFilesystem();
120ddb44b4cSGreg Roach        $old        = $media_file->filename();
121ddb44b4cSGreg Roach        $new        = $file;
122ddb44b4cSGreg Roach
123ddb44b4cSGreg Roach        // Update the filesystem, if we can.
124f7cf8a15SGreg Roach        if ($old !== $new && !$media_file->isExternal() && $filesystem->fileExists($old)) {
125ddb44b4cSGreg Roach            try {
126f0448b68SGreg Roach                $file_exists = $filesystem->fileExists($old);
127f0448b68SGreg Roach
128f0448b68SGreg Roach                if ($file_exists) {
129f0448b68SGreg Roach                    try {
130f7cf8a15SGreg Roach                        $filesystem->move($old, $new);
131ddb44b4cSGreg Roach                        FlashMessages::addMessage(I18N::translate('The media file %1$s has been renamed to %2$s.', Html::filename($media_file->filename()), Html::filename($file)), 'info');
13228d026adSGreg Roach                    } catch (FilesystemException | UnableToMoveFile) {
133ddb44b4cSGreg Roach                        // Don't overwrite existing file
134ddb44b4cSGreg Roach                        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');
135ddb44b4cSGreg Roach                        $file = $old;
136ddb44b4cSGreg Roach                    }
137ddb44b4cSGreg Roach                }
13828d026adSGreg Roach            } catch (FilesystemException | UnableToRetrieveMetadata) {
139f0448b68SGreg Roach                // File does not exist?
140f0448b68SGreg Roach            }
141f0448b68SGreg Roach        }
142ddb44b4cSGreg Roach
143ddb44b4cSGreg Roach        $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, '');
144ddb44b4cSGreg Roach
145ddb44b4cSGreg Roach        $media->updateFact($fact_id, $gedcom, true);
146ddb44b4cSGreg Roach
147ddb44b4cSGreg Roach        // Accept the changes, to keep the filesystem in sync with the GEDCOM data.
148ddb44b4cSGreg Roach        if ($old !== $new && !$media_file->isExternal()) {
149ddb44b4cSGreg Roach            $this->pending_changes_service->acceptRecord($media);
150ddb44b4cSGreg Roach        }
151ddb44b4cSGreg Roach
152ddb44b4cSGreg Roach        return redirect($media->url());
153ddb44b4cSGreg Roach    }
154ddb44b4cSGreg Roach}
155