update database, caching thumbnails and other few fixes

This commit is contained in:
Mateusz Kowalczyk 2020-01-23 17:29:47 +01:00
parent 9ba10d92e4
commit 27b762ef00
7 changed files with 162 additions and 69 deletions

View File

@ -1,15 +1,16 @@
package dev.mateuszkowalczyk.ffm.app; package dev.mateuszkowalczyk.ffm.app;
import dev.mateuszkowalczyk.ffm.app.cache.CacheService;
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo; import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
import dev.mateuszkowalczyk.ffm.data.database.photo.PhotoDAO; import dev.mateuszkowalczyk.ffm.data.database.photo.PhotoDAO;
import dev.mateuszkowalczyk.ffm.image.ThumbnailService;
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader; import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
import dev.mateuszkowalczyk.ffm.utils.Property; import dev.mateuszkowalczyk.ffm.utils.Property;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import java.io.File; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -18,6 +19,7 @@ import java.util.ArrayList;
public class DirectoryScanner implements Runnable { public class DirectoryScanner implements Runnable {
private WorkspaceService workspaceService = WorkspaceService.getInstance(); private WorkspaceService workspaceService = WorkspaceService.getInstance();
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance(); private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
private ThumbnailService thumbnailService = new ThumbnailService();
private PhotoDAO photoDAO = new PhotoDAO(); private PhotoDAO photoDAO = new PhotoDAO();
@Override @Override
@ -33,19 +35,26 @@ public class DirectoryScanner implements Runnable {
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.forEach(path -> { .forEach(path -> {
String filename = path.getFileName().toString(); String filename = path.getFileName().toString();
if(this.isImagePath(filename)) { if(this.isImagePath(filename) && !path.toString().contains(".cache")) {
ImageView imageView = new ImageView();
String newImagePath = CacheService.getInstance().createCachedThumbnail(path.toString());
Photo photo = new Photo(); Photo photo = new Photo();
photo.setPath(path.toString()); photo.setPath(path.toString());
photo.setCachedPath(newImagePath); photo.setFileName(filename);
BufferedImage image = this.thumbnailService.createThumbnail(photo);
this.photoDAO.save(photo); this.photoDAO.save(photo);
// Image image1 = new Image("file:" + path.toString(), 200, 200, false, true); WritableImage wr = null;
Image image1 = new Image("file:" + newImagePath); if (image != null) {
wr = new WritableImage(image.getWidth(), image.getHeight());
PixelWriter pw = wr.getPixelWriter();
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
pw.setArgb(x, y, image.getRGB(x, y));
}
}
}
imageView.setImage(image1); ImageView imageView = new ImageView(wr);
imageViews.add(imageView); imageViews.add(imageView);
Platform.runLater(() -> WorkspaceService.getInstance().getMainPageController().addImage(imageView)); Platform.runLater(() -> WorkspaceService.getInstance().getMainPageController().addImage(imageView));

View File

@ -1,68 +1,43 @@
package dev.mateuszkowalczyk.ffm.app.cache; package dev.mateuszkowalczyk.ffm.app.cache;
import com.sun.javafx.sg.prism.NGRectangle;
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader; import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
import dev.mateuszkowalczyk.ffm.utils.Property; import dev.mateuszkowalczyk.ffm.utils.Property;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CacheService { public class CacheService {
private static final CacheService instance = new CacheService(); private static CacheService instance;
private String path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY); private String path = "";
private CacheService () { private CacheService () {
this.check(); this.check();
} }
public static CacheService getInstance() { public synchronized static CacheService getInstance() {
if(instance == null) {
instance = new CacheService();
}
return instance; return instance;
} }
public void check() { public String getPath() {
CacheStructureChecker cacheStructureChecker = new CacheStructureChecker(); return path;
cacheStructureChecker.check();
} }
public String createCachedThumbnail(String path) { public void check() {
this.path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY); this.updatePath();
BufferedImage bufferedImage = null; if (this.path != null) {
File image = null; CacheStructureChecker cacheStructureChecker = new CacheStructureChecker();
File newImageFile = null; cacheStructureChecker.check(this.path);
String pathToNewFile = null; }
}
try { private String updatePath() {
image = new File(path); String path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY);
bufferedImage = ImageIO.read(image);
float divider; if (path != null) {
if (bufferedImage.getWidth() > bufferedImage.getHeight()) { this.path = path + "/.cache";
divider = bufferedImage.getWidth() / 200;
} else {
divider = bufferedImage.getHeight() / 200;
}
int width = (int) (bufferedImage.getWidth() / divider);
int height = (int) (bufferedImage.getHeight() / divider);
BufferedImage newImage = new BufferedImage(width, height, bufferedImage.getType());
Graphics2D graphics2D = newImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(bufferedImage, 0, 0, width, height, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), null);
graphics2D.dispose();
pathToNewFile = this.path + "/.cache/thumbnails/" + path.substring(this.path.length());
newImageFile = new File(pathToNewFile);
ImageIO.write(newImage, "jpg", newImageFile);
} catch (IOException e) {
e.printStackTrace();
} }
return pathToNewFile; return path;
} }
} }

View File

@ -1,10 +1,6 @@
package dev.mateuszkowalczyk.ffm.app.cache; package dev.mateuszkowalczyk.ffm.app.cache;
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
import dev.mateuszkowalczyk.ffm.utils.Property;
import java.io.File; import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -12,19 +8,24 @@ public class CacheStructureChecker {
private String path; private String path;
private List<String> listToCheck = new ArrayList<String>(); private List<String> listToCheck = new ArrayList<String>();
public CacheStructureChecker() {}
public void check() { public void check() {
this.path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY); this.check(CacheService.getInstance().getPath());
}
public void check(String path) {
this.path = path;
if (this.path != null) { if (this.path != null) {
this.path += "/.cache";
this.setupPathsToCheck(); this.setupPathsToCheck();
this.listToCheck.forEach(s -> this.checkDirectory(s)); this.listToCheck.forEach(this::checkDirectory);
} }
} }
private void setupPathsToCheck() { private void setupPathsToCheck() {
this.listToCheck.add(this.path); this.listToCheck.add(this.path);
this.listToCheck.add(this.path + "/thumbnails"); this.listToCheck.add(this.path + ThumbnailCacheService.DIRECTORY_NAME);
} }

View File

@ -0,0 +1,49 @@
package dev.mateuszkowalczyk.ffm.app.cache;
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ThumbnailCacheService implements Runnable {
protected static final String DIRECTORY_NAME = "/thumbnails";
private BufferedImage image;
private CacheService cacheService = CacheService.getInstance();
private String path;
public ThumbnailCacheService() {
}
public ThumbnailCacheService(BufferedImage bufferedImage) {
this.image = bufferedImage;
}
@Override
public void run() {
this.createCachedThumbnail();
}
public void createPath(Photo photo) {
this.path = this.cacheService.getPath() + DIRECTORY_NAME + "/" + photo.getFileName();
photo.setCachedPath(this.path);
}
private void createCachedThumbnail() {
if (image != null && this.path != null) {
File imageFile = new File(this.path);
try {
ImageIO.write(this.image, this.fileExtension(), imageFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String fileExtension() {
return this.path.substring(this.path.length() - 3);
}
}

View File

@ -32,10 +32,10 @@ public class DatabaseService {
public void connect() { public void connect() {
if (this.connection == null) { if (this.connection == null) {
String path = ResourceLoader.getInstance().getPath() + "app.db"; String path = ResourceLoader.getInstance().getPath("app.db");
try { try {
this.connection = DriverManager.getConnection(path); this.connection = DriverManager.getConnection("jdbc:sqlite:" + path);
} catch (SQLException e) { } catch (SQLException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }

View File

@ -10,13 +10,21 @@ public class Photo {
@Column(type = Column.Type.INT) @Column(type = Column.Type.INT)
@PrimaryKey @PrimaryKey
private long id; private long id;
@Column @Column
private String path; private String path;
@Column
private String fileName;
@Column @Column
private String cachedPath; private String cachedPath;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public long getId() { public long getId() {
return id; return id;
} }

View File

@ -0,0 +1,51 @@
package dev.mateuszkowalczyk.ffm.image;
import dev.mateuszkowalczyk.ffm.app.cache.ThumbnailCacheService;
import dev.mateuszkowalczyk.ffm.data.database.photo.Photo;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ThumbnailService {
public BufferedImage createThumbnail(Photo photo) {
BufferedImage originalImage;
BufferedImage image = null;
try {
File originalImageFile = new File(photo.getPath());
originalImage = ImageIO.read(originalImageFile);
float divider;
if (originalImage.getWidth() > originalImage.getHeight()) {
divider = originalImage.getWidth() / 200;
} else {
divider = originalImage.getHeight() / 200;
}
int width = (int) (originalImage.getWidth() / divider);
int height = (int) (originalImage.getHeight() / divider);
image = new BufferedImage(width, height, originalImage.getType());
Graphics2D graphics2D = image.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(originalImage, 0, 0, width, height, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
graphics2D.dispose();
ThumbnailCacheService thumbnailCacheService = new ThumbnailCacheService(image);
thumbnailCacheService.createPath(photo);
var t = new Thread(thumbnailCacheService);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
}