Skip to content
Snippets Groups Projects
Commit 7de264ef authored by Fabio Vandewaeter's avatar Fabio Vandewaeter
Browse files

synchro fonctionne quand dossier local vide

parent 1e5b179b
No related branches found
No related tags found
No related merge requests found
target/
flopbox-sync/
......@@ -235,46 +235,89 @@ public class FlopBoxClient {
*/
public void importFiles(String alias, String remotePath, JsonNode tree, Path localDir)
throws IOException, InterruptedException {
/*
* if (!filesMetadata.containsKey(alias)) {
* filesMetadata.put(alias, new HashMap<>());
* }
*
* // Traitement des dossiers
* if (tree.has("children") && tree.get("children").isArray()) {
* for (JsonNode dir : tree.get("children")) {
* String dirName = dir.get("name").asText();
* String newRemotePath = remotePath.isEmpty() ? dirName : remotePath + "/" +
* dirName;
* Path newLocalDir = localDir.resolve(dirName);
*
* Files.createDirectories(newLocalDir);
*
* // Récupération récursive du contenu du dossier
* JsonNode subTree = fetchFtpTree(alias, newRemotePath);
* importFiles(alias, newRemotePath, subTree, newLocalDir);
* }
* }
*
* // Traitement des fichiers
* if (tree.has("files") && tree.get("files").isArray()) {
* for (JsonNode file : tree.get("files")) {
* String fileName = file.get("name").asText();
* String fileRemotePath = remotePath.isEmpty() ? fileName : remotePath + "/" +
* fileName;
* Path localFile = localDir.resolve(fileName);
*
* // Skip .deleted directory files at root level
* if (fileRemotePath.startsWith(".deleted/")) {
* continue;
* }
*
* // Téléchargement du fichier
* downloadFile(alias, fileRemotePath, localFile);
*
* // Stockage des métadonnées
* long fileSize = Files.size(localFile);
* long lastModified = Files.getLastModifiedTime(localFile).toMillis();
*
* FileInfo fileInfo = new FileInfo(fileRemotePath, fileSize, lastModified);
* filesMetadata.get(alias).put(fileRemotePath, fileInfo);
* }
* }
*/
if (!filesMetadata.containsKey(alias)) {
filesMetadata.put(alias, new HashMap<>());
}
// Traitement des dossiers
// Traitement des éléments dans 'children' (répertoires et fichiers)
if (tree.has("children") && tree.get("children").isArray()) {
for (JsonNode dir : tree.get("children")) {
String dirName = dir.get("name").asText();
String newRemotePath = remotePath.isEmpty() ? dirName : remotePath + "/" + dirName;
Path newLocalDir = localDir.resolve(dirName);
for (JsonNode child : tree.get("children")) {
String childName = child.get("name").asText();
boolean isDirectory = child.get("isDirectory").asBoolean();
String newRemotePath = remotePath.isEmpty() ? childName : remotePath + "/" + childName;
Path newLocalDir = localDir.resolve(childName);
if (isDirectory) {
// Si c'est un répertoire, on le crée et on appelle récursivement pour son
// contenu
Files.createDirectories(newLocalDir);
JsonNode subTree = fetchFtpTree(alias, newRemotePath);
importFiles(alias, newRemotePath, subTree, newLocalDir);
} else {
// Si c'est un fichier, on le télécharge
Path localFile = newLocalDir;
// Skip .deleted directory files at root level
if (newRemotePath.startsWith(".deleted/")) {
continue;
}
Files.createDirectories(newLocalDir);
// Téléchargement du fichier
downloadFile(alias, newRemotePath, localFile);
// Récupération récursive du contenu du dossier
JsonNode subTree = fetchFtpTree(alias, newRemotePath);
importFiles(alias, newRemotePath, subTree, newLocalDir);
}
}
// Stockage des métadonnées
long fileSize = Files.size(localFile);
long lastModified = Files.getLastModifiedTime(localFile).toMillis();
// Traitement des fichiers
if (tree.has("files") && tree.get("files").isArray()) {
for (JsonNode file : tree.get("files")) {
String fileName = file.get("name").asText();
String fileRemotePath = remotePath.isEmpty() ? fileName : remotePath + "/" + fileName;
Path localFile = localDir.resolve(fileName);
// Skip .deleted directory files at root level
if (fileRemotePath.startsWith(".deleted/")) {
continue;
FileInfo fileInfo = new FileInfo(newRemotePath, fileSize, lastModified);
filesMetadata.get(alias).put(newRemotePath, fileInfo);
}
// Téléchargement du fichier
downloadFile(alias, fileRemotePath, localFile);
// Stockage des métadonnées
long fileSize = Files.size(localFile);
long lastModified = Files.getLastModifiedTime(localFile).toMillis();
FileInfo fileInfo = new FileInfo(fileRemotePath, fileSize, lastModified);
filesMetadata.get(alias).put(fileRemotePath, fileInfo);
}
}
}
......@@ -430,6 +473,89 @@ public class FlopBoxClient {
* @throws InterruptedException En cas d'interruption lors de la vérification
*/
public void checkRemoteChanges(String alias, String remotePath) throws IOException, InterruptedException {
/*
* JsonNode tree = fetchFtpTree(alias, remotePath);
* Path localDir = syncDirectory.resolve(alias);
*
* if (!remotePath.isEmpty()) {
* localDir = localDir.resolve(remotePath);
* }
*
* // Traitement des dossiers
* if (tree.has("children") && tree.get("children").isArray()) {
* for (JsonNode dir : tree.get("children")) {
* String dirName = dir.get("name").asText();
*
* // Skip .deleted directory
* if (dirName.equals(".deleted") && remotePath.isEmpty()) {
* continue;
* }
*
* String newRemotePath = remotePath.isEmpty() ? dirName : remotePath + "/" +
* dirName;
* Path newLocalDir = localDir.resolve(dirName);
*
* Files.createDirectories(newLocalDir);
*
* // Récursion pour les sous-répertoires
* checkRemoteChanges(alias, newRemotePath);
* }
* }
*
* // Traitement des fichiers
* if (tree.has("files") && tree.get("files").isArray()) {
* for (JsonNode file : tree.get("files")) {
* String fileName = file.get("name").asText();
* String fileRemotePath = remotePath.isEmpty() ? fileName : remotePath + "/" +
* fileName;
* Path localFile = localDir.resolve(fileName);
*
* long remoteSize = file.get("size").asLong();
* long remoteModified = 0;
*
* if (file.has("timestamp")) {
* remoteModified = file.get("timestamp").asLong();
* }
*
* Map<String, FileInfo> aliasMetadata = filesMetadata.getOrDefault(alias, new
* HashMap<>());
* FileInfo storedInfo = aliasMetadata.get(fileRemotePath);
*
* boolean needDownload = false;
*
* if (!Files.exists(localFile)) {
* // Fichier distant non présent localement
* needDownload = true;
* } else if (storedInfo == null) {
* // Pas d'information stockée pour ce fichier
* needDownload = true;
* } else if (remoteModified > 0 && remoteModified > storedInfo.lastModified) {
* // Fichier distant plus récent
* needDownload = true;
* } else if (remoteSize != storedInfo.size) {
* // Taille différente
* needDownload = true;
* }
*
* if (needDownload) {
* downloadFile(alias, fileRemotePath, localFile);
*
* // Mise à jour des métadonnées
* long actualSize = Files.size(localFile);
* long actualLastModified = Files.getLastModifiedTime(localFile).toMillis();
*
* if (!aliasMetadata.containsKey(fileRemotePath)) {
* aliasMetadata.put(fileRemotePath, new FileInfo(fileRemotePath, actualSize,
* actualLastModified));
* } else {
* storedInfo.size = actualSize;
* storedInfo.lastModified = actualLastModified;
* }
* }
* }
* }
*/
JsonNode tree = fetchFtpTree(alias, remotePath);
Path localDir = syncDirectory.resolve(alias);
......@@ -437,71 +563,67 @@ public class FlopBoxClient {
localDir = localDir.resolve(remotePath);
}
// Traitement des dossiers
// Traitement des éléments dans 'children' (répertoires et fichiers)
if (tree.has("children") && tree.get("children").isArray()) {
for (JsonNode dir : tree.get("children")) {
String dirName = dir.get("name").asText();
for (JsonNode child : tree.get("children")) {
String childName = child.get("name").asText();
boolean isDirectory = child.get("isDirectory").asBoolean();
// Skip .deleted directory
if (dirName.equals(".deleted") && remotePath.isEmpty()) {
if (childName.equals(".deleted") && remotePath.isEmpty()) {
continue;
}
String newRemotePath = remotePath.isEmpty() ? dirName : remotePath + "/" + dirName;
Path newLocalDir = localDir.resolve(dirName);
Files.createDirectories(newLocalDir);
// Récursion pour les sous-répertoires
checkRemoteChanges(alias, newRemotePath);
}
}
String newRemotePath = remotePath.isEmpty() ? childName : remotePath + "/" + childName;
Path newLocalDir = localDir.resolve(childName);
if (isDirectory) {
Files.createDirectories(newLocalDir);
// Récursion pour les sous-répertoires
checkRemoteChanges(alias, newRemotePath);
} else {
// Traitement des fichiers
Path localFile = newLocalDir;
long remoteSize = child.get("size").asLong();
long remoteModified = 0;
if (child.has("timestamp")) {
remoteModified = child.get("timestamp").asLong();
}
// Traitement des fichiers
if (tree.has("files") && tree.get("files").isArray()) {
for (JsonNode file : tree.get("files")) {
String fileName = file.get("name").asText();
String fileRemotePath = remotePath.isEmpty() ? fileName : remotePath + "/" + fileName;
Path localFile = localDir.resolve(fileName);
Map<String, FileInfo> aliasMetadata = filesMetadata.getOrDefault(alias, new HashMap<>());
FileInfo storedInfo = aliasMetadata.get(newRemotePath);
long remoteSize = file.get("size").asLong();
long remoteModified = 0;
boolean needDownload = false;
if (file.has("timestamp")) {
remoteModified = file.get("timestamp").asLong();
}
Map<String, FileInfo> aliasMetadata = filesMetadata.getOrDefault(alias, new HashMap<>());
FileInfo storedInfo = aliasMetadata.get(fileRemotePath);
boolean needDownload = false;
if (!Files.exists(localFile)) {
// Fichier distant non présent localement
needDownload = true;
} else if (storedInfo == null) {
// Pas d'information stockée pour ce fichier
needDownload = true;
} else if (remoteModified > 0 && remoteModified > storedInfo.lastModified) {
// Fichier distant plus récent
needDownload = true;
} else if (remoteSize != storedInfo.size) {
// Taille différente
needDownload = true;
}
if (!Files.exists(localFile)) {
// Fichier distant non présent localement
needDownload = true;
} else if (storedInfo == null) {
// Pas d'information stockée pour ce fichier
needDownload = true;
} else if (remoteModified > 0 && remoteModified > storedInfo.lastModified) {
// Fichier distant plus récent
needDownload = true;
} else if (remoteSize != storedInfo.size) {
// Taille différente
needDownload = true;
}
if (needDownload) {
downloadFile(alias, fileRemotePath, localFile);
if (needDownload) {
downloadFile(alias, newRemotePath, localFile);
// Mise à jour des métadonnées
long actualSize = Files.size(localFile);
long actualLastModified = Files.getLastModifiedTime(localFile).toMillis();
// Mise à jour des métadonnées
long actualSize = Files.size(localFile);
long actualLastModified = Files.getLastModifiedTime(localFile).toMillis();
if (!aliasMetadata.containsKey(fileRemotePath)) {
aliasMetadata.put(fileRemotePath, new FileInfo(fileRemotePath, actualSize, actualLastModified));
} else {
storedInfo.size = actualSize;
storedInfo.lastModified = actualLastModified;
if (!aliasMetadata.containsKey(newRemotePath)) {
aliasMetadata.put(newRemotePath,
new FileInfo(newRemotePath, actualSize, actualLastModified));
} else {
storedInfo.size = actualSize;
storedInfo.lastModified = actualLastModified;
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment