Class UploadToolUtils

java.lang.Object
io.meeds.mcp.server.tool.util.UploadToolUtils

public final class UploadToolUtils extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static final record 
    A downloaded image: its bytes, resolved mime type and a file name.
    static final record 
    The mutually exclusive ways to provide an image to resolveImage(AttachmentService, FileService, Identity, UploadToolUtils.ImageSource, long): a public http(s) URL, base64-encoded bytes, a reference to an existing platform attachment (attachmentObjectType + attachmentObjectId), or the id of a file the user sent through an upload link (uploadId, see UploadService.createUploadToken(String, String, long)).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    10 MB default ceiling for a fetched/decoded image.
    static final long
    Largest 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.
    static final String
    The refusal of a call naming no image source, or more than one.
    static final String
    The refusal of an upload id this server holds for no upload of the user's.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    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.
    fetchUrl(String url, long maxBytes, String defaultFileName)
    Downloads any file (not restricted to images) over http(s) through the platform's guarded fetcher.
    static String
    materialize(org.exoplatform.upload.UploadService uploadService, byte[] bytes, String fileName, String mimeType)
    Stages raw bytes to a temp file and registers an UploadResource, returning its uploadId.
    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 with UploadService.
    static void
    release(org.exoplatform.upload.UploadService uploadService, String uploadId)
    Removes 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)
    Deprecated.
    use resolveImage(AttachmentService, FileService, UploadService, Identity, ImageSource, long), which also takes an upload id and bounds and checks an attachment like the other sources; since 7.3.0, not for removal while an add-on reads a non-image attachment through this one
    resolveImage(org.exoplatform.social.attachment.AttachmentService attachmentService, org.exoplatform.commons.file.services.FileService fileService, org.exoplatform.upload.UploadService uploadService, org.exoplatform.services.security.Identity aclIdentity, UploadToolUtils.ImageSource source, long maxBytes)
    Resolves an image from exactly one of four mutually exclusive sources into its raw bytes: an http(s) URL, a base64 string, an ACL-checked reference to a file already attached to a platform object (attachment_object_type + attachment_object_id), or the upload id of a file the user sent through an upload link (upload_id).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_MAX_BYTES

      public static final long DEFAULT_MAX_BYTES
      10 MB default ceiling for a fetched/decoded image.
      See Also:
    • MAX_FETCH_BYTES

      public static final long MAX_FETCH_BYTES
      Largest 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:
    • ONE_IMAGE_SOURCE_MESSAGE

      public static final String ONE_IMAGE_SOURCE_MESSAGE
      The refusal of a call naming no image source, or more than one.
      See Also:
    • UNKNOWN_UPLOAD_MESSAGE

      public static final String UNKNOWN_UPLOAD_MESSAGE
      The refusal of an upload id this server holds for no upload of the user's.
      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 with UploadService.
      Parameters:
      uploadService - the upload registry
      imageUrl - the public http(s) URL, or null
      imageBase64 - the base64 bytes, or null
      maxBytes - the most bytes accepted; a URL is read up to at most MAX_FETCH_BYTES (15 MB), whatever this asks
      Returns:
      the generated uploadId to pass to a consumer API
    • resolveImage

      @Deprecated(since="7.3.0", forRemoval=false) 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
      Deprecated.
      use resolveImage(AttachmentService, FileService, UploadService, Identity, ImageSource, long), which also takes an upload id and bounds and checks an attachment like the other sources; since 7.3.0, not for removal while an add-on reads a non-image attachment through this one
      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 an uploadId pass the result to materialize(UploadService, byte[], String, 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 throws IllegalAccessException — no IDOR). This overload neither bounds the attachment's size nor checks that it is an image, which the add-ons reading any attached file through it rely on; an upload id is refused.

      Parameters:
      attachmentService - the attachments, read as the user
      fileService - the files
      aclIdentity - the user the attachment is read as
      source - the one source of the image
      maxBytes - the most bytes accepted from a URL or base64 (a URL is read up to at most MAX_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 attachment
      org.exoplatform.commons.exception.ObjectNotFoundException - when the attachment has no file
    • resolveImage

      public static UploadToolUtils.FetchedContent resolveImage(org.exoplatform.social.attachment.AttachmentService attachmentService, org.exoplatform.commons.file.services.FileService fileService, org.exoplatform.upload.UploadService uploadService, 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 four mutually exclusive sources into its raw bytes: an http(s) URL, a base64 string, an ACL-checked reference to a file already attached to a platform object (attachment_object_type + attachment_object_id), or the upload id of a file the user sent through an upload link (upload_id). Every source is bounded by maxBytes and must hold a png, jpeg, gif or webp image, told from its bytes, never from a declared type. Consumers that need an uploadId pass the result to materialize(UploadService, byte[], String, String); consumers that need raw bytes (avatars/banners) use it directly.

      The attachment is read as the given user, so platform ACLs are enforced. The upload is read through the owner-checked UploadService.getUploadResource(String, String), so only the user who uploaded it can consume it, and it is removed with UploadService.removeUploadResource(String, String) once read, whether the read succeeds or not; an upload still in progress is refused and left in place.

      Parameters:
      attachmentService - the attachments, read as the user
      fileService - the files
      uploadService - the upload registry an upload id is read from
      aclIdentity - the user the attachment and the upload are read as
      source - the one source of the image
      maxBytes - the most bytes accepted from any source; a URL is read up to at most MAX_FETCH_BYTES (15 MB), whatever this asks
      Returns:
      the resolved image bytes, mime type and file name
      Throws:
      IllegalAccessException - when the user may not read the attachment
      org.exoplatform.commons.exception.ObjectNotFoundException - when the attachment has no file, or the upload id is unknown to this server for this user
    • fetchImage

      public static UploadToolUtils.FetchedContent fetchImage(String url, long maxBytes)
      Downloads an image over http(s) through the platform's guarded fetcher.
      Parameters:
      url - the http(s) URL
      maxBytes - the most bytes accepted; a URL is read up to at most MAX_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 the Content-Type response header (falling back to application/octet-stream) and the file name is derived from the URL path (falling back to defaultFileName). Used by document / generic file upload tools.
      Parameters:
      url - the http(s) URL to download
      maxBytes - the most bytes accepted; a URL is read up to at most MAX_FETCH_BYTES (15 MB), whatever this asks
      defaultFileName - 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 an UploadResource, returning its uploadId.
      Parameters:
      uploadService - the upload registry
      bytes - the content
      fileName - the file name, the upload id when blank
      mimeType - the mime type
      Returns:
      the upload id
    • release

      public static void release(org.exoplatform.upload.UploadService uploadService, String uploadId)
      Removes the upload resource and its temp file. Safe to call in a finally.
      Parameters:
      uploadService - the upload registry
      uploadId - the upload id, null for nothing to release
    • decodeBase64

      public static byte[] decodeBase64(String data)
      Decodes base64 data, a data URI prefix dropped and white space ignored.
      Parameters:
      data - the base64 text
      Returns:
      the bytes