Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

98 lines
3.0KB

  1. <?php
  2. namespace Lc\SovBundle\Component;
  3. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. class FileComponent
  6. {
  7. protected ParameterBagInterface $parameterBag;
  8. protected CacheManager $liipCacheHelper;
  9. public function __construct(ParameterBagInterface $parameterBag, CacheManager $liipCacheHelper)
  10. {
  11. $this->parameterBag = $parameterBag;
  12. $this->liipCacheHelper = $liipCacheHelper;
  13. }
  14. /**
  15. * Retourne le chemin vers le dossier d'uploads de responsiveFilemanager
  16. *
  17. * @return string
  18. */
  19. public function getFileManagerFolder()
  20. {
  21. return $this->parameterBag->get('app.path.images');
  22. }
  23. // lcLiip
  24. public function liip($path, $thumb = 'tile', $default = 'default.jpg')
  25. {
  26. if (substr($path, 0, 1) === '/') {
  27. $path = substr($path, 1);
  28. }
  29. // gestion des accents et des espaces
  30. $path = urldecode($path);
  31. if ($path) {
  32. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  33. if (strpos($path, $fileManagerFolder) === false) {
  34. $path = $fileManagerFolder . '/' . $path;
  35. }
  36. if (file_exists($path)) {
  37. return $this->liipCacheHelper->getBrowserPath($path, $thumb);
  38. }
  39. }
  40. return $this->liipCacheHelper->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  41. }
  42. public function removeDir($dir)
  43. {
  44. $files = array_diff(scandir($dir), array('.', '..'));
  45. foreach ($files as $file) {
  46. (is_dir("$dir/$file")) ? $this->removeDir("$dir/$file") : unlink("$dir/$file");
  47. }
  48. return rmdir($dir);
  49. }
  50. function folderToZip($folder, &$zipFile, $subfolder = null)
  51. {
  52. if ($zipFile == null) {
  53. // no resource given, exit
  54. return false;
  55. }
  56. // we check if $folder has a slash at its end, if not, we append one
  57. $tabFolder = str_split($folder);
  58. $tabSubFolder = str_split($subfolder);
  59. $folder .= end($tabFolder) == "/" ? "" : "/";
  60. $subfolder .= end($tabSubFolder) == "/" ? "" : "/";
  61. // we start by going through all files in $folder
  62. $handle = opendir($folder);
  63. while ($f = readdir($handle)) {
  64. if ($f != "." && $f != "..") {
  65. if (is_file($folder . $f)) {
  66. // if we find a file, store it
  67. // if we have a subfolder, store it there
  68. if ($subfolder != null)
  69. $zipFile->addFile($folder . $f, $subfolder . $f);
  70. else
  71. $zipFile->addFile($folder . $f);
  72. } elseif (is_dir($folder . $f)) {
  73. // if we find a folder, create a folder in the zip
  74. $zipFile->addEmptyDir($f);
  75. // and call the function again
  76. folderToZip($folder . $f, $zipFile, $f);
  77. }
  78. }
  79. }
  80. }
  81. }