diff --git a/README.md b/README.md index e63268a3b068ae38d8279dcff8fb2c02ed971a6a..674122418c8c49c7d8fb0569bb43609787c59581 100644 --- a/README.md +++ b/README.md @@ -103,16 +103,28 @@ Note: Unnecessary use of -X or --request, GET is already inferred. **Note comprise entre 11 et 12 si—en plus—le proxy FlopBox, permet de télécharger (download) et téléverser (upload) un petit fichier texte:** -- download : +- download fichier : ```shell curl -X GET -H "Authorization: Bearer valid-token-1" http://localhost:8080/ftps/mon-ftp/test ``` -- upload : +-download dossier : +```shell +curl -X GET -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" http://localhost:8080/ftps/mon-ftp/dir4 -o test.zip + +unzip test.zip +``` + +- upload fichier : ```shell curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer valid-token-1" -d '{"alias":"mon-ftp","host":"localhost","port":2121}' http://localhost:8080/ftps ``` +- upload dossier : + +```shell +``` + **Note comprise entre 12 et 13 si—en plus—le proxy FlopBox, permet de télécharger (download) et téléverser (upload) un gros fichier binaire (image, vidéo, etc.):** **Note comprise entre 13 et 14 si—en plus—le proxy FlopBox permet de gérer plusieurs serveurs FTP différents (ajout, suppression, modification des serveurs):** diff --git a/src/main/java/fil/sr2/flopbox/FTPResource.java b/src/main/java/fil/sr2/flopbox/FTPResource.java index 6d5989a3a22338428220a14b3eb6e6a4ce0aa7c9..99b1eef74907901ab1e77144305a3251a0b768f8 100644 --- a/src/main/java/fil/sr2/flopbox/FTPResource.java +++ b/src/main/java/fil/sr2/flopbox/FTPResource.java @@ -297,9 +297,9 @@ public class FTPResource { ftp.setFileType(FTP.BINARY_FILE_TYPE); if (extractZip != null && extractZip) { - if (!ftp.changeWorkingDirectory(path)) { - return Response.status(400).entity("Le chemin n'est pas un dossier").build(); - } + // Créer le dossier cible et ses parents si nécessaire + createDirectories(ftp, path); + // Extraire le contenu du ZIP dans le dossier cible ZipInputStream zis = new ZipInputStream(fileStream); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { @@ -316,6 +316,10 @@ public class FTPResource { zis.close(); return Response.ok().build(); } else { + // Créer les répertoires parents si nécessaire + String parentDir = path.substring(0, path.lastIndexOf('/')); + createDirectories(ftp, parentDir); + // Upload du fichier boolean success = ftp.storeFile(path, fileStream); return success ? Response.ok().build() : Response.status(500).entity("Erreur d'upload").build(); } @@ -333,21 +337,6 @@ public class FTPResource { } } - private void createDirectories(FTPClient ftp, String path) throws IOException { - String[] dirs = path.split("/"); - StringBuilder sb = new StringBuilder(); - for (String dir : dirs) { - if (dir.isEmpty()) - continue; - sb.append("/").append(dir); - String currentDir = sb.toString(); - if (!ftp.changeWorkingDirectory(currentDir)) { - ftp.makeDirectory(currentDir); - ftp.changeWorkingDirectory(currentDir); - } - } - } - @POST @Path("/{alias}/{path: .+}") @Consumes({ MediaType.APPLICATION_OCTET_STREAM, MediaType.TEXT_PLAIN }) @@ -392,6 +381,28 @@ public class FTPResource { } } + private void createDirectories(FTPClient ftp, String path) throws IOException { + String[] dirs = path.split("/"); + StringBuilder sb = new StringBuilder(); + for (String dir : dirs) { + if (dir.isEmpty()) + continue; + if (sb.length() == 0) { + sb.append(dir); + } else { + sb.append("/").append(dir); + } + String currentDir = sb.toString(); + if (!ftp.changeWorkingDirectory(currentDir)) { + if (ftp.makeDirectory(currentDir)) { + ftp.changeWorkingDirectory(currentDir); + } else { + throw new IOException("Échec de création du dossier: " + currentDir); + } + } + } + } + // delete the file or the content of a folder recursively @DELETE @Path("/{alias}/{path: .+}")