Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to override default upload functionality #61

Open
volosan opened this issue Mar 15, 2021 · 2 comments
Open

How to override default upload functionality #61

volosan opened this issue Mar 15, 2021 · 2 comments
Assignees

Comments

@volosan
Copy link

volosan commented Mar 15, 2021

Hello! Thank you for your work!

I want to override default upload functionality. What exactly I want:

  1. Custom file paths and names. For example: /storage/app/public/uploads/media-library/<users_group_id>/<random_string>.jpg.
    Ok, 'baseUrl' and 'root' I can setup in /config/ckfinder.php. But how can I add 'users_group_id' (dynamic value) and random file name?
  2. Ideally I would like to use a disk.
  3. Make record in database for each uploaded file.
  4. Read files from custom folders.

My current solution:
I've made a custom plugin for CKFinder and it works fine for me. In my plugin I am listening to event "CKFinderEvent::FILE_UPLOAD". But how I can prevent defaul upload functionality? Now my files are duplicates.

My code:

<?php

namespace CKSource\CKFinder\Plugin\UploadPlugin;

use App\Models\Account;
use App\Models\Media;
use CKSource\CKFinder\CKFinder;
use CKSource\CKFinder\Config;
use CKSource\CKFinder\Event\CKFinderEvent;
use CKSource\CKFinder\Event\FileUploadEvent;
use CKSource\CKFinder\Plugin\PluginInterface;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class UploadPlugin implements PluginInterface, EventSubscriberInterface
{
    protected $app;

    public function setContainer(CKFinder $app)
    {
        $this->app = $app;
    }

    public function getDefaultConfig()
    {
        return [];
    }

    public function customAction(FileUploadEvent $event)
    {
        /* @var $account Account */
        $account = auth()->user();
        $businessId = $account->business->getKey();

        $uploadedFile = $event->getFile();

        $fileName = $businessId . DIRECTORY_SEPARATOR . Str::random(25) . '.' . $uploadedFile->getExtension();

        Storage::disk('media-library')->put($fileName, $uploadedFile->getContents());

        $path = Storage::disk('account')->path($fileName);
        $mime = mime_content_type($path);

        return Media::create(['path' => $path, 'name' => $fileName, 'type' => $mime]);
    }

    public static function getSubscribedEvents()
    {
        return [CKFinderEvent::FILE_UPLOAD => 'customAction'];
    }
}
@volosan
Copy link
Author

volosan commented Mar 15, 2021

I think, I have to override FileUpload command with my plugin, but I have no idea how.

@bmlotek
Copy link
Contributor

bmlotek commented Nov 23, 2022

Hello, do you still have the problem?

@bmlotek bmlotek self-assigned this Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants