exception

This commit is contained in:
Theophyl Hoguet committed 2023-03-13 22:52:22 +01:00
1 parent 43b15faa41
commit 7583274fff
2 files changed
+66 -55

No files matched your search

+57 -55
View File
@@ -16,68 +16,70 @@ import org.springframework.stereotype.Repository;
@Repository
public class ImageDao implements Dao<Image> {
private final Map<Long, Image> images = new HashMap<>();
private final Map<Long, Image> images = new HashMap<>();
public ImageDao() {
try {
final var folderPath = new ClassPathResource("images");
final var folder = folderPath.getFile();
getAllImages(folder);
} catch (final Exception e) {
e.printStackTrace();
}
}
public ImageDao() {
try {
final var folderPath = new ClassPathResource("images");
if (folderPath.exists())
throw new ImageResourceMissingException("folderPath is null");
final var folder = folderPath.getFile();
getAllImages(folder);
} catch (final Exception e) {
e.printStackTrace();
}
}
public void getAllImages(final File folder) {
for (final File file : folder.listFiles()) {
if (file.isDirectory()) {
getAllImages(file);
} else {
String extension = "";
public void getAllImages(final File folder) {
for (final File file : folder.listFiles()) {
if (file.isDirectory()) {
getAllImages(file);
} else {
String extension = "";
int i = file.getName().lastIndexOf('.');
if (i > 0) {
extension = file.getName().substring(i + 1);
}
if (!extension.equals("jpg") && !extension.equals("png")) {
continue;
}
byte[] fileContent;
try {
fileContent = Files.readAllBytes(file.toPath());
Image img = new Image(file.getName(), fileContent);
images.put(img.getId(), img);
} catch (final IOException e) {
e.printStackTrace();
}
}
}
}
int i = file.getName().lastIndexOf('.');
if (i > 0) {
extension = file.getName().substring(i + 1);
}
if (!extension.equals("jpg") && !extension.equals("png")) {
continue;
}
byte[] fileContent;
try {
fileContent = Files.readAllBytes(file.toPath());
Image img = new Image(file.getName(), fileContent);
images.put(img.getId(), img);
} catch (final IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public Optional<Image> retrieve(final long id) {
return Optional.ofNullable(images.get(id));
}
@Override
public Optional<Image> retrieve(final long id) {
return Optional.ofNullable(images.get(id));
}
@Override
public List<Image> retrieveAll() {
return new ArrayList<Image>(images.values());
}
@Override
public List<Image> retrieveAll() {
return new ArrayList<Image>(images.values());
}
@Override
public void create(final Image img) {
images.put(img.getId(), img);
}
@Override
public void create(final Image img) {
images.put(img.getId(), img);
}
@Override
public void update(final Image img, final String[] params) {
img.setName(Objects.requireNonNull(params[0], "Name cannot be null"));
@Override
public void update(final Image img, final String[] params) {
img.setName(Objects.requireNonNull(params[0], "Name cannot be null"));
images.put(img.getId(), img);
}
images.put(img.getId(), img);
}
@Override
public void delete(final Image img) {
images.remove(img.getId());
}
@Override
public void delete(final Image img) {
images.remove(img.getId());
}
}
@@ -0,0 +1,9 @@
package pdl.backend;
import java.io.FileNotFoundException;
public class ImageResourceMissingException extends FileNotFoundException {
public ImageResourceMissingException(String message) {
super(message);
}
}