Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SR2-projet1-VANDEWAETER
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fabio Vandewaeter
SR2-projet1-VANDEWAETER
Commits
d94d1ff6
Commit
d94d1ff6
authored
2 months ago
by
Fabio Vandewaeter
Browse files
Options
Downloads
Patches
Plain Diff
save en train de corriger l'ambiguité des chemins
parent
35404a82
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/fil/sr2/flopbox/FTPResource.java
+56
-91
56 additions, 91 deletions
src/main/java/fil/sr2/flopbox/FTPResource.java
with
56 additions
and
91 deletions
src/main/java/fil/sr2/flopbox/FTPResource.java
+
56
−
91
View file @
d94d1ff6
...
...
@@ -27,6 +27,62 @@ public class FTPResource {
return
Response
.
ok
(
servers
).
build
();
}
@GET
@Path
(
"/{alias}/{path: .+}"
)
public
Response
getFTPResource
(
@PathParam
(
"alias"
)
String
alias
,
@PathParam
(
"path"
)
String
path
,
@HeaderParam
(
"X-FTP-User"
)
String
user
,
@HeaderParam
(
"X-FTP-Pass"
)
String
pass
)
{
FTPServerConfig
config
=
FTPServerRepository
.
getInstance
().
getServer
(
alias
);
if
(
config
==
null
)
{
return
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
"Serveur FTP non trouvé"
).
build
();
}
FTPClient
ftpClient
=
new
FTPClient
();
try
{
// Connexion et authentification FTP
ftpClient
.
connect
(
config
.
getHost
(),
config
.
getPort
());
if
(!
ftpClient
.
login
(
user
,
pass
))
{
return
Response
.
status
(
Response
.
Status
.
UNAUTHORIZED
)
.
entity
(
"Authentification FTP échouée"
).
build
();
}
ftpClient
.
enterLocalPassiveMode
();
ftpClient
.
setFileType
(
FTP
.
BINARY_FILE_TYPE
);
// Vérifier l'existence et le type de la ressource
FTPFile
[]
files
=
ftpClient
.
listFiles
(
path
);
if
(
files
==
null
||
files
.
length
==
0
)
{
return
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
"Ressource non trouvée : "
+
path
).
build
();
}
FTPFile
file
=
files
[
0
];
if
(
file
.
isDirectory
())
{
// Si c'est un répertoire, renvoyer la liste des noms de fichiers/répertoires
String
[]
names
=
ftpClient
.
listNames
(
path
);
ftpClient
.
logout
();
ftpClient
.
disconnect
();
return
Response
.
ok
(
names
).
build
();
}
else
{
// Si c'est un fichier, renvoyer un flux binaire pour le téléchargement
InputStream
is
=
ftpClient
.
retrieveFileStream
(
path
);
if
(
is
==
null
)
{
return
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
)
.
entity
(
"Échec de la récupération du fichier"
).
build
();
}
// On utilise ici une classe utilitaire pour fermer proprement le flux et la connexion FTP
return
Response
.
ok
(
new
InputStreamResource
(
is
,
ftpClient
))
.
header
(
"Content-Disposition"
,
"attachment; filename=\""
+
getFileName
(
path
)
+
"\""
)
.
build
();
}
}
catch
(
IOException
e
)
{
return
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
)
.
entity
(
"Erreur FTP : "
+
e
.
getMessage
()).
build
();
}
}
// POST /ftps - enregistre un nouveau serveur FTP
@POST
@Consumes
(
MediaType
.
APPLICATION_JSON
)
...
...
@@ -43,42 +99,6 @@ public class FTPResource {
return
Response
.
created
(
builder
.
build
()).
entity
(
config
).
build
();
}
// GET /ftps/{alias}/{path} - récupère le contenu d'un répertoire ou d'un
// fichier sur le serveur FTP
@GET
@Path
(
"/{alias}/{path: .+}"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
Response
getFTPContent
(
@PathParam
(
"alias"
)
String
alias
,
@PathParam
(
"path"
)
String
path
,
@HeaderParam
(
"X-FTP-User"
)
String
ftpUser
,
@HeaderParam
(
"X-FTP-Pass"
)
String
ftpPass
)
{
System
.
out
.
println
(
"getFTPContent()"
);
FTPServerConfig
config
=
FTPServerRepository
.
getInstance
().
getServer
(
alias
);
if
(
config
==
null
)
{
return
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
"Alias de serveur FTP non trouvé"
).
build
();
}
FTPClient
ftpClient
=
new
FTPClient
();
try
{
ftpClient
.
connect
(
config
.
getHost
(),
config
.
getPort
());
boolean
login
=
ftpClient
.
login
(
ftpUser
,
ftpPass
);
if
(!
login
)
{
return
Response
.
status
(
Response
.
Status
.
UNAUTHORIZED
)
.
entity
(
"Échec de l'authentification FTP"
).
build
();
}
// Pour cet exemple, on se place dans le répertoire demandé et on renvoie la
// liste des fichiers.
ftpClient
.
changeWorkingDirectory
(
path
);
String
[]
files
=
ftpClient
.
listNames
();
ftpClient
.
logout
();
ftpClient
.
disconnect
();
return
Response
.
ok
(
files
).
build
();
}
catch
(
IOException
ex
)
{
return
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
)
.
entity
(
"Erreur FTP : "
+
ex
.
getMessage
()).
build
();
}
}
@DELETE
@Path
(
"/{alias}"
)
...
...
@@ -97,61 +117,6 @@ public class FTPResource {
return
updated
?
Response
.
ok
(
newConfig
).
build
()
:
Response
.
status
(
Response
.
Status
.
NOT_FOUND
).
build
();
}
@GET
@Path
(
"/{alias}/{path: .+}"
)
@Produces
(
MediaType
.
APPLICATION_OCTET_STREAM
)
public
Response
downloadFile
(
@PathParam
(
"alias"
)
String
alias
,
@PathParam
(
"path"
)
String
path
,
@HeaderParam
(
"X-FTP-User"
)
String
user
,
@HeaderParam
(
"X-FTP-Pass"
)
String
pass
)
{
System
.
out
.
println
(
"downloadFile()"
);
FTPServerConfig
config
=
FTPServerRepository
.
getInstance
().
getServer
(
alias
);
if
(
config
==
null
)
{
return
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
"Serveur FTP non trouvé"
).
build
();
}
FTPClient
ftp
=
new
FTPClient
();
try
{
// Connexion au serveur FTP
ftp
.
connect
(
config
.
getHost
(),
config
.
getPort
());
if
(!
ftp
.
login
(
user
,
pass
))
{
return
Response
.
status
(
Response
.
Status
.
UNAUTHORIZED
)
.
entity
(
"Authentification FTP échouée"
).
build
();
}
// Configuration du transfert
ftp
.
enterLocalPassiveMode
();
ftp
.
setFileType
(
FTP
.
BINARY_FILE_TYPE
);
// Vérification de l'existence du fichier
FTPFile
[]
files
=
ftp
.
listFiles
(
path
);
if
(
files
.
length
==
0
||
files
[
0
].
isDirectory
())
{
return
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
"Fichier non trouvé : "
+
path
).
build
();
}
// Récupération du flux FTP
InputStream
is
=
ftp
.
retrieveFileStream
(
path
);
if
(
is
==
null
)
{
return
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
)
.
entity
(
"Échec de la récupération du fichier"
).
build
();
}
// Création de la réponse avec fermeture automatique des ressources
return
Response
.
ok
(
new
InputStreamResource
(
is
,
ftp
))
.
header
(
"Content-Disposition"
,
"attachment; filename=\""
+
getFileName
(
path
)
+
"\""
)
.
build
();
}
catch
(
IOException
e
)
{
return
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
)
.
entity
(
"Erreur FTP : "
+
e
.
getMessage
()).
build
();
}
}
// Classe utilitaire pour gérer le flux et fermer les ressources
private
static
class
InputStreamResource
implements
StreamingOutput
{
private
final
InputStream
inputStream
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment