Skip to content

Asset module

Assets module

Introduction

The Assets module offers a way to upload files (assets) that have extra information attach to them, such as:

  • Name
    • With automatic deduplication feature - e.g. MyFile(2) if MyFile already exists
  • Publish/unpublish feature
  • Attach assets to models
  • Show assets in multiple applications with custom policies
    • Each application may define the policies when an upload may, for example, be viewed

Customization

Currently, it's not possible to customize the Asset module.

It is, however, possible to override the Asset Resource in Filament.

php
QoreAdminBasePlugin::make()->setAssetResource(App\Application\Admin\Resources\MyCustomAssetResource::class);

Viewing assets in other applications than Qore.admin

It's possible to show assets in other applications than Qore.admin (for example a Portal) to users. When doing this, you probably want to define a custom policy with custom rules when and if an upload may be shown to the user.

Creating a route to view assets

For example, to show assets in a portal application, register the route using:

php
// routes/portal.php
Qore::assetRoutes('portal-asset');

Get asset URL for a specific route

To get the URL to the newly generated portal-asset route, do this:

php
$asset = Asset::firstOrFail();

$asset->getUrl('application.portal.asset'); // http://localhost/portal-asset/01HMV3EEHB0R5T940CBSGNV8WZ/my-file.jpg

Defining custom policy

Simply create the policy in the application and it will automatically get used in the newly registered route:

php
// app/Application/Portal/Policies/AssetPolicy.php

declare(strict_types=1);

namespace App\Application\Portal\Policies;

use App\Models\User;
use QoreWorksBusiness\QoreAdminBase\Models\Asset;

class AssetPolicy
{
    public function view(?User $user, Asset $asset): bool
    {
        // add your logic here
    }
}

Asset types

You can define several asset types. Per asset type you can define an disk where to store the media. For example; you can create a asset type invoices with disk private and create an asset type document that can be stored on s3_public. When creating an asset, you'll be asked what type of asset it is. After storing the asset, the disk will be save on the media model.