|
|
|
@ -16,9 +16,12 @@ |
|
|
|
package org.thingsboard.server.dao.resource; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.lang3.tuple.Pair; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.common.util.RegexUtils; |
|
|
|
import org.thingsboard.server.common.data.ImageDescriptor; |
|
|
|
import org.thingsboard.server.common.data.ResourceType; |
|
|
|
import org.thingsboard.server.common.data.TbResource; |
|
|
|
@ -32,7 +35,9 @@ import org.thingsboard.server.dao.service.validator.ResourceDataValidator; |
|
|
|
import org.thingsboard.server.dao.util.ImageUtils; |
|
|
|
import org.thingsboard.server.dao.util.ImageUtils.ProcessedImage; |
|
|
|
|
|
|
|
import java.util.Objects; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
@ -42,17 +47,22 @@ public class BaseImageService extends BaseResourceService implements ImageServic |
|
|
|
super(resourceDao, resourceInfoDao, resourceValidator); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
@Override |
|
|
|
public TbResourceInfo saveImage(TbResource image) throws Exception { |
|
|
|
if (image.getId() == null) { |
|
|
|
image.setResourceKey(getUniqueKey(image.getTenantId(), image.getFileName())); |
|
|
|
} |
|
|
|
resourceValidator.validate(image, TbResourceInfo::getTenantId); |
|
|
|
|
|
|
|
ImageDescriptor descriptor = image.getDescriptor(ImageDescriptor.class); |
|
|
|
Pair<ImageDescriptor, byte[]> result = processImage(image.getData(), descriptor); |
|
|
|
image.setDescriptor(JacksonUtil.valueToTree(result.getLeft())); |
|
|
|
descriptor = result.getLeft(); |
|
|
|
image.setEtag(descriptor.getEtag()); |
|
|
|
image.setDescriptor(JacksonUtil.valueToTree(descriptor)); |
|
|
|
image.setPreview(result.getRight()); |
|
|
|
|
|
|
|
image = saveResource(image, false); |
|
|
|
return new TbResourceInfo(image); |
|
|
|
return new TbResourceInfo(doSaveResource(image)); |
|
|
|
} |
|
|
|
|
|
|
|
private Pair<ImageDescriptor, byte[]> processImage(byte[] data, ImageDescriptor descriptor) throws Exception { |
|
|
|
@ -62,17 +72,42 @@ public class BaseImageService extends BaseResourceService implements ImageServic |
|
|
|
descriptor.setWidth(image.getWidth()); |
|
|
|
descriptor.setHeight(image.getHeight()); |
|
|
|
descriptor.setSize(image.getSize()); |
|
|
|
descriptor.setEtag(calculateEtag(data)); |
|
|
|
|
|
|
|
ImageDescriptor previewDescriptor = new ImageDescriptor(); |
|
|
|
previewDescriptor.setWidth(preview.getWidth()); |
|
|
|
previewDescriptor.setHeight(preview.getHeight()); |
|
|
|
previewDescriptor.setMediaType(preview.getMediaType()); |
|
|
|
previewDescriptor.setSize(preview.getSize()); |
|
|
|
previewDescriptor.setEtag(preview.getData() != null ? calculateEtag(preview.getData()) : descriptor.getEtag()); |
|
|
|
descriptor.setPreviewDescriptor(previewDescriptor); |
|
|
|
|
|
|
|
return Pair.of(descriptor, preview.getData()); |
|
|
|
} |
|
|
|
|
|
|
|
private String getUniqueKey(TenantId tenantId, String filename) { |
|
|
|
if (!resourceInfoDao.existsByTenantIdAndResourceTypeAndResourceKey(tenantId, ResourceType.IMAGE, filename)) { |
|
|
|
return filename; |
|
|
|
} |
|
|
|
|
|
|
|
String basename = StringUtils.substringBeforeLast(filename, "."); |
|
|
|
String extension = StringUtils.substringAfterLast(filename, "."); |
|
|
|
|
|
|
|
Pattern similarImagesPattern = Pattern.compile( |
|
|
|
Pattern.quote(basename) + "_(\\d+)\\.?" + Pattern.quote(extension) |
|
|
|
); |
|
|
|
int maxImageIdx = resourceInfoDao.findKeysByTenantIdAndResourceTypeAndResourceKeyStartingWith( |
|
|
|
tenantId, ResourceType.IMAGE, basename + "_").stream() |
|
|
|
.map(key -> RegexUtils.getMatch(key, similarImagesPattern, 1)) |
|
|
|
.filter(Objects::nonNull).mapToInt(Integer::parseInt) |
|
|
|
.max().orElse(0); |
|
|
|
String uniqueKey = basename + "_" + (maxImageIdx + 1); |
|
|
|
if (!extension.isEmpty()) { |
|
|
|
uniqueKey += "." + extension; |
|
|
|
} |
|
|
|
return uniqueKey; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public TbResourceInfo saveImageInfo(TbResourceInfo imageInfo) { |
|
|
|
return saveResource(new TbResource(imageInfo)); |
|
|
|
|