mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 16:19:19 +02:00
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:
@@ -197,14 +197,17 @@ class ThesisCreateController
|
||||
$this->handleCoverUpload($thesisId, $files['couverture'] ?? null, $folderPath, $filePrefix);
|
||||
$this->handleNoteIntentionUpload($thesisId, $files['note_intention'] ?? null, $folderPath, $filePrefix);
|
||||
|
||||
// TFE files come from session temp (incremental upload via HTMX)
|
||||
$sessionUploads = $_SESSION['tfe_uploads'] ?? [];
|
||||
$nextNum = $this->handleTfeFilesFromSession($thesisId, $sessionUploads, $folderPath, $filePrefix, 1);
|
||||
// Clear session uploads after successful commit
|
||||
$this->cleanupSessionUploads();
|
||||
// TFE files from client-side JS queue (FormData)
|
||||
$queueFiles = $files['queue_file'] ?? [];
|
||||
$qTfe = $this->extractFilesSubArray($queueFiles, 'tfe');
|
||||
$qVideo = $this->extractFilesSubArray($queueFiles, 'video');
|
||||
$qAudio = $this->extractFilesSubArray($queueFiles, 'audio');
|
||||
$qAnnexe = $this->extractFilesSubArray($queueFiles, 'annexe');
|
||||
|
||||
$this->handleAnnexeFiles($thesisId, $files['annexes'] ?? null, $folderPath, $filePrefix, $post);
|
||||
// PeerTube file rows don't go on disk, but the uploads themselves are processed separately
|
||||
$nextNum = $this->handleTfeQueueFiles($thesisId, $qTfe, $folderPath, $filePrefix, 1);
|
||||
$nextNum = $this->handleTfeQueueFiles($thesisId, $qVideo, $folderPath, $filePrefix, $nextNum);
|
||||
$nextNum = $this->handleTfeQueueFiles($thesisId, $qAudio, $folderPath, $filePrefix, $nextNum);
|
||||
$this->handleAnnexeQueueFiles($thesisId, $qAnnexe, $folderPath, $filePrefix);
|
||||
|
||||
// ── 5b. PeerTube video / audio uploads ────────────────────────────────
|
||||
$this->handlePeerTubeUpload($thesisId, $data['titre'], $files, 'peertube_video');
|
||||
@@ -518,10 +521,14 @@ class ThesisCreateController
|
||||
$exemplaireErg = !empty($post['exemplaire_erg']);
|
||||
$cc2r = !empty($post['cc2r']);
|
||||
|
||||
// Annexes validation: if has_annexes is checked, at least one annexe file must be provided
|
||||
// Annexes validation: if has_annexes is checked, queue_file[annexe] must have at least one file
|
||||
$hasAnnexes = !empty($post['has_annexes']);
|
||||
if (!$adminMode && $hasAnnexes && empty($_FILES['annexes']['name'][0])) {
|
||||
throw new Exception('Veuillez fournir au moins un fichier d\'annexe.');
|
||||
if (!$adminMode && $hasAnnexes) {
|
||||
$queueAnnexes = $this->extractFilesSubArray($files['queue_file'] ?? [], 'annexe');
|
||||
$hasAnnexeFiles = is_array($queueAnnexes['name'] ?? null) && count(array_filter($queueAnnexes['name'])) > 0;
|
||||
if (!$hasAnnexeFiles) {
|
||||
throw new Exception('Veuillez fournir au moins un fichier d\'annexe.');
|
||||
}
|
||||
}
|
||||
|
||||
return compact(
|
||||
|
||||
@@ -411,22 +411,28 @@ class ThesisEditController
|
||||
}
|
||||
}
|
||||
|
||||
// ── New TFE files upload (from session via HTMX incremental upload) ──
|
||||
$sessionUploads = $_SESSION['tfe_uploads'] ?? [];
|
||||
if (!empty($sessionUploads)) {
|
||||
// Count existing TFE files to determine starting number
|
||||
$tfeCount = 0;
|
||||
foreach ($existingFiles as $f) {
|
||||
if (!in_array($f['file_type'] ?? '', ['cover', 'note_intention', 'website', 'annex', 'caption'], true)
|
||||
&& !str_starts_with($f['file_path'] ?? '', 'http')) {
|
||||
$tfeCount++;
|
||||
}
|
||||
// ── New TFE/video/audio files upload (from client-side JS queue) ──
|
||||
$queueFiles = $files['queue_file'] ?? [];
|
||||
$qTfe = $this->extractFilesSubArray($queueFiles, 'tfe');
|
||||
$qVideo = $this->extractFilesSubArray($queueFiles, 'video');
|
||||
$qAudio = $this->extractFilesSubArray($queueFiles, 'audio');
|
||||
$qAnnexe = $this->extractFilesSubArray($queueFiles, 'annexe');
|
||||
|
||||
$tfeCount = 0;
|
||||
foreach ($existingFiles as $f) {
|
||||
if (!in_array($f['file_type'] ?? '', ['cover', 'note_intention', 'website', 'annex', 'caption'], true)
|
||||
&& !str_starts_with($f['file_path'] ?? '', 'http')) {
|
||||
$tfeCount++;
|
||||
}
|
||||
$this->handleTfeFilesFromSession($thesisId, $sessionUploads, $folderPath, $filePrefix, $tfeCount + 1);
|
||||
$this->cleanupSessionUploads();
|
||||
}
|
||||
|
||||
// ── New annexe files upload ────────────────────────────────────────────
|
||||
$startNum = $tfeCount + 1;
|
||||
$startNum = $this->handleTfeQueueFiles($thesisId, $qTfe, $folderPath, $filePrefix, $startNum);
|
||||
$startNum = $this->handleTfeQueueFiles($thesisId, $qVideo, $folderPath, $filePrefix, $startNum);
|
||||
$this->handleTfeQueueFiles($thesisId, $qAudio, $folderPath, $filePrefix, $startNum);
|
||||
$this->handleAnnexeQueueFiles($thesisId, $qAnnexe, $folderPath, $filePrefix);
|
||||
|
||||
// Legacy annexe files (direct upload, non-queue path — kept for backwards compat)
|
||||
if (isset($files['annexes']) && is_array($files['annexes']['name'] ?? null)) {
|
||||
$this->handleAnnexeFiles($thesisId, $files['annexes'], $folderPath, $filePrefix, $post);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user