Overview
Comment:Utiliser openBlob pour écrire les fichiers
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 1fb74fb690ab70e30bfa24c30a3782f727f86f2b
User & Date: bohwaz on 2020-03-01 18:44:53
Other Links: branch diff | manifest | tags
Context
2020-03-01
18:45
Exiger d'avoir PHP 7.2 avant toute chose check-in: d834be67ff user: bohwaz tags: dev
18:44
Utiliser openBlob pour écrire les fichiers check-in: 1fb74fb690 user: bohwaz tags: dev
2020-02-29
23:18
Nouvelle version seulement compatible avec PHP 7.2+ check-in: 2f79c24d5f user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Fichiers.php from [630c28763b] to [10416ad902].

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,