Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
amvanbaren committed Jun 22, 2023
1 parent 9b92230 commit a673b85
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 59 deletions.
12 changes: 12 additions & 0 deletions server/src/main/java/org/eclipse/openvsx/ExtensionProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
* Processes uploaded extension files and extracts their metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ public void run(MigrationJobRequest jobRequest) throws Exception {
}

service.deleteWebResources(extVersion);
migrations.deleteFile(extensionFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void renameDownloadsMigration() {
private void setContentTypeMigration() {
var jobName = "SetContentTypeMigration";
var handler = SetContentTypeJobRequestHandler.class;
repositories.findNotMigratedContentTypes().forEach(item -> enqueueJob(jobName, handler, item));
repositories.findNotMigratedContentTypes().forEach(item -> migrations.enqueueMigration(jobName, handler, item));
}

private void extractVsixManifestMigration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public TempFile getExtensionFile(Map.Entry<FileResource, byte[]> entry) throws I

var content = entry.getValue();
if(content == null) {
var storage = getStorage(resource);
var uri = storage.getLocation(resource);
var storage = getStorage(entry.getKey());
var uri = storage.getLocation(entry.getKey());
backgroundRestTemplate.execute("{extensionLocation}", HttpMethod.GET, null, response -> {
try(var out = Files.newOutputStream(extensionFile.getPath())) {
response.getBody().transferTo(out);
Expand All @@ -95,14 +95,6 @@ public TempFile getExtensionFile(Map.Entry<FileResource, byte[]> entry) throws I
return extensionFile;
}

public void deleteFile(Path filePath) {
try {
Files.delete(filePath);
} catch (IOException e) {
throw new RuntimeException("Failed to delete file");
}
}

@Retryable
public void uploadFileResource(FileResource resource) {
if(resource.getStorageType().equals(FileResource.STORAGE_DB)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package org.eclipse.openvsx.migration;

import org.eclipse.openvsx.ExtensionProcessor;
import org.eclipse.openvsx.entities.ExtensionVersion;
import org.eclipse.openvsx.entities.FileResource;
import org.jobrunr.jobs.annotations.Job;
import org.jobrunr.jobs.lambdas.JobRequestHandler;
Expand All @@ -19,6 +18,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.function.Consumer;

Expand All @@ -36,28 +36,29 @@ public class SetContentTypeJobRequestHandler implements JobRequestHandler<Migrat
@Override
@Job(name = "Set content type for published file resources", retries = 3)
public void run(MigrationJobRequest jobRequest) throws Exception {
var extVersion = migrations.find(jobRequest, ExtensionVersion.class);
var extVersion = migrations.getExtension(jobRequest.getEntityId());
logger.info("Set content type for: {}.{}-{}@{}", extVersion.getExtension().getNamespace().getName(), extVersion.getExtension().getName(), extVersion.getVersion(), extVersion.getTargetPlatform());

var entry = migrations.getDownload(extVersion);
var extensionFile = migrations.getFile(entry);
try (var processor = new ExtensionProcessor(extensionFile)) {
Consumer<FileResource> consumer = resource -> {
var existingResource = service.getExistingResource(extVersion, resource);
if(existingResource == null) {
return;
}

var resourceFile = migrations.getFile(new AbstractMap.SimpleEntry<>(existingResource, resource.getContent()));
migrations.deleteResource(existingResource);
migrations.uploadResource(existingResource, resourceFile);
migrations.deleteFile(resourceFile);
};

processor.getFileResources(extVersion).forEach(consumer);
processor.processEachResource(extVersion, consumer);
}
try(var extensionFile = migrations.getExtensionFile(entry)) {
try (var processor = new ExtensionProcessor(extensionFile)) {
Consumer<FileResource> consumer = resource -> {
var existingResource = service.getExistingResource(extVersion, resource);
if (existingResource == null) {
return;
}

try (var resourceFile = migrations.getExtensionFile(new AbstractMap.SimpleEntry<>(existingResource, resource.getContent()))) {
migrations.deleteFileResource(existingResource);
migrations.uploadFileResource(existingResource, resourceFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
};

migrations.deleteFile(extensionFile);
processor.getFileResources(extVersion).forEach(consumer);
processor.processEachResource(extVersion, consumer);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import jakarta.transaction.Transactional;

@Component
public class SetContentTypeJobService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void run(MigrationJobRequest jobRequest) throws Exception {
try (var extensionFile = migrations.getExtensionFile(entry)) {
service.updatePreviewAndPreRelease(extVersion, extensionFile);
}
migrations.deleteFile(extensionFile);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.csrf.CsrfAuthenticationStrategy;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,31 @@ protected BlobContainerClient getContainerClient() {
@Override
public void uploadFile(FileResource resource) {
var blobName = getBlobName(resource);
uploadFile(resource.getContent(), resource.getName(), blobName);
uploadFile(resource.getContent(), resource.getName(), blobName, ""); // TODO add contentType
}

@Override
public void uploadNamespaceLogo(Namespace namespace) {
var blobName = getBlobName(namespace);
uploadFile(namespace.getLogoBytes(), namespace.getLogoName(), blobName);
uploadFile(namespace.getLogoBytes(), namespace.getLogoName(), blobName, ""); // TODO add contentType
}
protected void uploadFile(byte[] content, String fileName, String blobName) {

protected void uploadFile(byte[] content, String fileName, String blobName, String contentType) {
if (StringUtils.isEmpty(serviceEndpoint)) {
throw new IllegalStateException("Cannot upload file "
+ blobName + ": missing Azure blob service endpoint");
}

var blobClient = getContainerClient().getBlobClient(blobName);
var headers = new BlobHttpHeaders();
headers.setContentType(resource.getContentType());
if (resource.getName().endsWith(".vsix")) {
headers.setContentDisposition("attachment; filename=\"" + resource.getName() + "\"");
headers.setContentType(contentType);
if (fileName.endsWith(".vsix")) {
headers.setContentDisposition("attachment; filename=\"" + fileName + "\"");
} else {
var cacheControl = StorageUtil.getCacheControl(resource.getName());
var cacheControl = StorageUtil.getCacheControl(fileName);
headers.setCacheControl(cacheControl.getHeaderValue());
}

var content = resource.getContent();
try (var dataStream = new ByteArrayInputStream(content)) {
blobClient.upload(dataStream, content.length, true);
blobClient.setHttpHeaders(headers);
Expand All @@ -103,12 +102,12 @@ protected void uploadFile(byte[] content, String fileName, String blobName) {
}

@Override
public void uploadFile(FileResource resource, TempFile file) {
public void uploadFile(FileResource resource, TempFile file, String contentType) {
var blobName = getBlobName(resource);
uploadFile(file, resource.getName(), blobName);
uploadFile(file, resource.getName(), blobName, contentType);
}

protected void uploadFile(TempFile file, String fileName, String blobName) {
protected void uploadFile(TempFile file, String fileName, String blobName, String contentType) {
if (StringUtils.isEmpty(serviceEndpoint)) {
throw new IllegalStateException("Cannot upload file "
+ blobName + ": missing Azure blob service endpoint");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ public void uploadNamespaceLogo(Namespace namespace) {
+ objectId + ": missing Google bucket id");
}

uploadFile(namespace.getLogoBytes(), namespace.getLogoName(), objectId);
uploadFile(namespace.getLogoBytes(), namespace.getLogoName(), objectId, ""); // TODO add contentType
}

protected void uploadFile(byte[] content, String fileName, String objectId) {
protected void uploadFile(byte[] content, String fileName, String objectId, String contentType) {
var blobInfoBuilder = BlobInfo.newBuilder(BlobId.of(bucketId, objectId))
.setContentType(resource.getContentType());
if (resource.getName().endsWith(".vsix")) {
blobInfoBuilder.setContentDisposition("attachment; filename=\"" + resource.getName() + "\"");
.setContentType(contentType);
if (fileName.endsWith(".vsix")) {
blobInfoBuilder.setContentDisposition("attachment; filename=\"" + fileName + "\"");
} else {
var cacheControl = StorageUtil.getCacheControl(resource.getName());
var cacheControl = StorageUtil.getCacheControl(fileName);
blobInfoBuilder.setCacheControl(cacheControl.getHeaderValue());
}
getStorage().create(blobInfoBuilder.build(), resource.getContent());
getStorage().create(blobInfoBuilder.build(), content);
}

@Override
Expand All @@ -106,10 +106,10 @@ public void uploadFile(FileResource resource, TempFile file) {
+ objectId + ": missing Google bucket id");
}

uploadFile(file, resource.getName(), objectId);
uploadFile(file, resource.getName(), objectId, ""); // TODO add contentType
}

protected void uploadFile(TempFile file, String fileName, String objectId) {
protected void uploadFile(TempFile file, String fileName, String objectId, String contentType) {
var blobInfoBuilder = BlobInfo.newBuilder(BlobId.of(bucketId, objectId))
.setContentType(contentType);
if (fileName.endsWith(".vsix")) {
Expand Down

0 comments on commit a673b85

Please sign in to comment.