Export: add LINK.txt with PeerTube watch URLs to file ZIP export

This commit is contained in:
Pontoporeia
2026-05-20 13:21:41 +02:00
parent a251aeb500
commit 6246174fc5
3 changed files with 84 additions and 3 deletions

View File

@@ -53,6 +53,16 @@ class ExportController
return $this->db->getAllThesisFilesForExport($thesisIds);
}
/**
* Get the configured PeerTube instance URL, or empty string if not configured.
*/
private function getPeerTubeInstanceUrl(): string
{
require_once APP_ROOT . '/src/PeerTubeService.php';
$settings = PeerTubeService::getSettings($this->db);
return $settings['instance_url'];
}
/**
* Build a JSON manifest describing every thesis and its files.
*
@@ -153,17 +163,72 @@ class ExportController
// Add every thesis file under files/
$addedCount = 0;
$skippedCount = 0;
$peertubeLinks = []; // thesisId => [{uuid, watchUrl, fileType, label}]
$ptInstanceUrl = '';
foreach ($files as $f) {
$fullPath = $storageRoot . '/' . $f['file_path'];
$filePath = $f['file_path'];
// Collect PeerTube links per-thesis
if (str_starts_with($filePath, 'peertube_ids:')) {
$uuid = substr($filePath, strlen('peertube_ids:'));
if ($ptInstanceUrl === '') {
$ptInstanceUrl = $this->getPeerTubeInstanceUrl();
}
$tid = (int) $f['thesis_id'];
$peertubeLinks[$tid] = $peertubeLinks[$tid] ?? ['dirname' => '', 'links' => []];
$peertubeLinks[$tid]['links'][] = [
'uuid' => $uuid,
'url' => $ptInstanceUrl !== '' ? rtrim($ptInstanceUrl, '/') . '/videos/watch/' . $uuid : '',
'type' => $f['file_type'],
'label' => $f['display_label'] ?? '',
'name' => $f['file_name'],
];
$skippedCount++; // not a real file on disk
continue;
}
// Track the directory for each thesis (for LINK.txt placement)
$tid = (int) $f['thesis_id'];
if (isset($peertubeLinks[$tid]) && $peertubeLinks[$tid]['dirname'] === '') {
$peertubeLinks[$tid]['dirname'] = dirname($filePath);
}
$fullPath = $storageRoot . '/' . $filePath;
if (!is_file($fullPath) || !is_readable($fullPath)) {
$skippedCount++;
continue;
}
$zipPath = $baseDir . '/' . $f['file_path'];
$zipPath = $baseDir . '/' . $filePath;
$zip->addFile($fullPath, $zipPath);
$addedCount++;
}
// Add LINK.txt in each thesis directory that has PeerTube files
foreach ($peertubeLinks as $info) {
if ($info['dirname'] === '') {
continue;
}
$txt = "Liens PeerTube pour ce TFE\n"
. str_repeat('=', 40) . "\n\n";
foreach ($info['links'] as $link) {
$label = $link['label'] !== '' ? $link['label'] : $link['name'];
$txt .= ($label !== '' ? $label . "\n" : '');
$txt .= ($link['url'] !== '' ? $link['url'] . "\n" : '(instance PeerTube non configurée)' . "\n");
$txt .= "\n";
}
$txtPath = $baseDir . '/' . $info['dirname'] . '/LINK.txt';
$zip->addFromString($txtPath, $txt);
}
// Augment manifest with PeerTube link info per thesis
foreach ($manifest['theses'] as &$entry) {
$tid = $entry['id'];
if (isset($peertubeLinks[$tid])) {
$entry['peertube_links'] = $peertubeLinks[$tid]['links'];
}
}
unset($entry);
$zip->addFromString(
'manifest.json',
json_encode(array_merge($manifest, [