-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: master
Are you sure you want to change the base?
feat: file sync #321
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package org.zephyrsoft.sdb2.remote; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be better to log the exception, stdout is not watched |
||
} | ||
}); | ||
} | ||
|
||
} |
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; | ||
} | ||
} |
There was a problem hiding this comment.
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.