19 package org.turro.warnings;
21 import java.awt.Dimension;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.function.Consumer;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.web.ElContextMap;
34 import org.turro.i18n.I_;
35 import org.turro.util.ImageUtil;
46 return I_.
get(
"Files");
54 return Collections.EMPTY_LIST;
57 private List<IWarning> createModel() {
58 List<IWarning> warnings =
new ArrayList<>();
59 checkImages(warnings);
63 private void checkImages(List<IWarning> warnings) {
64 Consumer<Path> action = (path) -> {
65 String relative = ElephantContext.getRelativePath(path.toString());
67 Dimension dimension = ImageUtil.dimension(path.toFile());
68 double scaleTo = scaleFrom(dimension);
70 ImageUtil.scale(path.toFile(), path.toFile(), scaleTo);
71 Warning warning =
new Warning();
72 warning.setEntity(path.toFile());
73 warning.addMessage(I_.format(
"Image resized: %s", relative));
74 warnings.add(warning);
76 }
catch (IOException ex) {
77 Warning warning =
new Warning();
78 warning.setEntity(path.toFile());
79 warning.addMessage(I_.format(
"Could not be read: %s", relative));
80 warnings.add(warning);
83 walkContexts(
"(?i).*(jpg|png|gif)", action);
85 walkFolder(
"/_internal/files",
"(?i).*(jpg|png|gif)", action);
86 }
catch (IOException ex) {
87 Logger.getLogger(FileWarnings.class.getName()).log(Level.SEVERE,
null, ex);
91 private void walkContexts(String pattern, Consumer<Path> action) {
92 ElContextMap.getInstance().values().forEach(elContext -> {
94 walkFolder(elContext.getRepositoryPath(), pattern, action);
95 }
catch (IOException ex) {
96 Logger.getLogger(FileWarnings.class.getName()).log(Level.SEVERE,
null, ex);
101 private void walkFolder(String path, String pattern, Consumer<Path> action)
throws IOException {
102 Path folder = Path.of(ElephantContext.getRealPath(path));
103 if(Files.exists(folder)) Files.walk(folder)
104 .filter(Files::isRegularFile)
105 .filter(p -> p.getFileName().toString().matches(pattern))
106 .forEach(file -> action.accept(file));
109 private double scaleFrom(Dimension dimension) {
110 if(dimension.getWidth() > 1800 && ((dimension.getWidth() / dimension.getHeight()) < 4.5)) {
112 }
else if(dimension.getWidth() == 1800 && ((dimension.getWidth() / dimension.getHeight()) < 4.5)) {
115 if(dimension.getWidth() > 1200 && ((dimension.getWidth() / dimension.getHeight()) < 4.5)) {
boolean isInRole(String role)
static Application getApplication()
static String get(String msg)
List< IWarning > getWarnings()