BrightSide Workbench Full Report + Source Code
FileWarnings.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.warnings;
20 
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;
36 
41 //@ElephantWarning
42 public class FileWarnings implements IElephantWarning {
43 
44  @Override
45  public String getHeader() {
46  return I_.get("Files");
47  }
48 
49  @Override
50  public List<IWarning> getWarnings() {
51  if(Application.getApplication().isInRole("context:list")) {
52  return createModel();
53  }
54  return Collections.EMPTY_LIST;
55  }
56 
57  private List<IWarning> createModel() {
58  List<IWarning> warnings = new ArrayList<>();
59  checkImages(warnings);
60  return warnings;
61  }
62 
63  private void checkImages(List<IWarning> warnings) {
64  Consumer<Path> action = (path) -> {
65  String relative = ElephantContext.getRelativePath(path.toString());
66  try {
67  Dimension dimension = ImageUtil.dimension(path.toFile());
68  double scaleTo = scaleFrom(dimension);
69  if(scaleTo > -1) {
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);
75  }
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);
81  }
82  };
83  walkContexts("(?i).*(jpg|png|gif)", action);
84  try {
85  walkFolder("/_internal/files", "(?i).*(jpg|png|gif)", action);
86  } catch (IOException ex) {
87  Logger.getLogger(FileWarnings.class.getName()).log(Level.SEVERE, null, ex);
88  }
89  }
90 
91  private void walkContexts(String pattern, Consumer<Path> action) {
92  ElContextMap.getInstance().values().forEach(elContext -> {
93  try {
94  walkFolder(elContext.getRepositoryPath(), pattern, action);
95  } catch (IOException ex) {
96  Logger.getLogger(FileWarnings.class.getName()).log(Level.SEVERE, null, ex);
97  }
98  });
99  }
100 
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));
107  }
108 
109  private double scaleFrom(Dimension dimension) {
110  if(dimension.getWidth() > 1800 && ((dimension.getWidth() / dimension.getHeight()) < 4.5)) {
111  return 1800.0;
112  } else if(dimension.getWidth() == 1800 && ((dimension.getWidth() / dimension.getHeight()) < 4.5)) {
113  return -1;
114  }
115  if(dimension.getWidth() > 1200 && ((dimension.getWidth() / dimension.getHeight()) < 4.5)) {
116  return 1200.0;
117  }
118  return -1;
119  }
120 
121 }
static String get(String msg)
Definition: I_.java:41
List< IWarning > getWarnings()