Skip to content

Commit

Permalink
Merge pull request #696 from amvanbaren/bugfix/issue-695
Browse files Browse the repository at this point in the history
Unable to delete namespace logo
  • Loading branch information
amvanbaren authored Mar 10, 2023
2 parents e0947cb + 395c3c6 commit 2563617
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.openvsx.entities.ExtensionVersion;
import org.eclipse.openvsx.json.NamespaceDetailsJson;
import org.eclipse.openvsx.util.TargetPlatform;
import org.eclipse.openvsx.util.TimeUtil;
import org.eclipse.openvsx.util.VersionAlias;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -88,7 +89,7 @@ public List<Issue> validateNamespaceDetails(NamespaceDetailsJson json) {
var detectedType = tika.detect(in, json.logo);
var logoType = MimeTypes.getDefaultMimeTypes().getRegisteredMimeType(detectedType);
if(logoType != null) {
json.logo = "logo-" + json.name + logoType.getExtension();
json.logo = "logo-" + json.name + "-" + System.currentTimeMillis() + logoType.getExtension();
if(!logoType.getType().equals(MediaType.image("png")) && !logoType.getType().equals(MediaType.image("jpg"))) {
issues.add(new Issue("Namespace logo should be of png or jpg type"));
}
Expand Down
47 changes: 35 additions & 12 deletions server/src/main/java/org/eclipse/openvsx/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@
********************************************************************************/
package org.eclipse.openvsx;

import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import com.google.common.base.Joiner;
import org.apache.commons.io.IOUtils;
import org.eclipse.openvsx.cache.CacheService;
import org.eclipse.openvsx.entities.*;
import org.eclipse.openvsx.json.NamespaceDetailsJson;
import org.eclipse.openvsx.json.AccessTokenJson;
import org.eclipse.openvsx.json.NamespaceDetailsJson;
import org.eclipse.openvsx.json.ResultJson;
import org.eclipse.openvsx.repositories.RepositoryService;
import org.eclipse.openvsx.security.IdPrincipal;
Expand All @@ -35,6 +29,14 @@
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;
import java.util.UUID;

import static org.eclipse.openvsx.cache.CacheService.CACHE_NAMESPACE_DETAILS_JSON;
import static org.eclipse.openvsx.util.UrlUtil.createApiUrl;

Expand Down Expand Up @@ -234,23 +236,44 @@ public ResultJson updateNamespaceDetails(NamespaceDetailsJson details) {
if(!Objects.equals(details.socialLinks, namespace.getSocialLinks())) {
namespace.setSocialLinks(details.socialLinks);
}
if(!Arrays.equals(details.logoBytes, namespace.getLogoBytes())) {
if (details.logoBytes != null) {
if (namespace.getLogoBytes() != null) {
if(details.logoBytes == null) {
details.logoBytes = new byte[0];
}

boolean contentEquals;
var oldLogo = storageUtil.downloadNamespaceLogo(namespace);
try (
var newLogoInput = new ByteArrayInputStream(details.logoBytes);
var oldLogoInput = Files.newInputStream(oldLogo)
) {
contentEquals = IOUtils.contentEquals(newLogoInput, oldLogoInput);
} catch (IOException e) {
throw new RuntimeException(e);
}

if(!contentEquals) {
if (details.logoBytes.length > 0) {
if (namespace.getLogoStorageType() != null) {
storageUtil.removeNamespaceLogo(namespace);
}

namespace.setLogoName(details.logo);
namespace.setLogoBytes(details.logoBytes);
storeNamespaceLogo(namespace);
} else if (namespace.getLogoBytes() != null) {
} else if (namespace.getLogoStorageType() != null) {
storageUtil.removeNamespaceLogo(namespace);
namespace.setLogoName(null);
namespace.setLogoBytes(null);
namespace.setLogoStorageType(null);
}
}

try {
Files.delete(oldLogo);
} catch (IOException e) {
throw new RuntimeException(e);
}

return ResultJson.success("Updated details for namespace " + details.name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;

@Component
Expand Down Expand Up @@ -189,4 +190,15 @@ public URI getNamespaceLogoLocation(Namespace namespace) {
protected String getBlobName(Namespace namespace) {
return UrlUtil.createApiUrl("", namespace.getName(), "logo", namespace.getLogoName()).substring(1); // remove first '/'
}

@Override
public Path downloadNamespaceLogo(Namespace namespace) {
try {
var logoFile = Files.createTempFile("namespace-logo", ".png");
getContainerClient().getBlobClient(getBlobName(namespace)).downloadToFile(logoFile.toString(), true);
return logoFile;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -185,4 +186,24 @@ protected String getObjectId(Namespace namespace) {
return UrlUtil.createApiUrl("", namespace.getName(), "logo", namespace.getLogoName()).substring(1); // remove first '/'
}

@Override
public Path downloadNamespaceLogo(Namespace namespace) {
Path logoFile;
try {
logoFile = Files.createTempFile("namespace-logo", ".png");
} catch (IOException e) {
throw new RuntimeException(e);
}

try (
var reader = getStorage().reader(BlobId.of(bucketId, getObjectId(namespace)));
var output = new FileOutputStream(logoFile.toFile());
) {
output.getChannel().transferFrom(reader, 0, Long.MAX_VALUE);
} catch (IOException e) {
throw new RuntimeException(e);
}

return logoFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ public interface IStorageService {
* Returns the public access location of a namespace logo.
*/
URI getNamespaceLogoLocation(Namespace namespace);

Path downloadNamespaceLogo(Namespace namespace);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -221,6 +223,37 @@ public URI getNamespaceLogoLocation(Namespace namespace) {
}
}

public Path downloadNamespaceLogo(Namespace namespace) {
if(namespace.getLogoStorageType() == null) {
return createNamespaceLogoFile();
}

switch (namespace.getLogoStorageType()) {
case STORAGE_GOOGLE:
return googleStorage.downloadNamespaceLogo(namespace);
case STORAGE_AZURE:
return azureStorage.downloadNamespaceLogo(namespace);
case STORAGE_DB:
try {
var logoFile = createNamespaceLogoFile();
Files.write(logoFile, namespace.getLogoBytes());
return logoFile;
} catch (IOException e) {
throw new RuntimeException(e);
}
default:
return createNamespaceLogoFile();
}
}

private Path createNamespaceLogoFile() {
try {
return Files.createTempFile("namespace-logo", ".png");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private String getFileUrl(String name, ExtensionVersion extVersion, String serverUrl) {
return UrlUtil.createApiFileUrl(serverUrl, extVersion, name);
}
Expand Down

0 comments on commit 2563617

Please sign in to comment.