You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8 KiB

5 months ago
<?php
namespace App\Http\Controllers\Api;
5 months ago
use App\Http\Controllers\Concerns\EnsuresPublicDiskWritable;
5 months ago
use App\Http\Controllers\Concerns\StoresPublicUploadWithoutFileinfo;
5 months ago
use App\Http\Controllers\Controller;
use App\Models\WechatUser;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
5 months ago
use Illuminate\Support\Facades\Log;
18 hours ago
use Illuminate\Validation\ValidationException;
5 months ago
use Throwable;
5 months ago
class H5UploadController extends Controller
{
5 months ago
use EnsuresPublicDiskWritable;
5 months ago
use StoresPublicUploadWithoutFileinfo;
5 months ago
5 months ago
public function store(Request $request): JsonResponse
{
$user = $request->user();
if (! $user instanceof WechatUser) {
abort(403, '仅微信用户可上传');
}
5 months ago
if ($early = $this->ensurePublicDiskReady()) {
return $early;
}
18 hours ago
if (! $request->hasFile('file')) {
5 months ago
return response()->json(['message' => '未收到文件'], 422);
}
$uploaded = $request->file('file');
18 hours ago
if (! $uploaded->isValid()) {
5 months ago
return response()->json(['message' => $uploaded->getErrorMessage()], 422);
}
try {
18 hours ago
$stored = $this->storeUploadedFileAsUniqueName($uploaded, 'uploads/h5', 5120);
} catch (ValidationException $e) {
throw $e;
5 months ago
} catch (Throwable $e) {
report($e);
5 months ago
Log::error('h5_upload_putfile_failed', [
'message' => $e->getMessage(),
'public_path' => storage_path('app/public'),
]);
5 months ago
5 months ago
return response()->json([
'message' => '文件保存失败',
'detail' => config('app.debug') ? $e->getMessage() : null,
], 500);
5 months ago
}
5 months ago
return response()->json([
18 hours ago
'url' => url('/storage/'.$stored['path']),
'path' => $stored['path'],
5 months ago
]);
}
}