Replace HTMX+PHP file upload queues with client-side JS

Drops the session-backed HTMX incremental upload system in favour of a
single JS module that manages `File` objects client-side and injects
them into `FormData` on submit.

Key changes:

* `file-upload-queue.js`: client-side queues with validation, reorder
  (SortableJS), removal, dirty-state tracking, and fetch-based submit
  with manual redirect handling
* `fichiers-fragment.php`: empty queue containers for JS-managed queues;
  HTMX format switching still works with queue rehydration after swap;
  annexe uploads now support multiple files
* Form UI cleanup: moved existing files and cover preview into the
  `Fichiers` fieldset (edit mode); removed redundant queue labels while
  keeping labels for single-file inputs (`couverture`,
  `note d'intention`); added delete buttons for existing files
* `ThesisFileHandler.php`: added
  `handleTfeQueueFiles()`/`handleAnnexeQueueFiles()` reading from
  `$_FILES['queue_file']`; introduced `extractFilesSubArray()` for
  nested upload arrays; removed session-based queue handling
* `ThesisCreateController.php` &
  `ThesisEditController.php`: switched to extracted
  `['queue_file']` uploads
* `beforeunload-guard.js`: now also watches
  `window.__xamxamDirty`
* Deleted obsolete PHP upload/remove/reorder queue endpoints for
  `partage` and `admin`
* Cleaned up route dispatch in `partage/index.php`
* Misc form and styling updates in templates/CSS
* Added `docs/cms-migration-plan.html`
This commit is contained in:
Pontoporeia
2026-05-10 17:16:25 +02:00
parent 98ed83fac2
commit 13d26ded66
20 changed files with 2063 additions and 753 deletions

View File

