diff --git a/README.md b/README.md index 3c0d3e812fc26e03450321e3a6f1f4045420a977..c94724ffb1353c40cbc63e259990edfa79ec8ddd 100644 --- a/README.md +++ b/README.md @@ -19,17 +19,17 @@ La vidéo `video.mp4` montre pas à pas comment utiliser ce projet (voir la [sec Pour créer l'archive `FlopBox.jar`, il faut avoir Java 17 et Maven d'installés, et exécuter la commande suivante à la racine du projet : -``` +```shell mvn clean package ``` ### Utiliser le programme -``` +```shell java -jar target/FlopBox.jar ``` -``` +```shell pip install --user pyftpdlib python3 serveur_ftp.py ``` @@ -44,11 +44,11 @@ ou ftp.ubuntu.com Pour exécuter uniquement les tests il faut lancer la commande suivante : -``` +```shell mvn test ``` -``` +```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 curl -H "Authorization: Bearer valid-token-1" http://localhost:8080/ftps -v @@ -60,7 +60,7 @@ curl -H "Authorization: Bearer valid-token-1" -o fichier.html http://localhost:8 ### Générer la javadoc Lancer la commande suivante : -``` +```shell mvn javadoc:javadoc ``` @@ -79,7 +79,7 @@ Puis ouvrir le fichier `target/site/apidocs/index.html` **Note comprise entre 10 et 11 si le code compile et peut être lancé pour afficher l'arborescence d'un serveur FTP via le proxy FlopBox:** ```shell -> curl -X GET -v -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: anonymous" -H "X-FTP-Pass: anonymous" localhost:8080/ftps/mon-ftp/folder +curl -X GET -v -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: anonymous" -H "X-FTP-Pass: anonymous" localhost:8080/ftps/mon-ftp/folder Note: Unnecessary use of -X or --request, GET is already inferred. * Trying 127.0.0.1:8080... @@ -107,36 +107,48 @@ Note: Unnecessary use of -X or --request, GET is already inferred. **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):** +- ajout : ```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 +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 ``` +- suppression : ```shell -> curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer valid-token-1" http://localhost:8080/ftps +curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer valid-token-1" http://localhost:8080/ftps/mon-ftp ``` +- modification : ```shell -curl -X PUT http://localhost:8080/ftps/mon-ftp \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer valid-token-1" \ - -d '{"alias":"mon-ftp","host":"nouvelle-adresse","port":2221}' +curl -X PUT http://localhost:8080/ftps/mon-ftp -H "Content-Type: application/json" -H "Authorization: Bearer valid-token-1" -d '{"alias":"mon-ftp","host":"nouvelle-adresse","port":2221}' ``` +- lister les serveurs : ```shell -fabio.vandewaeter.etu@b10p21:~/M1/SR2/TEMPO$ curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer valid-token-1" http://localhost:8080/ftps +curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer valid-token-1" http://localhost:8080/ftps [{"alias":"mon-ftp","host":"nouvelle-adresse","port":2221}] ``` **Note comprise entre 14 et 15 si—en plus—le proxy FlopBox, permet de créer, supprimer, renommer une ressource directement sur l'un des serveurs FTP gérés (fichier ou répertoire):** +- créer dossier : +```shell +curl -X POST -H "X-Resource-Type: file" -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" http://localhost:8080/ftps/mon-ftp/dossier +``` + +- créer fichier : +```shell +curl -X POST -H "X-Resource-Type: file" -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" http://localhost:8080/ftps/mon-ftp/dossier/fichier +``` +- renommer : ```shell -> curl -X DELETE -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" http://localhost:8080/ftps/mon-ftp/dir +curl -X POST -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" -H "Content-Type: text/plain" -d "nouveau_nom" http://localhost:8080/ftps/mon-ftp/dossier/rename ``` +- supprimer dossier/fichier : ```shell -> curl -X POST -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" -H "Content-Type: text/plain" -d "nouveau_chemin" http://localhost:8080/ftps/mon-ftp/test/rename +curl -X DELETE -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" http://localhost:8080/ftps/mon-ftp/nouveau_nom ``` **Note comprise entre 15 et 16 si—en plus—le proxy FlopBox, permet de chercher des fichiers/répertoires stockés dans plusieurs serveurs FTP (le proxy retourne la liste des URLs pour chaque fichier trouvé):** diff --git a/src/main/java/fil/sr2/flopbox/FTPResource.java b/src/main/java/fil/sr2/flopbox/FTPResource.java index 66019ab5d831799fbab95f36b717be27ea211527..c495c8accad1ca6319fb549124ad4e231e46c205 100644 --- a/src/main/java/fil/sr2/flopbox/FTPResource.java +++ b/src/main/java/fil/sr2/flopbox/FTPResource.java @@ -174,65 +174,32 @@ public class FTPResource { } } - /* - * @POST - * - * @Path("/{alias}/{path: .+}") - * public Response createDirectory( - * - * @PathParam("alias") String alias, - * - * @PathParam("path") String path, - * - * @HeaderParam("X-FTP-User") String user, - * - * @HeaderParam("X-FTP-Pass") String pass) throws SocketException, IOException { - * - * System.out.println("createFirectory()"); - * FTPClient ftp = new FTPClient(); - * try { - * FTPServerConfig config = FTPServerRepository.getInstance().getServer(alias); - * ftp.connect(config.getHost(), config.getPort()); - * ftp.login(user, pass); - * return ftp.makeDirectory(path) ? Response.created(URI.create(path)).build() - * : Response.status(Response.Status.BAD_REQUEST).build(); - * } finally { - * try { - * if (ftp.isConnected()) { - * ftp.logout(); - * ftp.disconnect(); - * } - * } catch (IOException e) { - * e.printStackTrace(); - * } - * } - * } - */ @POST @Path("/{alias}/{path: .+}") - @Consumes({ MediaType.APPLICATION_OCTET_STREAM, MediaType.TEXT_PLAIN }) // Accepter les fichiers + @Consumes({ MediaType.APPLICATION_OCTET_STREAM, MediaType.TEXT_PLAIN }) public Response createResource( @PathParam("alias") String alias, @PathParam("path") String path, @HeaderParam("X-FTP-User") String user, @HeaderParam("X-FTP-Pass") String pass, - InputStream inputStream) { // Contenu optionnel + @HeaderParam("X-Resource-Type") String resourceType, // Nouveau header + InputStream inputStream) { + System.out.println("createResource()"); FTPClient ftp = new FTPClient(); try { FTPServerConfig config = FTPServerRepository.getInstance().getServer(alias); ftp.connect(config.getHost(), config.getPort()); ftp.login(user, pass); - // Si le flux est vide (0 octet), créer un répertoire - if (inputStream.available() == 0) { + // Déterminer le type de ressource via le header + if ("directory".equalsIgnoreCase(resourceType)) { boolean dirCreated = ftp.makeDirectory(path); return dirCreated ? Response.created(URI.create(path)).build() : Response.status(400).entity("Erreur création répertoire").build(); - } - // Sinon, créer un fichier - else { + } else { + // Créer un fichier (même avec un flux vide) boolean fileCreated = ftp.storeFile(path, inputStream); return fileCreated ? Response.created(URI.create(path)).build() @@ -262,6 +229,7 @@ public class FTPResource { @HeaderParam("X-FTP-User") String user, @HeaderParam("X-FTP-Pass") String pass) { + System.out.println("deleteResource()"); FTPClient ftp = new FTPClient(); try { FTPServerConfig config = FTPServerRepository.getInstance().getServer(alias); @@ -289,31 +257,34 @@ public class FTPResource { } } - /** - * Méthode récursive pour supprimer un répertoire et son contenu. - */ private boolean deleteRecursive(FTPClient ftp, String path) throws IOException { - // Vérifie si le chemin est un répertoire + // Tente de supprimer le chemin en tant que fichier + if (ftp.deleteFile(path)) { + return true; + } + + // Si ce n'est pas un fichier, vérifie si c'est un répertoire FTPFile[] files = ftp.listFiles(path); - if (files != null && files.length > 0) { - // Parcours des fichiers et sous-répertoires - for (FTPFile file : files) { - String fullPath = path + "/" + file.getName(); - if (file.isDirectory()) { - // Suppression récursive des sous-répertoires - if (!deleteRecursive(ftp, fullPath)) { - return false; - } - } else { - // Suppression des fichiers - if (!ftp.deleteFile(fullPath)) { - return false; - } + if (files == null) { + // Le chemin n'existe pas + return false; + } + + // Supprime récursivement le contenu du répertoire + for (FTPFile file : files) { + String fullPath = path + "/" + file.getName(); + if (file.isDirectory()) { + if (!deleteRecursive(ftp, fullPath)) { + return false; + } + } else { + if (!ftp.deleteFile(fullPath)) { + return false; } } } - // Suppression du répertoire lui-même + // Supprime le répertoire lui-même return ftp.removeDirectory(path); }