1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jobtech\LaravelChunky\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Jobtech\LaravelChunky\Http\Requests\AddChunkRequest; |
7
|
|
|
|
8
|
|
|
trait ChunkyRequestHelpers |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Check if folder is a valid string, otherwise guess the folder from the |
12
|
|
|
* request file input. |
13
|
|
|
* |
14
|
|
|
* @param \Jobtech\LaravelChunky\Http\Requests\AddChunkRequest $request |
15
|
|
|
* @param string|null $folder |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
protected function checkFolder(AddChunkRequest $request, ?string $folder) |
20
|
|
|
{ |
21
|
|
|
$file = $request->fileInput(); |
22
|
|
|
|
23
|
|
|
if ($folder !== null) { |
24
|
|
|
return Str::slug($folder); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->chunkFolderNameFor( |
|
|
|
|
28
|
|
|
str_replace( |
29
|
|
|
$file->guessExtension(), |
30
|
|
|
'', |
31
|
|
|
$file->getClientOriginalName() |
32
|
|
|
) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Retrieve last chunk index from chunk request by calculating the ceil value of |
38
|
|
|
* the total size of the file divided by the size of the single chunk. |
39
|
|
|
* |
40
|
|
|
* @param \Jobtech\LaravelChunky\Http\Requests\AddChunkRequest $request |
41
|
|
|
* |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
|
|
public function lastIndexFrom(AddChunkRequest $request): int |
45
|
|
|
{ |
46
|
|
|
$total_size = $request->totalSizeInput(); |
47
|
|
|
$chunk_size = $request->chunkSizeInput(); |
48
|
|
|
|
49
|
|
|
if ($total_size < $chunk_size) { |
50
|
|
|
// In this case usually it means that there's only a chunk |
51
|
|
|
return 1; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return ceil($total_size / $chunk_size); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check if current index refers to the last chunk. |
59
|
|
|
* |
60
|
|
|
* @param \Jobtech\LaravelChunky\Http\Requests\AddChunkRequest $request |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function isLastIndex(AddChunkRequest $request): bool |
65
|
|
|
{ |
66
|
|
|
$last_index = $this->lastIndexFrom($request) |
67
|
|
|
+ ($this->settings->defaultIndex() - 1); |
68
|
|
|
|
69
|
|
|
return $request->indexInput() == $last_index; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|