Less home code more F3
This commit is contained in:
@@ -100,7 +100,8 @@ class Media extends DB\SQL\Mapper
|
||||
|
||||
// F3 Image : load() utilise imagecreatefromstring + imagesavealpha.
|
||||
$img = new \Image();
|
||||
if (!$img->load(file_get_contents($srcPath))) {
|
||||
$f3 = Base::instance();
|
||||
if (!$img->load($f3->read($srcPath))) {
|
||||
throw new RuntimeException('Fichier image invalide ou format source non supporté.');
|
||||
}
|
||||
|
||||
@@ -110,11 +111,11 @@ class Media extends DB\SQL\Mapper
|
||||
|
||||
// Nom aléatoire : empêche le path traversal et la devinabilité des URLs.
|
||||
$fileName = bin2hex(random_bytes(16)) . '.' . $extension;
|
||||
$target = app_public_media_dir() . '/' . $fileName;
|
||||
$target = rtrim((string) $f3->get('paths.media_dir'), '/\\') . DIRECTORY_SEPARATOR . $fileName;
|
||||
|
||||
// dump() appelle image{format}($data, NULL, $quality).
|
||||
$binary = $isJpeg ? $img->dump('jpeg', 85) : $img->dump('png', 6);
|
||||
if ($binary === '' || file_put_contents($target, $binary) === false) {
|
||||
if ($binary === '' || $f3->write($target, $binary) === false) {
|
||||
throw new RuntimeException('Impossible d\'enregistrer cette image.');
|
||||
}
|
||||
|
||||
@@ -166,7 +167,7 @@ class Media extends DB\SQL\Mapper
|
||||
throw new RuntimeException('Image introuvable.');
|
||||
}
|
||||
|
||||
$path = app_public_media_dir() . '/' . $this->file_name;
|
||||
$path = rtrim((string) Base::instance()->get('paths.media_dir'), '/\\') . DIRECTORY_SEPARATOR . $this->file_name;
|
||||
|
||||
$this->db->begin();
|
||||
try {
|
||||
@@ -224,6 +225,15 @@ class Media extends DB\SQL\Mapper
|
||||
return mb_strtoupper(mb_substr($name, 0, 1)) . mb_strtolower(mb_substr($name, 1));
|
||||
}
|
||||
|
||||
private function mediaUrl(string $fileName): string
|
||||
{
|
||||
$f3 = Base::instance();
|
||||
$base = rtrim((string) $f3->get('BASE'), '/');
|
||||
$prefix = '/' . trim((string) $f3->get('paths.media_base'), '/');
|
||||
|
||||
return $base . $prefix . '/' . rawurlencode($fileName);
|
||||
}
|
||||
|
||||
private function decorate(array $row): array
|
||||
{
|
||||
$alt = (string) $row['alt'];
|
||||
@@ -235,7 +245,7 @@ class Media extends DB\SQL\Mapper
|
||||
'width' => (int) $row['width'],
|
||||
'height' => (int) $row['height'],
|
||||
'created_at' => (string) $row['created_at'],
|
||||
'url' => app_media_url((string) $row['file_name']),
|
||||
'url' => $this->mediaUrl((string) $row['file_name']),
|
||||
'markdown' => '',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -47,9 +47,7 @@ class User extends DB\SQL\Mapper
|
||||
|
||||
public function create(string $username, string $password): int
|
||||
{
|
||||
$f3 = Base::instance();
|
||||
$f3->scrub($username);
|
||||
$username = trim($username);
|
||||
$username = Base::instance()->clean($username);
|
||||
|
||||
if ($username === '' || $password === '') {
|
||||
throw new RuntimeException('Nom d’utilisateur et mot de passe obligatoires.');
|
||||
|
||||
Reference in New Issue
Block a user