xref: /webtrees/app/Http/RequestHandlers/ManageMediaData.php (revision ddb44b4cf356ab8fd0c5d21becf3fce4c4e46244)
18ce3bd73SGreg Roach<?php
28ce3bd73SGreg Roach
38ce3bd73SGreg Roach/**
48ce3bd73SGreg Roach * webtrees: online genealogy
58ce3bd73SGreg Roach * Copyright (C) 2020 webtrees development team
68ce3bd73SGreg Roach * This program is free software: you can redistribute it and/or modify
78ce3bd73SGreg Roach * it under the terms of the GNU General Public License as published by
88ce3bd73SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98ce3bd73SGreg Roach * (at your option) any later version.
108ce3bd73SGreg Roach * This program is distributed in the hope that it will be useful,
118ce3bd73SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128ce3bd73SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138ce3bd73SGreg Roach * GNU General Public License for more details.
148ce3bd73SGreg Roach * You should have received a copy of the GNU General Public License
158ce3bd73SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168ce3bd73SGreg Roach */
178ce3bd73SGreg Roach
188ce3bd73SGreg Roachdeclare(strict_types=1);
198ce3bd73SGreg Roach
208ce3bd73SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
218ce3bd73SGreg Roach
228ce3bd73SGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
238ce3bd73SGreg Roachuse Fisharebest\Webtrees\I18N;
248ce3bd73SGreg Roachuse Fisharebest\Webtrees\Media;
258ce3bd73SGreg Roachuse Fisharebest\Webtrees\Mime;
268ce3bd73SGreg Roachuse Fisharebest\Webtrees\Registry;
278ce3bd73SGreg Roachuse Fisharebest\Webtrees\Services\DatatablesService;
288ce3bd73SGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
298ce3bd73SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
308ce3bd73SGreg Roachuse Fisharebest\Webtrees\Webtrees;
318ce3bd73SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
328ce3bd73SGreg Roachuse Illuminate\Database\Query\Builder;
338ce3bd73SGreg Roachuse Illuminate\Database\Query\Expression;
348ce3bd73SGreg Roachuse Illuminate\Database\Query\JoinClause;
358ce3bd73SGreg Roachuse League\Flysystem\FileNotFoundException;
368ce3bd73SGreg Roachuse League\Flysystem\FilesystemInterface;
378ce3bd73SGreg Roachuse Psr\Http\Message\ResponseInterface;
388ce3bd73SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
398ce3bd73SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
408ce3bd73SGreg Roachuse stdClass;
418ce3bd73SGreg Roachuse Throwable;
428ce3bd73SGreg Roach
438ce3bd73SGreg Roachuse function assert;
448ce3bd73SGreg Roachuse function e;
458ce3bd73SGreg Roachuse function getimagesize;
468ce3bd73SGreg Roachuse function intdiv;
478ce3bd73SGreg Roachuse function route;
488ce3bd73SGreg Roachuse function str_starts_with;
498ce3bd73SGreg Roachuse function strlen;
508ce3bd73SGreg Roachuse function substr;
518ce3bd73SGreg Roachuse function view;
528ce3bd73SGreg Roach
538ce3bd73SGreg Roach/**
548ce3bd73SGreg Roach * Manage media from the control panel.
558ce3bd73SGreg Roach */
568ce3bd73SGreg Roachclass ManageMediaData implements RequestHandlerInterface
578ce3bd73SGreg Roach{
588ce3bd73SGreg Roach    /** @var DatatablesService */
598ce3bd73SGreg Roach    private $datatables_service;
608ce3bd73SGreg Roach
618ce3bd73SGreg Roach    /** @var MediaFileService */
628ce3bd73SGreg Roach    private $media_file_service;
638ce3bd73SGreg Roach
648ce3bd73SGreg Roach    /** @var TreeService */
658ce3bd73SGreg Roach    private $tree_service;
668ce3bd73SGreg Roach
678ce3bd73SGreg Roach    /**
688ce3bd73SGreg Roach     * MediaController constructor.
698ce3bd73SGreg Roach     *
708ce3bd73SGreg Roach     * @param DatatablesService $datatables_service
718ce3bd73SGreg Roach     * @param MediaFileService  $media_file_service
728ce3bd73SGreg Roach     * @param TreeService       $tree_service
738ce3bd73SGreg Roach     */
748ce3bd73SGreg Roach    public function __construct(
758ce3bd73SGreg Roach        DatatablesService $datatables_service,
768ce3bd73SGreg Roach        MediaFileService $media_file_service,
778ce3bd73SGreg Roach        TreeService $tree_service
788ce3bd73SGreg Roach    ) {
798ce3bd73SGreg Roach        $this->datatables_service = $datatables_service;
808ce3bd73SGreg Roach        $this->media_file_service = $media_file_service;
818ce3bd73SGreg Roach        $this->tree_service       = $tree_service;
828ce3bd73SGreg Roach    }
838ce3bd73SGreg Roach
848ce3bd73SGreg Roach    /**
858ce3bd73SGreg Roach     * @param ServerRequestInterface $request
868ce3bd73SGreg Roach     *
878ce3bd73SGreg Roach     * @return ResponseInterface
888ce3bd73SGreg Roach     */
898ce3bd73SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
908ce3bd73SGreg Roach    {
918ce3bd73SGreg Roach        $data_filesystem = Registry::filesystem()->data();
928ce3bd73SGreg Roach
938ce3bd73SGreg Roach        $files = $request->getQueryParams()['files']; // local|external|unused
948ce3bd73SGreg Roach
958ce3bd73SGreg Roach        // Files within this folder
968ce3bd73SGreg Roach        $media_folder = $request->getQueryParams()['media_folder'];
978ce3bd73SGreg Roach
988ce3bd73SGreg Roach        // Show sub-folders within $media_folder
998ce3bd73SGreg Roach        $subfolders = $request->getQueryParams()['subfolders']; // include|exclude
1008ce3bd73SGreg Roach
1018ce3bd73SGreg Roach        $search_columns = ['multimedia_file_refn', 'descriptive_title'];
1028ce3bd73SGreg Roach
1038ce3bd73SGreg Roach        $sort_columns = [
1048ce3bd73SGreg Roach            0 => 'multimedia_file_refn',
10596c9e653SGreg Roach            2 => new Expression('descriptive_title || multimedia_file_refn'),
1068ce3bd73SGreg Roach        ];
1078ce3bd73SGreg Roach
1088ce3bd73SGreg Roach        // Convert a row from the database into a row for datatables
1098ce3bd73SGreg Roach        $callback = function (stdClass $row): array {
1108ce3bd73SGreg Roach            $tree  = $this->tree_service->find((int) $row->m_file);
1118ce3bd73SGreg Roach            $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom);
1128ce3bd73SGreg Roach            assert($media instanceof Media);
1138ce3bd73SGreg Roach
1148ce3bd73SGreg Roach            $path = $row->media_folder . $row->multimedia_file_refn;
1158ce3bd73SGreg Roach
1168ce3bd73SGreg Roach            try {
1178ce3bd73SGreg Roach                $mime_type = Registry::filesystem()->data()->getMimeType($path) ?: Mime::DEFAULT_TYPE;
1188ce3bd73SGreg Roach
1198ce3bd73SGreg Roach                if (str_starts_with($mime_type, 'image/')) {
1208ce3bd73SGreg Roach                    $url = route(AdminMediaFileThumbnail::class, ['path' => $path]);
1218ce3bd73SGreg Roach                    $img = '<img src="' . e($url) . '">';
1228ce3bd73SGreg Roach                } else {
1238ce3bd73SGreg Roach                    $img = view('icons/mime', ['type' => $mime_type]);
1248ce3bd73SGreg Roach                }
1258ce3bd73SGreg Roach
1268ce3bd73SGreg Roach                $url = route(AdminMediaFileDownload::class, ['path' => $path]);
1278ce3bd73SGreg Roach                $img = '<a href="' . e($url) . '" type="' . $mime_type . '" class="gallery">' . $img . '</a>';
1288ce3bd73SGreg Roach            } catch (FileNotFoundException $ex) {
1298ce3bd73SGreg Roach                $url = route(AdminMediaFileThumbnail::class, ['path' => $path]);
1308ce3bd73SGreg Roach                $img = '<img src="' . e($url) . '">';
1318ce3bd73SGreg Roach            }
1328ce3bd73SGreg Roach
1338ce3bd73SGreg Roach            return [
1348ce3bd73SGreg Roach                $row->multimedia_file_refn,
1358ce3bd73SGreg Roach                $img,
1368ce3bd73SGreg Roach                $this->mediaObjectInfo($media),
1378ce3bd73SGreg Roach            ];
1388ce3bd73SGreg Roach        };
1398ce3bd73SGreg Roach
1408ce3bd73SGreg Roach        switch ($files) {
1418ce3bd73SGreg Roach            case 'local':
1428ce3bd73SGreg Roach                $query = DB::table('media_file')
1438ce3bd73SGreg Roach                    ->join('media', static function (JoinClause $join): void {
1448ce3bd73SGreg Roach                        $join
1458ce3bd73SGreg Roach                            ->on('media.m_file', '=', 'media_file.m_file')
1468ce3bd73SGreg Roach                            ->on('media.m_id', '=', 'media_file.m_id');
1478ce3bd73SGreg Roach                    })
1488ce3bd73SGreg Roach                    ->join('gedcom_setting', 'gedcom_id', '=', 'media.m_file')
1498ce3bd73SGreg Roach                    ->where('setting_name', '=', 'MEDIA_DIRECTORY')
1508ce3bd73SGreg Roach                    ->where('multimedia_file_refn', 'NOT LIKE', 'http://%')
1518ce3bd73SGreg Roach                    ->where('multimedia_file_refn', 'NOT LIKE', 'https://%')
1528ce3bd73SGreg Roach                    ->select([
1538ce3bd73SGreg Roach                        'media.*',
1548ce3bd73SGreg Roach                        'multimedia_file_refn',
1558ce3bd73SGreg Roach                        'descriptive_title',
1568ce3bd73SGreg Roach                        'setting_value AS media_folder',
1578ce3bd73SGreg Roach                    ]);
1588ce3bd73SGreg Roach
1598ce3bd73SGreg Roach                $query->where(new Expression('setting_value || multimedia_file_refn'), 'LIKE', $media_folder . '%');
1608ce3bd73SGreg Roach
1618ce3bd73SGreg Roach                if ($subfolders === 'exclude') {
1628ce3bd73SGreg Roach                    $query->where(new Expression('setting_value || multimedia_file_refn'), 'NOT LIKE', $media_folder . '%/%');
1638ce3bd73SGreg Roach                }
1648ce3bd73SGreg Roach
1658ce3bd73SGreg Roach                return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
1668ce3bd73SGreg Roach
1678ce3bd73SGreg Roach            case 'external':
1688ce3bd73SGreg Roach                $query = DB::table('media_file')
1698ce3bd73SGreg Roach                    ->join('media', static function (JoinClause $join): void {
1708ce3bd73SGreg Roach                        $join
1718ce3bd73SGreg Roach                            ->on('media.m_file', '=', 'media_file.m_file')
1728ce3bd73SGreg Roach                            ->on('media.m_id', '=', 'media_file.m_id');
1738ce3bd73SGreg Roach                    })
1748ce3bd73SGreg Roach                    ->where(static function (Builder $query): void {
1758ce3bd73SGreg Roach                        $query
1768ce3bd73SGreg Roach                            ->where('multimedia_file_refn', 'LIKE', 'http://%')
1778ce3bd73SGreg Roach                            ->orWhere('multimedia_file_refn', 'LIKE', 'https://%');
1788ce3bd73SGreg Roach                    })
1798ce3bd73SGreg Roach                    ->select([
1808ce3bd73SGreg Roach                        'media.*',
1818ce3bd73SGreg Roach                        'multimedia_file_refn',
1828ce3bd73SGreg Roach                        'descriptive_title',
18396c9e653SGreg Roach                        new Expression("'' AS media_folder"),
1848ce3bd73SGreg Roach                    ]);
1858ce3bd73SGreg Roach
1868ce3bd73SGreg Roach                return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
1878ce3bd73SGreg Roach
1888ce3bd73SGreg Roach            case 'unused':
1898ce3bd73SGreg Roach                // Which trees use which media folder?
1908ce3bd73SGreg Roach                $media_trees = DB::table('gedcom')
1918ce3bd73SGreg Roach                    ->join('gedcom_setting', 'gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
1928ce3bd73SGreg Roach                    ->where('setting_name', '=', 'MEDIA_DIRECTORY')
1938ce3bd73SGreg Roach                    ->where('gedcom.gedcom_id', '>', 0)
1948ce3bd73SGreg Roach                    ->pluck('setting_value', 'gedcom_name');
1958ce3bd73SGreg Roach
1968ce3bd73SGreg Roach                $disk_files = $this->media_file_service->allFilesOnDisk($data_filesystem, $media_folder, $subfolders === 'include');
1978ce3bd73SGreg Roach                $db_files   = $this->media_file_service->allFilesInDatabase($media_folder, $subfolders === 'include');
1988ce3bd73SGreg Roach
1998ce3bd73SGreg Roach                // All unused files
2008ce3bd73SGreg Roach                $unused_files = $disk_files->diff($db_files)
2018ce3bd73SGreg Roach                    ->map(static function (string $file): array {
2028ce3bd73SGreg Roach                        return (array) $file;
2038ce3bd73SGreg Roach                    });
2048ce3bd73SGreg Roach
2058ce3bd73SGreg Roach                $search_columns = [0];
2068ce3bd73SGreg Roach                $sort_columns   = [0 => 0];
2078ce3bd73SGreg Roach
2088ce3bd73SGreg Roach                $callback = function (array $row) use ($data_filesystem, $media_trees): array {
2098ce3bd73SGreg Roach                    $mime_type = $data_filesystem->getMimeType($row[0]) ?: Mime::DEFAULT_TYPE;
2108ce3bd73SGreg Roach
2118ce3bd73SGreg Roach                    if (str_starts_with($mime_type, 'image/')) {
2128ce3bd73SGreg Roach                        $url = route(AdminMediaFileThumbnail::class, ['path' => $row[0]]);
2138ce3bd73SGreg Roach                        $img = '<img src="' . e($url) . '">';
2148ce3bd73SGreg Roach                    } else {
2158ce3bd73SGreg Roach                        $img = view('icons/mime', ['type' => $mime_type]);
2168ce3bd73SGreg Roach                    }
2178ce3bd73SGreg Roach
2188ce3bd73SGreg Roach                    $url = route(AdminMediaFileDownload::class, ['path' => $row[0]]);
2198ce3bd73SGreg Roach                    $img = '<a href="' . e($url) . '">' . $img . '</a>';
2208ce3bd73SGreg Roach
2218ce3bd73SGreg Roach                    // Form to create new media object in each tree
2228ce3bd73SGreg Roach                    $create_form = '';
2238ce3bd73SGreg Roach                    foreach ($media_trees as $media_tree => $media_directory) {
2248ce3bd73SGreg Roach                        if (str_starts_with($row[0], $media_directory)) {
2258ce3bd73SGreg Roach                            $tmp         = substr($row[0], strlen($media_directory));
2268ce3bd73SGreg Roach                            $create_form .=
227*ddb44b4cSGreg Roach                                '<p><a href="#" data-toggle="modal" data-backdrop="static" data-target="#modal-create-media-from-file" data-file="' . e($tmp) . '" data-url="' . e(route(CreateMediaObjectFromFile::class, ['tree' => $media_tree])) . '" onclick="document.getElementById(\'modal-create-media-from-file-form\').action=this.dataset.url; document.getElementById(\'file\').value=this.dataset.file;">' . I18N::translate('Create') . '</a> — ' . e($media_tree) . '<p>';
2288ce3bd73SGreg Roach                        }
2298ce3bd73SGreg Roach                    }
2308ce3bd73SGreg Roach
2318ce3bd73SGreg Roach                    $delete_link = '<p><a data-confirm="' . I18N::translate('Are you sure you want to delete “%s”?', e($row[0])) . '" data-post-url="' . e(route(DeletePath::class, [
2328ce3bd73SGreg Roach                            'path' => $row[0],
2338ce3bd73SGreg Roach                        ])) . '" href="#">' . I18N::translate('Delete') . '</a></p>';
2348ce3bd73SGreg Roach
2358ce3bd73SGreg Roach                    return [
2368ce3bd73SGreg Roach                        $this->mediaFileInfo($data_filesystem, $row[0]) . $delete_link,
2378ce3bd73SGreg Roach                        $img,
2388ce3bd73SGreg Roach                        $create_form,
2398ce3bd73SGreg Roach                    ];
2408ce3bd73SGreg Roach                };
2418ce3bd73SGreg Roach
2428ce3bd73SGreg Roach                return $this->datatables_service->handleCollection($request, $unused_files, $search_columns, $sort_columns, $callback);
2438ce3bd73SGreg Roach
2448ce3bd73SGreg Roach            default:
2458ce3bd73SGreg Roach                throw new HttpNotFoundException();
2468ce3bd73SGreg Roach        }
2478ce3bd73SGreg Roach    }
2488ce3bd73SGreg Roach
2498ce3bd73SGreg Roach    /**
2508ce3bd73SGreg Roach     * Generate some useful information and links about a media object.
2518ce3bd73SGreg Roach     *
2528ce3bd73SGreg Roach     * @param Media $media
2538ce3bd73SGreg Roach     *
2548ce3bd73SGreg Roach     * @return string HTML
2558ce3bd73SGreg Roach     */
2568ce3bd73SGreg Roach    private function mediaObjectInfo(Media $media): string
2578ce3bd73SGreg Roach    {
2588ce3bd73SGreg Roach        $html = '<b><a href="' . e($media->url()) . '">' . $media->fullName() . '</a></b>' . '<br><i>' . e($media->getNote()) . '</i></br><br>';
2598ce3bd73SGreg Roach
2608ce3bd73SGreg Roach        $linked = [];
2618ce3bd73SGreg Roach        foreach ($media->linkedIndividuals('OBJE') as $link) {
2628ce3bd73SGreg Roach            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
2638ce3bd73SGreg Roach        }
2648ce3bd73SGreg Roach        foreach ($media->linkedFamilies('OBJE') as $link) {
2658ce3bd73SGreg Roach            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
2668ce3bd73SGreg Roach        }
2678ce3bd73SGreg Roach        foreach ($media->linkedSources('OBJE') as $link) {
2688ce3bd73SGreg Roach            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
2698ce3bd73SGreg Roach        }
2708ce3bd73SGreg Roach        foreach ($media->linkedNotes('OBJE') as $link) {
2718ce3bd73SGreg Roach            // Invalid GEDCOM - you cannot link a NOTE to an OBJE
2728ce3bd73SGreg Roach            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
2738ce3bd73SGreg Roach        }
2748ce3bd73SGreg Roach        foreach ($media->linkedRepositories('OBJE') as $link) {
2758ce3bd73SGreg Roach            // Invalid GEDCOM - you cannot link a REPO to an OBJE
2768ce3bd73SGreg Roach            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
2778ce3bd73SGreg Roach        }
2788ce3bd73SGreg Roach        foreach ($media->linkedLocations('OBJE') as $link) {
2798ce3bd73SGreg Roach            $linked[] = '<a href="' . e($link->url()) . '">' . $link->fullName() . '</a>';
2808ce3bd73SGreg Roach        }
2818ce3bd73SGreg Roach        if ($linked !== []) {
2828ce3bd73SGreg Roach            $html .= '<ul>';
2838ce3bd73SGreg Roach            foreach ($linked as $link) {
2848ce3bd73SGreg Roach                $html .= '<li>' . $link . '</li>';
2858ce3bd73SGreg Roach            }
2868ce3bd73SGreg Roach            $html .= '</ul>';
2878ce3bd73SGreg Roach        } else {
2888ce3bd73SGreg Roach            $html .= '<div class="alert alert-danger">' . I18N::translate('There are no links to this media object.') . '</div>';
2898ce3bd73SGreg Roach        }
2908ce3bd73SGreg Roach
2918ce3bd73SGreg Roach        return $html;
2928ce3bd73SGreg Roach    }
2938ce3bd73SGreg Roach
2948ce3bd73SGreg Roach    /**
2958ce3bd73SGreg Roach     * Generate some useful information and links about a media file.
2968ce3bd73SGreg Roach     *
2978ce3bd73SGreg Roach     * @param FilesystemInterface $data_filesystem
2988ce3bd73SGreg Roach     * @param string              $file
2998ce3bd73SGreg Roach     *
3008ce3bd73SGreg Roach     * @return string
3018ce3bd73SGreg Roach     */
3028ce3bd73SGreg Roach    private function mediaFileInfo(FilesystemInterface $data_filesystem, string $file): string
3038ce3bd73SGreg Roach    {
3048ce3bd73SGreg Roach        $html = '<dl>';
3058ce3bd73SGreg Roach        $html .= '<dt>' . I18N::translate('Filename') . '</dt>';
3068ce3bd73SGreg Roach        $html .= '<dd>' . e($file) . '</dd>';
3078ce3bd73SGreg Roach
3088ce3bd73SGreg Roach        if ($data_filesystem->has($file)) {
3098ce3bd73SGreg Roach            $size = $data_filesystem->getSize($file);
3108ce3bd73SGreg Roach            $size = intdiv($size + 1023, 1024); // Round up to next KB
3118ce3bd73SGreg Roach            /* I18N: size of file in KB */
3128ce3bd73SGreg Roach            $size = I18N::translate('%s KB', I18N::number($size));
3138ce3bd73SGreg Roach            $html .= '<dt>' . I18N::translate('File size') . '</dt>';
3148ce3bd73SGreg Roach            $html .= '<dd>' . $size . '</dd>';
3158ce3bd73SGreg Roach
3168ce3bd73SGreg Roach            try {
3178ce3bd73SGreg Roach                // This will work for local filesystems.  For remote filesystems, we will
3188ce3bd73SGreg Roach                // need to copy the file locally to work out the image size.
3198ce3bd73SGreg Roach                $imgsize = getimagesize(Webtrees::DATA_DIR . $file);
3208ce3bd73SGreg Roach                $html    .= '<dt>' . I18N::translate('Image dimensions') . '</dt>';
3218ce3bd73SGreg Roach                /* I18N: image dimensions, width × height */
3228ce3bd73SGreg Roach                $html .= '<dd>' . I18N::translate('%1$s × %2$s pixels', I18N::number($imgsize['0']), I18N::number($imgsize['1'])) . '</dd>';
3238ce3bd73SGreg Roach            } catch (Throwable $ex) {
3248ce3bd73SGreg Roach                // Not an image, or not a valid image?
3258ce3bd73SGreg Roach            }
3268ce3bd73SGreg Roach        }
3278ce3bd73SGreg Roach
3288ce3bd73SGreg Roach        $html .= '</dl>';
3298ce3bd73SGreg Roach
3308ce3bd73SGreg Roach        return $html;
3318ce3bd73SGreg Roach    }
3328ce3bd73SGreg Roach}
333