Skip to content

Commit

Permalink
Merge pull request #6 from hans-thomas/new-hls-export-action
Browse files Browse the repository at this point in the history
- Config file updated
- New HlsExport action implemented
- New `hlsExport` method added to docs
- More tests added
  • Loading branch information
hans-thomas authored Oct 14, 2023
2 parents aac232a + f144f57 commit 6dd74d2
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 43 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

It's a file uploader and manager with below features:

- Upload pictures and videos and files
- Store external links
- Upload any file type in single or batch mode
- Store external files
- HLS support
- Collect file's detail
- Classification for files
- Automatic optimization
- Collect file's details automatically
- Classification for uploaded files
- Optimization for images and videos
- Export images in different resolution

for more information [see documentation](https://docs-alicia.vercel.app/).

Expand Down
4 changes: 2 additions & 2 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
| however if you want to protect your links leave it true
|
*/
'signed' => true,
'signed' => false,

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -128,7 +128,7 @@
|
*/
'hls' => [
'enable' => true,
'enable' => false,
'bitrate' => [250, 500, 1000],
'setSegmentLength' => 10,
'setKeyFrameInterval' => 48,
Expand Down
27 changes: 22 additions & 5 deletions docs/content/en/docs/getting-started/facade.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,37 @@ will introduce them.

<div class="method">

[batchDelete](#batchDelete)
[batchDelete](#batchdelete)
</div>

<div class="method">

[deleteFile](#deleteFile)
[deleteFile](#deletefile)
</div>

<div class="method">

[makeExternal](#makeExternal)
[makeExternal](#makeexternal)
</div>

<div class="method">

[fromFile](#fromFile)
[fromFile](#fromfile)
</div>

<div class="method">

[getData](#getData)
[hlsExport](#hlsexport)
</div>

<div class="method">

[getData](#getdata)
</div>


</div>

### batch

Uploads and stores files and links at once.
Expand Down Expand Up @@ -170,6 +174,19 @@ $file = '/path/to/file.extension';
$model = Alicia::fromFile( $file )->getData();
```

### hlsExport

HLS is disabled by default, but if you want to get a HLS export for your uploaded video file, you can use `hlsExport`
method.

```php
use Hans\Alicia\Facades\Alicia;

Alicia::upload( request()->file('file') )->hlsExport()->getData();
```

This method dispatch a job in the background to get HLS export for your video file.

### getData

After you create a resource, you can call `getData` method to get your created resources. if your action created one
Expand Down
4 changes: 1 addition & 3 deletions src/Contracts/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,7 @@ protected function processModel(ResourceModel $model): void
in_array($model->extension, alicia_config('extensions.images'))
) {
OptimizePictureJob::dispatch($model);
} elseif (
in_array($model->extension, alicia_config('extensions.videos'))
) {
} elseif (in_array($model->extension, alicia_config('extensions.videos'))) {
if (alicia_config('optimization.videos')) {
$jobs[] = new OptimizeVideoJob($model);
}
Expand Down
1 change: 0 additions & 1 deletion src/Exceptions/AliciaErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class AliciaErrorCode
public const UNKNOWN_FILE_TYPE = 5007;
public const FAILED_TO_PROCESS_MODEL = 5008;
public const FAILED_TO_DELETE_RESOURCE_MODEL = 5009;
public const INVALID_MODEL_TO_EXPORT = 5010;
public const MODEL_IS_EXTERNAL_ALREADY = 5011;
public const FAILED_TO_MAKE_MODEL_EXTERNAL = 5012;
public const FILE_DOEST_NOT_EXIST = 5013;
Expand Down
1 change: 1 addition & 0 deletions src/Facades/Alicia.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @method static bool deleteFile( string $path )
* @method static AliciaService makeExternal( Resource $model, string $url )
* @method static AliciaService fromFile( string $path )
* @method static AliciaService hlsExport()
* @method static Resource|Collection|null getData()
*
* @see AliciaService
Expand Down
11 changes: 3 additions & 8 deletions src/Services/Actions/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Hans\Alicia\Services\Actions;

use Hans\Alicia\Contracts\Actions;
use Hans\Alicia\Exceptions\AliciaErrorCode;
use Hans\Alicia\Exceptions\AliciaException;
use Hans\Alicia\Models\Resource;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand All @@ -22,23 +20,20 @@ public function __construct(
/**
* Contain action's logic.
*
* @throws AliciaException
* @throws InvalidManipulation
*
* @return Collection
*/
public function run(): Collection
{
$data = collect();
if (
$this->model->isExternal() or
!in_array($this->model->extension, alicia_config('extensions.images'))
) {
throw new AliciaException(
'Invalid model for exportation!',
AliciaErrorCode::INVALID_MODEL_TO_EXPORT
);
return $data;
}
$data = collect();

foreach ($this->resolutions ?: alicia_config('export') as $height => $width) {
$fileName = Str::remove('.'.$this->model->extension, $this->model->file).
"-{$height}x{$width}.".$this->model->extension;
Expand Down
30 changes: 30 additions & 0 deletions src/Services/Actions/HlsExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Hans\Alicia\Services\Actions;

use Hans\Alicia\Contracts\Actions;
use Hans\Alicia\Jobs\GenerateHLSJob;
use Hans\Alicia\Models\Resource;
use Illuminate\Support\Collection;

class HlsExport extends Actions
{
public function __construct(
protected readonly Resource $model
) {
}

/**
* Contain action's logic.
*
* @return resource|Collection
*/
public function run(): Resource|Collection
{
if (in_array($this->model->extension, alicia_config('extensions.videos')) and !alicia_config('hls.enable')) {
GenerateHLSJob::dispatch($this->model);
}

return $this->model;
}
}
3 changes: 1 addition & 2 deletions src/Services/Actions/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function run(): Resource
'options' => $this->getOptions($this->file),
]);
$this->storeOnDisk($model, $this->file);
$this->processModel($model);
} catch (Throwable $e) {
DB::rollBack();

Expand All @@ -48,8 +49,6 @@ public function run(): Resource
}
DB::commit();

$this->processModel($model);

return $model->refresh();
}
}
39 changes: 27 additions & 12 deletions src/Services/AliciaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Hans\Alicia\Services\Actions\Export;
use Hans\Alicia\Services\Actions\External;
use Hans\Alicia\Services\Actions\FromFile;
use Hans\Alicia\Services\Actions\HlsExport;
use Hans\Alicia\Services\Actions\MakeExternal;
use Hans\Alicia\Services\Actions\Upload;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Spatie\Image\Exceptions\InvalidManipulation;

Expand Down Expand Up @@ -41,7 +41,7 @@ public function __construct()
*/
public function batch(array $files): self
{
$this->data = ( new BatchUpload($files) )->run();
$this->data = (new BatchUpload($files))->run();

return $this;
}
Expand All @@ -57,7 +57,7 @@ public function batch(array $files): self
*/
public function upload(UploadedFile $file): self
{
$this->data->push(( new Upload($file) )->run());
$this->data->push((new Upload($file))->run());

return $this;
}
Expand All @@ -73,7 +73,7 @@ public function upload(UploadedFile $file): self
*/
public function external(string $file): self
{
$this->data->push(( new External($file) )->run());
$this->data->push((new External($file))->run());

return $this;
}
Expand All @@ -89,13 +89,14 @@ public function external(string $file): self
*/
public function export(array $resolutions = null): self
{
// TODO: write test for batch()->export()
$exports = collect($data = Arr::wrap($this->getData()));
foreach ($data as $model) {
$exports->push(( new Export($model, $resolutions) )->run()->toArray());
$exports = collect();
foreach ($this->data as $model) {
$exports->push((new Export($model, $resolutions))->run());
}

$this->data = $exports->flatten(1)
$this->data = $exports->merge($this->data)
->flatten(1)
->filter()
->groupBy(
fn ($item, $key) => $item['parent_id'] ?
$item['parent_id'].'-children' :
Expand All @@ -117,7 +118,7 @@ public function export(array $resolutions = null): self
*/
public function makeExternal(Resource $model, string $url): self
{
$this->data->push(( new MakeExternal($model, $url) )->run());
$this->data->push((new MakeExternal($model, $url))->run());

return $this;
}
Expand All @@ -133,7 +134,21 @@ public function makeExternal(Resource $model, string $url): self
*/
public function fromFile(string $path): self
{
$this->data->push(( new FromFile($path) )->run());
$this->data->push((new FromFile($path))->run());

return $this;
}

/**
* Generate Hls export of uploaded video files in the background.
*
* @return $this
*/
public function HlsExport(): self
{
foreach ($this->data as $model) {
(new HlsExport($model))->run();
}

return $this;
}
Expand All @@ -155,7 +170,7 @@ public function delete(Resource|int $model): bool
->select(['id', 'directory', 'external'])
->findOrFail($model);

( new Delete($model) )->run();
(new Delete($model))->run();

return true;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Actions/BatchUploadActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BatchUploadActionTest extends TestCase
*/
public function batchUpload()
{
config()->set('alicia.hls.enable', true);
$data = Alicia::batch(
[
UploadedFile::fake()
Expand Down
6 changes: 4 additions & 2 deletions tests/Feature/Actions/DeleteActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class DeleteActionTest extends TestCase
*
* @return void
*/
public function deleteResource(): void
public function deleteResourceWithHls(): void
{
config()->set('alicia.hls.enable', true);
$model = Alicia::upload(
UploadedFile::fake()
->createWithContent(
Expand Down Expand Up @@ -44,8 +45,9 @@ public function deleteResource(): void
*
* @return void
*/
public function batchDelete(): void
public function batchDeleteWithHls(): void
{
config()->set('alicia.hls.enable', true);
$modelA = Alicia::upload(
UploadedFile::fake()
->createWithContent(
Expand Down
Loading

1 comment on commit 6dd74d2

@vercel
Copy link

@vercel vercel bot commented on 6dd74d2 Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

alicia – ./

docs-alicia.vercel.app
alicia-git-master-hans-thomas.vercel.app
alicia-hans-thomas.vercel.app

Please sign in to comment.