FileService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Services;
  3. use App\Models\File;
  4. use Exception;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Support\Str;
  7. use RecursiveIteratorIterator;
  8. use RuntimeException;
  9. use ZipArchive;
  10. class FileService
  11. {
  12. /**
  13. * @param $path
  14. * @param $file
  15. * @return File
  16. */
  17. public function saveUploadedFile($path, $file): File
  18. {
  19. $originalName = $this->sanitizeOriginalName((string)$file->getClientOriginalName());
  20. $storedFilename = $this->buildStoredFilename($originalName);
  21. $relativePath = $this->buildUniquePath($path, $storedFilename);
  22. try {
  23. Storage::disk('public')->put($relativePath, $file->getContent());
  24. } catch (\Throwable $e) {
  25. throw new RuntimeException('Не удалось сохранить файл. Проверьте имя файла и повторите попытку.', 0, $e);
  26. }
  27. return File::query()->create([
  28. 'link' => url('/storage/' . $relativePath),
  29. 'path' => $relativePath,
  30. 'user_id' => auth()->user()->id,
  31. 'original_name' => $originalName,
  32. 'mime_type' => $file->getClientMimeType(),
  33. ]);
  34. }
  35. private function sanitizeOriginalName(string $originalName): string
  36. {
  37. $name = basename(str_replace('\\', '/', $originalName));
  38. if (class_exists('\Normalizer')) {
  39. $normalized = \Normalizer::normalize($name, \Normalizer::FORM_C);
  40. if (is_string($normalized)) {
  41. $name = $normalized;
  42. }
  43. }
  44. // Remove control/format unicode symbols (e.g. zero-width, LRM/RLM), then normalize spaces.
  45. $name = preg_replace('/\p{C}+/u', '', $name) ?? '';
  46. $name = preg_replace('/\p{Z}+/u', ' ', $name) ?? '';
  47. $name = trim($name);
  48. return $name === '' ? 'file' : $name;
  49. }
  50. private function buildStoredFilename(string $originalName): string
  51. {
  52. $baseName = pathinfo($originalName, PATHINFO_FILENAME);
  53. $extension = pathinfo($originalName, PATHINFO_EXTENSION);
  54. $baseName = preg_replace('/[<>:|?*\/\\\\]+/u', '_', $baseName) ?? '';
  55. $baseName = preg_replace('/\s+/u', ' ', $baseName) ?? '';
  56. $baseName = trim($baseName, " .\t\n\r\0\x0B");
  57. if ($baseName === '' || $baseName === '.' || $baseName === '..') {
  58. $baseName = 'file';
  59. }
  60. if (function_exists('mb_substr')) {
  61. $baseName = mb_substr($baseName, 0, 150);
  62. } else {
  63. $baseName = substr($baseName, 0, 150);
  64. }
  65. $extension = preg_replace('/[^A-Za-z0-9]+/', '', $extension) ?? '';
  66. return $extension === '' ? $baseName : $baseName . '.' . $extension;
  67. }
  68. private function buildUniquePath(string $directory, string $filename): string
  69. {
  70. $directory = trim($directory, '/');
  71. $basePath = $directory === '' ? $filename : $directory . '/' . $filename;
  72. if (!Storage::disk('public')->exists($basePath)) {
  73. return $basePath;
  74. }
  75. $baseName = pathinfo($filename, PATHINFO_FILENAME);
  76. $extension = pathinfo($filename, PATHINFO_EXTENSION);
  77. for ($index = 1; $index < 10000; $index++) {
  78. $candidateName = $baseName . ' (' . $index . ')';
  79. if ($extension !== '') {
  80. $candidateName .= '.' . $extension;
  81. }
  82. $candidatePath = $directory === '' ? $candidateName : $directory . '/' . $candidateName;
  83. if (!Storage::disk('public')->exists($candidatePath)) {
  84. return $candidatePath;
  85. }
  86. }
  87. return $basePath;
  88. }
  89. /**
  90. * @param string $path
  91. * @param string $archiveName
  92. * @param int|null $userId
  93. * @return File
  94. * @throws Exception
  95. */
  96. public function createZipArchive(string $path, string $archiveName, int $userId = null): File
  97. {
  98. if (!($tempFile = tempnam(sys_get_temp_dir(), Str::random()))) {
  99. throw new Exception('Cant create temporary file!');
  100. }
  101. $zip = new ZipArchive();
  102. $fullPath = storage_path('app/public/' . $path);
  103. $zip->open($tempFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  104. $files = new RecursiveIteratorIterator(
  105. new \RecursiveDirectoryIterator($fullPath),
  106. RecursiveIteratorIterator::LEAVES_ONLY
  107. );
  108. foreach ($files as $file) {
  109. $filePath = $file->getRealPath();
  110. $relativePath = substr($filePath, strlen($fullPath) + 1);
  111. if(!$file->isDir()) {
  112. $zip->addFile($filePath, $relativePath);
  113. } else {
  114. $zip->addEmptyDir($relativePath);
  115. }
  116. }
  117. $zip->close();
  118. $fileModel = File::query()->updateOrCreate([
  119. 'link' => url('/storage/') . '/' . Str::replace('/tmp', '', $path) . '/' .$archiveName,
  120. 'path' => $path . '/' .$archiveName,
  121. 'user_id' => $userId ?? auth()->user()->id,
  122. 'original_name' => $archiveName,
  123. 'mime_type' => 'application/zip',
  124. ]);
  125. $contents = file_get_contents($tempFile);
  126. Storage::disk('public')->put($path . '/../' .$archiveName, $contents);
  127. unlink($tempFile);
  128. return $fileModel;
  129. }
  130. }