FriendCloudProto/src/main/java/moe/nekojimi/friendcloud/SharedFileManager.java

113 lines
3 KiB
Java
Raw Normal View History

package moe.nekojimi.friendcloud;
import engineering.swat.watch.Watch;
import moe.nekojimi.friendcloud.objects.NetworkFile;
import moe.nekojimi.friendcloud.objects.NetworkObject;
import moe.nekojimi.friendcloud.protos.ObjectStatements;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class SharedFileManager
{
private final List<Watch> watches = new ArrayList<>();
public void addSharedFiles(Set<File> files)
{
if (files.isEmpty())
return;
Controller controller = Main.getInstance().getModel();
2025-10-02 00:13:49 +01:00
try(ObjectChangeTransaction transaction = ObjectChangeTransaction.startTransaction(controller.getLocalPeer().getObjectID()))
{
List<NetworkObject> knownFiles = controller.listObjects(Set.of(ObjectStatements.ObjectType.OBJECT_TYPE_FILE));
for (NetworkObject knownFile: knownFiles)
{
NetworkFile f = (NetworkFile) knownFile;
boolean removed = files.remove(f.getLocalFile());
if (removed)
System.out.println("Identified known local file " + f.getObjectID() + " = " + f.getLocalFile());
}
for (File sharedFile: files)
{
if (sharedFile.exists())
{
System.out.println("Adding shared network file: " + sharedFile.getAbsolutePath());
NetworkFile networkFile = controller.createObjectByType(ObjectStatements.ObjectType.OBJECT_TYPE_FILE);
transaction.addObjectBeforeChange(networkFile);
networkFile.updateFromLocalFile(sharedFile);
controller.objectChanged(networkFile);
}
}
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void scanForFileChanges()
{
List<NetworkObject> objects = Main.getInstance().getModel().listObjects(Set.of(ObjectStatements.ObjectType.OBJECT_TYPE_FILE));
scanForFileChanges(objects);
}
public void scanForFileChanges(List<NetworkObject> objects)
{
// check every NetworkFile to see if any are different from our records
for (NetworkObject object: objects)
{
NetworkFile file = (NetworkFile) object;
if (!file.hasLocalFile())
continue;
File localFile = file.getLocalFile();
boolean changed = (localFile.length() != file.getSize());
if (!changed)
{
Util.HashOutput hashOutput = Util.hashFile(localFile, file.getPieceSize());
changed = ! Arrays.equals(hashOutput.totalDigest, file.getHash());
}
}
}
public void scanForDirectoryChanges()
{
List<SharedDirectory> directories = Main.getInstance().getModel().getDataStore().getDAOForClass(SharedDirectory.class).getAll();
for (SharedDirectory directory: directories)
{
for (File file : Objects.requireNonNull(directory.getDirectory().listFiles()))
{
}
}
}
public boolean startWatchingFiles()
{
// if (watchService == null)
// {
// try
// {
// watchService = FileSystems.getDefault().newWatchService();
// } catch (IOException e)
// {
// e.printStackTrace(System.err);
// return false;
// }
// }
throw new UnsupportedOperationException("NYI");
}
private void filesChanged(Collection<NetworkFile> changed)
{
}
private void newFiles(Collection<File> newFiles)
{
}
}