Skip to content

feat: file sync #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/zephyrsoft/sdb2/FileAndDirectoryLocations.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class FileAndDirectoryLocations {
private static final String STATISTICS_FILE_STRING = "statistics.xml";
private static final String DB_FILE_STRING = "db.xml";
private static final String DB_PROPERTIES_FILE_STRING = "db.properties.xml";
private static final String DB_BLOB_SUBDIR_STRING = "blobs";

private static final DateTimeFormatter YEAR_MONTH = DateTimeFormatter.ofPattern("yyyy-MM");

Expand Down Expand Up @@ -90,6 +91,15 @@ public static String getStatisticsMonthlyExportFileName(LocalDate date) {
return getStatisticsDir() + File.separator + YEAR_MONTH.format(date) + ".xls";
}

/** this is used when remote control (via MQTT) is active */
public static String getDBBlobDir() {
if (Options.getInstance().getBlobDatabaseDir() == null) {
return getDir(DB_BLOB_SUBDIR_STRING, true);
} else {
return getDir(Options.getInstance().getBlobDatabaseDir(), false);
}
}

/** this is used when remote control (via MQTT) is active */
private static String getDBDir() {
if (Options.getInstance().getDatabaseDir() == null) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/zephyrsoft/sdb2/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public static Options getInstance() {
@Option(name = "--database", aliases = "-db", metaVar = "<DIR>", usage = "use this directory as database storage (optional, the default is ~/.songdatabase/db/)")
private String databaseDir = null;

private static final String PROP_BLOB_DATABASE_DIR = "blob_database";
/** this is used when remote control (via MQTT) is active */
@Option(name = "--blob-database", aliases = "-blob-db", metaVar = "<DIR>", usage = "use this directory as blob database storage (optional, the default is ~/.songdatabase/blobs/)")
private String blobDatabaseDir = null;

private static final String PROP_SONGS_FILE = "songs-file";
@Argument(metaVar = "<FILE>", usage = "use this file to load from and save to (optional, the default is ~/.songdatabase/songs/songs.xml)")
private String songsFile = null;
Expand Down Expand Up @@ -177,6 +182,14 @@ private void setDatabaseDir(String databaseDir) {
this.databaseDir = databaseDir;
}

public String getBlobDatabaseDir() {
return blobDatabaseDir;
}

private void setBlobDatabaseDir(String blobDatabaseDir) {
this.blobDatabaseDir = blobDatabaseDir;
}

public String getSongsFile() {
return songsFile;
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/zephyrsoft/sdb2/gui/SongCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.awt.Insets;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
Expand All @@ -31,6 +33,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zephyrsoft.sdb2.FileAndDirectoryLocations;
import org.zephyrsoft.sdb2.model.Song;
import org.zephyrsoft.sdb2.util.StringTools;
import org.zephyrsoft.sdb2.util.gui.ImageTools;
Expand Down Expand Up @@ -128,7 +131,11 @@ public void setImage(final String imageUrl, int degreesToRotateRight) {
this.image.setVisible(false);
} else {
try {
ImageIcon imageIcon = new ImageIcon(URI.create(imageUrl).toURL());
URI imageUri = URI.create(imageUrl);
if (imageUri.getScheme().equals("sdb")){
imageUri = Paths.get(FileAndDirectoryLocations.getDBBlobDir(), imageUrl.replace("sdb://", "")).toUri();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of duplicated by the code in PresenterWindow.java around line 239. Maybe a common method handling this replacement could be added? Maybe even more high-level in logic flow, like after loading the songs database?

BTW: It should also be checked if the newly referenced file exists - if not, this will fail when trying to display the image.

}
ImageIcon imageIcon = new ImageIcon(imageUri.toURL());
Image image = imageIcon.getImage();
image = ImageTools.rotate(image, degreesToRotateRight);
double factor = (songTitle.getPreferredSize().getHeight() + firstLine.getPreferredSize().getHeight()
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/zephyrsoft/sdb2/model/XMLConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.zephyrsoft.sdb2.model.settings.SettingsModel;
import org.zephyrsoft.sdb2.model.statistics.StatisticsModel;
import org.zephyrsoft.sdb2.remote.ChangeReject;
import org.zephyrsoft.sdb2.remote.FileRequest;
import org.zephyrsoft.sdb2.remote.FileSetResponse;
import org.zephyrsoft.sdb2.remote.PatchRequest;
import org.zephyrsoft.sdb2.remote.Patches;
import org.zephyrsoft.sdb2.remote.Position;
Expand Down Expand Up @@ -65,7 +67,7 @@ public static <T extends Persistable> T fromXMLToPersistable(InputStream xmlInpu

private static JAXBContext createContext() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(SongsModel.class, SettingsModel.class, StatisticsModel.class, Version.class,
PatchRequest.class, Song.class, Patches.class, ChangeReject.class, Position.class);
PatchRequest.class, Song.class, Patches.class, ChangeReject.class, Position.class, FileRequest.class, FileSetResponse.class);
return context;
}

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/zephyrsoft/sdb2/presenter/PresenterWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand All @@ -34,6 +35,7 @@
import org.jdesktop.core.animation.timing.interpolators.LinearInterpolator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zephyrsoft.sdb2.FileAndDirectoryLocations;
import org.zephyrsoft.sdb2.MainController;
import org.zephyrsoft.sdb2.model.AddressablePart;
import org.zephyrsoft.sdb2.model.SelectableDisplay;
Expand Down Expand Up @@ -159,7 +161,9 @@ && noSettingsWereChanged()) {
toFront();
return;
} else if (presentationPosition == null) {
songView.moveToLine(null, null, true);
if (songView != null ) {
songView.moveToLine(null, null, true);
}
this.presentationPosition = null;
toFront();
return;
Expand Down Expand Up @@ -229,8 +233,16 @@ && noSettingsWereChanged()) {
} else if (presentable.getImage() != null || ( presentable.getSong() != null && !StringTools.isEmpty(presentable.getSong().getImage()))) {
// display the image (fullscreen, but with margin)
try {
ImageIcon imageIcon = presentable.getImage() == null ? new ImageIcon(URI.create(presentable.getSong().getImage()).toURL())
: new ImageIcon(presentable.getImage());
ImageIcon imageIcon;
if (presentable.getImage() == null) {
URI imageUri = URI.create(presentable.getSong().getImage());
if (imageUri.getScheme().equals("sdb")){
imageUri = Paths.get(FileAndDirectoryLocations.getDBBlobDir(), imageUri.toString().replace("sdb://", "")).toUri();
}
imageIcon = new ImageIcon(imageUri.toURL());
}else {
imageIcon = new ImageIcon(presentable.getImage());
}
Image image = imageIcon.getImage();
if (presentable.getImage() == null) {
image = ImageTools.rotate(image, presentable.getSong().getImageRotationAsInt());
Expand All @@ -257,7 +269,7 @@ && noSettingsWereChanged()) {
revalidate();
repaint();

if (presentable.getSong() != null) {
if (presentable.getSong() != null && StringTools.isEmpty(presentable.getSong().getImage())) {
PresentationPosition.forSong(presentationPosition)
.ifPresent(spp -> {
// queue for LATER because the revalidate/repaint above only will run
Expand Down
154 changes: 154 additions & 0 deletions src/main/java/org/zephyrsoft/sdb2/remote/FileController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.zephyrsoft.sdb2.remote;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file header is missing, containing license information.


import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;

import org.zephyrsoft.sdb2.FileAndDirectoryLocations;
import org.zephyrsoft.sdb2.model.Song;
import org.zephyrsoft.sdb2.model.SongsModel;
import org.zephyrsoft.sdb2.remote.MqttObject.OnChangeListener;
import org.zephyrsoft.sdb2.util.StringTools;

public class FileController {

private RemoteController remoteController;

public FileController(RemoteController remoteController) {
this.remoteController = remoteController;
this.remoteController.getFilesRequestFile().onRemoteChange((data, args) -> {
try {
Files.createDirectories(Paths.get(FileAndDirectoryLocations.getDBBlobDir()));
} catch (IOException e) {
e.printStackTrace();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to log the exception, stdout is not watched

}
try {
Files.write(Paths.get(FileAndDirectoryLocations.getDBBlobDir(), (String) args[0]), data);
} catch (IOException e) {
e.printStackTrace();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to log the exception, stdout is not watched

}
});
}

private Collection<String> getMissingFiles(Song song){
HashSet<String> missingFiles = new HashSet<>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this create a set if it adds at most one entry?


if(!StringTools.isEmpty(song.getImage()) && URI.create(song.getImage()).getScheme().equals("sdb")) {
Path path = Paths.get(FileAndDirectoryLocations.getDBBlobDir(), song.getImage().replace("sdb://", ""));
if (!Files.isRegularFile(path)) {
missingFiles.add(path.getFileName().toString());
}
}
return missingFiles;
}

public void downloadFiles(Song song, Runnable callback) {
this.downloadFiles(getMissingFiles(song), callback);
}

public void downloadFiles(SongsModel songsModel, Runnable callback) {
HashSet<String> missingFiles = new HashSet<>();

for (Song song: songsModel.getSongs()) {
missingFiles.addAll(getMissingFiles(song));
}

this.downloadFiles(missingFiles, callback);
}

public void downloadFiles(Collection<String> files, Runnable callback) {
if(files.isEmpty()) {
callback.run();
return;
}
HashSet<String> missingFiles = new HashSet<>(files);

this.remoteController.getFilesRequestFile().onRemoteChange(new OnChangeListener<byte[]>() {
@Override
public void onChange(byte[] object, Object... args) {
String fileName = (String)args[0];
if ( missingFiles.contains(fileName)){
missingFiles.remove(fileName);
if (missingFiles.isEmpty()) {
callback.run();
remoteController.getFilesRequestFile().removeOnRemoteChangeListener(this);
}
}
}
});
missingFiles.forEach((fileName) -> remoteController.getFilesRequestGet().set(new FileRequest(fileName)));
}


public void uploadFiles(SongsModel songsModel, Consumer<SongsModel> callback) {
HashSet<String> localFiles = new HashSet<>();

for (Song song: songsModel.getSongs()) {
if(!StringTools.isEmpty(song.getImage()) && URI.create(song.getImage()).getScheme().equals("file")) {
Path path = Paths.get(URI.create(song.getImage()));
String oldFilename = path.getFileName().toString();
Optional<String> fileExtension = Optional.ofNullable(oldFilename)
.filter(f -> f.contains("."))
.map(f -> f.substring(oldFilename.lastIndexOf(".")));
String newFilename = StringTools.createUUID() + fileExtension.get();
localFiles.add(newFilename);
Path dbPath = Paths.get(FileAndDirectoryLocations.getDBBlobDir(), newFilename);
try {
Files.createDirectories(Paths.get(FileAndDirectoryLocations.getDBBlobDir()));
} catch (IOException e) {
e.printStackTrace();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to log the exception, stdout is not watched

}
try {
Files.copy(path, dbPath, StandardCopyOption.COPY_ATTRIBUTES);
} catch (IOException e) {
e.printStackTrace();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to log the exception, stdout is not watched

}
song.setImage("sdb://"+newFilename);
}
}
if(localFiles.isEmpty()) {
callback.accept(songsModel);
return;
}

this.remoteController.getFilesRequestSetResponse().onRemoteChange(new OnChangeListener<FileSetResponse>() {
@Override
public void onChange(FileSetResponse object, Object... args) {
String fileName = object.getUuid();
if ( localFiles.contains(fileName) ){
if(!object.isOk()) {
System.err.println("Could not upload file " + fileName + " Reason: " + object.getReason());
remoteController.getFilesRequestSetResponse().removeOnRemoteChangeListener(this);
}else {
localFiles.remove(fileName);
if (localFiles.isEmpty()) {
callback.accept(songsModel);
remoteController.getFilesRequestSetResponse().removeOnRemoteChangeListener(this);
}
}
}
}
});

localFiles.forEach((fileName) -> {
Path dbPath = Paths.get(FileAndDirectoryLocations.getDBBlobDir(), fileName);
try {
byte[] content = Files.readAllBytes(dbPath);
remoteController.getFilesRequestSet().set(content, fileName);
} catch (IOException e) {
e.printStackTrace();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to log the exception, stdout is not watched

}
});
}

}
49 changes: 49 additions & 0 deletions src/main/java/org/zephyrsoft/sdb2/remote/FileRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of the Song Database (SDB).
*
* SDB is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License 3.0 as published by
* the Free Software Foundation.
*
* SDB is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License 3.0 for more details.
*
* You should have received a copy of the GNU General Public License 3.0
* along with SDB. If not, see <http://www.gnu.org/licenses/>.
*/
package org.zephyrsoft.sdb2.remote;

import org.zephyrsoft.sdb2.model.Persistable;

import jakarta.xml.bind.annotation.XmlAccessOrder;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorOrder;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "fileRequest")
@XmlAccessorType(XmlAccessType.NONE)
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class FileRequest implements Persistable {
private static final long serialVersionUID = 8867565661007188776L;

@XmlElement(name = "uuid")
private String uuid;

public FileRequest() {
initIfNecessary();
}

public FileRequest(String uuid) {
this();
this.uuid = uuid;
}

@Override
public void initIfNecessary() {
uuid = null;
}
}
Loading