1
0

downgrade to kirby v3

This commit is contained in:
Philip Wagner
2024-09-01 10:47:15 +02:00
parent a4b2aece7b
commit af86acb7a1
1085 changed files with 54743 additions and 65042 deletions

View File

@@ -3,8 +3,6 @@
namespace Kirby\Cms;
use Kirby\Data\Data;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Filesystem\Dir;
use Kirby\Filesystem\F;
use Kirby\Toolkit\Str;
@@ -25,12 +23,14 @@ class Media
/**
* Tries to find a file by model and filename
* and to copy it to the media folder.
*
* @param \Kirby\Cms\Model|null $model
* @param string $hash
* @param string $filename
* @return \Kirby\Cms\Response|false
*/
public static function link(
Page|Site|User $model = null,
string $hash,
string $filename
): Response|false {
public static function link(Model $model = null, string $hash, string $filename)
{
if ($model === null) {
return false;
}
@@ -46,10 +46,10 @@ class Media
// if at least the token was correct, redirect
if (Str::startsWith($hash, $file->mediaToken() . '-') === true) {
return Response::redirect($file->mediaUrl(), 307);
} else {
// don't leak the correct token, render the error page
return false;
}
// don't leak the correct token, render the error page
return false;
}
// send the file to the browser
@@ -57,16 +57,15 @@ class Media
}
// try to generate a thumb for the file
try {
return static::thumb($model, $hash, $filename);
} catch (NotFoundException) {
// render the error page if there is no job for this filename
return false;
}
return static::thumb($model, $hash, $filename);
}
/**
* Copy the file to the final media folder location
*
* @param \Kirby\Cms\File $file
* @param string $dest
* @return bool
*/
public static function publish(File $file, string $dest): bool
{
@@ -88,74 +87,66 @@ class Media
* Tries to find a job file for the
* given filename and then calls the thumb
* component to create a thumbnail accordingly
*
* @param \Kirby\Cms\Model|string $model
* @param string $hash
* @param string $filename
* @return \Kirby\Cms\Response|false
*/
public static function thumb(
File|Page|Site|User|string $model,
string $hash,
string $filename
): Response|false {
public static function thumb($model, string $hash, string $filename)
{
$kirby = App::instance();
$root = match (true) {
// assets
is_string($model)
=> $kirby->root('media') . '/assets/' . $model . '/' . $hash,
// parent files for file model that already included hash
$model instanceof File
=> dirname($model->mediaRoot()),
// model files
default
=> $model->mediaRoot() . '/' . $hash
};
$thumb = $root . '/' . $filename;
$job = $root . '/.jobs/' . $filename . '.json';
// assets
if (is_string($model) === true) {
$root = $kirby->root('media') . '/assets/' . $model . '/' . $hash;
// parent files for file model that already included hash
} elseif (is_a($model, '\Kirby\Cms\File')) {
$root = dirname($model->mediaRoot());
// model files
} else {
$root = $model->mediaRoot() . '/' . $hash;
}
try {
$thumb = $root . '/' . $filename;
$job = $root . '/.jobs/' . $filename . '.json';
$options = Data::read($job);
} catch (Throwable) {
// send a customized error message to make clearer what happened here
throw new NotFoundException('The thumbnail configuration could not be found');
}
if (empty($options['filename']) === true) {
throw new InvalidArgumentException('Incomplete thumbnail configuration');
}
if (empty($options) === true) {
return false;
}
try {
// find the correct source file depending on the model
// this adds support for custom assets
$source = match (true) {
is_string($model) === true
=> $kirby->root('index') . '/' . $model . '/' . $options['filename'],
default
=> $model->file($options['filename'])->root()
};
if (is_string($model) === true) {
$source = $kirby->root('index') . '/' . $model . '/' . $options['filename'];
} else {
$source = $model->file($options['filename'])->root();
}
// generate the thumbnail and save it in the media folder
$kirby->thumb($source, $thumb, $options);
// remove the job file once the thumbnail has been created
F::remove($job);
// read the file and send it to the browser
return Response::file($thumb);
try {
$kirby->thumb($source, $thumb, $options);
F::remove($job);
return Response::file($thumb);
} catch (Throwable $e) {
F::remove($thumb);
return Response::file($source);
}
} catch (Throwable $e) {
// remove potentially broken thumbnails
F::remove($thumb);
throw $e;
return false;
}
}
/**
* Deletes all versions of the given file
* within the parent directory
*
* @param string $directory
* @param \Kirby\Cms\File $file
* @param string|null $ignore
* @return bool
*/
public static function unpublish(
string $directory,
File $file,
string|null $ignore = null
): bool {
public static function unpublish(string $directory, File $file, string $ignore = null): bool
{
if (is_dir($directory) === false) {
return true;
}