Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
A tester #83
Browse files Browse the repository at this point in the history
  • Loading branch information
pprev94 committed Jul 12, 2022
1 parent 8070797 commit 6b9c2c1
Show file tree
Hide file tree
Showing 2 changed files with 1,708 additions and 1,667 deletions.
49 changes: 48 additions & 1 deletion src/Listener/UploadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Oneup\UploaderBundle\Event\PostUploadEvent;
use Symfony\Component\Filesystem\Filesystem;
use ZipArchive;

/**
* @author pprevautel
Expand All @@ -12,6 +13,9 @@ class UploadListener
{
public function onUpload(PostUploadEvent $event)
{
$uploadedFile = $event->getRequest()->files->get('upload')['file'];
$originalName = $uploadedFile-> getClientOriginalName();

$response = $event->getResponse();
$file = $event->getFile();

Expand All @@ -38,7 +42,12 @@ public function onUpload(PostUploadEvent $event)
// Verification des srid (le srid doit être unique pour toutes couches gpkg et zip avec gpkg)
$response['status'] = "OK";
$response['srid'] = (count($unicity) == 1) ? $unicity[0] : "";
$response['filename'] = $file->getFilename();

// Si c'est un fichier csv, on le zippe pour conserver le nom
if ('csv' == $extension) {
$response['filename'] = $this->zip($file, $originalName);
} else $response['filename'] = $file->getFilename();

} catch (\Exception $e) {
$response['status'] = "ERROR";
$response['error'] = $e->getMessage();
Expand Down Expand Up @@ -112,6 +121,44 @@ private function checkArchive($file)
}
}

/**
* Cree un fichier archive qui va contenir ce fichier $file (csv uniquement)
* Le fichier zip est de la forme <basename>.zip
*
* @param File $file
* @param string $originalName
* @return void
*/
private function zip($file, $originalName) {
$fs = new Filesystem();

$folder = realpath($file->getPath());

// On renomme le fichier
$filepath = realpath($file->getPathName());
$outfile = join(DIRECTORY_SEPARATOR, [$folder, "$originalName"]);
$fs->rename($filepath, $outfile, true);

// Creation de l'archive
$extension = $file->getExtension();
$uuid = $file->getBasename(".$extension");
$zipFile = join(DIRECTORY_SEPARATOR, [$folder, "$uuid.zip"]);

$zip = new \ZipArchive();
$res = $zip->open($zipFile, ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFile($outfile, "$originalName");
$zip->close();
}

$fs->remove($outfile);
if ($res === TRUE) {
return "$uuid.zip";
}

throw new \Exception("La création de l'archive a échoué.");
}

/**
*
*/
Expand Down
Loading

0 comments on commit 6b9c2c1

Please sign in to comment.