hryvinskyi/magento2-media-uploader

Media uploader module for Magento 2

Installs: 2

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:magento2-module

pkg:composer/hryvinskyi/magento2-media-uploader

1.0.0 2026-01-31 09:47 UTC

This package is auto-updated.

Last update: 2026-01-31 09:52:43 UTC


README

A Magento 2 module for handling media file uploads with support for temporary storage and permanent file management.

Installation

Via Composer

composer require hryvinskyi/module-media-uploader

Manual Installation

  1. Create directory app/code/Hryvinskyi/MediaUploader
  2. Copy module files to this directory
  3. Run setup commands:
bin/magento module:enable Hryvinskyi_MediaUploader
bin/magento setup:upgrade
bin/magento setup:di:compile

Configuration via DI Virtual Types

Configure file upload settings using virtual types in your module's di.xml:

<virtualType name="Your\Module\Model\FileUploader" type="Hryvinskyi\MediaUploader\Model\FileUploader">
    <arguments>
        <argument name="baseTmpPath" xsi:type="string">your_module/tmp/image</argument>
        <argument name="basePath" xsi:type="string">your_module/image</argument>
        <argument name="allowedExtensions" xsi:type="array">
            <item name="jpg" xsi:type="string">jpg</item>
            <item name="jpeg" xsi:type="string">jpeg</item>
            <item name="png" xsi:type="string">png</item>
            <item name="gif" xsi:type="string">gif</item>
            <item name="webp" xsi:type="string">webp</item>
        </argument>
        <argument name="allowedMimeTypes" xsi:type="array">
            <item name="jpg" xsi:type="string">image/jpg</item>
            <item name="jpeg" xsi:type="string">image/jpeg</item>
            <item name="png" xsi:type="string">image/png</item>
            <item name="gif" xsi:type="string">image/gif</item>
            <item name="webp" xsi:type="string">image/webp</item>
        </argument>
    </arguments>
</virtualType>

Usage Examples

Upload Command

use Hryvinskyi\MediaUploader\Api\Command\UploadInterface;

class YourController
{
    public function __construct(
        private readonly UploadInterface $uploadCommand
    ) {}

    public function execute(): array
    {
        // Upload file from $_FILES['image']
        return $this->uploadCommand->execute('image');
    }
}

Move File from Temporary Storage

use Hryvinskyi\MediaUploader\Api\Command\MoveFileFromTmpInterface;

class YourSaveController
{
    public function __construct(
        private readonly MoveFileFromTmpInterface $moveFileFromTmp
    ) {}

    public function execute(array $imageData): string
    {
        // Move file from tmp to permanent storage
        return $this->moveFileFromTmp->execute($imageData);
    }
}

Using FileUploader Directly

use Hryvinskyi\MediaUploader\Api\FileUploaderInterface;

class YourService
{
    public function __construct(
        private readonly FileUploaderInterface $fileUploader
    ) {}

    public function uploadAndMove(string $fileId): string
    {
        // Save to temporary directory
        $result = $this->fileUploader->saveFileToTmpDir($fileId);

        // Move to permanent storage
        return $this->fileUploader->moveFileFromTmp($result['name']);
    }
}

API Reference

FileUploaderInterface

Method Description
getBaseTmpPath(): string Get base temporary path for uploads
getBasePath(): string Get base path for permanent storage
getAllowedExtensions(): array Get list of allowed file extensions
getFilePath(string $path, string $imageName): string Build full file path
moveFileFromTmp(string $imageName): string Move file from tmp to permanent storage
saveFileToTmpDir(string $fileId): array Save uploaded file to temporary directory

UploadInterface

Method Description
execute(string $imageId = 'image'): array Execute file upload to temporary directory

MoveFileFromTmpInterface

Method Description
execute(array $image): string Move file from tmp and return relative path

Data Providers

PrepareImageToArray

Converts image path string to array format for UI components (used when loading data).

PrepareImageToString

Converts image array format to string path and moves file from tmp directory (used when saving data).

Requirements

  • PHP 8.1+
  • Magento 2.4.x
  • Magento_MediaStorage
  • Magento_Store

License

Proprietary