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
e97089a8
Commit
e97089a8
authored
1 month ago
by
Fabio Vandewaeter
Browse files
Options
Downloads
Patches
Plain Diff
save
parent
d94d1ff6
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
+50
-49
50 additions, 49 deletions
src/main/java/fil/sr2/flopbox/FTPResource.java
with
50 additions
and
49 deletions
src/main/java/fil/sr2/flopbox/FTPResource.java
+
50
−
49
View file @
e97089a8
...
...
@@ -28,60 +28,62 @@ public class FTPResource {
}
@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
);
@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
)
{
// Vérifier l'existence et le type de la res
source
FTP
File
[]
files
=
ftpClient
.
listFiles
(
path
);
if
(
files
==
null
||
files
.
length
==
0
)
{
System
.
out
.
println
(
"GetFTPRe
source
()"
);
FTP
ServerConfig
config
=
FTPServerRepository
.
getInstance
().
getServer
(
alias
);
if
(
config
==
null
)
{
return
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
"
Ressource non trouvée : "
+
path
).
build
();
.
entity
(
"
Serveur FTP non trouvé"
).
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
();
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
();
}
// 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
();
}
}
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
...
...
@@ -99,7 +101,6 @@ public Response getFTPResource(
return
Response
.
created
(
builder
.
build
()).
entity
(
config
).
build
();
}
@DELETE
@Path
(
"/{alias}"
)
public
Response
removeFTPServer
(
@PathParam
(
"alias"
)
String
alias
)
{
...
...
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