1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-26 07:02:24 +00:00

Optimisation des téléchargements ZIP

This commit is contained in:
Yohann 2019-09-09 12:15:48 +02:00 committed by galaxyoyo
parent 190039a5e8
commit 57d2bb9bec
7 changed files with 90 additions and 59 deletions

View File

@ -18,12 +18,11 @@ if (isset($_POST["validate"])) {
if (isset($_POST["select"])) { if (isset($_POST["select"])) {
$team->selectForFinal(true); $team->selectForFinal(true);
$team->setValidationStatus(ValidationStatus::NOT_READY); $team->setValidationStatus(ValidationStatus::NOT_READY);
$tournament = Tournament::fromId($team->getTournamentId());
/** @noinspection SqlAggregates */ $sols = $tournament->getAllSolutions($team->getId());
$sols_req = $DB->prepare("SELECT `file_id`, `problem`, COUNT(`problem`) AS `version` FROM `solutions` WHERE `team` = ? AND `tournament` = ? GROUP BY `problem` ORDER BY `problem`, `uploaded_at` DESC;"); /** @var Solution $sol */
$sols_req->execute([$team->getId(), $team->getTournamentId()]); foreach ($sols as $sol) {
while (($sol_data = $sols_req->fetch()) !== false) { $old_id = $sol->getFileId();
$old_id = $sol_data["file_id"];
do do
$id = genRandomPhrase(64); $id = genRandomPhrase(64);
while (file_exists("$LOCAL_PATH/files/$id")); while (file_exists("$LOCAL_PATH/files/$id"));
@ -31,11 +30,24 @@ if (isset($_POST["select"])) {
copy("$LOCAL_PATH/files/$old_id", "$LOCAL_PATH/files/$id"); copy("$LOCAL_PATH/files/$old_id", "$LOCAL_PATH/files/$id");
$req = $DB->prepare("INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`) VALUES (?, ?, ?, ?);"); $req = $DB->prepare("INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`) VALUES (?, ?, ?, ?);");
$req->execute([$id, $team->getId(), $_SESSION["final_id"], $sol_data["problem"]]); $req->execute([$id, $team->getId(), $FINAL->getId(), $sol->getFileId()]);
} }
} }
// TODO Télécharger en zip les documents personnels if (isset($_POST["download_zip"])) {
$final = isset($_POST["final"]);
$tournament = $final ? $FINAL : Tournament::fromId($team->getTournamentId());
$file_name = getZipFile(DocumentType::PARENTAL_CONSENT, $tournament->getId(), $team->getId());
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"Documents de l'équipe " . $team->getTrigram() . ".zip\"");
header("Content-Length: " . strval(filesize($file_name)));
readfile($file_name);
exit();
}
$documents = $tournament->getAllDocuments($team->getId()); $documents = $tournament->getAllDocuments($team->getId());
$documents_final = null; $documents_final = null;

View File

@ -5,36 +5,15 @@ if (!isset($_SESSION["role"]) || $_SESSION["role"] != Role::ADMIN && $_SESSION["
if (isset($_POST["download_zip"])) { if (isset($_POST["download_zip"])) {
$id = $_POST["tournament"]; $id = $_POST["tournament"];
$tournament = Tournament::fromId($id);
$sols = $tournament->getAllSolutions(); $sols = $tournament->getAllSolutions();
$zip = new ZipArchive(); $file_name = getZipFile(DocumentType::SOLUTION, $id);
$temp = tempnam("tmp", "tfjm-");
if ($zip->open($temp, ZipArchive::CREATE) !== true) {
die("Impossible de créer le fichier zip.");
}
/** @var Solution $sol */
foreach ($sols as $sol) {
$file_id = $sol->getFileId();
$problem = $sol->getProblem();
$version = $sol->getVersion();
$team = Team::fromId($sol->getTeamId());
$team_name = $team->getName();
$team_trigram = $team->getTrigram();
$zip->addFile("$LOCAL_PATH/files/$file_id", "Problème $problem $team_trigram.pdf");
}
$zip->close();
header("Content-Type: application/zip"); header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"Solutions du tournoi de " . $tournament->getName() . ".zip\""); header("Content-Disposition: attachment; filename=\"Solutions du tournoi de " . $tournament->getName() . ".zip\"");
header("Content-Length: " . strval(filesize($temp))); header("Content-Length: " . strval(filesize($file_name)));
readfile($temp); readfile($file_name);
exit(); exit();
} }

View File

@ -4,35 +4,14 @@
if (isset($_POST["download_zip"])) { if (isset($_POST["download_zip"])) {
$id = $_POST["tournament"]; $id = $_POST["tournament"];
$tournament = Tournament::fromId($id); $tournament = Tournament::fromId($id);
$syntheses = $tournament->getAllSyntheses();
$zip = new ZipArchive(); $file_name = getZipFile(DocumentType::SYNTHESIS, $id);
$temp = tempnam("tmp", "tfjm-");
if ($zip->open($temp, ZipArchive::CREATE) !== true) {
die("Impossible de créer le fichier zip.");
}
/** @var Synthesis $synthesis */
foreach ($syntheses as $synthesis) {
$file_id = $synthesis->getFileId();
$dest = $synthesis->getDest();
$version = $synthesis->getVersion();
$team = Team::fromId($synthesis->getTeamId());
$team_name = $team->getName();
$team_trigram = $team->getTrigram();
$zip->addFile("$LOCAL_PATH/files/$file_id", "Note de synthèse $team_trigram pour " . ($dest == DestType::OPPOSANT ? "l'opposant" : "le rapporteur") . ".pdf");
}
$zip->close();
header("Content-Type: application/zip"); header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"Notes de syntèses du tournoi de " . $tournament->getName() . ".zip\""); header("Content-Disposition: attachment; filename=\"Notes de syntèses du tournoi de " . $tournament->getName() . ".zip\"");
header("Content-Length: " . filesize($temp)); header("Content-Length: " . filesize($file_name));
readfile($temp); readfile($file_name);
exit(); exit();
} }

