Migrate Laysense to upstream LaySite 1.4.3 with external site data
This commit is contained in:
+29
-18
@@ -4,11 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use App\Config\Environment;
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMXPath;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
use Webman\Http\UploadFile;
|
||||
@@ -34,21 +33,7 @@ final class Media
|
||||
}
|
||||
unset($file);
|
||||
|
||||
$root = dirname(__DIR__, 2) . '/public/assets';
|
||||
$builtIn = [];
|
||||
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root)) as $file) {
|
||||
if (!$file->isFile() || $file->isLink() || str_starts_with($file->getFilename(), '.')) {
|
||||
continue;
|
||||
}
|
||||
$path = str_replace(DIRECTORY_SEPARATOR, '/', substr($file->getPathname(), strlen($root) + 1));
|
||||
$label = $file->getFilename();
|
||||
$type = array_search(strtolower($file->getExtension()), self::EXTENSIONS, true) ?: 'application/octet-stream';
|
||||
$builtIn[] = ['label' => $label, 'name' => '', 'mime' => $type, 'size' => $file->getSize(),
|
||||
'url' => '/assets/' . $path, 'archived' => 0];
|
||||
}
|
||||
usort($builtIn, static fn (array $left, array $right): int => strnatcasecmp($left['url'], $right['url']));
|
||||
|
||||
return [...$files, ...$builtIn];
|
||||
return $files;
|
||||
}
|
||||
|
||||
public static function upload(UploadFile $file, string $label): void
|
||||
@@ -100,7 +85,33 @@ final class Media
|
||||
|
||||
public static function directory(): string
|
||||
{
|
||||
return rtrim(getenv('LAYSENSE_MEDIA_DIR') ?: dirname(__DIR__, 2) . '/storage/media', '/');
|
||||
return rtrim(Environment::path('LAYSITE_MEDIA_DIR', 'storage/media'), '/');
|
||||
}
|
||||
|
||||
public static function usage(): array
|
||||
{
|
||||
$bytes = Database::connect()->query('SELECT COALESCE(SUM(size), 0) FROM media')->fetchColumn();
|
||||
$directory = self::directory();
|
||||
while ($directory !== '' && $directory !== dirname($directory) && !@is_dir($directory)) {
|
||||
$directory = dirname($directory);
|
||||
}
|
||||
$free = @is_dir($directory) ? @disk_free_space($directory) : false;
|
||||
|
||||
return [
|
||||
'size' => SystemInfo::bytes((int) $bytes),
|
||||
'free' => $free === false ? null : SystemInfo::bytes($free),
|
||||
];
|
||||
}
|
||||
|
||||
public static function validateBackup(string $path, string $name, string $mime, int $size): void
|
||||
{
|
||||
if ((new \finfo(FILEINFO_MIME_TYPE))->file($path) !== $mime
|
||||
|| !isset(self::EXTENSIONS[$mime]) || self::EXTENSIONS[$mime] !== pathinfo($name, PATHINFO_EXTENSION)) {
|
||||
throw new RuntimeException('备份资源类型与清单不一致。');
|
||||
}
|
||||
if ($mime === 'image/svg+xml') {
|
||||
self::safeSvg($path, $size);
|
||||
}
|
||||
}
|
||||
|
||||
private static function safeSvg(string $source, int $size): string
|
||||
|
||||
Reference in New Issue
Block a user