Browse Source

Refactor resources validation

pull/9753/head
ViacheslavKlimov 3 years ago
parent
commit
605e9987fb
  1. 6
      application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java
  2. 2
      dao/src/main/java/org/thingsboard/server/dao/resource/TbResourceDao.java
  3. 2
      dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java
  4. 7
      dao/src/main/java/org/thingsboard/server/dao/service/validator/ResourceDataValidator.java
  5. 5
      dao/src/main/java/org/thingsboard/server/dao/sql/resource/JpaTbResourceDao.java
  6. 3
      dao/src/main/java/org/thingsboard/server/dao/sql/resource/TbResourceRepository.java

6
application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java

@ -53,6 +53,9 @@ public class DefaultTbResourceService extends AbstractTbEntityService implements
@Override
public TbResource save(TbResource resource, User user) throws ThingsboardException {
if (resource.getResourceType() == ResourceType.IMAGE) {
throw new IllegalArgumentException("Image resource type is not supported");
}
ActionType actionType = resource.getId() == null ? ActionType.ADDED : ActionType.UPDATED;
TenantId tenantId = resource.getTenantId();
try {
@ -74,6 +77,9 @@ public class DefaultTbResourceService extends AbstractTbEntityService implements
@Override
public void delete(TbResource tbResource, User user) {
if (tbResource.getResourceType() == ResourceType.IMAGE) {
throw new IllegalArgumentException("Image resource type is not supported");
}
TbResourceId resourceId = tbResource.getId();
TenantId tenantId = tbResource.getTenantId();
try {

2
dao/src/main/java/org/thingsboard/server/dao/resource/TbResourceDao.java

@ -46,4 +46,6 @@ public interface TbResourceDao extends Dao<TbResource>, TenantEntityWithDataDao,
byte[] getResourcePreview(TenantId tenantId, TbResourceId resourceId);
long getResourceSize(TenantId tenantId, TbResourceId resourceId);
}

2
dao/src/main/java/org/thingsboard/server/dao/service/DataValidator.java

@ -161,7 +161,7 @@ public abstract class DataValidator<D extends BaseData<?>> {
}
static void validateQueueNameOrTopic(String value, String fieldName) {
if (StringUtils.isEmpty(value) || value.trim().length() == 0 ) {
if (StringUtils.isEmpty(value) || value.trim().length() == 0) {
throw new DataValidationException(String.format("Queue %s should be specified!", fieldName));
}
if (!QUEUE_PATTERN.matcher(value).matches()) {

7
dao/src/main/java/org/thingsboard/server/dao/service/validator/ResourceDataValidator.java

@ -90,7 +90,12 @@ public class ResourceDataValidator extends DataValidator<TbResource> {
throw new IllegalArgumentException("Resource exceeds the maximum size of " + maxResourceSize + " bytes");
}
long maxSumResourcesDataInBytes = profileConfiguration.getMaxResourcesInBytes();
validateMaxSumDataSizePerTenant(tenantId, resourceDao, maxSumResourcesDataInBytes, resource.getData().length, TB_RESOURCE);
int dataSize = resource.getData().length;
if (resource.getId() != null) {
long prevSize = resourceDao.getResourceSize(tenantId, resource.getId());
dataSize -= prevSize;
}
validateMaxSumDataSizePerTenant(tenantId, resourceDao, maxSumResourcesDataInBytes, dataSize, TB_RESOURCE);
}
if (StringUtils.isEmpty(resource.getFileName())) {
throw new DataValidationException("Resource file name should be specified!");

5
dao/src/main/java/org/thingsboard/server/dao/sql/resource/JpaTbResourceDao.java

@ -104,6 +104,11 @@ public class JpaTbResourceDao extends JpaAbstractDao<TbResourceEntity, TbResourc
return resourceRepository.getPreviewById(resourceId.getId());
}
@Override
public long getResourceSize(TenantId tenantId, TbResourceId resourceId) {
return resourceRepository.getDataSizeById(resourceId.getId());
}
@Override
public Long sumDataSizeByTenantId(TenantId tenantId) {
return resourceRepository.sumDataSizeByTenantId(tenantId.getId());

3
dao/src/main/java/org/thingsboard/server/dao/sql/resource/TbResourceRepository.java

@ -88,6 +88,9 @@ public interface TbResourceRepository extends JpaRepository<TbResourceEntity, UU
@Query(value = "SELECT COALESCE(preview, data) FROM resource WHERE id = :id", nativeQuery = true)
byte[] getPreviewById(@Param("id") UUID id);
@Query(value = "SELECT length(r.data) FROM resource r WHERE r.id = :id", nativeQuery = true)
long getDataSizeById(@Param("id") UUID id);
@Query("SELECT externalId FROM TbResourceInfoEntity WHERE id = :id")
UUID getExternalIdByInternal(@Param("id") UUID internalId);

Loading…
Cancel
Save