-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use uploadcare to upload and manage files (#616)
- Loading branch information
Showing
29 changed files
with
687 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use App\Models\Company\File; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
|
||
class FileDeleted | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public File $file; | ||
|
||
/** | ||
* Create a new event instance. | ||
*/ | ||
public function __construct(File $file) | ||
{ | ||
$this->file = $file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class EnvVariablesNotSetException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use Uploadcare\Api; | ||
use App\Models\Company\File; | ||
use Illuminate\Bus\Queueable; | ||
use Uploadcare\Configuration; | ||
use Illuminate\Queue\SerializesModels; | ||
use Http\Client\Exception\HttpException; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Uploadcare\File\File as UploadcareFile; | ||
use App\Exceptions\EnvVariablesNotSetException; | ||
use Uploadcare\Interfaces\File\FileInfoInterface; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
class DeleteFileInUploadcare implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* The file instance. | ||
* | ||
* @var File | ||
*/ | ||
public File $file; | ||
|
||
/** | ||
* The file in Uploadcare instance. | ||
* | ||
* @var FileInfoInterface | ||
*/ | ||
public FileInfoInterface $fileInUploadcare; | ||
|
||
/** | ||
* The API used to query Uploadcare. | ||
* | ||
* @var Api | ||
*/ | ||
public Api $api; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param File $file | ||
*/ | ||
public function __construct(File $file) | ||
{ | ||
$this->file = $file; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$this->checkAPIKeyPresence(); | ||
$this->getFileFromUploadcare(); | ||
$this->deleteFile(); | ||
} | ||
|
||
private function checkAPIKeyPresence(): void | ||
{ | ||
if (is_null(config('officelife.uploadcare_private_key'))) { | ||
throw new EnvVariablesNotSetException(); | ||
} | ||
|
||
if (is_null(config('officelife.uploadcare_public_key'))) { | ||
throw new EnvVariablesNotSetException(); | ||
} | ||
} | ||
|
||
private function getFileFromUploadcare(): void | ||
{ | ||
$configuration = Configuration::create(config('officelife.uploadcare_public_key'), config('officelife.uploadcare_private_key')); | ||
$this->api = new Api($configuration); | ||
|
||
try { | ||
$this->fileInUploadcare = $this->api->file()->fileInfo($this->file->uuid); | ||
} catch (HttpException $e) { | ||
throw new BadRequestHttpException($e->getMessage()); | ||
} | ||
} | ||
|
||
private function deleteFile(): void | ||
{ | ||
// if (! $this->fileInUploadcare instanceof UploadcareFile) { | ||
$this->api->file()->deleteFile($this->fileInUploadcare); | ||
// } else { | ||
//$this->fileInUploadcare->delete(); | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Events\FileDeleted; | ||
use App\Jobs\DeleteFileInUploadcare; | ||
|
||
class DeleteFileInStorage | ||
{ | ||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param FileDeleted $event | ||
*/ | ||
public function handle(FileDeleted $event) | ||
{ | ||
DeleteFileInUploadcare::dispatch($event->file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.