Upload images through the API, and from the MCP server
POST /api/v1/media stores an image where the panel keeps a page's and answers with the path to send as `featured_image`. Without it the MCP server could create a page but never give it a picture. The file goes through Media::for(), so it is processed, served and pruned like one uploaded in the panel. Two ways in, one per request: a multipart `file`, or JSON with a `url` the API downloads itself. JPEG, PNG, WebP, GIF or AVIF only, decided from the bytes; no SVG, which is a document that can carry script. The URL is fetched from inside the stack's network, so RemoteImage guards it: HTTPS only, every resolved address must be public, the connection is pinned to the checked address with CURLOPT_RESOLVE so a changing DNS answer cannot swap in a private one, redirects are not followed, and the body is read to a ceiling whatever its headers say. Uploading needs permission to write pages. MediaPolicy::create stays false, so the panel gains no free-standing "new file" button. In the MCP server a multipart binary field becomes `<field>_path` over stdio: the server reads the file from the caller's disk and sends it as multipart, instead of the model pushing base64 through its context. Over HTTP the server's disk is not the caller's, so only `body.url` is offered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,12 +8,19 @@
|
||||
* expired upstream costs one extra round trip, not a broken session.
|
||||
*/
|
||||
|
||||
import { readFile, stat } from 'node:fs/promises';
|
||||
import { basename } from 'node:path';
|
||||
|
||||
// Refresh this long before the stated expiry, so a token never expires between
|
||||
// the check and the request.
|
||||
const EXPIRY_MARGIN_MS = 60_000;
|
||||
|
||||
const TIMEOUT_MS = 30_000;
|
||||
|
||||
// Above the API's own 20 MB so its answer, not this one, is what a caller
|
||||
// usually sees; this only stops something absurd being read into memory.
|
||||
const MAX_UPLOAD_BYTES = 25 * 1024 * 1024;
|
||||
|
||||
export class LandingClient {
|
||||
/**
|
||||
* @param {object} options
|
||||
@@ -55,7 +62,7 @@ export class LandingClient {
|
||||
* @returns {Promise<{status: number, body: unknown}>}
|
||||
*/
|
||||
async call(operation, args = {}) {
|
||||
const request = this.buildRequest(operation, args);
|
||||
const request = await this.buildRequest(operation, args);
|
||||
|
||||
if (!operation.requiresAuth) {
|
||||
if (this.previewToken) request.headers['X-Preview-Token'] = this.previewToken;
|
||||
@@ -77,7 +84,7 @@ export class LandingClient {
|
||||
return response;
|
||||
}
|
||||
|
||||
buildRequest(operation, args) {
|
||||
async buildRequest(operation, args) {
|
||||
let path = operation.path;
|
||||
const query = new URLSearchParams();
|
||||
|
||||
@@ -100,7 +107,20 @@ export class LandingClient {
|
||||
const headers = { Accept: 'application/json' };
|
||||
let body;
|
||||
|
||||
if (operation.hasBody && args.body !== undefined) {
|
||||
const uploads = (operation.files ?? []).filter((field) => args[`${field}_path`]);
|
||||
|
||||
if (uploads.length > 0) {
|
||||
// No Content-Type: fetch writes it, boundary included, from the form.
|
||||
body = new FormData();
|
||||
|
||||
for (const [key, value] of Object.entries(args.body ?? {})) {
|
||||
body.append(key, typeof value === 'object' ? JSON.stringify(value) : String(value));
|
||||
}
|
||||
|
||||
for (const field of uploads) {
|
||||
body.append(field, ...(await this.readUpload(args[`${field}_path`])));
|
||||
}
|
||||
} else if (operation.hasBody && args.body !== undefined) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
body = JSON.stringify(args.body);
|
||||
}
|
||||
@@ -115,6 +135,22 @@ export class LandingClient {
|
||||
};
|
||||
}
|
||||
|
||||
/** @returns {Promise<[Blob, string]>} */
|
||||
async readUpload(path) {
|
||||
let info;
|
||||
|
||||
try {
|
||||
info = await stat(path);
|
||||
} catch {
|
||||
throw new Error(`Cannot read ${path}: no such file`);
|
||||
}
|
||||
|
||||
if (!info.isFile()) throw new Error(`${path} is not a file`);
|
||||
if (info.size > MAX_UPLOAD_BYTES) throw new Error(`${path} is ${Math.round(info.size / 1048576)} MB; the limit is 20 MB`);
|
||||
|
||||
return [new Blob([await readFile(path)]), basename(path)];
|
||||
}
|
||||
|
||||
async send({ url, method, headers, body }) {
|
||||
const response = await this.fetch(url, {
|
||||
method,
|
||||
|
||||
Reference in New Issue
Block a user