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