diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaArtifactManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaArtifactManagement.java index 1376518519..a8bbea6678 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaArtifactManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaArtifactManagement.java @@ -108,20 +108,20 @@ public Artifact create(final ArtifactUpload artifactUpload) { assertMaxArtifactSizeQuota(filename, moduleId, artifactUpload.getFilesize()); assertMaxArtifactStorageQuota(filename, artifactUpload.getFilesize()); - return getOrCreateArtifact(artifactUpload) - .map(artifact -> storeArtifactMetadata(softwareModule, filename, artifact, existing)) - .orElse(null); + return getOrCreateArtifact(artifactUpload) + .map(artifact -> storeArtifactMetadata(softwareModule, filename, artifact, existing)).orElse(null); } private Optional getOrCreateArtifact(final ArtifactUpload artifactUpload) { final String providedSha1Sum = artifactUpload.getProvidedSha1Sum(); - AbstractDbArtifact artifact = null; - if (!StringUtils.isEmpty(providedSha1Sum)) { - artifact = artifactRepository.getArtifactBySha1(tenantAware.getCurrentTenant(), providedSha1Sum); + if (!StringUtils.isEmpty(providedSha1Sum) + && artifactRepository.existsByTenantAndSha1(tenantAware.getCurrentTenant(), providedSha1Sum)) { + return Optional + .ofNullable(artifactRepository.getArtifactBySha1(tenantAware.getCurrentTenant(), providedSha1Sum)); } - artifact = (artifact == null) ? storeArtifact(artifactUpload) : artifact; - return Optional.ofNullable(artifact); + + return Optional.of(storeArtifact(artifactUpload)); } private AbstractDbArtifact storeArtifact(final ArtifactUpload artifactUpload) { @@ -244,7 +244,9 @@ private void throwExceptionIfSoftwareModuleDoesNotExist(final Long swId) { @Override public Optional loadArtifactBinary(final String sha1Hash) { - return Optional.ofNullable(artifactRepository.getArtifactBySha1(tenantAware.getCurrentTenant(), sha1Hash)); + return Optional.ofNullable(artifactRepository.existsByTenantAndSha1(tenantAware.getCurrentTenant(), sha1Hash) + ? artifactRepository.getArtifactBySha1(tenantAware.getCurrentTenant(), sha1Hash) + : null); } private Artifact storeArtifactMetadata(final SoftwareModule softwareModule, final String providedFilename, diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDownloadResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDownloadResource.java index f895738006..d4b90e3b28 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDownloadResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDownloadResource.java @@ -64,7 +64,9 @@ public ResponseEntity downloadArtifactByDownloadId(@PathVariable("t AbstractDbArtifact artifact = null; if (DownloadType.BY_SHA1 == artifactCache.getDownloadType()) { - artifact = artifactRepository.getArtifactBySha1(tenant, artifactCache.getId()); + artifact = artifactRepository.existsByTenantAndSha1(tenant, artifactCache.getId()) + ? artifactRepository.getArtifactBySha1(tenant, artifactCache.getId()) + : null; } else { LOGGER.warn("Download Type {} not supported", artifactCache.getDownloadType()); }