@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
    @PostMapping(value = "/api/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public TbResourceInfo uploadImage(@RequestPart MultipartFile file,
                                      @RequestPart(required = false) String title,
                                      @RequestPart(required = false) String imageSubType) throws Exception {
        SecurityUser user = getCurrentUser();
        TbResource image = new TbResource();
        image.setTenantId(user.getTenantId());
        accessControlService.checkPermission(user, Resource.TB_RESOURCE, Operation.CREATE, null, image);
        resourceValidator.validateResourceSize(user.getTenantId(), null, file.getSize());

        image.setFileName(file.getOriginalFilename());
        if (StringUtils.isNotEmpty(title)) {
            image.setTitle(title);
        } else {
            image.setTitle(file.getOriginalFilename());
        }

        ResourceSubType subType = ResourceSubType.IMAGE;
        if (StringUtils.isNotEmpty(imageSubType)) {
            subType = ResourceSubType.valueOf(imageSubType);
        }

        image.setResourceType(ResourceType.IMAGE);
        image.setResourceSubType(subType);
        ImageDescriptor descriptor = new ImageDescriptor();
        descriptor.setMediaType(file.getContentType());
        image.setDescriptorValue(descriptor);
        image.setData(file.getBytes());
        image.setPublic(true);
        return tbImageService.save(image, user);
    }
@Test
    public void testUploadImageWithSameFilename() throws Exception {
        String filename = "my_jpeg_image.jpg";
        TbResourceInfo imageInfo1 = uploadImage(HttpMethod.POST, "/api/image", filename, "image/jpeg", JPEG_IMAGE);
        assertThat(imageInfo1.getTitle()).isEqualTo(filename);
        assertThat(imageInfo1.getFileName()).isEqualTo(filename);
        assertThat(imageInfo1.getResourceKey()).isEqualTo(filename);

        TbResourceInfo imageInfo2 = uploadImage(HttpMethod.POST, "/api/image", filename, "image/jpeg", JPEG_IMAGE);
        assertThat(imageInfo2.getTitle()).isEqualTo(filename);
        assertThat(imageInfo2.getFileName()).isEqualTo(filename);
        assertThat(imageInfo2.getResourceKey()).isEqualTo("my_jpeg_image_(1).jpg");

        TbResourceInfo imageInfo3 = uploadImage(HttpMethod.POST, "/api/image", filename, "image/jpeg", JPEG_IMAGE);
        assertThat(imageInfo3.getTitle()).isEqualTo(filename);
        assertThat(imageInfo3.getFileName()).isEqualTo(filename);
        assertThat(imageInfo3.getResourceKey()).isEqualTo("my_jpeg_image_(2).jpg");
    }