update database, caching thumbnails and other few fixes
This commit is contained in:
parent
9ba10d92e4
commit
27b762ef00
@ -1,15 +1,16 @@
|
||||
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.PhotoDAO;
|
||||
import dev.mateuszkowalczyk.ffm.image.ThumbnailService;
|
||||
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
|
||||
import dev.mateuszkowalczyk.ffm.utils.Property;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.image.Image;
|
||||
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.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
@ -18,6 +19,7 @@ import java.util.ArrayList;
|
||||
public class DirectoryScanner implements Runnable {
|
||||
private WorkspaceService workspaceService = WorkspaceService.getInstance();
|
||||
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
||||
private ThumbnailService thumbnailService = new ThumbnailService();
|
||||
private PhotoDAO photoDAO = new PhotoDAO();
|
||||
|
||||
@Override
|
||||
@ -33,19 +35,26 @@ public class DirectoryScanner implements Runnable {
|
||||
.filter(Files::isRegularFile)
|
||||
.forEach(path -> {
|
||||
String filename = path.getFileName().toString();
|
||||
if(this.isImagePath(filename)) {
|
||||
ImageView imageView = new ImageView();
|
||||
String newImagePath = CacheService.getInstance().createCachedThumbnail(path.toString());
|
||||
|
||||
if(this.isImagePath(filename) && !path.toString().contains(".cache")) {
|
||||
Photo photo = new Photo();
|
||||
photo.setPath(path.toString());
|
||||
photo.setCachedPath(newImagePath);
|
||||
photo.setFileName(filename);
|
||||
|
||||
BufferedImage image = this.thumbnailService.createThumbnail(photo);
|
||||
this.photoDAO.save(photo);
|
||||
|
||||
// Image image1 = new Image("file:" + path.toString(), 200, 200, false, true);
|
||||
Image image1 = new Image("file:" + newImagePath);
|
||||
WritableImage wr = null;
|
||||
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);
|
||||
|
||||
Platform.runLater(() -> WorkspaceService.getInstance().getMainPageController().addImage(imageView));
|
||||
|
@ -1,68 +1,43 @@
|
||||
package dev.mateuszkowalczyk.ffm.app.cache;
|
||||
|
||||
import com.sun.javafx.sg.prism.NGRectangle;
|
||||
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
|
||||
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 {
|
||||
private static final CacheService instance = new CacheService();
|
||||
private String path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY);
|
||||
private static CacheService instance;
|
||||
private String path = "";
|
||||
|
||||
private CacheService () {
|
||||
this.check();
|
||||
}
|
||||
|
||||
public static CacheService getInstance() {
|
||||
public synchronized static CacheService getInstance() {
|
||||
if(instance == null) {
|
||||
instance = new CacheService();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void check() {
|
||||
CacheStructureChecker cacheStructureChecker = new CacheStructureChecker();
|
||||
cacheStructureChecker.check();
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public String createCachedThumbnail(String path) {
|
||||
this.path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY);
|
||||
BufferedImage bufferedImage = null;
|
||||
File image = null;
|
||||
File newImageFile = null;
|
||||
String pathToNewFile = null;
|
||||
public void check() {
|
||||
this.updatePath();
|
||||
if (this.path != null) {
|
||||
CacheStructureChecker cacheStructureChecker = new CacheStructureChecker();
|
||||
cacheStructureChecker.check(this.path);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
image = new File(path);
|
||||
bufferedImage = ImageIO.read(image);
|
||||
private String updatePath() {
|
||||
String path = PropertiesLoader.getInstance().get(Property.PATH_TO_DIRECTORY);
|
||||
|
||||
float divider;
|
||||
if (bufferedImage.getWidth() > bufferedImage.getHeight()) {
|
||||
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();
|
||||
if (path != null) {
|
||||
this.path = path + "/.cache";
|
||||
}
|
||||
|
||||
return pathToNewFile;
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,6 @@
|
||||
package dev.mateuszkowalczyk.ffm.app.cache;
|
||||
|
||||
import dev.mateuszkowalczyk.ffm.utils.PropertiesLoader;
|
||||
import dev.mateuszkowalczyk.ffm.utils.Property;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -12,19 +8,24 @@ public class CacheStructureChecker {
|
||||
private String path;
|
||||
private List<String> listToCheck = new ArrayList<String>();
|
||||
|
||||
public CacheStructureChecker() {}
|
||||
|
||||
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) {
|
||||
this.path += "/.cache";
|
||||
this.setupPathsToCheck();
|
||||
this.listToCheck.forEach(s -> this.checkDirectory(s));
|
||||
this.listToCheck.forEach(this::checkDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
private void setupPathsToCheck() {
|
||||
this.listToCheck.add(this.path);
|
||||
this.listToCheck.add(this.path + "/thumbnails");
|
||||
this.listToCheck.add(this.path + ThumbnailCacheService.DIRECTORY_NAME);
|
||||
}
|
||||
|
||||
|
||||
|
49
src/main/java/dev/mateuszkowalczyk/ffm/app/cache/ThumbnailCacheService.java
vendored
Normal file
49
src/main/java/dev/mateuszkowalczyk/ffm/app/cache/ThumbnailCacheService.java
vendored
Normal 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);
|
||||
}
|
||||
}
|
@ -32,10 +32,10 @@ public class DatabaseService {
|
||||
|
||||
public void connect() {
|
||||
if (this.connection == null) {
|
||||
String path = ResourceLoader.getInstance().getPath() + "app.db";
|
||||
String path = ResourceLoader.getInstance().getPath("app.db");
|
||||
|
||||
try {
|
||||
this.connection = DriverManager.getConnection(path);
|
||||
this.connection = DriverManager.getConnection("jdbc:sqlite:" + path);
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
@ -10,13 +10,21 @@ public class Photo {
|
||||
@Column(type = Column.Type.INT)
|
||||
@PrimaryKey
|
||||
private long id;
|
||||
|
||||
@Column
|
||||
private String path;
|
||||
|
||||
@Column
|
||||
private String fileName;
|
||||
@Column
|
||||
private String cachedPath;
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user