summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorGreg Roach <greg@subaqua.co.uk>2021-07-26 10:01:52 +0100
committerGreg Roach <greg@subaqua.co.uk>2021-07-26 10:01:52 +0100
commitc6bad570f5cd737965ffd04bf731ab3a4f827e94 (patch)
treeba77bfb750810a4af579b151f6bc47d2340bc7b5 /app
parent78fa3281a59b3821e582123be84e8aac9b13d53e (diff)
downloadwebtrees-c6bad570f5cd737965ffd04bf731ab3a4f827e94.tar.gz
webtrees-c6bad570f5cd737965ffd04bf731ab3a4f827e94.tar.bz2
webtrees-c6bad570f5cd737965ffd04bf731ab3a4f827e94.zip
Fix: #3860 - hide system media folders, Fix: #3943 - default tree settings not saved to disk
Diffstat (limited to 'app')
-rw-r--r--app/Services/MediaFileService.php58
1 files changed, 39 insertions, 19 deletions
diff --git a/app/Services/MediaFileService.php b/app/Services/MediaFileService.php
index bccb43cdb0..1c6e637ee2 100644
--- a/app/Services/MediaFileService.php
+++ b/app/Services/MediaFileService.php
@@ -25,6 +25,7 @@ use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Tree;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Query\Expression;
+use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use League\Flysystem\Filesystem;
@@ -77,6 +78,18 @@ class MediaFileService
'tiff' => 'tif',
];
+ private const IGNORE_FOLDERS = [
+ // Old versions of webtrees
+ 'thumbs',
+ 'watermarks',
+ // Windows
+ 'Thumbs.db',
+ // Synology
+ '@eaDir',
+ // QNAP,
+ '.@__thumb',
+ ];
+
/**
* What is the largest file a user may upload?
*/
@@ -134,7 +147,7 @@ class MediaFileService
->pluck('multimedia_file_refn')
->all();
- $media_filesystem = $disk_files = $tree->mediaFilesystem($data_filesystem);
+ $media_filesystem = $tree->mediaFilesystem($data_filesystem);
$disk_files = $this->allFilesOnDisk($media_filesystem, '', Filesystem::LIST_DEEP)->all();
$unused_files = array_diff($disk_files, $used_files);
@@ -284,7 +297,7 @@ class MediaFileService
try {
$files = $filesystem->listContents($folder, $subfolders)
->filter(function (StorageAttributes $attributes): bool {
- return $attributes->isFile() && !$this->isLegacyFolder($attributes->path());
+ return $attributes->isFile() && !$this->ignorePath($attributes->path());
})
->map(static function (StorageAttributes $attributes): string {
return $attributes->path();
@@ -335,20 +348,27 @@ class MediaFileService
public function allMediaFolders(FilesystemOperator $data_filesystem): Collection
{
$db_folders = DB::table('media_file')
- ->join('gedcom_setting', 'gedcom_id', '=', 'm_file')
- ->where('setting_name', '=', 'MEDIA_DIRECTORY')
+ ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
+ $join
+ ->on('gedcom_id', '=', 'm_file')
+ ->where('setting_name', '=', 'MEDIA_DIRECTORY');
+ })
->where('multimedia_file_refn', 'NOT LIKE', 'http://%')
->where('multimedia_file_refn', 'NOT LIKE', 'https://%')
- ->select(new Expression('setting_value || multimedia_file_refn AS path'))
+ ->select(new Expression("COALESCE(setting_value, 'media/') || multimedia_file_refn AS path"))
->pluck('path')
->map(static function (string $path): string {
return dirname($path) . '/';
});
- $media_roots = DB::table('gedcom_setting')
- ->where('setting_name', '=', 'MEDIA_DIRECTORY')
- ->where('gedcom_id', '>', '0')
- ->pluck('setting_value')
+ $media_roots = DB::table('gedcom')
+ ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
+ $join
+ ->on('gedcom.gedcom_id', '=', 'gedcom_setting.gedcom_id')
+ ->where('setting_name', '=', 'MEDIA_DIRECTORY');
+ })
+ ->where('gedcom.gedcom_id', '>', '0')
+ ->pluck(new Expression("COALESCE(setting_value, 'media/')"))
->uniqueStrict();
$disk_folders = new Collection($media_roots);
@@ -356,7 +376,7 @@ class MediaFileService
foreach ($media_roots as $media_folder) {
$tmp = $data_filesystem->listContents($media_folder, Filesystem::LIST_DEEP)
->filter(function (StorageAttributes $attributes): bool {
- return $attributes->isDir() && !$this->isLegacyFolder($attributes->path());
+ return $attributes->isDir() && !$this->ignorePath($attributes->path());
})
->map(static function (StorageAttributes $attributes): string {
return $attributes->path() . '/';
@@ -374,20 +394,20 @@ class MediaFileService
}
/**
- * Some special media folders were created by earlier versions of webtrees.
+ * Ignore certain media folders.
*
* @param string $path
*
* @return bool
*/
- private function isLegacyFolder(string $path): bool
+ private function ignorePath(string $path): bool
{
- return
- str_starts_with($path, 'thumbs/') ||
- str_contains($path, '/thumbs/') ||
- str_ends_with($path, '/thumbs') ||
- str_starts_with($path, 'watermarks/') ||
- str_contains($path, '/watermarks/') ||
- str_ends_with($path, '/watermarks');
+ foreach (explode('/', $path) as $part) {
+ if (in_array($part, self::IGNORE_FOLDERS, true)) {
+ return true;
+ }
+ }
+
+ return false;
}
}