Files
outline/app/utils/files.ts
T
Yinhao HuangandClaude Opus 4.8 6286d08171 feat: support presigned PUT uploads for S3-compatible storage (#12748)
* feat: support presigned PUT uploads for S3-compatible storage

Add `AWS_S3_UPLOAD_METHOD` config ("post" default / "put") so providers
that do not implement presigned POST (e.g. Cloudflare R2) can use
presigned PUT instead.

- BaseStorage.getPresignedPut() returns undefined by default; S3Storage
  overrides with PutObjectCommand + getSignedUrl.
- Content-Length is signed into the presigned PUT URL so S3 rejects
  uploads that do not match the declared size.
- API route and MCP tool branch on the config: only one upload method
  is returned per request.
- Frontend uses existing presignedPutUrl presence check — unchanged.
- When put is configured but unsupported (e.g. LocalStorage), a clear
  InvalidRequestError is returned.

Defaults to "post" — zero behavior change for existing deployments.

* refactor: return explicit upload mode instead of inferring from fields

Address review feedback from @tommoor: the client should not need to
infer the upload method from the presence of presigned-PUT-specific
fields. The API now returns an explicit `mode` ("put" | "post"), and the
PUT branch uses generic `url` / `headers` fields so the client has no
special knowledge of "presigned puts".

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* refactor: use this.getBucket() in presigned post/put methods

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 21:05:16 -04:00

210 lines
6.2 KiB
TypeScript

import invariant from "invariant";
import { AttachmentPreset } from "@shared/types";
import { dataUrlToBlob } from "@shared/utils/files";
import { client } from "./ApiClient";
import Logger from "./Logger";
type UploadOptions = {
/** The user generated ID of the file */
id?: string;
/** The user facing name of the file */
name?: string;
/** The document that this file was uploaded in, if any */
documentId?: string;
/** The preset to use for attachment configuration */
preset: AttachmentPreset;
/** Callback will be passed a number between 0-1 as upload progresses */
onProgress?: (fractionComplete: number) => void;
};
/**
* Upload a file from a URL
*
* @param url The remote URL to download the file from
* @param options The upload options
* @returns The attachment object
*/
export const uploadFileFromUrl = async (
url: string,
options: UploadOptions
) => {
const response = await client.post("/attachments.createFromUrl", {
documentId: options.documentId,
url,
id: options.id,
});
return response.data;
};
/**
* Upload a file
*
* @param file The file to upload
* @param options The upload options
* @returns The attachment object
*/
export const uploadFile = async (
file: File | Blob,
options: UploadOptions = {
name: "",
preset: AttachmentPreset.DocumentAttachment,
}
) => {
const name = file instanceof File ? file.name : options.name;
const response = await client.post("/attachments.create", {
preset: options.preset,
documentId: options.documentId,
contentType: file.type,
size: file.size,
name,
id: options.id,
});
invariant(response, "Response should be available");
const data = response.data;
const attachment = data.attachment;
const usePut = data.mode === "put";
// Using XMLHttpRequest instead of fetch because fetch doesn't support progress
const xhr = new XMLHttpRequest();
const success = await new Promise((resolve) => {
xhr.upload.addEventListener("progress", (event) => {
if (event.lengthComputable && options.onProgress) {
options.onProgress(event.loaded / event.total);
}
});
xhr.addEventListener("error", () => {
const extra = {
status: xhr.status,
statusText: xhr.statusText,
contentType: file.type,
size: file.size,
};
if (xhr.status === 0) {
Logger.warn("File upload failed before response", extra);
return;
}
Logger.error(
"File upload failed",
new Error(`${xhr.status} ${xhr.statusText}`),
extra
);
});
xhr.addEventListener("loadend", () => {
resolve(xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 400);
});
if (usePut) {
xhr.open("PUT", data.url, true);
if (data.headers) {
for (const [key, value] of Object.entries(
data.headers as Record<string, string>
)) {
xhr.setRequestHeader(key, value);
}
}
// @ts-expect-error ts-migrate(2339) FIXME: Property 'blob' does not exist on type 'File | Blo...
xhr.send(file.blob ? file.file : file);
} else {
const formData = new FormData();
for (const key in data.form) {
formData.append(key, data.form[key]);
}
// @ts-expect-error ts-migrate(2339) FIXME: Property 'blob' does not exist on type 'File | Blo...
if (file.blob) {
// @ts-expect-error ts-migrate(2339) FIXME: Property 'file' does not exist on type 'File | Blo...
formData.append("file", file.file);
} else {
formData.append("file", file);
}
// Do not send credentials if uploading to a different origin, as the
// combination of CORS and cookies will cause preflight request failure.
// However S3-like storage on the same host can work with credentials.
if (data.uploadUrl.startsWith("/")) {
xhr.withCredentials = true;
} else {
const parsed = new URL(data.uploadUrl);
const requiresPreflightRequest =
parsed.origin !== window.location.origin;
xhr.withCredentials = !requiresPreflightRequest;
}
xhr.open("POST", data.uploadUrl, true);
xhr.send(formData);
}
});
if (!success) {
throw new Error("Upload failed");
}
return attachment;
};
export { dataUrlToBlob };
const CHAR_FORWARD_SLASH = 47; /* / */
const CHAR_DOT = 46; /* . */
// Based on the NodeJS Library https://github.com/nodejs/node/blob/896b75a4da58a7283d551c4595e0aa454baca3e0/lib/path.js
// Copyright Joyent, Inc. and other Node contributors.
export const extname = (path: string) => {
if (typeof path !== "string") {
throw new TypeError(
`The "path" argument must be of type string. Received type ${typeof path}`
);
}
let startDot = -1;
let startPart = 0;
let end = -1;
let matchedSlash = true;
// Track the state of characters (if any) we see before our first dot and
// after any path separator we find
let preDotState = 0;
for (let i = path.length - 1; i >= 0; --i) {
const code = path.charCodeAt(i);
if (code === CHAR_FORWARD_SLASH) {
// If we reached a path separator that was not part of a set of path
// separators at the end of the string, stop now
if (!matchedSlash) {
startPart = i + 1;
break;
}
continue;
}
if (end === -1) {
// We saw the first non-path separator, mark this as the end of our
// extension
matchedSlash = false;
end = i + 1;
}
if (code === CHAR_DOT) {
// If this is our first dot, mark it as the start of our extension
if (startDot === -1) {
startDot = i;
} else if (preDotState !== 1) {
preDotState = 1;
}
} else if (startDot !== -1) {
// We saw a non-dot and non-path separator before our dot, so we should
// have a good chance at having a non-empty extension
preDotState = -1;
}
}
if (
startDot === -1 ||
end === -1 ||
// We saw a non-dot character immediately before the dot
preDotState === 0 ||
// The (right-most) trimmed path component is exactly '..'
(preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
) {
return "";
}
return path.slice(startDot, end);
};