1212import org .springframework .web .bind .annotation .GetMapping ;
1313import org .springframework .web .bind .annotation .PathVariable ;
1414import org .springframework .web .bind .annotation .PostMapping ;
15+ import org .springframework .web .bind .annotation .PutMapping ;
1516import org .springframework .web .bind .annotation .RequestMapping ;
1617import org .springframework .web .bind .annotation .RequestParam ;
1718import org .springframework .web .bind .annotation .RestController ;
@@ -30,10 +31,10 @@ public class AwsS3Controller {
3031
3132 private final AwsS3Service awsS3Service ;
3233
33-
34- // Endpoint para subir un archivo
35- // POST /api/s3/upload
36-
34+ /**
35+ * Endpoint para subir un archivo
36+ * POST /api/s3/upload
37+ */
3738 @ PostMapping ("/upload" )
3839 public ResponseEntity <UploadResponse > uploadFile (@ RequestParam ("file" ) MultipartFile file ) {
3940
@@ -70,10 +71,61 @@ public ResponseEntity<UploadResponse> uploadFile(@RequestParam("file") Multipart
7071 }
7172 }
7273
73-
74- // Endpoint para listar todos los archivos
75- // GET /api/s3/files
76-
74+ /**
75+ * Endpoint para actualizar/sobrescribir un archivo existente
76+ * PUT /api/s3/files/{fileName}
77+ */
78+ @ PutMapping ("/files/{fileName}" )
79+ public ResponseEntity <UploadResponse > updateFile (
80+ @ PathVariable String fileName ,
81+ @ RequestParam ("file" ) MultipartFile file ) {
82+
83+ // Validaciones
84+ if (file .isEmpty ()) {
85+ UploadResponse errorResponse = new UploadResponse (
86+ "No se ha seleccionado ningún archivo" ,
87+ null , null , 0 , false );
88+ return ResponseEntity .badRequest ().body (errorResponse );
89+ }
90+
91+ // Validar tamaño del archivo (Máximo 50MB)
92+ if (file .getSize () > 50 * 1024 * 1024 ) {
93+ UploadResponse errorResponse = new UploadResponse (
94+ "El archivo es demasiado grande. Máximo 50MB permitido" ,
95+ null , null , 0 , false );
96+ return ResponseEntity .badRequest ().body (errorResponse );
97+ }
98+
99+ try {
100+ // Verificar si el archivo existe
101+ if (!awsS3Service .fileExists (fileName )) {
102+ UploadResponse errorResponse = new UploadResponse (
103+ "El archivo especificado no existe: " + fileName ,
104+ null , null , 0 , false );
105+ return ResponseEntity .status (HttpStatus .NOT_FOUND ).body (errorResponse );
106+ }
107+
108+ // Actualizar el archivo
109+ UploadResponse response = awsS3Service .updateFile (fileName , file );
110+
111+ if (response .isSuccess ()) {
112+ return ResponseEntity .ok (response );
113+ } else {
114+ return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).body (response );
115+ }
116+
117+ } catch (Exception e ) {
118+ UploadResponse errorResponse = new UploadResponse (
119+ "Error interno del servidor: " + e .getMessage (),
120+ null , null , 0 , false );
121+ return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).body (errorResponse );
122+ }
123+ }
124+
125+ /**
126+ * Endpoint para listar todos los archivos
127+ * GET /api/s3/files
128+ */
77129 @ GetMapping ("/files" )
78130 public ResponseEntity <List <S3FileInfo >> listFiles () {
79131 try {
@@ -84,10 +136,10 @@ public ResponseEntity<List<S3FileInfo>> listFiles() {
84136 }
85137 }
86138
87-
88- // Endpoint para descargar un archivo
89- // GET /api/s3/download/{fileName}
90-
139+ /**
140+ * Endpoint para descargar un archivo
141+ * GET /api/s3/download/{fileName}
142+ */
91143 @ GetMapping ("/download/{fileName}" )
92144 public ResponseEntity <byte []> downloadFile (@ PathVariable String fileName ) {
93145 try {
@@ -122,10 +174,10 @@ public ResponseEntity<byte[]> downloadFile(@PathVariable String fileName) {
122174 }
123175 }
124176
125-
126- // Endpoint para eliminar un archivo
127- // DELETE /api/s3/files/{fileName}
128-
177+ /**
178+ * Endpoint para eliminar un archivo
179+ * DELETE /api/s3/files/{fileName}
180+ */
129181 @ DeleteMapping ("/files/{fileName}" )
130182 public ResponseEntity <Map <String , Object >> deleteFile (@ PathVariable String fileName ) {
131183 Map <String , Object > response = new HashMap <>();
@@ -159,10 +211,10 @@ public ResponseEntity<Map<String, Object>> deleteFile(@PathVariable String fileN
159211 }
160212 }
161213
162-
163- // Endpoint para obtener información de un archivo específico
164- // GET /api/s3/files/{fileName}/info
165-
214+ /**
215+ * Endpoint para obtener información de un archivo específico
216+ * GET /api/s3/files/{fileName}/info
217+ */
166218 @ GetMapping ("/files/{fileName}/info" )
167219 public ResponseEntity <S3FileInfo > getFileInfo (@ PathVariable String fileName ) {
168220 try {
@@ -178,10 +230,10 @@ public ResponseEntity<S3FileInfo> getFileInfo(@PathVariable String fileName) {
178230 }
179231 }
180232
181-
182- // Endpoint para verificar si un archivo existe
183- // GET /api/s3/files/{fileName}/exists
184-
233+ /**
234+ * Endpoint para verificar si un archivo existe
235+ * GET /api/s3/files/{fileName}/exists
236+ */
185237 @ GetMapping ("/files/{fileName}/exists" )
186238 public ResponseEntity <Map <String , Object >> checkFileExists (@ PathVariable String fileName ) {
187239 Map <String , Object > response = new HashMap <>();
@@ -199,10 +251,10 @@ public ResponseEntity<Map<String, Object>> checkFileExists(@PathVariable String
199251 }
200252 }
201253
202-
203- // Endpoint de salud para verificar conectividad con S3
204- // GET /api/s3/health
205-
254+ /**
255+ * Endpoint de salud para verificar conectividad con S3
256+ * GET /api/s3/health
257+ */
206258 @ GetMapping ("/health" )
207259 public ResponseEntity <Map <String , Object >> healthCheck () {
208260 Map <String , Object > response = new HashMap <>();
0 commit comments