-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1045 from amvanbaren/azure-user-agent
Serve resource files on demand
- Loading branch information
Showing
13 changed files
with
585 additions
and
405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
server/src/main/java/org/eclipse/openvsx/adapter/WebResourceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2024 Precies. Software OU and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.adapter; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.eclipse.openvsx.entities.FileResource; | ||
import org.eclipse.openvsx.repositories.RepositoryService; | ||
import org.eclipse.openvsx.storage.StorageUtilService; | ||
import org.eclipse.openvsx.util.ErrorResultException; | ||
import org.eclipse.openvsx.util.NamingUtil; | ||
import org.eclipse.openvsx.util.UrlUtil; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.stream.Collectors; | ||
import java.util.zip.ZipFile; | ||
|
||
import static org.eclipse.openvsx.cache.CacheService.CACHE_WEB_RESOURCE_FILES; | ||
import static org.eclipse.openvsx.cache.CacheService.GENERATOR_FILES; | ||
|
||
@Component | ||
public class WebResourceService { | ||
|
||
private final StorageUtilService storageUtil; | ||
private final RepositoryService repositories; | ||
|
||
public WebResourceService(StorageUtilService storageUtil, RepositoryService repositories) { | ||
this.storageUtil = storageUtil; | ||
this.repositories = repositories; | ||
} | ||
|
||
@Cacheable(value = CACHE_WEB_RESOURCE_FILES, keyGenerator = GENERATOR_FILES) | ||
public Path getWebResource(String namespace, String extension, String targetPlatform, String version, String name, boolean browse) { | ||
var download = repositories.findFileByType(namespace, extension, targetPlatform, version, FileResource.DOWNLOAD); | ||
if(download == null) { | ||
return null; | ||
} | ||
|
||
Path path; | ||
try { | ||
path = storageUtil.getCachedFile(download); | ||
} catch(IOException e) { | ||
throw new ErrorResultException("Failed to get file for download " + NamingUtil.toLogFormat(download.getExtension())); | ||
} | ||
if(path == null) { | ||
return null; | ||
} | ||
|
||
try(var zip = new ZipFile(path.toFile())) { | ||
var fileEntry = zip.getEntry(name); | ||
if(fileEntry != null) { | ||
var fileExtIndex = fileEntry.getName().lastIndexOf('.'); | ||
var fileExt = fileExtIndex != -1 ? fileEntry.getName().substring(fileExtIndex) : ""; | ||
var file = Files.createTempFile("webresource_", fileExt); | ||
try(var in = zip.getInputStream(fileEntry)) { | ||
Files.copy(in, file, StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
|
||
return file; | ||
} else if (browse) { | ||
var dirName = name.isEmpty() || name.endsWith("/") ? name : name + "/"; | ||
var dirEntries = zip.stream() | ||
.filter(entry -> entry.getName().startsWith(dirName)) | ||
.map(entry -> { | ||
var folderNameEndIndex = entry.getName().indexOf("/", dirName.length()); | ||
return folderNameEndIndex == -1 ? entry.getName() : entry.getName().substring(0, folderNameEndIndex + 1); | ||
}) | ||
.collect(Collectors.toSet()); | ||
if(dirEntries.isEmpty()) { | ||
return null; | ||
} | ||
|
||
var file = Files.createTempFile("webresource_", ".unpkg.json"); | ||
var baseUrl = UrlUtil.createApiUrl(UrlUtil.getBaseUrl(), "vscode", "unpkg", namespace, extension, version); | ||
var mapper = new ObjectMapper(); | ||
var node = mapper.createArrayNode(); | ||
for(var entry : dirEntries) { | ||
node.add(baseUrl + "/" + entry); | ||
} | ||
mapper.writeValue(file.toFile(), node); | ||
return file; | ||
} else { | ||
return null; | ||
} | ||
} catch (IOException e) { | ||
throw new ErrorResultException("Failed to read extension files for " + NamingUtil.toLogFormat(download.getExtension())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
server/src/main/java/org/eclipse/openvsx/cache/ExpiredFileListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2024 Precies. Software OU and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.cache; | ||
|
||
import org.ehcache.event.CacheEvent; | ||
import org.ehcache.event.CacheEventListener; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class ExpiredFileListener implements CacheEventListener<String, Path> { | ||
protected final Logger logger = LoggerFactory.getLogger(ExpiredFileListener.class); | ||
@Override | ||
public void onEvent(CacheEvent<? extends String, ? extends Path> cacheEvent) { | ||
logger.info("Expired file cache event: {} | key: {}", cacheEvent.getType(), cacheEvent.getKey()); | ||
var path = cacheEvent.getOldValue(); | ||
try { | ||
var deleted = Files.deleteIfExists(path); | ||
if(deleted) { | ||
logger.info("Deleted expired file {} successfully", path); | ||
} else { | ||
logger.warn("Did NOT delete expired file {}", path); | ||
} | ||
} catch (IOException e) { | ||
logger.error("Failed to delete expired file", e); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
server/src/main/java/org/eclipse/openvsx/cache/FilesCacheKeyGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2024 Precies. Software OU and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.cache; | ||
|
||
import org.eclipse.openvsx.adapter.WebResourceService; | ||
import org.eclipse.openvsx.entities.FileResource; | ||
import org.eclipse.openvsx.storage.IStorageService; | ||
import org.eclipse.openvsx.util.UrlUtil; | ||
import org.springframework.cache.interceptor.KeyGenerator; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
@Component | ||
public class FilesCacheKeyGenerator implements KeyGenerator { | ||
@Override | ||
public Object generate(Object target, Method method, Object... params) { | ||
if(target instanceof WebResourceService) { | ||
var namespace = (String) params[0]; | ||
var extension = (String) params[1]; | ||
var targetPlatform = (String) params[2]; | ||
var version = (String) params[3]; | ||
var name = (String) params[4]; | ||
return generate(namespace, extension, targetPlatform, version, name); | ||
} | ||
if(target instanceof IStorageService) { | ||
var resource = (FileResource) params[0]; | ||
var extVersion = resource.getExtension(); | ||
var extension = extVersion.getExtension(); | ||
var namespace = extension.getNamespace(); | ||
return generate(namespace.getName(), extension.getName(), extVersion.getTargetPlatform(), extVersion.getVersion(), resource.getName()); | ||
} | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
private String generate(String namespace, String extension, String targetPlatform, String version, String name) { | ||
return UrlUtil.createApiFileUrl("", namespace, extension, targetPlatform, version, name); | ||
} | ||
} |
Oops, something went wrong.