Browse Source

Rename to publicResourceKey

pull/9846/head
ViacheslavKlimov 3 years ago
parent
commit
a058f865da
  1. 8
      application/src/main/data/upgrade/3.6.1/schema_update.sql
  2. 4
      application/src/main/java/org/thingsboard/server/controller/ImageController.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/queue/DefaultTbCoreConsumerService.java
  4. 8
      application/src/main/java/org/thingsboard/server/service/resource/DefaultTbImageService.java
  5. 2
      common/data/src/main/java/org/thingsboard/server/common/data/ImageExportData.java
  6. 6
      common/data/src/main/java/org/thingsboard/server/common/data/TbResourceInfo.java
  7. 2
      common/proto/src/main/proto/queue.proto
  8. 2
      dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java
  9. 10
      dao/src/main/java/org/thingsboard/server/dao/model/sql/TbResourceEntity.java
  10. 10
      dao/src/main/java/org/thingsboard/server/dao/model/sql/TbResourceInfoEntity.java
  11. 12
      dao/src/main/java/org/thingsboard/server/dao/resource/BaseImageService.java
  12. 4
      dao/src/main/java/org/thingsboard/server/dao/resource/ImageCacheKey.java
  13. 4
      dao/src/main/java/org/thingsboard/server/dao/resource/TbResourceInfoDao.java
  14. 8
      dao/src/main/java/org/thingsboard/server/dao/sql/resource/JpaTbResourceInfoDao.java
  15. 4
      dao/src/main/java/org/thingsboard/server/dao/sql/resource/TbResourceInfoRepository.java
  16. 2
      dao/src/main/resources/sql/schema-entities-idx.sql
  17. 2
      dao/src/main/resources/sql/schema-entities.sql

8
application/src/main/data/upgrade/3.6.1/schema_update.sql

@ -20,10 +20,10 @@ ALTER TABLE resource ADD COLUMN IF NOT EXISTS descriptor varchar;
ALTER TABLE resource ADD COLUMN IF NOT EXISTS preview bytea;
ALTER TABLE resource ADD COLUMN IF NOT EXISTS external_id uuid;
ALTER TABLE resource ADD COLUMN IF NOT EXISTS is_public boolean default true;
ALTER TABLE resource ADD COLUMN IF NOT EXISTS public_key varchar(35) unique;
ALTER TABLE resource ADD COLUMN IF NOT EXISTS public_resource_key varchar(32) unique;
CREATE INDEX IF NOT EXISTS idx_resource_etag ON resource(tenant_id, etag);
CREATE INDEX IF NOT EXISTS idx_resource_image_public_key ON resource(public_key) WHERE resource_type = 'IMAGE';
CREATE INDEX IF NOT EXISTS idx_resource_type_public_resource_key ON resource(resource_type, public_resource_key);
CREATE OR REPLACE FUNCTION generate_resource_public_key()
RETURNS text AS $$
@ -31,7 +31,7 @@ DECLARE
chars text := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
result text := '';
BEGIN
FOR i IN 1..35 LOOP
FOR i IN 1..32 LOOP
result := result || substr(chars, floor(random()*62)::int + 1, 1);
END LOOP;
RETURN result;
@ -47,7 +47,7 @@ $$
UPDATE resource SET data = decode(base64_data, 'base64') WHERE base64_data IS NOT NULL;
ALTER TABLE resource DROP COLUMN base64_data;
ELSE
UPDATE resource SET public_key = generate_resource_public_key() WHERE resource_type = 'IMAGE' AND public_key IS NULL;
UPDATE resource SET public_resource_key = generate_resource_public_key() WHERE resource_type = 'IMAGE' AND public_resource_key IS NULL;
END IF;
END;
$$;

4
application/src/main/java/org/thingsboard/server/controller/ImageController.java

