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 fn(MediaFile $media_file): bool => $media_file->factId() === $fact_id); 89 90 // Media file does not exist? 91 if ($media_file === null) { 92 return redirect(route(TreePage::class, ['tree' => $tree->name()])); 93 } 94 95 // We can edit the file as either a URL or a folder/file 96 if ($remote !== '') { 97 $file = $remote; 98 } else { 99 $new_file = str_replace('\\', '/', $new_file); 100 $folder = str_replace('\\', '/', $folder); 101 $folder = trim($folder, '/'); 102 103 if ($folder === '') { 104 $file = $new_file; 105 } else { 106 $file = $folder . '/' . $new_file; 107 } 108 } 109 110 // Invalid filename? Do not change it. 111 if ($new_file === '') { 112 $file = $media_file->filename(); 113 } 114 115 $filesystem = $media->tree()->mediaFilesystem(); 116 $old = $media_file->filename(); 117 $new = $file; 118 119 // Update the filesystem, if we can. 120 if ($old !== $new && !$media_file->isExternal() && $filesystem->fileExists($old)) { 121 try { 122 $file_exists = $filesystem->fileExists($old); 123 124 if ($file_exists) { 125 try { 126 $filesystem->move($old, $new); 127 FlashMessages::addMessage(I18N::translate('The media file %1$s has been renamed to %2$s.', Html::filename($media_file->filename()), Html::filename($file)), 'info'); 128 } catch (FilesystemException | UnableToMoveFile) { 129 // Don't overwrite existing file 130 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'); 131 $file = $old; 132 } 133 } 134 } catch (FilesystemException | UnableToRetrieveMetadata) { 135 // File does not exist? 136 } 137 } 138 139 $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, ''); 140 141 $media->updateFact($fact_id, $gedcom, true); 142 143 // Accept the changes, to keep the filesystem in sync with the GEDCOM data. 144 if ($old !== $new && !$media_file->isExternal()) { 145 $this->pending_changes_service->acceptRecord($media); 146 } 147 148 return redirect($media->url()); 149 } 150} 151