Skip to content

Commit

Permalink
Merge pull request #7 from jobtech-dev/develop
Browse files Browse the repository at this point in the history
Compatibility fix
  • Loading branch information
ilgala committed Aug 31, 2020
2 parents 4ad9813 + af9ab0d commit 5287839
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 62 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: PHP Composer
name: tests

on:
push:
pull_request:

jobs:
run-tests:
runs-on: [self-hosted, Linux, X64]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
Expand All @@ -28,8 +28,11 @@ jobs:
- name: Install ffmpeg
run: sudo apt-get install ffmpeg

- name: Checkout code
uses: actions/checkout@v2

- name: Cache dependencies
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
Expand All @@ -48,3 +51,9 @@ jobs:
- name: Execute tests
run: vendor/bin/phpunit
env:
CHUNKY_CHUNK_DISK: local
CHUNKY_MERGE_DISK: local
CHUNKY_AUTO_MERGE: true
CHUNKY_MERGE_CONNECTION: default
CHUNKY_MERGE_QUEUE: null
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: laravel
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

## Getting Started

Laravel chunky has been written to easily handle chunk upload for large files in Laravel 6.x and 7.x. It will automatically handle the upload request (see the [usage](#examples) section below) and save all the chunks into the desired disk.
Laravel chunky has been written to easily handle chunk upload for large files in Laravel 6.x and 7.x. It will automatically handle the upload request (see the [usage](#usage) section below) and save all the chunks into the desired disk.

Once the upload completes, the package will dispatch a job in order to merge all the files into a single one and save in the same chunks disks or in another one.

Expand Down Expand Up @@ -120,15 +120,15 @@ $app->configure('chunky');

This package has been designed to leave you the full control of the chunks upload and simple use the helper methods to handle the merge strategy as well as an _all-in-one_ solution for a fast scaffolding of the controllers delegated to handle large files upload.

At the moment, this package doesn't include any wrapper for the frontend forms for the file uploads but, in the examples section, you can find two ways of integrate the package with [Dropzone](https://www.dropzonejs.com/) and [ResumableJs](http://resumablejs.com/).
At the moment, this package doesn't include any wrapper for the frontend forms for the file uploads but, in the `config/chunky.php` configuration file, you can find two ways of integrate the package with [Dropzone](https://www.dropzonejs.com/) and [ResumableJs](http://resumablejs.com/).

### Chunks

Laravel Chunky handles the chunks as an _ordered list_ of files. This is a **must** and if a wrong file index has been uploaded, an exception will be thrown in order to guarantee the integrity of the final merged file. Once all the chunks have been uploaded, and the merge strategy is executing, another integrity check will be made to all the chunks. If the sum of each file size is lower than the original file size, another exception will be thrown. For this reason a chunk request must include both the chunk and these attributes:

* **An `index`**: indicates the current chunk that is uploading. The first index can be set in the configuration file.
* **A `file size`**: the original file size. Will be used for the integrity check.
* **A `chunk size`**: the chunk file size. Will be used for the integrity check.
* An `index`: indicates the current chunk that is uploading. The first index can be set in the configuration file.
* A `file size`: the original file size. Will be used for the integrity check.
* A `chunk size`: the chunk file size. Will be used for the integrity check.

#### Configuration

Expand Down Expand Up @@ -468,21 +468,19 @@ We're working on:

## Contributing

Please see CONTRIBUTING.md for more details.
Please see [CONTRIBUTING.md](https://github.com/jobtech-dev/laravel-chunky/blob/master/CONTRIBUTING.md) for more details.

## License

Distributed under the MIT License. See `LICENSE` for more information.
Distributed under the MIT License. See [LICENSE](https://github.com/jobtech-dev/laravel-chunky/blob/master/LICENSE) for more information.

## Contact

Jobtech dev team - [dev@jobtech.it](mailto:dev@jobtech.it)

Project Link: [https://github.com/jobtech-dev/laravel-chunky](https://github.com/jobtech-dev/laravel-chunky)

## Credits

Laravel Chunky is a Laravel package made with :hearth: by the JT nerds.
Laravel Chunky is a Laravel package made with :heart: by the JT nerds.

Thanks to:

Expand All @@ -493,4 +491,7 @@ We've used these packages for the merge strategies:

* Keven Godet - [Flysystem concatenate](https://github.com/kevengodet/flysystem-concatenate)
* Protone Media - [Laravel FFMpeg](https://github.com/protonemedia/laravel-ffmpeg)

And this repository for the readme boilerplate:

* Othneil Drew - [Best-README-Template](https://github.com/othneildrew/Best-README-Template)
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
"illuminate/filesystem": "~5.8.0|^6.0|^7.0",
"illuminate/support": "~5.8.0|^6.0|^7.0",
"pbmedia/laravel-ffmpeg": "^7.0",
"keven/flysystem-concatenate": "^1.0"
"keven/flysystem-concatenate": "^1.0",
"keven/append-stream": "^1.0.5"
},
"require-dev": {
"orchestra/testbench": "3.8.*|4.*|5.*",
"phpunit/phpunit": "^8.4|^9.0"
"phpunit/phpunit": "^8.4|^9.0",
"mockery/mockery": "^1.4"
},
"suggest": {
"league/flysystem-aws-s3-v3": "Required to use AWS S3 file storage"
Expand Down
4 changes: 2 additions & 2 deletions config/chunky.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
|
*/

'auto_merge' => env('CHUNKY_AUTO_MERGE', false),
'auto_merge' => env('CHUNKY_AUTO_MERGE', true),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -189,7 +189,7 @@
'video/*' => \Jobtech\LaravelChunky\Strategies\VideoStrategy::class,
],

'connection' => env('CHUNKY_MERGE_CONNECTION', 'default'),
'connection' => env('CHUNKY_MERGE_CONNECTION', 'sync'),

'queue' => env('CHUNKY_MERGE_QUEUE'),
],
Expand Down
4 changes: 2 additions & 2 deletions src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function storeIn(string $folder, $options = []): Chunk
->disk($disk)
->putFileAs(
$folder,
$this->file->getRealPath(),
$this->file,
$chunk_name,
$options
);
Expand Down Expand Up @@ -199,7 +199,7 @@ public function toResource()

public function __call($method, $parameters)
{
if (!method_exists($this, $method)) {
if (! method_exists($this, $method)) {
return $this->forwardCallTo($this->file, $method, $parameters);
}

Expand Down
12 changes: 7 additions & 5 deletions src/ChunksManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,21 @@ public function checkChunkIntegrity(string $folder, int $index): bool
$path = $this->fullPath($folder);
$default = $this->settings->defaultIndex();

if ($this->chunksFilesystem()->exists($path)) {
if (! $this->chunksFilesystem()->exists($path) && $index != $default) {
return false;
} elseif ($this->chunksFilesystem()->exists($path)) {
if (ChunkySettings::INDEX_ZERO != $default) {
$index -= $default;
}

return count($this->chunksFilesystem()->files($path)) == $index;
} elseif ($index == $default) {
if (!$this->chunksFilesystem()->makeDirectory($path)) {
if (! $this->chunksFilesystem()->makeDirectory($path)) {
throw new ChunksIntegrityException("Cannot create chunks folder $path");
}

return true;
}

return true;
}

/**
Expand All @@ -221,7 +223,7 @@ public function checkChunkIntegrity(string $folder, int $index): bool
public function addChunk(UploadedFile $file, int $index, string $folder): Chunk
{
// Check integrity
if (!$this->checkChunkIntegrity($folder, $index)) {
if (! $this->checkChunkIntegrity($folder, $index)) {
throw new ChunksIntegrityException("Uploaded chunk with index {$index} violates the integrity");
}

Expand Down
4 changes: 2 additions & 2 deletions src/ChunkySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function chunksFolder(): string

if ($folder === null) {
return '';
} elseif (!Str::endsWith($folder, '/')) {
} elseif (! Str::endsWith($folder, '/')) {
$folder .= DIRECTORY_SEPARATOR;
}

Expand Down Expand Up @@ -87,7 +87,7 @@ public function mergeFolder(): string

if ($folder === null) {
return '';
} elseif (!Str::endsWith($folder, '/')) {
} elseif (! Str::endsWith($folder, '/')) {
$folder .= DIRECTORY_SEPARATOR;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Commands/ClearChunks.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ public function handle(ChunksManager $manager)
{
$this->manager = $manager;

if (!$this->confirmToProceed()) {
if (! $this->confirmToProceed()) {
return;
}

$root = $this->manager->getChunksFolder();
$folder = $this->argument('folder');

if (!empty($folder)) {
if (! empty($folder)) {
$root .= $folder;

if (!$this->manager->deleteChunks($root)) {
if (! $this->manager->deleteChunks($root)) {
$this->error("An error occurred while deleting folder {$root}");

return;
}

$this->info("folder {$root} cleared!");
} else {
if (!$this->manager->deleteAllChunks()) {
if (! $this->manager->deleteAllChunks()) {
$this->error("An error occurred while deleting folder {$folder}");

return;
Expand Down
12 changes: 6 additions & 6 deletions src/Concerns/ChunksHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait ChunksHelpers
*/
public function chunksFolderExists($folder = ''): bool
{
if (!Str::startsWith($folder, $this->getChunksFolder())) {
if (! Str::startsWith($folder, $this->getChunksFolder())) {
$folder = $this->getChunksFolder().$folder;
}

Expand All @@ -37,18 +37,18 @@ public function deleteAllChunks(): bool
$this->getChunksFolder()
);

if (app()->runningInConsole() && !app()->runningUnitTests()) {
if (app()->runningInConsole() && ! app()->runningUnitTests()) {
$this->progress_bar = $this->output->createProgressBar(
count($folders)
);
}

foreach ($folders as $folder) {
if (!$this->deleteChunks($folder)) {
if (! $this->deleteChunks($folder)) {
return false;
}

if (app()->runningInConsole() && !app()->runningUnitTests()) {
if (app()->runningInConsole() && ! app()->runningUnitTests()) {
$this->progress_bar->advance();
}
}
Expand All @@ -65,7 +65,7 @@ public function deleteAllChunks(): bool
*/
public function deleteChunks(string $folder): bool
{
if (!$this->chunksFolderExists($folder)) {
if (! $this->chunksFolderExists($folder)) {
return false;
}

Expand All @@ -76,7 +76,7 @@ public function deleteChunks(string $folder): bool
$deleted = $this->chunksFilesystem()
->delete($file);

if (!$deleted) {
if (! $deleted) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/MergeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function strategy($strategy = null): ?MergeStrategy

public function __call($method, $parameters)
{
if (!method_exists($this, $method)) {
if (! method_exists($this, $method)) {
return $this->forwardCallTo($this->strategy, $method, $parameters);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/AddChunkRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function additionalRules(): array

foreach (config('chunky.validation') as $input => $config) {
if (
!in_array($input, ['index', 'file', 'chunkSize', 'totalSize'])
! in_array($input, ['index', 'file', 'chunkSize', 'totalSize'])
&& Arr::has($config, 'key')
&& Arr::has($config, 'rules')
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/MergeChunks.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function handle()
$this->request->fileInput()->getMimeType()
);

if (!$handler->checkIntegrity($this->request->chunkSizeInput(), $this->request->totalSizeInput())) {
if (! $handler->checkIntegrity($this->request->chunkSizeInput(), $this->request->totalSizeInput())) {
throw new ChunksIntegrityException('Chunks total file size doesnt match with original file size');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Strategies/Concerns/ChecksIntegrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function checkIntegrity(int $chunk_size, int $total_size): bool
foreach ($chunks->all() as $chunk) {
$size = $this->manager()->chunksFilesystem()->size($chunk['path']);

if ($size < $chunk_size && !$this->isLastChunk($chunk['index'], $total_size, $chunk_size)) {
if ($size < $chunk_size && ! $this->isLastChunk($chunk['index'], $total_size, $chunk_size)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Strategies/FlysystemStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function mergeChunks(array $chunks): bool
{
$chunk = Arr::first($chunks);

if (!$this->manager->chunksFilesystem()->concatenate($chunk, ...$chunks)) {
if (! $this->manager->chunksFilesystem()->concatenate($chunk, ...$chunks)) {
throw new StrategyException('Unable to concatenate chunks');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Strategies/MergeStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function chunksFolder($folder = null): string
{
if (is_string($folder) && $this->chunksFolderExists($this->folder)) {
$this->folder = $folder;
} elseif (empty($this->folder) || !$this->chunksFolderExists($this->folder)) {
} elseif (empty($this->folder) || ! $this->chunksFolderExists($this->folder)) {
throw new StrategyException('Chunks folder cannot be empty');
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public function mergeContents()

public function __call($method, $parameters)
{
if (!method_exists($this, $method)) {
if (! method_exists($this, $method)) {
return $this->forwardCallTo($this->manager, $method, $parameters);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Strategies/StrategyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(array $config)
*/
private function buildInstance(string $strategy, $manager = null)
{
if (!method_exists($strategy, 'newInstance')) {
if (! method_exists($strategy, 'newInstance')) {
throw new StrategyException('Cannot instantiate strategy instance');
}

Expand All @@ -41,7 +41,7 @@ private function buildInstance(string $strategy, $manager = null)
*/
public function default($manager = null): MergeStrategy
{
if (!Arr::has($this->config, 'default')) {
if (! Arr::has($this->config, 'default')) {
throw new StrategyException('Undefined default strategy');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Strategies/VideoStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handleVideoMerge(): void

$exporter->concatWithoutTranscoding();

if (!empty($visibility = $this->visibility())) {
if (! empty($visibility = $this->visibility())) {
$exporter->withVisibility($visibility);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function getEnvironmentSetUp($app)
}
File::makeDirectory(__DIR__.'/tmp');

if (!File::isDirectory(__DIR__.'/tmp/resources')) {
if (! File::isDirectory(__DIR__.'/tmp/resources')) {
File::copyDirectory(__DIR__.'/resources', __DIR__.'/tmp/resources');
}

Expand All @@ -49,7 +49,7 @@ protected function getEnvironmentSetUp($app)

protected function loadEnvironmentVariables()
{
if (!file_exists(__DIR__.'/../.env')) {
if (! file_exists(__DIR__.'/../.env')) {
return;
}

Expand Down
Loading

0 comments on commit 5287839

Please sign in to comment.