Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
- Terminal commands shown in the control panel and console output for the current environment now use `ddev craft` and `ddev composer` when Craft is running in DDEV, including the Plugin Store’s installation instructions and the OAuth signing key instructions on the **Settings** → **MCP** and **My Account** → **Tokens** screens. The Plugin Store no longer shows separate “Shell” and “DDEV” tabs. ([#20487](https://github.com/craftcms/cms/pull/20487))
- Commands suggested by the `entrify/*`, `entry-types/merge`, `fields/merge`, `fields/auto-merge`, `setup`, `setup/welcome`, and `update` console commands now use `ddev craft` when Craft is running in DDEV, and `php craft` otherwise. ([#20702](https://github.com/craftcms/cms/pull/20702))

### Assets

- Assets can now be uploaded straight to filesystems that accept direct uploads, such as Craft Cloud’s, so files don’t pass through Craft’s request handling or its request size limits. Uploads are started with the `uploads/start` action, sent to the returned URL, and completed with `uploads/complete`.
- The `assets_upload_url` MCP tool now accepts an optional `size` argument, and returns `headers` that must be sent with the upload. Its upload URLs send files straight to the temporary asset upload filesystem when it accepts direct uploads.
- Asset indexes, Assets fields and the “Replace file” action upload files this way for volumes whose filesystem implements `craft\base\TemporaryUploadUrlInterface`.
- Front-end forms can upload files the same way by registering the `craft\web\assets\uploads\UploadsAsset` asset bundle and adding a `data-upload-session` attribute to file inputs. Chosen files are uploaded right away and replaced with hidden inputs holding the new asset IDs.
- Guests can upload assets when a `craft\services\Uploads::EVENT_AUTHORIZE_UPLOAD` event handler allows them. Guests can start 60 uploads per minute per IP address, unless the `defaultRateLimit` config setting is disabled.

### Development

- Added the `craftCommand()` Twig function. ([#20487](https://github.com/craftcms/cms/pull/20487))
Expand All @@ -23,6 +31,19 @@
- Added `craft\helpers\App::craftCommand()`. ([#20487](https://github.com/craftcms/cms/pull/20487))
- Added `craft\helpers\App::isDdev()`. ([#20487](https://github.com/craftcms/cms/pull/20487))
- Added `craft\helpers\ElementHelper::getProvisionalDraftUser()`. ([#20587](https://github.com/craftcms/cms/issues/20587))
- Added `craft\base\ApplicationTrait::getUploads()`.
- Added `craft\base\TemporaryUploadUrlInterface`.
- Added `craft\controllers\UploadsController`.
- Added `craft\events\AuthorizeUploadEvent`.
- Added `craft\services\Uploads`.
- Added `craft\uploads\AssetUploadHandler`.
- Added `craft\uploads\StagedUpload`.
- Added `craft\uploads\TemporaryUploadHandler`.
- Added `craft\uploads\UploadHandlerInterface`.
- Added `craft\web\assets\uploads\UploadsAsset`.
- Added `Craft.UploadSessionUploader` and `Craft.Uploads.FileUpload`.
- `Craft.createUploader()` now returns a `Craft.UploadSessionUploader` for filesystems that implement `craft\base\TemporaryUploadUrlInterface`.
- Removed the `mcp/upload` action. MCP upload URLs now use `uploads/transfer`.

### Performance

Expand Down
5 changes: 1 addition & 4 deletions src/assetpreviews/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Craft;
use craft\base\AssetPreviewHandler;
use craft\helpers\FileHelper;
use craft\helpers\Html;

/**
Expand All @@ -25,9 +24,7 @@ class Text extends AssetPreviewHandler
*/
public function getPreviewHtml(array $variables = []): string
{
$localCopy = $this->asset->getCopyOfFile();
$contents = Html::encode(file_get_contents($localCopy));
FileHelper::unlink($localCopy);
$contents = Html::encode($this->asset->getContents());

return Craft::$app->getView()->renderTemplate('assets/_previews/text.twig',
array_merge([
Expand Down
13 changes: 13 additions & 0 deletions src/base/ApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
use craft\services\TemplateCaches;
use craft\services\Tokens;
use craft\services\Updates;
use craft\services\Uploads;
use craft\services\UserGroups;
use craft\services\UserPermissions;
use craft\services\Users;
Expand Down Expand Up @@ -193,6 +194,7 @@
* @property-read TemplateCaches $templateCaches The template caches service
* @property-read Tokens $tokens The tokens service
* @property-read Updates $updates The updates service
* @property-read Uploads $uploads The uploads service
* @property-read UrlManager $urlManager The URL manager for this application
* @property-read UserGroups $userGroups The user groups service
* @property-read UserPermissions $userPermissions The user permissions service
Expand Down Expand Up @@ -1544,6 +1546,17 @@ public function getUpdates(): Updates
return $this->get('updates');
}

/**
* Returns the uploads service.
*
* @return Uploads The uploads service
* @since 5.13.0
*/
public function getUploads(): Uploads
{
return $this->get('uploads');
}

/**
* Returns the user groups service.
*
Expand Down
37 changes: 37 additions & 0 deletions src/base/TemporaryUploadUrlInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\base;

use DateTimeInterface;

/**
* TemporaryUploadUrlInterface defines a filesystem that can accept files directly from clients, such as through a
* presigned S3 URL, so uploads don’t pass through Craft’s request handling.
*
* The method mirrors Laravel’s `AwsS3V3Adapter::temporaryUploadUrl()`.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 5.13.0
*/
interface TemporaryUploadUrlInterface
{
/**
* Returns a URL that stores the body of a `PUT` request at a path, and the headers the request must include.
*
* The file must appear at the path all at once, when the request completes. The caller removes files that are
* never used.
*
* @param string $path The path, relative to the filesystem root.
* @param DateTimeInterface $expiresAt When the URL stops accepting requests.
* @param array{ContentType?: string, ContentLength?: int} $options Values the URL must be signed for, so that
* requests with other values are rejected.
* @return array{url: string, headers: array<string, string>}|null The URL and every header it was signed with
* (other than `Host`), or `null` if the filesystem can’t accept direct uploads right now.
*/
public function temporaryUploadUrl(string $path, DateTimeInterface $expiresAt, array $options = []): ?array;
}
5 changes: 4 additions & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '5.12.1',
'schemaVersion' => '5.13.0.0',
'schemaVersion' => '5.13.0.1',
'minVersionRequired' => '4.5.0',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down Expand Up @@ -150,6 +150,9 @@
'updates' => [
'class' => craft\services\Updates::class,
],
'uploads' => [
'class' => craft\services\Uploads::class,
],
'urlManager' => [
'class' => craft\web\UrlManager::class,
'enablePrettyUrl' => true,
Expand Down
43 changes: 3 additions & 40 deletions src/controllers/McpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use craft\filters\IpRateLimitIdentity;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\mcp\AssetUploads;
use craft\mcp\HttpTransportMiddleware;
use craft\mcp\ServerFactory;
use craft\web\Controller;
Expand All @@ -16,7 +15,6 @@
use Mcp\Server;
use Mcp\Server\Transport\StreamableHttpTransport;
use yii\filters\RateLimiter;
use yii\web\HttpException;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
Expand Down Expand Up @@ -52,8 +50,8 @@ public function beforeAction($action): bool
}

// The public server and discovery only live on the front end, as with their routes in Settings::requestRoute(),
// and not on the control panel’s own host. Upload URLs use the authenticated server’s endpoint, wherever it is.
if (!in_array($action->id, ['authenticated', 'upload'], true) && ($this->request->getIsCpRequest() || $settings->isCpHostRequest())) {
// and not on the control panel’s own host.
if ($action->id !== 'authenticated' && ($this->request->getIsCpRequest() || $settings->isCpHostRequest())) {
throw new NotFoundHttpException();
}

Expand Down Expand Up @@ -82,16 +80,10 @@ public function beforeAction($action): bool
// Authenticated users go through the same checks as any other signed-in request, including control
// panel access for control panel requests.
$this->allowAnonymous = self::ALLOW_ANONYMOUS_NEVER;
} elseif ($action->id === 'public' || $action->id === 'upload') {
} elseif ($action->id === 'public') {
Craft::$app->getUser()->enableSession = false;
Craft::$app->getUser()->enableAutoLogin = false;
Craft::$app->getUser()->setIdentity(null);

// The single-use token authorizes uploads, which the user requested via the authenticated server,
// so they work while the system is offline too
if ($action->id === 'upload') {
$this->allowAnonymous = self::ALLOW_ANONYMOUS_LIVE | self::ALLOW_ANONYMOUS_OFFLINE;
}
}

return parent::beforeAction($action);
Expand Down Expand Up @@ -121,35 +113,6 @@ public function actionPublic(): Response
return $this->handle($this->request->getIsOptions() ? $servers->preflight() : $servers->public());
}

/**
* Receives a file for an upload URL created by the `assets_upload_url` tool. The token in the URL
* authorizes the request, and each upload accepts a single file.
*/
public function actionUpload(string $uploadId): Response
{
try {
$this->requireToken();

if (!$this->request->getIsPut() && !$this->request->getIsPost()) {
throw new MethodNotAllowedHttpException('Send the file as the body of a PUT request.');
}

$stream = fopen('php://input', 'rb');

try {
$size = Craft::$app->get('mcp')->getCapabilityContainer()->get(AssetUploads::class)->receiveUpload($uploadId, $stream);
} finally {
fclose($stream);
}
} catch (HttpException $exception) {
// Upload clients such as curl don't ask for JSON, so the error handler would render an HTML page
$this->response->setStatusCode($exception->statusCode);
return $this->asJson(['error' => $exception->getMessage()]);
}

return $this->asJson(['upload_id' => $uploadId, 'size' => $size]);
}

/** Lists the discoverable servers in an AI Catalog, for `/.well-known/ai-catalog.json`, per SEP-2127. */
public function actionCatalog(): Response
{
Expand Down
Loading
Loading