alareqi/smart-upload

Laravel package for mobile file uploads with Livewire-style temporary storage.

Maintainers

Package info

github.com/aymanalareqi/smart-upload

Homepage

pkg:composer/alareqi/smart-upload

Fund package maintenance!

aymanalareqi

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 1

v1.3 2026-04-25 16:06 UTC

README

Laravel package for mobile file uploads with temporary storage - no database required.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a simple way to handle file uploads from mobile apps. Files are uploaded to a temporary location, then moved to their final destination when the form is submitted.

Installation

composer require alareqi/smart-upload

No migrations needed - this package uses file-based temporary storage.

How It Works

┌─────────────┐     /upload-file ┌─────────────┐
│ Mobile App  │ ────────────▶ │   Laravel  │
│             │               │   Server   │
│ 1.Select   │ ◀─────────── │ 2.Return  │
│    file    │    upload   │   signed  │
│             │     URL    │    URL   │
│ 3.Upload   │ ────────────▶ │          │
│    to URL   │  4.Upload  │          │
│             │    file   │          │
│5.Submit     │ ────────────▶ │6.Move to │
│    form    │   form     │ final    │
│             │   data    │ location │
└─────────────┘             └─────────────┘

API Endpoints

Method Endpoint Description
POST /api/upload-file Upload file

Upload File

Upload a file directly to the endpoint:

POST /api/upload-file
Content-Type: multipart/form-data

------WebKitFormBoundary
Content-Disposition: form-data; name="file"; filename="photo.jpg"
Content-Type: image/jpeg

[file content]
------WebKitFormBoundary--

Response:

{
    "uuid": "abc-123-uuid",
    "path": "tmp/abc-123.jpg",
    "original_name": "photo.jpg",
    "size": 1024,
    "mime_type": "image/jpeg",
    "temp_url": "https://yourapp.com/storage/tmp/abc-123.jpg",
    "expires_at": "2024-01-01T12:00:00Z"
}

Laravel Controller Usage

Use the HasFileUploads trait in your controller:

use Alareqi\SmartUpload\Concerns\HasFileUploads;
use Illuminate\Http\Request;

class PostController extends Controller
{
    use HasFileUploads;

    public function store(Request $request)
    {
        // Convert temporary upload to permanent storage
        $path = $this->convertUpload(
            $request->image_uuid,  // UUID from mobile
            'posts/images'        // Final directory
        );

        // Save to database
        Post::create([
            'title' => $request->title,
            'image' => $path,
        ]);
    }
}

Multiple Files

For multiple file uploads, pass an array of UUIDs:

use Alareqi\SmartUpload\Concerns\HasFileUploads;
use Illuminate\Http\Request;

class PostController extends Controller
{
    use HasFileUploads;

    public function store(Request $request)
    {
        $imagePaths = [];

        // $request->image_uploads is array: ['uuid1', 'uuid2', 'uuid3']
        foreach ($request->image_uploads as $uuid) {
            $imagePaths[] = $this->convertUpload(
                $uuid,
                'posts/images'
            );
        }

        // Save to database
        Post::create([
            'title' => $request->title,
            'images' => json_encode($imagePaths),
        ]);
    }
}

Or convert each with custom filename:

foreach ($request->images as $index => $uuid) {
    $path = $this->convertUpload(
        $uuid,
        'posts/images',
        'post_' . $post->id . '_image_' . $index . '.jpg'  // Custom filename
    );
}

Configuration

Edit config/smart-upload.php:

return [
    // Final storage disk
    'disk' => env('SMART_UPLOAD_DISK', 'local'),

    // Temporary directory
    'temp_directory' => env('SMART_UPLOAD_TEMP_DIR', 'smart-upload-tmp'),

    // Hours until temp file expires (also used as cache TTL)
    'expiration_hours' => 24,

    // Max file size in KB
    'max_file_size' => 10240,

    // Allowed mimes
    'allowed_mimes' => ['jpg', 'jpeg', 'png', 'gif', 'pdf'],

    // Cache driver for metadata
    'cache' => [
        'driver' => env('SMART_UPLOAD_CACHE_DRIVER', 'file'),
    ],

    // Temporary upload settings
    'temporary_file_upload' => [
        'disk' => 'local',
        'directory' => 'tmp',
    ],
];

Cleanup Command

Run cleanup to delete expired temporary files:

php artisan smart-upload:cleanup

Schedule it in app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
{
    $schedule->command('smart-upload:cleanup')->hourly();
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.