462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
}
if (!is_uploaded_file($file['tmp_name']))
{
throw new \RuntimeException('Le fichier n\'a pas été envoyé de manière conventionnelle.');
}
$max_blob_size = self::getMaxBlobSize();
// Vérifier que le fichier peut rentrer en base de données (dans PHP < 7.2 on n'utilise pas openBlob)
if (null !== $max_blob_size && $file['size'] > $max_blob_size) {
unlink($file['tmp_name']);
throw new UserException('Taille du fichier supérieure au maximum autorisé en base de données');
}
$name = preg_replace('/\s+/', '_', $file['name']);
$name = preg_replace('/[^\d\w._-]/ui', '', $name);
return self::storeFile($name, $file['tmp_name']);
}
/**
* Returns the maximum value size that can be handled by a bindValue
* @return null|integer
*/
static public function getMaxBlobSize()
{
$memory_limit = Utils::return_bytes(ini_get('memory_limit'));
if (!$memory_limit) {
return null;
}
return round(($memory_limit - memory_get_usage()) * 0.9);
}
/**
* Upload de fichier à partir d'une chaîne en base64
* @param string $name
* @param string $content
* @return Fichiers
*/
static public function storeFromBase64($name, $content)
|
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
-
-
-
-
+
+
+
+
+
|
$is_image = preg_match('/^image\/(?:png|jpe?g|gif)$/', $type);
$db = DB::getInstance();
$db->begin();
// Il peut arriver que l'on renvoie ici un fichier déjà stocké, auquel cas, ne pas le re-stocker
if (!($id_contenu = $db->firstColumn('SELECT id FROM fichiers_contenu WHERE hash = ?;', $hash)))
{
$db->insert('fichiers_contenu', [
'hash' => $hash,
if (!($id_contenu = $db->firstColumn('SELECT id FROM fichiers_contenu WHERE hash = ?;', $hash))) {
$db->preparedQuery('INSERT INTO fichiers_contenu (hash, taille, contenu) VALUES (?, ?, zeroblob(?));',
[$hash, (int)$size, (int)$size]);
$id_contenu = $db->lastInsertRowID();
// Écrire le contenu
$blob = $db->openBlob('fichiers_contenu', 'contenu', $id_contenu, 'main', SQLITE3_OPEN_READWRITE);
'taille' => (int)$size,
'contenu' => [\SQLITE3_BLOB, $content ?: file_get_contents($path)],
]);
if (null !== $content) {
fwrite($blob, $content);
}
// FIXME: utiliser Sqlite3::openBlob pour écrire quand dispo dans PHP
// cf. https://github.com/php/php-src/pull/2528
$id_contenu = $db->lastInsertRowID();
else{
fwrite($blob, file_get_contents($path));
}
fclose($blob);
}
$db->insert('fichiers', [
'id_contenu' => (int)$id_contenu,
'nom' => $name,
'type' => $type,
'image' => (int)$is_image,
|