Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 2912: Support folder browsing for static resources #2917

Merged
Prev Previous commit
Next Next commit
Issue 2912: Support folder browsing for static resources + fix veloci…
…ty template loading
  • Loading branch information
okolesn committed Nov 9, 2022
commit 96c3267c56018b1e6aa02fdd1d56f272128cf258
Original file line number Diff line number Diff line change
Expand Up @@ -1092,12 +1092,8 @@ public class SystemPreferences {

// Static Resources Group
public static final StringPreference STATIC_RESOURCES_FOLDER_TEMPLATE_PATH =
new StringPreference("static.resources.folder.template.path", "classpath:views/",
new StringPreference("static.resources.folder.template.path", "classpath:/views/folder.vm",
STATIC_RESOURCES_GROUP, pass);
public static final StringPreference STATIC_RESOURCES_FOLDER_TEMPLATE =
new StringPreference("static.resources.folder.template", "folder.vm",
STATIC_RESOURCES_GROUP, pass);


private static final Pattern GIT_VERSION_PATTERN = Pattern.compile("(\\d)\\.(\\d)");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.app.Velocity;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;

import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Comparator;
Expand Down Expand Up @@ -69,31 +70,25 @@ public DataStorageStreamingContent getContent(final String path) {
final DataStorageItemType itemType = dataStorageManager.getItemType(storage, filePath, null);
if (itemType == DataStorageItemType.Folder) {
final List<AbstractDataStorageItem> items = dataStorageManager.getDataStorageItems(storage.getId(),
path, false, null, null).getResults();
final String tmplPath = preferenceManager.getPreference(
filePath, false, null, null).getResults();
final String templatePath = preferenceManager.getPreference(
SystemPreferences.STATIC_RESOURCES_FOLDER_TEMPLATE_PATH);
final String tmplName = preferenceManager.getPreference(SystemPreferences.STATIC_RESOURCES_FOLDER_TEMPLATE);
final String staticResourcesPrefix = preferenceManager.getPreference(SystemPreferences.BASE_API_HOST)
+ STATIC_RESOURCES;
final String html = buildHtml(items, tmplPath, tmplName, staticResourcesPrefix);
final String html = buildHtml(items, templatePath, staticResourcesPrefix);
return new DataStorageStreamingContent(new ByteArrayInputStream(html.getBytes()), filePath);
}
return dataStorageManager.getStreamingContent(storage.getId(), filePath, null);
}

public static String buildHtml(final List<AbstractDataStorageItem> items,
final String templatePath,
final String templateName,
final String staticResourcesPrefix) throws IOException{
final VelocityEngine engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath);
engine.init();
final Template template = engine.getTemplate(templateName);
final VelocityContext velocityContext = new VelocityContext();
velocityContext.put("items", getHtmlStorageItems(items, staticResourcesPrefix));

final String template = IOUtils.toString(new FileReader(ResourceUtils.getFile(templatePath)));
try (StringWriter out = new StringWriter()) {
template.merge(velocityContext, out);
Velocity.evaluate(velocityContext, out, "folder", template);
return out.getBuffer().toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@

public class StaticResourceServiceTest extends AbstractSpringTest {

private static final String TEMPLATES_PATH = "classpath:views/";
private static final String TEMPLATE_NAME = "folder.vm";
private static final String TEMPLATES_PATH = "classpath:/views/folder.vm";

@Autowired
private ApplicationContext context;
Expand All @@ -54,8 +53,8 @@ public void getHtmlContentTest() throws IOException {
123, "10/24/20, 9:27:20 PM");
items.add(file2);

assertThat(buildHtml(items, context.getResource(TEMPLATES_PATH).getFile().getAbsolutePath(),
TEMPLATE_NAME, "")).isNotBlank();
assertThat(buildHtml(items, context.getResource(TEMPLATES_PATH).getFile().getAbsolutePath(), ""))
.isNotBlank();
}

private DataStorageFolder getDataStorageFolder(final String name, final String path) {
Expand Down