Package io.meeds.mcp.server.tool.util
Class UploadToolUtils
java.lang.Object
io.meeds.mcp.server.tool.util.UploadToolUtils
Helpers to turn an image (from a URL or base64) into a platform
UploadService uploadId that any upload-consuming API
(activity/comment attachments, note featured image, avatars, documents…)
accepts. This is the missing "materialize" side of UploadService,
which otherwise only registers/reads resources.
Fetching from a URL happens server-side, through the platform's
SafeHttpFetcher: only public http/https hosts are reached, and the
address is judged by the HTTP client's own resolver when the connection
opens, so the address checked is the address dialled — a name answering a
public address once and an internal one at the next lookup is refused at
the connection. No redirect is followed; the body is bounded by the caller's
limit, within the fetcher's own ceiling, and the whole read by a deadline.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA downloaded image: its bytes, resolved mime type and a file name.static final recordThe three mutually-exclusive ways to provide an image toresolveImage(org.exoplatform.social.attachment.AttachmentService, org.exoplatform.commons.file.services.FileService, org.exoplatform.services.security.Identity, io.meeds.mcp.server.tool.util.UploadToolUtils.ImageSource, long): a public http(s) URL, base64-encoded bytes, or a reference to an existing platform attachment (attachmentObjectType+attachmentObjectId). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long10 MB default ceiling for a fetched/decoded image.static final longLargest body the shared fetcher reads, whatever a caller asks: 15 MB, the largest limit a caller passes (the onlyoffice image tools), so that no caller is capped below its own limit. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]decodeBase64(String data) Decodes base64 data, a data URI prefix dropped and white space ignored.fetchImage(String url, long maxBytes) Downloads an image over http(s) through the platform's guarded fetcher.Downloads any file (not restricted to images) over http(s) through the platform's guarded fetcher.static Stringmaterialize(org.exoplatform.upload.UploadService uploadService, byte[] bytes, String fileName, String mimeType) Stages raw bytes to a temp file and registers anUploadResource, returning itsuploadId.static StringmaterializeFromUrlOrBase64(org.exoplatform.upload.UploadService uploadService, String imageUrl, String imageBase64, long maxBytes) Resolves an image from exactly one of an http(s) URL or a base64 string, stages it and registers it withUploadService.static voidRemoves the upload resource and its temp file.resolveImage(org.exoplatform.social.attachment.AttachmentService attachmentService, org.exoplatform.commons.file.services.FileService fileService, org.exoplatform.services.security.Identity aclIdentity, UploadToolUtils.ImageSource source, long maxBytes) Resolves an image from exactly one of three mutually exclusive sources — an http(s) URL, a base64 string, or an ACL-checked reference to a file already attached to a platform object (attachment_object_type+attachment_object_id) — into its raw bytes.
-
Field Details
-
DEFAULT_MAX_BYTES
public static final long DEFAULT_MAX_BYTES10 MB default ceiling for a fetched/decoded image.- See Also:
-
MAX_FETCH_BYTES
public static final long MAX_FETCH_BYTESLargest body the shared fetcher reads, whatever a caller asks: 15 MB, the largest limit a caller passes (the onlyoffice image tools), so that no caller is capped below its own limit. A request narrows the policy's limit, never widens it.- See Also:
-
-
Method Details
-
materializeFromUrlOrBase64
public static String materializeFromUrlOrBase64(org.exoplatform.upload.UploadService uploadService, String imageUrl, String imageBase64, long maxBytes) Resolves an image from exactly one of an http(s) URL or a base64 string, stages it and registers it withUploadService.- Parameters:
uploadService- the upload registryimageUrl- the public http(s) URL, or nullimageBase64- the base64 bytes, or nullmaxBytes- the most bytes accepted; a URL is read up to at mostMAX_FETCH_BYTES(15 MB), whatever this asks- Returns:
- the generated
uploadIdto pass to a consumer API
-
resolveImage
public static UploadToolUtils.FetchedContent resolveImage(org.exoplatform.social.attachment.AttachmentService attachmentService, org.exoplatform.commons.file.services.FileService fileService, org.exoplatform.services.security.Identity aclIdentity, UploadToolUtils.ImageSource source, long maxBytes) throws IllegalAccessException, org.exoplatform.commons.exception.ObjectNotFoundException Resolves an image from exactly one of three mutually exclusive sources — an http(s) URL, a base64 string, or an ACL-checked reference to a file already attached to a platform object (attachment_object_type+attachment_object_id) — into its raw bytes. Consumers that need anuploadIdpass the result tomaterialize(org.exoplatform.upload.UploadService, byte[], java.lang.String, java.lang.String); consumers that need raw bytes (avatars/banners) use it directly.The attachment reference is read as the given user via
AttachmentService.getAttachmentFileIds(String, String, Identity), so platform ACLs are enforced (an unreadable object throwsIllegalAccessException— no IDOR).- Parameters:
attachmentService- the attachments, read as the userfileService- the filesaclIdentity- the user the attachment is read assource- the one source of the imagemaxBytes- the most bytes accepted from a URL or base64 (a URL is read up to at mostMAX_FETCH_BYTES, 15 MB, whatever this asks); an attachment is not bounded here- Returns:
- the resolved image bytes, mime type and file name
- Throws:
IllegalAccessException- when the user may not read the attachmentorg.exoplatform.commons.exception.ObjectNotFoundException- when the attachment has no file
-
fetchImage
Downloads an image over http(s) through the platform's guarded fetcher.- Parameters:
url- the http(s) URLmaxBytes- the most bytes accepted; a URL is read up to at mostMAX_FETCH_BYTES(15 MB), whatever this asks- Returns:
- the image
-
fetchUrl
public static UploadToolUtils.FetchedContent fetchUrl(String url, long maxBytes, String defaultFileName) Downloads any file (not restricted to images) over http(s) through the platform's guarded fetcher. The mime type is resolved from theContent-Typeresponse header (falling back toapplication/octet-stream) and the file name is derived from the URL path (falling back todefaultFileName). Used by document / generic file upload tools.- Parameters:
url- the http(s) URL to downloadmaxBytes- the most bytes accepted; a URL is read up to at mostMAX_FETCH_BYTES(15 MB), whatever this asksdefaultFileName- a file name to use when the URL path has none- Returns:
- the downloaded bytes, resolved mime type and file name
-
materialize
public static String materialize(org.exoplatform.upload.UploadService uploadService, byte[] bytes, String fileName, String mimeType) Stages raw bytes to a temp file and registers anUploadResource, returning itsuploadId.- Parameters:
uploadService- the upload registrybytes- the contentfileName- the file name, the upload id when blankmimeType- the mime type- Returns:
- the upload id
-
release
Removes the upload resource and its temp file. Safe to call in a finally.- Parameters:
uploadService- the upload registryuploadId- the upload id, null for nothing to release
-
decodeBase64
Decodes base64 data, a data URI prefix dropped and white space ignored.- Parameters:
data- the base64 text- Returns:
- the bytes
-