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\FlashMessages; 23use Fisharebest\Webtrees\Html; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\MediaFile; 26use Fisharebest\Webtrees\Registry; 27use Fisharebest\Webtrees\Services\MediaFileService; 28use Fisharebest\Webtrees\Services\PendingChangesService; 29use Fisharebest\Webtrees\Tree; 30use League\Flysystem\FileExistsException; 31use League\Flysystem\FileNotFoundException; 32use League\Flysystem\FilesystemException; 33use League\Flysystem\UnableToMoveFile; 34use League\Flysystem\Util; 35use Psr\Http\Message\ResponseInterface; 36use Psr\Http\Message\ServerRequestInterface; 37use Psr\Http\Server\RequestHandlerInterface; 38 39use function assert; 40use function is_string; 41use function preg_replace; 42use function redirect; 43use function route; 44use function str_replace; 45use function trim; 46 47/** 48 * Edit a media file. 49 */ 50class EditMediaFileAction implements RequestHandlerInterface 51{ 52 /** @var MediaFileService */ 53 private $media_file_service; 54 55 /** @var PendingChangesService */ 56 private $pending_changes_service; 57 58 /** 59 * EditMediaFileAction constructor. 60 * 61 * @param MediaFileService $media_file_service 62 * @param PendingChangesService $pending_changes_service 63 */ 64 public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service) 65 { 66 $this->media_file_service = $media_file_service; 67 $this->pending_changes_service = $pending_changes_service; 68 } 69 70 /** 71 * Save an edited media file. 72 * 73 * @param ServerRequestInterface $request 74 * 75 * @return ResponseInterface 76 */ 77 public function handle(ServerRequestInterface $request): ResponseInterface 78 { 79 $tree = $request->getAttribute('tree'); 80 assert($tree instanceof Tree); 81 82 $xref = $request->getAttribute('xref'); 83 assert(is_string($xref)); 84 85 $fact_id = $request->getAttribute('fact_id'); 86 assert(is_string($fact_id)); 87 88 $data_filesystem = Registry::filesystem()->data(); 89 90 $params = (array) $request->getParsedBody(); 91 $folder = $params['folder'] ?? ''; 92 $new_file = $params['new_file'] ?? ''; 93 $remote = $params['remote'] ?? ''; 94 $title = $params['title'] ?? ''; 95 $type = $params['type'] ?? ''; 96 $media = Registry::mediaFactory()->make($xref, $tree); 97 98 // Tidy non-printing characters 99 $type = trim(preg_replace('/\s+/', ' ', $type)); 100 $title = trim(preg_replace('/\s+/', ' ', $title)); 101 102 // Media object oes not exist? Media object is read-only? 103 if ($media === null || $media->isPendingDeletion() || !$media->canEdit()) { 104 return redirect(route(TreePage::class, ['tree' => $tree->name()])); 105 } 106 107 // Find the fact to edit 108 $media_file = $media->mediaFiles() 109 ->first(static function (MediaFile $media_file) use ($fact_id): bool { 110 return $media_file->factId() === $fact_id; 111 }); 112 113 // Media file does not exist? 114 if ($media_file === null) { 115 return redirect(route(TreePage::class, ['tree' => $tree->name()])); 116 } 117 118 // We can edit the file as either a URL or a folder/file 119 if ($remote !== '') { 120 $file = $remote; 121 } else { 122 $new_file = str_replace('\\', '/', $new_file); 123 $folder = str_replace('\\', '/', $folder); 124 $folder = trim($folder, '/'); 125 126 if ($folder === '') { 127 $file = $new_file; 128 } else { 129 $file = $folder . '/' . $new_file; 130 } 131 } 132 133 // Invalid filename? Do not change it. 134 if ($new_file === '') { 135 $file = $media_file->filename(); 136 } 137 138 $filesystem = $media->tree()->mediaFilesystem($data_filesystem); 139 $old = $media_file->filename(); 140 $new = $file; 141 142 // Update the filesystem, if we can. 143 if ($old !== $new && !$media_file->isExternal() && $filesystem->fileExists($old)) { 144 try { 145 $filesystem->move($old, $new); 146 FlashMessages::addMessage(I18N::translate('The media file %1$s has been renamed to %2$s.', Html::filename($media_file->filename()), Html::filename($file)), 'info'); 147 } catch (FilesystemException | UnableToMoveFile $ex) { 148 // Don't overwrite existing file 149 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'); 150 $file = $old; 151 } 152 } 153 154 $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, ''); 155 156 $media->updateFact($fact_id, $gedcom, true); 157 158 // Accept the changes, to keep the filesystem in sync with the GEDCOM data. 159 if ($old !== $new && !$media_file->isExternal()) { 160 $this->pending_changes_service->acceptRecord($media); 161 } 162 163 return redirect($media->url()); 164 } 165} 166