You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.4KB

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