View File

@ -134,3 +134,63 @@ function printDocuments($documents) {
echo "$name de $first_name $surname (version $version) : <a href=\"$URL_BASE/file/$file_id\">Télécharger</a><br />"; echo "$name de $first_name $surname (version $version) : <a href=\"$URL_BASE/file/$file_id\">Télécharger</a><br />";
} }
} }
function getZipFile($document_type, $tournament_id, $team_id = -1) {
global $LOCAL_PATH;
$tournament = Tournament::fromId($tournament_id);
$zip = new ZipArchive();
$file_name = tempnam("tmp", "tfjm-");
if ($zip->open($file_name, ZipArchive::CREATE) !== true) {
die("Impossible de créer le fichier zip.");
}
switch ($document_type) {
case DocumentType::SOLUTION:
$data = $tournament->getAllSolutions($team_id);
break;
case DocumentType::SYNTHESIS:
$data = $tournament->getAllSyntheses($team_id);
break;
default:
$data = $tournament->getAllDocuments($team_id);
break;
}
/** @var Document | Solution | Synthesis $file */
foreach ($data as $file) {
$file_id = $file->getFileId();
$team = Team::fromId($file->getTeamId());
switch ($document_type) {
case DocumentType::SOLUTION:
$name = "Problème " . $file->getProblem() . " " . $team->getTrigram() . ".pdf";
break;
case DocumentType::SYNTHESIS:
$name = "Note de synthèse " . $team->getTrigram() . " pour " . ($file->getDest() == DestType::OPPOSANT ? "l'opposant" : "le rapporteur") . ".pdf";
break;
default:
$user = User::fromId($file->getUserId());
switch ($file->getType()) {
case DocumentType::PARENTAL_CONSENT:
$name = "Autorisation parentale de " . $user->getFirstName() . " " . $user->getSurname() . ".pdf";
break;
case DocumentType::PHOTO_CONSENT:
$name = "Autorisation de droit à l'image de " . $user->getFirstName() . " " . $user->getSurname() . ".pdf";
break;
default:
$name = "Fiche sanitaire de " . $user->getFirstName() . " " . $user->getSurname() . ".pdf";
break;
}
break;
}
$zip->addFile("$LOCAL_PATH/files/$file_id", $name);
}
$zip->close();
return $file_name;
}

View File

@ -57,7 +57,7 @@ if (isset($tournament)) {
<label for="price">Prix par participant</label> <label for="price">Prix par participant</label>
</td> </td>
<td> <td>
<input style="width: 100%;" type="number" id="price" name="price" min="0" max="21" value="21" required /> <input style="width: 100%;" type="number" id="price" name="price" min="0" max="50" value="21" required />
</td> </td>
</tr> </tr>
<tr> <tr>

View File

@ -41,7 +41,8 @@ if ($team->isSelectedForFinal()) {
<h2>Autorisations pour la finale</h2> <h2>Autorisations pour la finale</h2>
<?php printDocuments($documents_final) ?> <?php printDocuments($documents_final) ?>
<form method="POST"> <form method="POST">
<input style="width: 100%;" type="submit" name="download_zip_final" value="Télécharger l'archive"/> <input type="hidden" name="final" value="true" />
<input style="width: 100%;" type="submit" name="download_zip" value="Télécharger l'archive"/>
</form> </form>
<?php } ?> <?php } ?>

View File

@ -152,7 +152,7 @@ else {
<label for="price">Prix par participant</label> <label for="price">Prix par participant</label>
</td> </td>
<td> <td>
<input style="width: 100%;" type="number" id="price" name="price" min="0" max="21" value="<?= $tournament->getPrice() ?>" required /> <input style="width: 100%;" type="number" id="price" name="price" min="0" max="50" value="<?= $tournament->getPrice() ?>" required />
</td> </td>
</tr> </tr>
<tr> <tr>