alareqi/smart-upload 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alareqi/smart-upload

最新稳定版本:v1.3

Composer 安装命令:

composer require alareqi/smart-upload

包简介

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

README 文档

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.

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固