@ -173,7 +173,7 @@ public class ImageController extends BaseController {
.title(imageInfo.getTitle())
.resourceKey(imageInfo.getResourceKey())
.isPublic(imageInfo.isPublic())
.publicKey(imageInfo.getPublicKey())
.publicResourceKey(imageInfo.getPublicResourceKey())
.data(Base64Utils.encodeToString(data))
.build();
}
@ -195,7 +195,7 @@ public class ImageController extends BaseController {
image.setResourceType(ResourceType.IMAGE);
image.setResourceKey(imageData.getResourceKey());
image.setPublic(imageData.isPublic());
image.setPublicKey(imageData.getPublicKey());
image.setPublicResourceKey(imageData.getPublicResourceKey());
ImageDescriptor descriptor = new ImageDescriptor();
descriptor.setMediaType(imageData.getMediaType());
image.setDescriptorValue(descriptor);

2
application/src/main/java/org/thingsboard/server/service/queue/DefaultTbCoreConsumerService.java

@ -564,7 +564,7 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore
if (cacheKeyProto.hasResourceKey()) {
return ImageCacheKey.forImage(tenantId, cacheKeyProto.getResourceKey());
} else {
return ImageCacheKey.forPublicImage(cacheKeyProto.getPublicKey());
return ImageCacheKey.forPublicImage(cacheKeyProto.getPublicResourceKey());
}
}).forEach(imageService::evictETags);
callback.onSuccess();

8
application/src/main/java/org/thingsboard/server/service/resource/DefaultTbImageService.java

@ -77,7 +77,7 @@ public class DefaultTbImageService extends AbstractTbEntityService implements Tb
@Override
public void evictETags(ImageCacheKey imageCacheKey) {
etagCache.invalidate(imageCacheKey);
if (imageCacheKey.getPublicKey() == null) {
if (imageCacheKey.getPublicResourceKey() == null) {
etagCache.invalidate(imageCacheKey.withPreview(true));
}
}
@ -104,12 +104,12 @@ public class DefaultTbImageService extends AbstractTbEntityService implements Tb
if (newEtag.isPresent() && !oldEtag.get().equals(newEtag.get())) {
toEvict.add(ImageCacheKey.forImage(tenantId, image.getResourceKey()));
if (image.isPublic()) {
toEvict.add(ImageCacheKey.forPublicImage(savedImage.getPublicKey()));
toEvict.add(ImageCacheKey.forPublicImage(savedImage.getPublicResourceKey()));
}
}
}
if (existingImage != null && image.isPublic() != existingImage.isPublic()) {
toEvict.add(ImageCacheKey.forPublicImage(image.getPublicKey()));
toEvict.add(ImageCacheKey.forPublicImage(image.getPublicResourceKey()));
}
if (!toEvict.isEmpty()) {
evictFromCache(tenantId, toEvict);
@ -159,7 +159,7 @@ public class DefaultTbImageService extends AbstractTbEntityService implements Tb
List<ImageCacheKey> toEvict = new ArrayList<>();
toEvict.add(ImageCacheKey.forImage(tenantId, imageInfo.getResourceKey()));
if (imageInfo.isPublic()) {
toEvict.add(ImageCacheKey.forPublicImage(imageInfo.getPublicKey()));
toEvict.add(ImageCacheKey.forPublicImage(imageInfo.getPublicResourceKey()));
}
evictFromCache(tenantId, toEvict);
}

2
common/data/src/main/java/org/thingsboard/server/common/data/ImageExportData.java

@ -31,7 +31,7 @@ public class ImageExportData {
private final String title;
private final String resourceKey;
private final boolean isPublic;
private final String publicKey;
private final String publicResourceKey;
private final String data;
}

6
common/data/src/main/java/org/thingsboard/server/common/data/TbResourceInfo.java

