diff --git a/README.md b/README.md index ef4fe88022c8c76e6c8a5184b09f0b2d29b4ef31..7b8433134dccbc6ff13a9e903d74c9ff52298035 100644 --- a/README.md +++ b/README.md @@ -104,12 +104,15 @@ unzip dossier1.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 +curl -X PUT -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" --upload-file fichier2 http://localhost:8080/ftps/mon-ftp/fichier2 ``` - upload dossier : ```shell +curl -X PUT -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" --upload-file dossier2.zip http://localhost:8080/ftps/mon-ftp/dossier2.zip + +curl -X POST -H "Authorization: Bearer valid-token-1" -H "X-FTP-User: user" -H "X-FTP-Pass: password" -H "Content-Type: application/octet-stream" --data-binary @dossier2.zip http://localhost:8080/ftps/mon-ftp/upload-dir/ ``` **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.):** diff --git a/dossier_serveur_ftp/fichier2 b/dossier_serveur_ftp/fichier2 new file mode 100644 index 0000000000000000000000000000000000000000..deae8dcdc3f3a6d87cbc145dec37ea4b711bf4f4 --- /dev/null +++ b/dossier_serveur_ftp/fichier2 @@ -0,0 +1 @@ +FICHIER2 diff --git a/src/main/java/fil/sr2/flopbox/FTPResource.java b/src/main/java/fil/sr2/flopbox/FTPResource.java index 9281a6aa253e847f146427ce10dd92bc5ce0d6a3..5c48aca5d3e5be82b0a2012b322fa4820e36e275 100644 --- a/src/main/java/fil/sr2/flopbox/FTPResource.java +++ b/src/main/java/fil/sr2/flopbox/FTPResource.java @@ -162,6 +162,27 @@ public class FTPResource { } } + @POST + @Path("/{alias}/upload-dir/{path: .*}") + @Consumes(MediaType.APPLICATION_OCTET_STREAM) + public Response uploadDirectory( + @PathParam("alias") String alias, + @PathParam("path") String path, + @HeaderParam("X-FTP-User") String user, + @HeaderParam("X-FTP-Pass") String pass, + InputStream zipStream) { + + System.out.println("uploadDirectory()"); + try { + ftpService.uploadDirectoryAsZip(alias, path, user, pass, zipStream); + return Response.ok().build(); + } catch (FTPException e) { + return Response.status(e.getStatus()).entity(e.getMessage()).build(); + } catch (IOException e) { + return Response.serverError().entity("Erreur FTP : " + e.getMessage()).build(); + } + } + // POST pour créer une ressource (fichier ou répertoire) @POST @Path("/{alias}/{path: .+}") diff --git a/src/main/java/fil/sr2/flopbox/FTPService.java b/src/main/java/fil/sr2/flopbox/FTPService.java index c380c8180c6c2cd16eab48af2c3b8e25a739c8bf..90fe9e08e1f4c7a2fb0d86f3ab939b8c6a48fd9e 100644 --- a/src/main/java/fil/sr2/flopbox/FTPService.java +++ b/src/main/java/fil/sr2/flopbox/FTPService.java @@ -9,6 +9,10 @@ import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import java.util.zip.ZipInputStream; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; public class FTPService { @@ -257,4 +261,94 @@ public class FTPService { } } } + + public void uploadDirectoryAsZip(String alias, String targetPath, String user, String pass, InputStream zipStream) + throws IOException, FTPException { + + System.out.println("[DEBUG] Début upload ZIP vers: " + targetPath); + FTPClient ftp = FTPClientFactory.createClient(alias); + try { + if (!ftp.login(user, pass)) { + throw new FTPException("Authentification FTP échouée", 401); + } + ftp.enterLocalPassiveMode(); + ftp.setFileType(FTPClient.BINARY_FILE_TYPE); + + try (ZipInputStream zis = new ZipInputStream(zipStream)) { + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + String entryName = entry.getName(); + String fullPath = constructPath(targetPath, entryName); + System.out.println("[DEBUG] Traitement entrée: " + entry.getName() + + " → " + fullPath); + + if (entry.isDirectory()) { + createDirectories(ftp, fullPath); + } else { + createParentDirectories(ftp, fullPath); + uploadFileEntry(ftp, fullPath, zis); + } + zis.closeEntry(); + } + } + } finally { + FTPClientFactory.disconnect(ftp); + } + System.out.println("[DEBUG] Upload ZIP terminé avec succès"); + } + + private String constructPath(String basePath, String entryPath) { + // Ignorer le premier segment du chemin (dossier racine du ZIP) + String[] parts = entryPath.split("/"); + if (parts.length > 1) { + String normalizedEntry = String.join("/", Arrays.copyOfRange(parts, 1, parts.length)); + return basePath.isEmpty() ? normalizedEntry : basePath + "/" + normalizedEntry; + } + return entryPath; // Cas où l'entrée est déjà à la racine + } + + private void createParentDirectories(FTPClient ftp, String filePath) throws IOException { + int lastSlash = filePath.lastIndexOf('/'); + if (lastSlash != -1) { + String parentDir = filePath.substring(0, lastSlash); + createDirectories(ftp, parentDir); + } + } + + private void createDirectories(FTPClient ftp, String path) throws IOException { + String normalizedPath = path.replaceAll("/+", "/"); + if (normalizedPath.startsWith("/")) { + ftp.changeWorkingDirectory("/"); + } + + String[] parts = normalizedPath.split("/"); + StringBuilder currentPath = new StringBuilder(); + + for (String part : parts) { + if (part.isEmpty()) + continue; + + currentPath.append(part).append("/"); + + if (!ftp.changeWorkingDirectory(currentPath.toString())) { + if (!ftp.makeDirectory(currentPath.toString())) { + throw new IOException("Échec création dossier: " + currentPath); + } + ftp.changeWorkingDirectory(currentPath.toString()); + } + } + } + + private void uploadFileEntry(FTPClient ftp, String path, InputStream is) + throws IOException, FTPException { + + // Supprimer URLDecoder.decode() ! + String decodedPath = path; + + try (InputStream bais = is) { + if (!ftp.storeFile(decodedPath, bais)) { + throw new FTPException("Échec upload: " + decodedPath, 500); + } + } + } }