-
Notifications
You must be signed in to change notification settings - Fork 59
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
Changes from 1 commit
5fbcdd9
960af38
0552570
5b0c948
96c3267
68553c4
b18faa9
5bbfd0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… -> velocity
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,26 +26,24 @@ | |
import com.epam.pipeline.manager.datastorage.DataStorageManager; | ||
import com.epam.pipeline.manager.preference.PreferenceManager; | ||
import com.epam.pipeline.manager.preference.SystemPreferences; | ||
import freemarker.template.Configuration; | ||
import freemarker.template.Template; | ||
import freemarker.template.Version; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import org.apache.commons.io.FileUtils; | ||
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.springframework.stereotype.Service; | ||
import org.springframework.util.Assert; | ||
import freemarker.template.TemplateException; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.epam.pipeline.controller.resource.StaticResourcesController.STATIC_RESOURCES; | ||
|
@@ -55,9 +53,6 @@ | |
public class StaticResourcesService { | ||
|
||
private static final String DELIMITER = "/"; | ||
private static final String TEMPLATE = "folder.ftlh"; | ||
private static final String VERSION = "2.3.23"; | ||
private static final String TEMPLATES_FOLDER = "/views"; | ||
|
||
private final DataStorageManager dataStorageManager; | ||
private final MessageHelper messageHelper; | ||
|
@@ -76,32 +71,37 @@ public DataStorageStreamingContent getContent(final String path) { | |
if (Files.isDirectory(Paths.get(path))) { | ||
final List<AbstractDataStorageItem> items = dataStorageManager.getDataStorageItems(storage.getId(), | ||
path, false, null, null).getResults(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix |
||
final String html = buildHtml(items); | ||
final String tmplPath = 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); | ||
return new DataStorageStreamingContent(new ByteArrayInputStream(html.getBytes()), filePath); | ||
} | ||
dataStorageManager.checkDataStorageObjectExists(storage, filePath, null); | ||
return dataStorageManager.getStreamingContent(storage.getId(), filePath, null); | ||
} | ||
|
||
public String buildHtml(final List<AbstractDataStorageItem> items) throws IOException, TemplateException { | ||
final Configuration cfg = new Configuration(new Version(VERSION)); | ||
|
||
cfg.setClassForTemplateLoading(StaticResourcesService.class, TEMPLATES_FOLDER); | ||
cfg.setDefaultEncoding("UTF-8"); | ||
|
||
final Template template = cfg.getTemplate(TEMPLATE); | ||
final Map<String, Object> templateData = new HashMap<>(); | ||
templateData.put("items", getHtmlStorageItems(items)); | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this configuration, classpath template is not loaded. We need to support both classpath and filesystem resources. Maybe it is easier to use an approach from |
||
engine.init(); | ||
final Template template = engine.getTemplate(templateName); | ||
final VelocityContext velocityContext = new VelocityContext(); | ||
velocityContext.put("items", getHtmlStorageItems(items, staticResourcesPrefix)); | ||
|
||
try (StringWriter out = new StringWriter()) { | ||
template.process(templateData, out); | ||
template.merge(velocityContext, out); | ||
return out.getBuffer().toString(); | ||
} | ||
} | ||
|
||
private List<HtmlStorageItem> getHtmlStorageItems(final List<AbstractDataStorageItem> items) { | ||
final String staticResourcesPrefix = preferenceManager.getPreference(SystemPreferences.BASE_API_HOST) | ||
+ STATIC_RESOURCES; | ||
private static List<HtmlStorageItem> getHtmlStorageItems(final List<AbstractDataStorageItem> items, | ||
final String staticResourcesPrefix) { | ||
return items.stream() | ||
.map(i -> HtmlStorageItem.builder() | ||
.name(i.getName()) | ||
|
@@ -114,7 +114,7 @@ private List<HtmlStorageItem> getHtmlStorageItems(final List<AbstractDataStorage | |
.collect(Collectors.toList()); | ||
} | ||
|
||
private String getFileSize(final AbstractDataStorageItem item) { | ||
private static String getFileSize(final AbstractDataStorageItem item) { | ||
return item.getType() == DataStorageItemType.File ? | ||
FileUtils.byteCountToDisplaySize(((DataStorageFile) item).getSize()) : ""; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check won't work for cloud resource, I will add a new method to check cloud object type