Class SafeImageService

java.lang.Object
cloud.opencode.base.image.security.SafeImageService
All Implemented Interfaces:
AutoCloseable

public class SafeImageService extends Object implements AutoCloseable
Safe Image Service 安全图片服务

Thread-safe Raster-based image processing service with security controls (size validation, timeout, concurrency limits).

带安全控制(大小验证、超时、并发限制)的线程安全 Raster 图像处理服务。

Features | 特性:

  • Size validation | 大小验证
  • Timeout control | 超时控制
  • Concurrency limits | 并发限制

Usage Examples | 使用示例:

SafeImageService service = SafeImageService.builder()
    .maxFileSize(5_000_000)
    .maxWidth(4000)
    .maxHeight(4000)
    .timeout(Duration.ofSeconds(30))
    .maxConcurrent(10)
    .build();

Raster raster = service.read(path);
Raster resized = service.process(raster, r -> ResizeOp.resize(r, 800, 600));
Since:
JDK 25, opencode-base-image V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • SafeImageService

      public SafeImageService(long maxFileSize, int maxWidth, int maxHeight, Duration timeout, int maxConcurrent)
      Create safe image service 创建安全图片服务
      Parameters:
      maxFileSize - maximum file size in bytes | 最大文件大小(字节)
      maxWidth - maximum width | 最大宽度
      maxHeight - maximum height | 最大高度
      timeout - operation timeout | 操作超时时间
      maxConcurrent - maximum concurrent operations | 最大并发操作数
  • Method Details

    • builder

      public static SafeImageService.Builder builder()
      Create builder 创建构建器
      Returns:
      the builder | 构建器
    • createDefault

      public static SafeImageService createDefault()
      Create with default settings 使用默认设置创建
      Returns:
      the service | 服务
    • read

      public Raster read(Path path) throws ImageException
      Read raster safely from path 从路径安全读取 raster
      Parameters:
      path - the file path | 文件路径
      Returns:
      the raster | raster
      Throws:
      ImageException - if reading or validation fails | 如果读取或验证失败
    • read

      public Raster read(byte[] bytes) throws ImageException
      Read raster safely from bytes 从字节数组安全读取 raster
      Parameters:
      bytes - the image bytes | 图片字节数组
      Returns:
      the raster | raster
      Throws:
      ImageException - if reading or validation fails | 如果读取或验证失败
    • process

      public Raster process(Raster raster, ImageOperation operation) throws ImageException
      Process raster with operation under timeout / concurrency controls. 在超时与并发控制下处理 raster。
      Parameters:
      raster - the input raster | 输入 raster
      operation - the operation | 操作
      Returns:
      the processed raster | 处理后的 raster
      Throws:
      ImageException - if processing fails | 如果处理失败
      Since:
      opencode-base-image V1.0.4
    • save

      public void save(Raster raster, Path path, Path baseDir) throws ImageException
      Save raster safely 安全保存 raster
      Parameters:
      raster - the raster | raster
      path - the output path | 输出路径
      baseDir - the base directory for validation | 用于验证的基础目录
      Throws:
      ImageException - if saving fails | 如果保存失败
    • save

      public void save(Raster raster, Path path, ImageFormat format, Path baseDir) throws ImageException
      Save raster safely with explicit format 以指定格式安全保存 raster
      Parameters:
      raster - the raster | raster
      path - the output path | 输出路径
      format - the image format | 图片格式
      baseDir - the base directory for validation | 用于验证的基础目录
      Throws:
      ImageException - if saving fails | 如果保存失败
    • getInfo

      public ImageInfo getInfo(Path path) throws ImageException
      Get image info safely 安全获取图片信息
      Parameters:
      path - the file path | 文件路径
      Returns:
      the image info | 图片信息
      Throws:
      ImageException - if reading fails | 如果读取失败
    • getActiveCount

      public int getActiveCount()
      Get active operation count 获取活动操作数
      Returns:
      the count | 数量
    • getAvailablePermits

      public int getAvailablePermits()
      Get available permits 获取可用许可数
      Returns:
      the available permits | 可用许可数
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable