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.

95 line
2.9KB

  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. if ($path) {
  30. $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
  31. if (strpos($path, $fileManagerFolder) === false) {
  32. $path = $fileManagerFolder . '/' . $path;
  33. }
  34. if (file_exists($path)) {
  35. return $this->liipCacheHelper->getBrowserPath($path, $thumb);
  36. }
  37. }
  38. return $this->liipCacheHelper->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
  39. }
  40. public function removeDir($dir)
  41. {
  42. $files = array_diff(scandir($dir), array('.', '..'));
  43. foreach ($files as $file) {
  44. (is_dir("$dir/$file")) ? $this->removeDir("$dir/$file") : unlink("$dir/$file");
  45. }
  46. return rmdir($dir);
  47. }
  48. function folderToZip($folder, &$zipFile, $subfolder = null)
  49. {
  50. if ($zipFile == null) {
  51. // no resource given, exit
  52. return false;
  53. }
  54. // we check if $folder has a slash at its end, if not, we append one
  55. $tabFolder = str_split($folder);
  56. $tabSubFolder = str_split($subfolder);
  57. $folder .= end($tabFolder) == "/" ? "" : "/";
  58. $subfolder .= end($tabSubFolder) == "/" ? "" : "/";
  59. // we start by going through all files in $folder
  60. $handle = opendir($folder);
  61. while ($f = readdir($handle)) {
  62. if ($f != "." && $f != "..") {
  63. if (is_file($folder . $f)) {
  64. // if we find a file, store it
  65. // if we have a subfolder, store it there
  66. if ($subfolder != null)
  67. $zipFile->addFile($folder . $f, $subfolder . $f);
  68. else
  69. $zipFile->addFile($folder . $f);
  70. } elseif (is_dir($folder . $f)) {
  71. // if we find a folder, create a folder in the zip
  72. $zipFile->addEmptyDir($f);
  73. // and call the function again
  74. folderToZip($folder . $f, $zipFile, $f);
  75. }
  76. }
  77. }
  78. }
  79. }