|
3 | 3 | namespace SolutionForest\InspireCms\Exports\Exporters; |
4 | 4 |
|
5 | 5 | use Illuminate\Database\Eloquent\Model; |
| 6 | +use Illuminate\Support\Facades\Storage; |
6 | 7 | use SolutionForest\InspireCms\Exports\ExportResult; |
| 8 | +use SolutionForest\InspireCms\Helpers\ExportDataHelper; |
7 | 9 | use SolutionForest\InspireCms\Helpers\FileHelper; |
8 | 10 | use SolutionForest\InspireCms\Models\Contracts\Export; |
9 | 11 |
|
@@ -33,49 +35,85 @@ public static function getLabel(): string |
33 | 35 | return str(static::class)->classBasename()->snake()->replace('_', ' ')->apa()->toString(); |
34 | 36 | } |
35 | 37 |
|
| 38 | + protected function getTempFolderPath($folder) |
| 39 | + { |
| 40 | + return collect([ |
| 41 | + ExportDataHelper::getTempDirectory(), |
| 42 | + $folder, |
| 43 | + ])->filter(fn ($item) => ! empty($item))->implode(DIRECTORY_SEPARATOR); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return \Illuminate\Contracts\Filesystem\Filesystem |
| 48 | + */ |
| 49 | + protected function getTempDisk() |
| 50 | + { |
| 51 | + return Storage::disk(ExportDataHelper::getTempDiskDriver()); |
| 52 | + } |
| 53 | + |
36 | 54 | /** |
37 | 55 | * @param string $folderName |
38 | 56 | */ |
39 | 57 | protected function generateTempFolder($folderName) |
40 | 58 | { |
41 | | - $disk = $this->record->getDisk(); |
| 59 | + $disk = $this->getTempDisk(); |
| 60 | + $path = $this->getTempFolderPath($folderName); |
42 | 61 |
|
43 | 62 | // Create directory with permissions |
44 | | - if (! $disk->exists($folderName)) { |
45 | | - $disk->makeDirectory($folderName, 0777, true); |
| 63 | + if (! $disk->exists($path)) { |
| 64 | + $disk->makeDirectory($path, 0777, true); |
46 | 65 | } |
47 | 66 |
|
48 | | - return [$disk, $disk->path($folderName)]; |
| 67 | + return [$disk, $disk->path($path)]; |
49 | 68 | } |
50 | 69 |
|
51 | 70 | protected function generateTempFolderForImport($folderName, array $importTypes = []) |
52 | 71 | { |
53 | 72 | [$fs, $fullPath] = $this->generateTempFolder($folderName); |
54 | 73 |
|
55 | 74 | $subFolders = []; |
| 75 | + |
56 | 76 | foreach ($importTypes as $importType) { |
57 | | - $folderPath = $folderName . '/' . $importType; |
58 | | - if (! $fs->exists($folderPath)) { |
59 | | - $fs->makeDirectory($folderPath, 0777, true); |
| 77 | + |
| 78 | + $path = collect([ |
| 79 | + ExportDataHelper::getTempDirectory(), |
| 80 | + $folderName, |
| 81 | + $importType, |
| 82 | + ])->filter(fn ($item) => ! empty($item))->implode(DIRECTORY_SEPARATOR); |
| 83 | + |
| 84 | + if (! $fs->exists($path)) { |
| 85 | + $fs->makeDirectory($path, 0777, true); |
60 | 86 | } |
61 | | - $subFolders[$importType] = $folderPath; |
| 87 | + $subFolders[$importType] = $path; |
62 | 88 | } |
63 | 89 |
|
64 | 90 | return [$fs, $fullPath, $subFolders]; |
65 | 91 | } |
66 | 92 |
|
67 | 93 | protected function zipTempFolder($folderName, bool $deleteFolder = true) |
68 | 94 | { |
| 95 | + $folderDisk = $this->getTempDisk(); |
| 96 | + $folderPath = $this->getTempFolderPath($folderName); |
| 97 | + $folderFullPath = $folderDisk->path($folderPath); |
| 98 | + |
69 | 99 | $disk = $this->record->getDisk(); |
70 | | - $folderFullPath = $disk->path($folderName); |
| 100 | + $zipPath = collect([ |
| 101 | + ExportDataHelper::getDirectory(), |
| 102 | + $folderName . '.zip', |
| 103 | + ])->filter(fn ($item) => ! empty($item))->implode(DIRECTORY_SEPARATOR); |
| 104 | + $zipDir = str($zipPath)->beforeLast('/')->toString(); |
| 105 | + |
| 106 | + // Create directory with permissions |
| 107 | + if (filled($zipDir) && ! $disk->exists($zipDir)) { |
| 108 | + $disk->makeDirectory($zipDir, 0777, true); |
| 109 | + } |
71 | 110 |
|
72 | | - $zipPath = $folderName . '.zip'; |
73 | 111 | $zipFullPath = $disk->path($zipPath); |
74 | 112 |
|
75 | 113 | FileHelper::buildZipFromFolder($folderFullPath, $zipFullPath); |
76 | 114 |
|
77 | 115 | if ($deleteFolder) { |
78 | | - $disk->deleteDirectory($folderName); |
| 116 | + $folderDisk->deleteDirectory($folderPath); |
79 | 117 | } |
80 | 118 |
|
81 | 119 | return $zipPath; |
|
0 commit comments