@@ -304,20 +304,40 @@ trait ThesisFileHandler
}
/**
* Process TFE file uploads from session-stored temp paths.
* Extract a flat $_FILES-style sub-array from PHP's nested upload structure.
*
* Used when files are uploaded incrementally via HTMX fragments (upload-tfe-file.php)
* rather than submitted in a single multipart form.
* PHP normalises FormData names like "queue_file[tfe][]" into:
* $_FILES['queue_file']['name']['tfe'] = [file1, file2, ...]
* $_FILES['queue_file']['tmp_name']['tfe'] = [/tmp/..., /tmp/...]
* This helper extracts ['tfe'] → ['name' => [...], 'tmp_name' => [...], ...]
*/
protected function extractFilesSubArray(array $parent, string $key): ?array
{
if (!isset($parent['name'][$key]) || !is_array($parent['name'][$key])) {
return null;
}
$result = [];
foreach (['name', 'tmp_name', 'error', 'size', 'type'] as $field) {
$result[$field] = $parent[$field][$key] ?? [];
}
return $result;
}
/**
* Process TFE file uploads from client-side JS queue (FormData).
*
* Files arrive via $_FILES['queue_file'] with PHP-nested key extraction.
* They are written in the order the user specified (preserved in FormData order).
*
* @param int $thesisId
* @param array $uploads Array of ['orig_name', 'size', 'mime', 'tmp_path']
* @param array|null $uploads Flat $_FILES-style array with 'name', 'tmp_name', etc.
* @param string $folderPath
* @param string $filePrefix
* @param int $startNum
*/
protected function handleTfeFilesFromSession(int $thesisId, array $uploads, string $folderPath, string $filePrefix, int $startNum = 1): int
protected function handleTfeQueueFiles(int $thesisId, ?array $uploads, string $folderPath, string $filePrefix, int $startNum = 1): int
{
if (empty($uploads)) {
if (!$uploads || !is_array($uploads['name'] ?? null)) {
return $startNum;
}
@@ -326,77 +346,146 @@ trait ThesisFileHandler
mkdir($dir, 0755, true);
}
$files = [];
foreach ($uploads as $f) {
$mimeType = $f['mime'];
$absPath = STORAGE_ROOT . '/' . $f['tmp_path'];
// Build entries, validate, and classify
$files = [];
$vttQueue = [];
$count = count($uploads['name']);
if (!file_exists($absPath)) {
error_log("ThesisFileHandler: session temp file missing {$f['tmp_path']}, skipping");
for ($i = 0; $i < $count; $i++) {
if (($uploads['error'][$i] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
continue;
}
$ext = strtolower(pathinfo($f['orig_name'], PATHINFO_EXTENSION));
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->file($uploads['tmp_name'][$i]);
$ext = strtolower(pathinfo($uploads['name'][$i], PATHINFO_EXTENSION));
if ($mimeType === 'text/plain' && $ext === 'vtt') {
$mimeType = 'text/vtt';
}
if ($mimeType === 'application/octet-stream' && !in_array($ext, self::ALLOWED_EXTENSIONS, true)) {
error_log("ThesisFileHandler: queue file extension not allowed {$uploads['name'][$i]} ($ext), skipping");
continue;
}
if (!in_array($mimeType, self::ALLOWED_MIME_TYPES, true)
&& !in_array($ext, self::ALLOWED_EXTENSIONS, true)) {
error_log("ThesisFileHandler: invalid queue file type {$uploads['name'][$i]} ($mimeType / $ext), skipping");
continue;
}
$isPdf = ($mimeType === 'application/pdf' || $ext === 'pdf');
$sizeLimit = $isPdf ? self::MAX_PDF_SIZE : self::MAX_FILE_SIZE;
if ($uploads['size'][$i] > $sizeLimit) {
error_log("ThesisFileHandler: queue file too large {$uploads['name'][$i]} (" . round($uploads['size'][$i] / 1024 / 1024) . ' MB), skipping');
continue;
}
$entry = [
'mimeType' => $mimeType,
'ext' => $ext,
'size' => $uploads['size'][$i],
'tmpName' => $uploads['tmp_name'][$i],
'origName' => $uploads['name'][$i],
'label' => '',
'sortOrder' => null,
'fileType' => $this->detectFileType($mimeType, $ext),
];
// VTTs are collected and paired with their preceding video
if ($entry['fileType'] === 'caption') {
$vttQueue[] = $entry;
} else {
$files[] = $entry;
}
}
// Files are written in the order they arrived (user-specified via JS queue order).
// VTTs are inserted immediately after the video they follow.
$num = $startNum;
$videosSeen = 0;
foreach ($files as $f) {
$this->writeTfeFile($f, $thesisId, $dir, $folderPath, $filePrefix, $num);
$num++;
if ($f['fileType'] === 'video' && isset($vttQueue[$videosSeen])) {
$this->writeTfeFile($vttQueue[$videosSeen], $thesisId, $dir, $folderPath, $filePrefix, $num);
$num++;
$videosSeen++;
} elseif ($f['fileType'] === 'video') {
$videosSeen++;
}
}
// Orphaned VTTs (no preceding video in this batch)
for ($i = $videosSeen; $i < count($vttQueue); $i++) {
$this->writeTfeFile($vttQueue[$i], $thesisId, $dir, $folderPath, $filePrefix, $num);
$num++;
}
return $num;
}
/**
* Process annexe file uploads from client-side JS queue (FormData).
*
* Files arrive via $_FILES['queue_file']['annexe'].
*
* @param int $thesisId
* @param array|null $uploads $_FILES['queue_file']['annexe']-style array
* @param string $folderPath
* @param string $filePrefix
*/
protected function handleAnnexeQueueFiles(int $thesisId, ?array $uploads, string $folderPath, string $filePrefix): void
{
if (!$uploads || !is_array($uploads['name'] ?? null)) {
return;
}
$dir = STORAGE_ROOT . '/' . $folderPath;
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
$num = 1;
$count = count($uploads['name']);
for ($i = 0; $i < $count; $i++) {
if (($uploads['error'][$i] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
continue;
}
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->file($uploads['tmp_name'][$i]);
$ext = strtolower(pathinfo($uploads['name'][$i], PATHINFO_EXTENSION));
if ($mimeType === 'text/plain' && $ext === 'vtt') {
$mimeType = 'text/vtt';
}
$files[] = [
'mimeType' => $mimeType,
'ext' => $ext,
'size' => $f['size'],
'origName' => $f['orig_name'],
'label' => '',
'sortOrder' => null,
'hierarchy' => $this->tfeHierarchyRank($mimeType, $ext),
'fileType' => $this->detectFileType($mimeType, $ext),
// Pass the absolute path so writeTfeFile knows where to copy from
'srcPath' => $absPath,
];
}
$padded = sprintf('%02d', $num);
$targetName = $filePrefix . '_ANNEXE_' . $padded . '.' . $ext;
$targetPath = $dir . $targetName;
// Sort by hierarchy rank
usort($files, fn($a, $b) => $a['hierarchy'] - $b['hierarchy']);
$videoCount = 0;
$vttQueue = [];
foreach ($files as $f) {
if ($f['fileType'] === 'video') {
$videoCount++;
}
}
$num = $startNum;
foreach ($files as $f) {
if ($f['fileType'] === 'caption') {
$vttQueue[] = $f;
if (!move_uploaded_file($uploads['tmp_name'][$i], $targetPath)) {
error_log("ThesisFileHandler: failed to move queue annexe {$uploads['name'][$i]}");
continue;
}
if ($f['fileType'] === 'video') {
$this->writeTfeFileFromSrc($f, $thesisId, $dir, $folderPath, $filePrefix, $num);
$num++;
chmod($targetPath, 0644);
$relPath = $folderPath . $targetName;
if (!empty($vttQueue)) {
$vtt = array_shift($vttQueue);
$this->writeTfeFileFromSrc($vtt, $thesisId, $dir, $folderPath, $filePrefix, $num);
$num++;
}
} else {
$this->writeTfeFileFromSrc($f, $thesisId, $dir, $folderPath, $filePrefix, $num);
$num++;
}
}
foreach ($vttQueue as $vtt) {
$this->writeTfeFileFromSrc($vtt, $thesisId, $dir, $folderPath, $filePrefix, $num);
$this->db->insertThesisFile(
$thesisId, 'annex',
$relPath,
basename($uploads['name'][$i]),
$uploads['size'][$i],
$mimeType,
null,
null
);
error_log("ThesisFileHandler: annexe (queue) moved → $targetName");
$num++;
}
return $num;
}
/**
@@ -517,60 +606,9 @@ trait ThesisFileHandler
error_log("ThesisFileHandler: TFE uploaded → $targetName ({$f['fileType']})");
}
/**
* Write a single TFE file from a source path (session temp) to the thesis folder.
* Used by handleTfeFilesFromSession instead of move_uploaded_file.
*/
protected function writeTfeFileFromSrc(array $f, int $thesisId, string $dir, string $folderPath, string $filePrefix, int $num): void
{
$padded = sprintf('%02d', $num);
$targetName = $filePrefix . '_TFE_' . $padded . '.' . $f['ext'];
$targetPath = $dir . $targetName;
if (!rename($f['srcPath'], $targetPath)) {
// Fallback: copy + unlink
if (!copy($f['srcPath'], $targetPath)) {
error_log("ThesisFileHandler: failed to move session TFE {$f['origName']}");
return;
}
unlink($f['srcPath']);
}
chmod($targetPath, 0644);
$relPath = $folderPath . $targetName;
$this->db->insertThesisFile(
$thesisId, $f['fileType'],
$relPath,
basename($f['origName']),
$f['size'],
$f['mimeType'],
$f['label'] !== '' ? $f['label'] : null,
$f['sortOrder']
);
error_log("ThesisFileHandler: TFE (session) moved → $targetName ({$f['fileType']})");
}
/**
* Clean up session upload temp files and clear the session entry.
* Call after successful commit of TFE files.
*/
protected function cleanupSessionUploads(): void
{
$sessionId = session_id();
$tempDir = STORAGE_ROOT . '/uploads/' . $sessionId;
// Remove any remaining files in the temp dir
if (is_dir($tempDir)) {
$files = glob($tempDir . '/*');
foreach ($files as $file) {
@unlink($file);
}
@rmdir($tempDir);
}
unset($_SESSION['tfe_uploads']);
}
/**
* Assign a hierarchy rank for sorting TFE files.