Skip to content

Commit

Permalink
Fix upload artifact (eclipse-hawkbit#852)
Browse files Browse the repository at this point in the history
* fixed repository to check for file existence before trying to get the artifact
* added additional existence checks before retrieving the artifact from repository

Signed-off-by: Bogdan Bondar <[email protected]>
  • Loading branch information
bogdan-bondar authored and Jeroen Laverman committed Jun 13, 2019
1 parent 72af1d9 commit 54bc6ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractDbArtifact> 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) {
Expand Down Expand Up @@ -244,7 +244,9 @@ private void throwExceptionIfSoftwareModuleDoesNotExist(final Long swId) {

@Override
public Optional<AbstractDbArtifact> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public ResponseEntity<InputStream> 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());
}
Expand Down

0 comments on commit 54bc6ce

Please sign in to comment.