@ -52,7 +52,7 @@ public class TbResourceInfo extends BaseData<TbResourceId> implements HasName, H
@ApiModelProperty(position = 6, value = "Resource key.", example = "19_1.0", accessMode = ApiModelProperty.AccessMode.READ_ONLY)
private String resourceKey;
private boolean isPublic;
private String publicKey;
private String publicResourceKey;
@ApiModelProperty(position = 7, value = "Resource search text.", example = "19_1.0:binaryappdatacontainer", accessMode = ApiModelProperty.AccessMode.READ_ONLY)
private String searchText;
@ -82,7 +82,7 @@ public class TbResourceInfo extends BaseData<TbResourceId> implements HasName, H
this.resourceKey = resourceInfo.resourceKey;
this.searchText = resourceInfo.searchText;
this.isPublic = resourceInfo.isPublic;
this.publicKey = resourceInfo.publicKey;
this.publicResourceKey = resourceInfo.publicResourceKey;
this.etag = resourceInfo.etag;
this.fileName = resourceInfo.fileName;
this.descriptor = resourceInfo.descriptor != null ? resourceInfo.descriptor.deepCopy() : null;
@ -114,7 +114,7 @@ public class TbResourceInfo extends BaseData<TbResourceId> implements HasName, H
public String getLink() {
if (resourceType == ResourceType.IMAGE) {
if (isPublic) {
return "/api/images/public/" + getPublicKey();
return "/api/images/public/" + getPublicResourceKey();
} else {
String type = (tenantId != null && tenantId.isSysTenantId()) ? "system" : "tenant"; // tenantId is null in case of export to git
return "/api/images/" + type + "/" + resourceKey;

2
common/proto/src/main/proto/queue.proto

@ -314,7 +314,7 @@ message ResourceCacheInvalidateMsg {
message ImageCacheKeyProto {
optional string resourceKey = 1;
optional string publicKey = 2;
optional string publicResourceKey = 2;
}
message LwM2MRegistrationRequestMsg {

2
dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java

@ -502,7 +502,7 @@ public class ModelConstants {
public static final String RESOURCE_DESCRIPTOR_COLUMN = "descriptor";
public static final String RESOURCE_PREVIEW_COLUMN = "preview";
public static final String RESOURCE_IS_PUBLIC_COLUMN = "is_public";
public static final String RESOURCE_PUBLIC_KEY_COLUMN = "public_key";
public static final String PUBLIC_RESOURCE_KEY_COLUMN = "public_resource_key";
/**
* Ota Package constants.

10
dao/src/main/java/org/thingsboard/server/dao/model/sql/TbResourceEntity.java

@ -40,7 +40,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_FILE_NAME
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_IS_PUBLIC_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_KEY_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_PREVIEW_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_PUBLIC_KEY_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.PUBLIC_RESOURCE_KEY_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_TABLE_NAME;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_TENANT_ID_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_TITLE_COLUMN;
@ -88,8 +88,8 @@ public class TbResourceEntity extends BaseSqlEntity<TbResource> {
@Column(name = RESOURCE_IS_PUBLIC_COLUMN)
private Boolean isPublic;
@Column(name = RESOURCE_PUBLIC_KEY_COLUMN, unique = true, updatable = false)
private String publicKey;
@Column(name = PUBLIC_RESOURCE_KEY_COLUMN, unique = true, updatable = false)
private String publicResourceKey;
@Column(name = EXTERNAL_ID_PROPERTY)
private UUID externalId;
@ -115,7 +115,7 @@ public class TbResourceEntity extends BaseSqlEntity<TbResource> {
this.descriptor = resource.getDescriptor();
this.preview = resource.getPreview();
this.isPublic = resource.isPublic();
this.publicKey = resource.getPublicKey();
this.publicResourceKey = resource.getPublicResourceKey();
this.externalId = getUuid(resource.getExternalId());
}
@ -134,7 +134,7 @@ public class TbResourceEntity extends BaseSqlEntity<TbResource> {
resource.setDescriptor(descriptor);
resource.setPreview(preview);
resource.setPublic(isPublic == null || isPublic);
resource.setPublicKey(publicKey);
resource.setPublicResourceKey(publicResourceKey);
resource.setExternalId(getEntityId(externalId, TbResourceId::new));
return resource;
}

10
dao/src/main/java/org/thingsboard/server/dao/model/sql/TbResourceInfoEntity.java

@ -39,7 +39,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_ETAG_COLU
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_FILE_NAME_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_IS_PUBLIC_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_KEY_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_PUBLIC_KEY_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.PUBLIC_RESOURCE_KEY_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_TABLE_NAME;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_TENANT_ID_COLUMN;
import static org.thingsboard.server.dao.model.ModelConstants.RESOURCE_TITLE_COLUMN;
@ -81,8 +81,8 @@ public class TbResourceInfoEntity extends BaseSqlEntity<TbResourceInfo> implemen
@Column(name = RESOURCE_IS_PUBLIC_COLUMN)
private Boolean isPublic;
@Column(name = RESOURCE_PUBLIC_KEY_COLUMN, unique = true, updatable = false)
private String publicKey;
@Column(name = PUBLIC_RESOURCE_KEY_COLUMN, unique = true, updatable = false)
private String publicResourceKey;
@Column(name = EXTERNAL_ID_PROPERTY)
private UUID externalId;
@ -104,7 +104,7 @@ public class TbResourceInfoEntity extends BaseSqlEntity<TbResourceInfo> implemen
this.fileName = resource.getFileName();
this.descriptor = resource.getDescriptor();
this.isPublic = resource.isPublic();
this.publicKey = resource.getPublicKey();
this.publicResourceKey = resource.getPublicResourceKey();
this.externalId = getUuid(resource.getExternalId());
}
@ -121,7 +121,7 @@ public class TbResourceInfoEntity extends BaseSqlEntity<TbResourceInfo> implemen
resource.setFileName(fileName);
resource.setDescriptor(descriptor);
resource.setPublic(isPublic == null || isPublic);
resource.setPublicKey(publicKey);
resource.setPublicResourceKey(publicResourceKey);
resource.setExternalId(getEntityId(externalId, TbResourceId::new));
return resource;
}

12
dao/src/main/java/org/thingsboard/server/dao/resource/BaseImageService.java

@ -151,11 +151,11 @@ public class BaseImageService extends BaseResourceService implements ImageServic
image.setPreview(result.getRight());
if (image.getId() == null) {
if (StringUtils.isEmpty(image.getPublicKey())) {
image.setPublicKey(generatePublicKey());
if (StringUtils.isEmpty(image.getPublicResourceKey())) {
image.setPublicResourceKey(generatePublicResourceKey());
} else {
if (resourceInfoDao.existsByPublicKey(ResourceType.IMAGE, image.getPublicKey())) {
image.setPublicKey(generatePublicKey());
if (resourceInfoDao.existsByPublicResourceKey(ResourceType.IMAGE, image.getPublicResourceKey())) {
image.setPublicResourceKey(generatePublicResourceKey());
}
}
}
@ -204,7 +204,7 @@ public class BaseImageService extends BaseResourceService implements ImageServic
return resourceKey;
}
private String generatePublicKey() {
private String generatePublicResourceKey() {
return RandomStringUtils.randomAlphanumeric(35);
}
@ -222,7 +222,7 @@ public class BaseImageService extends BaseResourceService implements ImageServic
@Override
public TbResourceInfo getPublicImageInfoByPublicKey(String publicKey) {
return resourceInfoDao.findPublicResourceByPublicKey(ResourceType.IMAGE, publicKey);
return resourceInfoDao.findPublicResourceByKey(ResourceType.IMAGE, publicKey);
}
@Override

4
dao/src/main/java/org/thingsboard/server/dao/resource/ImageCacheKey.java

@ -31,7 +31,7 @@ public class ImageCacheKey {
@With
private final boolean preview;
private final String publicKey;
private final String publicResourceKey;
public static ImageCacheKey forImage(TenantId tenantId, String key, boolean preview) {
return new ImageCacheKey(tenantId, key, preview, null);
@ -50,7 +50,7 @@ public class ImageCacheKey {
if (resourceKey != null) {
msg.setResourceKey(resourceKey);
} else {
msg.setPublicKey(publicKey);
msg.setPublicResourceKey(publicResourceKey);
}
return msg.build();
}

4
dao/src/main/java/org/thingsboard/server/dao/resource/TbResourceInfoDao.java

@ -42,8 +42,8 @@ public interface TbResourceInfoDao extends Dao<TbResourceInfo> {
TbResourceInfo findSystemOrTenantImageByEtag(TenantId tenantId, ResourceType resourceType, String etag);
boolean existsByPublicKey(ResourceType resourceType, String publicKey);
boolean existsByPublicResourceKey(ResourceType resourceType, String publicResourceKey);
TbResourceInfo findPublicResourceByPublicKey(ResourceType resourceType, String publicKey);
TbResourceInfo findPublicResourceByKey(ResourceType resourceType, String publicResourceKey);
}

8
dao/src/main/java/org/thingsboard/server/dao/sql/resource/JpaTbResourceInfoDao.java

@ -111,12 +111,12 @@ public class JpaTbResourceInfoDao extends JpaAbstractDao<TbResourceInfoEntity, T
}
@Override
public boolean existsByPublicKey(ResourceType resourceType, String publicKey) {
return resourceInfoRepository.existsByResourceTypeAndPublicKey(resourceType.name(), publicKey);
public boolean existsByPublicResourceKey(ResourceType resourceType, String publicResourceKey) {
return resourceInfoRepository.existsByResourceTypeAndPublicResourceKey(resourceType.name(), publicResourceKey);
}
@Override
public TbResourceInfo findPublicResourceByPublicKey(ResourceType resourceType, String publicKey) {
return DaoUtil.getData(resourceInfoRepository.findByResourceTypeAndPublicKeyAndIsPublicTrue(resourceType.name(), publicKey));
public TbResourceInfo findPublicResourceByKey(ResourceType resourceType, String publicResourceKey) {
return DaoUtil.getData(resourceInfoRepository.findByResourceTypeAndPublicResourceKeyAndIsPublicTrue(resourceType.name(), publicResourceKey));
}
}

4
dao/src/main/java/org/thingsboard/server/dao/sql/resource/TbResourceInfoRepository.java

@ -71,8 +71,8 @@ public interface TbResourceInfoRepository extends JpaRepository<TbResourceInfoEn
@Param("resourceType") String resourceType,
@Param("etag") String etag);
boolean existsByResourceTypeAndPublicKey(String resourceType, String publicKey);
boolean existsByResourceTypeAndPublicResourceKey(String resourceType, String publicResourceKey);
TbResourceInfoEntity findByResourceTypeAndPublicKeyAndIsPublicTrue(String resourceType, String publicKey);
TbResourceInfoEntity findByResourceTypeAndPublicResourceKeyAndIsPublicTrue(String resourceType, String publicResourceKey);
}

2
dao/src/main/resources/sql/schema-entities-idx.sql

@ -128,4 +128,4 @@ CREATE INDEX IF NOT EXISTS idx_resource_etag ON resource(tenant_id, etag);
CREATE INDEX IF NOT EXISTS idx_resource_etag ON resource(tenant_id, etag);
CREATE INDEX IF NOT EXISTS idx_resource_image_public_key ON resource(public_key) WHERE resource_type = 'IMAGE';
CREATE INDEX IF NOT EXISTS idx_resource_type_public_resource_key ON resource(resource_type, public_resource_key);

2
dao/src/main/resources/sql/schema-entities.sql

@ -719,7 +719,7 @@ CREATE TABLE IF NOT EXISTS resource (
descriptor varchar,
preview bytea,
is_public boolean default true,
public_key varchar(35) unique,
public_resource_key varchar(32) unique,
external_id uuid,
CONSTRAINT resource_unq_key UNIQUE (tenant_id, resource_type, resource_key)
);

Loading…
Cancel
Save