Skip to content

Commit

Permalink
#10 - wip - basic asset upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
spo committed Aug 30, 2024
1 parent a2fe651 commit fb3aaa2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace App\Enums;

enum EnumStorageDataType: string
enum EnumAssetSource: string
{
case FILE = 'file';
case STRING = 'string';
Expand Down
116 changes: 69 additions & 47 deletions app/Http/Controllers/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace App\Http\Controllers;

use App\Enums\EnumStorageDataType;
use App\Enums\EnumAssetSource;
use App\Enums\EnumStorageType;
use App\Events\APICacheAssetsUpdated;
use App\Http\Resources\Asset as AssetResource;
Expand All @@ -30,57 +30,12 @@
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\MessageBag;
use Validator;

class AssetController extends ApiController
{
public function checks(Request $request): MessageBag|bool
{
// First basic check.
$validator = Validator::make(
$request->all(),
Asset::rules(),
Asset::$rule_messages
);

if ($validator->fails()) {

$validator->errors();
}

// Checks depending on asset storage type.
switch (EnumStorageType::from($request->get('storage_type'))) {

case EnumStorageType::DATABASE:
{
if (EnumStorageDataType::from($request->get('data_type')) === EnumStorageDataType::STRING) {

$validator->addRules(
[
'data_content' => 'required',
]);
}

break;
}

case EnumStorageType::FILESYSTEM:
case EnumStorageType::ONLINE:
default:
{
return new MessageBag(['Unable to recognize asset\'s storage type.']);
}
}

if ($validator->fails()) {

return $validator->errors();
}

return true;
}

public function destroy(Request $request, Asset $asset): JsonResponse
{
$this->genericAuthorize($request, $asset);
Expand Down Expand Up @@ -157,4 +112,71 @@ public function update(Request $request, Asset $asset): JsonResponse

return $this->successResponse(new AssetResource($asset), 'The asset has been updated successfully.');
}

public function upload(Request $request, Asset $asset): JsonResponse
{
if ($asset->storage_type === EnumStorageType::FILESYSTEM) {

$asset_path = $request->file('image')->storeAs(
'assets', $asset->id
);
$asset->update(['pathname' => $asset_path]);
}

return $this->successResponse(new AssetResource($asset), 'The asset has been uploaded successfully.');
}

private function checks(Request $request): MessageBag|bool
{
// First basic check.
$validator = Validator::make(
$request->all(),
Asset::rules(),
Asset::$rule_messages
);

if ($validator->fails()) {

return $validator->errors();
}

// Checks depending on asset storage type.
switch (EnumStorageType::from($request->get('storage_type'))) {

case EnumStorageType::DATABASE:
{
if ($request->get('asset_source') === EnumAssetSource::STRING) {

$validator->addRules(
[
'data_content' => 'required',
]);
} else if ($request->get('asset_source') === EnumAssetSource::FILE && (int)$request->get('id') > 0) {
$this->handleUpload($request);
}

break;
}

case EnumStorageType::FILESYSTEM:
// Handle upload.
if ((int)$request->get('id') > 0) {
$this->handleUpload($request);
}

break;
case EnumStorageType::ONLINE:
default:
{
return new MessageBag(['Unable to recognize asset\'s storage type.']);
}
}

if ($validator->fails()) {

return $validator->errors();
}

return true;
}
}
5 changes: 0 additions & 5 deletions app/Http/Resources/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ function () {
return $this->data_content;
}
),
'data_type' => $this->when($request->get('complete_resource'),
function () {
return $this->data_type;
}
),
'pathname' => $this->when($request->get('complete_resource'),
function () {
return $this->pathname;
Expand Down
7 changes: 1 addition & 6 deletions app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace App\Models;

use App\Enums\EnumStorageDataType;
use App\Enums\EnumAssetSource;
use App\Enums\EnumStorageType;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -34,7 +34,6 @@
* @property string $name
* @property EnumStorageType $storage_type
* @property string|null $data_content When storage type = database
* @property EnumStorageDataType|null $data_type When storage type = database
* @property int $organization_id
* @property string|null $pathname When storage type = filesystem
* @property string|null $url When storage type = online
Expand All @@ -45,7 +44,6 @@
* @method static Builder|Asset query()
* @method static Builder|Asset whereCreatedAt($value)
* @method static Builder|Asset whereData($value)
* @method static Builder|Asset whereDataType($value)
* @method static Builder|Asset whereId($value)
* @method static Builder|Asset whereName($value)
* @method static Builder|Asset whereOrganizationId($value)
Expand All @@ -66,7 +64,6 @@ class Asset extends ApiModel
'pathname',
'url',
'data_content',
'data_type',
];

public static function rules(): array
Expand All @@ -75,12 +72,10 @@ public static function rules(): array
'name' => 'required|between:2,255',
'organization_id' => 'integer|exists:organizations,id',
'storage_type' => [new Enum(EnumStorageType::class)],
'data_type' => [new Enum(EnumStorageDataType::class)],
];
}

protected $casts = [
'storage_type' => EnumStorageType::class,
'data_type' => EnumStorageDataType::class,
];
}
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
->middleware(['auth:sanctum']);

Route::apiResource('asset', App\Http\Controllers\AssetController::class)->middleware('auth:sanctum');
Route::post('asset/{asset}/upload', 'App\Http\Controllers\AssetController@upload')
->name('asset-upload')
->middleware(['auth:sanctum']);

Route::get('cache', 'App\Http\Controllers\CacheController@index')
->name('cache')
Expand Down

0 comments on commit fb3aaa2

Please sign in to comment.