xref: /webtrees/app/Http/RequestHandlers/DeletePath.php (revision 1d1f373cc23093682b1736881f66a527f62ccc46)
1fd6c003fSGreg Roach<?php
2fd6c003fSGreg Roach
3fd6c003fSGreg Roach/**
4fd6c003fSGreg Roach * webtrees: online genealogy
5fd6c003fSGreg Roach * Copyright (C) 2019 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
15fd6c003fSGreg Roach * along with this program. If not, see <http://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;
24fd6c003fSGreg Roachuse InvalidArgumentException;
25fd6c003fSGreg Roachuse League\Flysystem\FilesystemInterface;
26fd6c003fSGreg Roachuse Psr\Http\Message\ResponseInterface;
27fd6c003fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
28fd6c003fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
29fd6c003fSGreg Roachuse Throwable;
30fd6c003fSGreg Roach
31fd6c003fSGreg Roachuse function assert;
32fd6c003fSGreg Roachuse function e;
33fd6c003fSGreg Roachuse function is_string;
34fd6c003fSGreg Roachuse function response;
35fd6c003fSGreg Roach
36fd6c003fSGreg Roach/**
37fd6c003fSGreg Roach * Delete a file.
38fd6c003fSGreg Roach */
39*1d1f373cSGreg Roachclass DeletePath implements RequestHandlerInterface
40fd6c003fSGreg Roach{
41fd6c003fSGreg Roach    /** @var FilesystemInterface */
42fd6c003fSGreg Roach    private $filesystem;
43fd6c003fSGreg Roach
44fd6c003fSGreg Roach    /**
45fd6c003fSGreg Roach     * @param FilesystemInterface $filesystem
46fd6c003fSGreg Roach     */
47fd6c003fSGreg Roach    public function __construct(FilesystemInterface $filesystem)
48fd6c003fSGreg Roach    {
49fd6c003fSGreg Roach        $this->filesystem = $filesystem;
50fd6c003fSGreg Roach    }
51fd6c003fSGreg Roach
52fd6c003fSGreg Roach    /**
53fd6c003fSGreg Roach     * @param ServerRequestInterface $request
54fd6c003fSGreg Roach     *
55fd6c003fSGreg Roach     * @return ResponseInterface
56fd6c003fSGreg Roach     */
57fd6c003fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
58fd6c003fSGreg Roach    {
59fd6c003fSGreg Roach        $path = $request->getQueryParams()['path'];
60fd6c003fSGreg Roach        assert(is_string($path), new InvalidArgumentException());
61fd6c003fSGreg Roach
62fd6c003fSGreg Roach        if ($this->filesystem->has($path)) {
63fd6c003fSGreg Roach            $metadata = $this->filesystem->getMetadata($path);
64fd6c003fSGreg Roach
65fd6c003fSGreg Roach            switch ($metadata['type']) {
66fd6c003fSGreg Roach                case 'file':
67fd6c003fSGreg Roach                    try {
68fd6c003fSGreg Roach                        $this->filesystem->delete($path);
69fd6c003fSGreg Roach                        FlashMessages::addMessage(I18N::translate('The file %s has been deleted.', e($path)), 'success');
70fd6c003fSGreg Roach                    } catch (Throwable $ex) {
71fd6c003fSGreg Roach                        FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger');
72fd6c003fSGreg Roach                    }
73fd6c003fSGreg Roach                    break;
74fd6c003fSGreg Roach
75fd6c003fSGreg Roach                case 'dir':
76fd6c003fSGreg Roach                    try {
77fd6c003fSGreg Roach                        $this->filesystem->deleteDir($path);
78fd6c003fSGreg Roach                        FlashMessages::addMessage(I18N::translate('The folder %s has been deleted.', e($path)), 'success');
79fd6c003fSGreg Roach                    } catch (Throwable $ex) {
80fd6c003fSGreg Roach                        FlashMessages::addMessage(I18N::translate('The folder %s could not be deleted.', e($path)), 'danger');
81fd6c003fSGreg Roach                    }
82fd6c003fSGreg Roach                    break;
83fd6c003fSGreg Roach            }
84fd6c003fSGreg Roach        } else {
85fd6c003fSGreg Roach            FlashMessages::addMessage(I18N::translate('The file %s could not be deleted.', e($path)), 'danger');
86fd6c003fSGreg Roach        }
87fd6c003fSGreg Roach
88*1d1f373cSGreg Roach        return response();
89fd6c003fSGreg Roach    }
90fd6c003fSGreg Roach}
91