Skip to content

Commit e09b390

Browse files
authored
Fix extension of merged file (#42)
* Fix extension of merged file * Fix missing variable
1 parent 131c778 commit e09b390

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/Helper/ChunkHelpers.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodingSocks\ChunkUploader\Helper;
44

5+
use Illuminate\Contracts\Filesystem\Filesystem;
56
use Illuminate\Http\File;
67
use Illuminate\Http\UploadedFile;
78
use Illuminate\Support\Facades\Storage;
@@ -45,7 +46,7 @@ public function mergeChunks(StorageConfig $config, array $chunks, string $target
4546
}
4647
}
4748

48-
return $targetPath;
49+
return $this->correctMergedExt($disk, $mergedDirectory, $targetFilename);
4950
}
5051

5152
/**
@@ -67,6 +68,7 @@ public function deleteChunkDirectory(StorageConfig $config, string $uuid): void
6768
* @param \CodingSocks\ChunkUploader\Range\Range $range
6869
* @param \Illuminate\Http\UploadedFile $file
6970
* @param string $uuid
71+
*
7072
* @return array
7173
*/
7274
public function storeChunk(StorageConfig $config, Range $range, UploadedFile $file, string $uuid): array
@@ -127,10 +129,32 @@ public function chunkExists(StorageConfig $config, string $uuid, string $chunkna
127129
$directory = $config->getChunkDirectory() . '/' . $uuid;
128130
$disk = Storage::disk($config->getDisk());
129131

130-
if (! $disk->exists($directory)) {
132+
if (!$disk->exists($directory)) {
131133
return false;
132134
}
133135

134136
return $chunkname === null || $disk->exists($directory . '/' . $chunkname);
135137
}
138+
139+
/**
140+
* @param \Illuminate\Contracts\Filesystem\Filesystem $disk
141+
* @param string $mergedDirectory
142+
* @param string $targetFilename
143+
*
144+
* @return string
145+
*/
146+
private function correctMergedExt(Filesystem $disk, string $mergedDirectory, string $targetFilename): string
147+
{
148+
$targetPath = $mergedDirectory . '/' . $targetFilename;
149+
$ext = pathinfo($targetFilename, PATHINFO_EXTENSION);
150+
if ($ext === 'bin') {
151+
$var = $disk->path($targetPath);
152+
$uploadedFile = new UploadedFile($var, $targetFilename);
153+
$filename = pathinfo($targetFilename, PATHINFO_FILENAME);
154+
$fixedTargetPath = $mergedDirectory . '/' . $filename . '.' . $uploadedFile->guessExtension();
155+
$disk->move($targetPath, $fixedTargetPath);
156+
$targetPath = $fixedTargetPath;
157+
}
158+
return $targetPath;
159+
}
136160
}

0 commit comments

Comments
 (0)