xref: /webtrees/app/Http/RequestHandlers/EditMediaFileAction.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
1ddb44b4cSGreg Roach<?php
2ddb44b4cSGreg Roach
3ddb44b4cSGreg Roach/**
4ddb44b4cSGreg Roach * webtrees: online genealogy
5d11be702SGreg 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     * @param MediaFileService      $media_file_service
55ddb44b4cSGreg Roach     * @param PendingChangesService $pending_changes_service
56ddb44b4cSGreg Roach     */
57ddb44b4cSGreg Roach    public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service)
58ddb44b4cSGreg Roach    {
59ddb44b4cSGreg Roach        $this->media_file_service      = $media_file_service;
60ddb44b4cSGreg Roach        $this->pending_changes_service = $pending_changes_service;
61ddb44b4cSGreg Roach    }
62ddb44b4cSGreg Roach
63ddb44b4cSGreg Roach    /**
64ddb44b4cSGreg Roach     * Save an edited media file.
65ddb44b4cSGreg Roach     *
66ddb44b4cSGreg Roach     * @param ServerRequestInterface $request
67ddb44b4cSGreg Roach     *
68ddb44b4cSGreg Roach     * @return ResponseInterface
69ddb44b4cSGreg Roach     */
70ddb44b4cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
71ddb44b4cSGreg Roach    {
72b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
73b55cbc6bSGreg Roach        $xref     = Validator::attributes($request)->isXref()->string('xref');
74b55cbc6bSGreg Roach        $fact_id  = Validator::attributes($request)->string('fact_id');
75748dbe15SGreg Roach        $folder   = Validator::parsedBody($request)->string('folder');
76748dbe15SGreg Roach        $new_file = Validator::parsedBody($request)->string('new_file');
77748dbe15SGreg Roach        $remote   = Validator::parsedBody($request)->string('remote');
78748dbe15SGreg Roach        $title    = Validator::parsedBody($request)->string('title');
79748dbe15SGreg Roach        $type     = Validator::parsedBody($request)->string('type');
80ddb44b4cSGreg Roach        $media    = Registry::mediaFactory()->make($xref, $tree);
8152ed2878SGreg Roach        $media    = Auth::checkMediaAccess($media, true);
82ddb44b4cSGreg Roach
8303f0aef8SGreg Roach        $type  = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->canonical($type);
8403f0aef8SGreg Roach        $title = Registry::elementFactory()->make('OBJE:FILE:TITL')->canonical($title);
85ddb44b4cSGreg Roach
86ddb44b4cSGreg Roach        // Find the fact to edit
87ddb44b4cSGreg Roach        $media_file = $media->mediaFiles()
88*f25fc0f9SGreg Roach            ->first(static fn (MediaFile $media_file): bool => $media_file->factId() === $fact_id);
89ddb44b4cSGreg Roach
90ddb44b4cSGreg Roach        // Media file does not exist?
91ddb44b4cSGreg Roach        if ($media_file === null) {
92ddb44b4cSGreg Roach            return redirect(route(TreePage::class, ['tree' => $tree->name()]));
93ddb44b4cSGreg Roach        }
94ddb44b4cSGreg Roach
95ddb44b4cSGreg Roach        // We can edit the file as either a URL or a folder/file
96ddb44b4cSGreg Roach        if ($remote !== '') {
97ddb44b4cSGreg Roach            $file = $remote;
98ddb44b4cSGreg Roach        } else {
99ddb44b4cSGreg Roach            $new_file = str_replace('\\', '/', $new_file);
100ddb44b4cSGreg Roach            $folder   = str_replace('\\', '/', $folder);
101ddb44b4cSGreg Roach            $folder   = trim($folder, '/');
102ddb44b4cSGreg Roach
103ddb44b4cSGreg Roach            if ($folder === '') {
104ddb44b4cSGreg Roach                $file = $new_file;
105ddb44b4cSGreg Roach            } else {
106ddb44b4cSGreg Roach                $file = $folder . '/' . $new_file;
107ddb44b4cSGreg Roach            }
108ddb44b4cSGreg Roach        }
109ddb44b4cSGreg Roach
110ddb44b4cSGreg Roach        // Invalid filename?  Do not change it.
111ddb44b4cSGreg Roach        if ($new_file === '') {
112ddb44b4cSGreg Roach            $file = $media_file->filename();
113ddb44b4cSGreg Roach        }
114ddb44b4cSGreg Roach
1159458f20aSGreg Roach        $filesystem = $media->tree()->mediaFilesystem();
116ddb44b4cSGreg Roach        $old        = $media_file->filename();
117ddb44b4cSGreg Roach        $new        = $file;
118ddb44b4cSGreg Roach
119ddb44b4cSGreg Roach        // Update the filesystem, if we can.
120f7cf8a15SGreg Roach        if ($old !== $new && !$media_file->isExternal() && $filesystem->fileExists($old)) {
121ddb44b4cSGreg Roach            try {
122f0448b68SGreg Roach                $file_exists = $filesystem->fileExists($old);
123f0448b68SGreg Roach
124f0448b68SGreg Roach                if ($file_exists) {
125f0448b68SGreg Roach                    try {
126f7cf8a15SGreg Roach                        $filesystem->move($old, $new);
127ddb44b4cSGreg 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');
12828d026adSGreg Roach                    } catch (FilesystemException | UnableToMoveFile) {
129ddb44b4cSGreg Roach                        // Don't overwrite existing file
130ddb44b4cSGreg 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');
131ddb44b4cSGreg Roach                        $file = $old;
132ddb44b4cSGreg Roach                    }
133ddb44b4cSGreg Roach                }
13428d026adSGreg Roach            } catch (FilesystemException | UnableToRetrieveMetadata) {
135f0448b68SGreg Roach                // File does not exist?
136f0448b68SGreg Roach            }
137f0448b68SGreg Roach        }
138ddb44b4cSGreg Roach
139ddb44b4cSGreg Roach        $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, '');
140ddb44b4cSGreg Roach
141ddb44b4cSGreg Roach        $media->updateFact($fact_id, $gedcom, true);
142ddb44b4cSGreg Roach
143ddb44b4cSGreg Roach        // Accept the changes, to keep the filesystem in sync with the GEDCOM data.
144ddb44b4cSGreg Roach        if ($old !== $new && !$media_file->isExternal()) {
145ddb44b4cSGreg Roach            $this->pending_changes_service->acceptRecord($media);
146ddb44b4cSGreg Roach        }
147ddb44b4cSGreg Roach
148ddb44b4cSGreg Roach        return redirect($media->url());
149ddb44b4cSGreg Roach    }
150ddb44b4cSGreg Roach}
151