query('expires'); $ttl = max( self::CACHE_TTL_BUFFER_SECONDS, $expiresAt - now()->timestamp + self::CACHE_TTL_BUFFER_SECONDS, ); $cacheKey = self::CLAIM_CACHE_PREFIX.$token; if (! Cache::add($cacheKey, true, $ttl)) { abort(Response::HTTP_CONFLICT); } if (Media::where('upload_token', $token)->exists()) { abort(Response::HTTP_CONFLICT); } try { $workspace = Workspace::findOrFail((string) $request->query('workspace_id')); $file = $request->file('media'); $path = $file->getRealPath(); // Stream from PHP's temp upload path — do not load the whole file into // memory (addMedia() uses file_get_contents; videos can be up to 1GB). if ($path === false) { abort(Response::HTTP_UNPROCESSABLE_ENTITY, 'Unable to read uploaded file.'); } $media = DB::transaction(function () use ($workspace, $file, $path, $token): Media { $media = $workspace->addMediaFromPath( $path, $file->getClientOriginalName(), 'assets', mimeType: (string) $file->getMimeType(), ); $media->upload_token = $token; $media->save(); return $media; }); } catch (Throwable $e) { // Claim is only permanent after Media is stored — release so the // signed URL can be retried after a transient disk/storage failure. Cache::forget($cacheKey); throw $e; } return MediaUploadResource::make($media) ->response() ->setStatusCode(Response::HTTP_CREATED); } }