1fd6c003fSGreg Roach<?php 2fd6c003fSGreg Roach 3fd6c003fSGreg Roach/** 4fd6c003fSGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 6fd6c003fSGreg Roach * This program is free software: you can redistribute it and/or modify 7fd6c003fSGreg Roach * it under the terms of the GNU General Public License as published by 8fd6c003fSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9fd6c003fSGreg Roach * (at your option) any later version. 10fd6c003fSGreg Roach * This program is distributed in the hope that it will be useful, 11fd6c003fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12fd6c003fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13fd6c003fSGreg Roach * GNU General Public License for more details. 14fd6c003fSGreg 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/>. 16fd6c003fSGreg Roach */ 17fcfa147eSGreg Roach 18fd6c003fSGreg Roachdeclare(strict_types=1); 19fd6c003fSGreg Roach 20fd6c003fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21fd6c003fSGreg Roach 22fd6c003fSGreg Roachuse Fisharebest\Webtrees\FlashMessages; 23fd6c003fSGreg Roachuse Fisharebest\Webtrees\I18N; 246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 25*f7cf8a15SGreg Roachuse League\Flysystem\FilesystemException; 26*f7cf8a15SGreg Roachuse League\Flysystem\UnableToDeleteDirectory; 27*f7cf8a15SGreg Roachuse League\Flysystem\UnableToDeleteFile; 28fd6c003fSGreg Roachuse Psr\Http\Message\ResponseInterface; 29fd6c003fSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 30fd6c003fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 31fd6c003fSGreg Roachuse Throwable; 32fd6c003fSGreg Roach 33fd6c003fSGreg Roachuse function assert; 34fd6c003fSGreg Roachuse function e; 35fd6c003fSGreg Roachuse function is_string; 36fd6c003fSGreg Roachuse function response; 37*f7cf8a15SGreg Roachuse function str_ends_with; 38fd6c003fSGreg Roach 39fd6c003fSGreg Roach/** 4013aa75d8SGreg Roach * Delete a file or folder from the data filesystem. 41fd6c003fSGreg Roach */ 421d1f373cSGreg Roachclass DeletePath implements RequestHandlerInterface 43fd6c003fSGreg Roach{ 44fd6c003fSGreg Roach /** 45fd6c003fSGreg Roach * @param ServerRequestInterface $request 46fd6c003fSGreg Roach * 47fd6c003fSGreg Roach * @return ResponseInterface 48fd6c003fSGreg Roach */ 49fd6c003fSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 50fd6c003fSGreg Roach { 516b9cb339SGreg Roach $data_filesystem = Registry::filesystem()->data(); 52a04bb9a2SGreg Roach 53fd6c003fSGreg Roach $path = $request->getQueryParams()['path']; 5475964c75SGreg Roach assert(is_string($path)); 55fd6c003fSGreg Roach 56*f7cf8a15SGreg Roach if (str_ends_with($path, '/')) { 57*f7cf8a15SGreg Roach try { 58*f7cf8a15SGreg Roach $data_filesystem->deleteDirectory($path); 59*f7cf8a15SGreg Roach FlashMessages::addMessage(I18N::translate('The folder %s has been deleted.', e($path)), 'success'); 60*f7cf8a15SGreg Roach } catch (FilesystemException | UnableToDeleteDirectory $ex) { 61*f7cf8a15SGreg Roach FlashMessages::addMessage(I18N::translate('The folder %s could not be deleted.', e($path)), 'danger'); 62*f7cf8a15SGreg Roach } 63*f7cf8a15SGreg Roach } else { 64fd6c003fSGreg Roach try { 65a04bb9a2SGreg Roach $data_filesystem->delete($path); 66fd6c003fSGreg Roach FlashMessages::addMessage(I18N::translate('The file %s has been deleted.', e($path)), 'success'); 67*f7cf8a15SGreg Roach } catch (FilesystemException | UnableToDeleteFile $ex) { 68fd6c003fSGreg Roach FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger'); 69fd6c003fSGreg Roach } 70fd6c003fSGreg Roach } 71fd6c003fSGreg Roach 721d1f373cSGreg Roach return response(); 73fd6c003fSGreg Roach } 74fd6c003fSGreg Roach} 75