Skip to content

Commit

Permalink
feat: 文件上传组件, 支持分块上传
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Jun 9, 2024
1 parent a861a3e commit 45eeb36
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Renderers/FileControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public function __construct()
{
$this->set('type', 'input-file');

$this->receiver($this->uploadFilePath());
$this->receiver(admin_url('upload_file'));
$this->startChunkApi(admin_url('upload_chunk_start'));
$this->chunkApi(admin_url('upload_chunk'));
$this->finishChunkApi(admin_url('upload_chunk_finish'));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Support/Cores/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ private static function baseRoutes($prefix)
$router->get('page_schema', [IndexController::class, 'pageSchema']);

$router->any('upload_file', [IndexController::class, 'uploadFile']);
$router->any('upload_chunk_start', [IndexController::class, 'chunkUploadStart']);
$router->any('upload_chunk', [IndexController::class, 'chunkUpload']);
$router->any('upload_chunk_finish', [IndexController::class, 'chunkUploadFinish']);
$router->any('upload_rich', [IndexController::class, 'uploadRich']);
$router->any('upload_image', [IndexController::class, 'uploadImage']);
$router->get('user_setting', [AuthController::class, 'userSetting']);
Expand Down
67 changes: 67 additions & 0 deletions src/Traits/UploadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Slowlyo\OwlAdmin\Traits;

use Slowlyo\OwlAdmin\Admin;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;

trait UploadTrait
Expand Down Expand Up @@ -87,4 +88,70 @@ protected function upload($type = 'file')

return $this->response()->success(['value' => $path]);
}

public function chunkUploadStart()
{
$uploadId = Str::uuid();

cache()->put($uploadId, [], 600);

app('filesystem')->makeDirectory(storage_path('app/public/chunk/' . $uploadId));

return $this->response()->success(compact('uploadId'));
}

public function chunkUpload()
{
$uploadId = request('uploadId');
$partNumber = request('partNumber');
$file = request()->file('file');

$path = 'chunk/' . $uploadId;

$file->storeAs($path, $partNumber, 'public');

$eTag = md5(Storage::disk('public')->get($path . '/' . $partNumber));

return $this->response()->success(compact('eTag'));
}

public function chunkUploadFinish()
{
$fileName = request('filename');
$partList = request('partList');
$uploadId = request('uploadId');
$type = request('t');

$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$path = $type . '/' . $uploadId . '.' . $ext;
$fullPath = storage_path('app/public/' . $path);

$dir = dirname($fullPath);
if (!is_dir($dir)) {
app('filesystem')->makeDirectory($dir);
}

for ($i = 0; $i < count($partList); $i++) {
$partNumber = $partList[$i]['partNumber'];
$eTag = $partList[$i]['eTag'];

$partPath = 'chunk/' . $uploadId . '/' . $partNumber;

$partETag = md5(Storage::disk('public')->get($partPath));

if ($eTag != $partETag) {
return $this->response()->fail('分片上传失败');
}

file_put_contents($fullPath, Storage::disk('public')->get($partPath), FILE_APPEND);
}

clearstatcache();

$value = admin_resource_full_path($path);

app('files')->deleteDirectory(storage_path('app/public/chunk/' . $uploadId));

return $this->response()->success(['value' => $value], '上传成功');
}
}

0 comments on commit 45eeb36

Please sign in to comment.