Skip to content

Commit 9f5e7ec

Browse files
authored
Update AwsEfsService.java
1 parent 8d0c8cc commit 9f5e7ec

1 file changed

Lines changed: 98 additions & 9 deletions

File tree

src/main/java/com/tienda/microservicio/service/AwsEfsService.java

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.web.multipart.MultipartFile;
1717

1818
import com.tienda.microservicio.dto.EfsFileInfo;
19+
import com.tienda.microservicio.dto.RenameResponse;
1920
import com.tienda.microservicio.dto.UploadResponse;
2021

2122
import lombok.extern.slf4j.Slf4j;
@@ -28,7 +29,6 @@ public class AwsEfsService {
2829
private String efsMountPath;
2930

3031
// Sube un archivo al sistema EFS
31-
3232
public UploadResponse uploadFile(MultipartFile file) {
3333
try {
3434
// Validamos que el directorio EFS existe
@@ -67,8 +67,78 @@ public UploadResponse uploadFile(MultipartFile file) {
6767
}
6868
}
6969

70-
// Listamos todos los archivos del directorio EFS
70+
// NUEVA FUNCIONALIDAD: Renombrar un archivo en EFS
71+
public RenameResponse renameFile(String oldFileName, String newFileName) {
72+
try {
73+
// Validar que el archivo origen existe
74+
Path oldPath = Paths.get(efsMountPath, oldFileName);
75+
if (!Files.exists(oldPath)) {
76+
log.warn("Archivo origen no encontrado: {}", oldFileName);
77+
return new RenameResponse(
78+
"Archivo origen no encontrado: " + oldFileName,
79+
oldFileName,
80+
null,
81+
null,
82+
false
83+
);
84+
}
85+
86+
// Validar que el nuevo nombre no esté vacío
87+
if (newFileName == null || newFileName.trim().isEmpty()) {
88+
return new RenameResponse(
89+
"El nuevo nombre de archivo no puede estar vacío",
90+
oldFileName,
91+
null,
92+
null,
93+
false
94+
);
95+
}
96+
97+
// Limpiar el nuevo nombre de archivo (remover caracteres peligrosos)
98+
String sanitizedNewFileName = sanitizeFileName(newFileName.trim());
99+
100+
// Verificar que el archivo destino no existe
101+
Path newPath = Paths.get(efsMountPath, sanitizedNewFileName);
102+
if (Files.exists(newPath)) {
103+
log.warn("El archivo destino ya existe: {}", sanitizedNewFileName);
104+
return new RenameResponse(
105+
"Ya existe un archivo con el nombre: " + sanitizedNewFileName,
106+
oldFileName,
107+
sanitizedNewFileName,
108+
null,
109+
false
110+
);
111+
}
112+
113+
// Obtener información del archivo antes del renombrado
114+
EfsFileInfo oldFileInfo = convertToEfsFileInfo(oldPath);
115+
116+
// Realizar el renombrado
117+
Files.move(oldPath, newPath);
118+
119+
log.info("Archivo renombrado exitosamente de '{}' a '{}'", oldFileName, sanitizedNewFileName);
120+
121+
return new RenameResponse(
122+
"Archivo renombrado exitosamente",
123+
oldFileName,
124+
sanitizedNewFileName,
125+
newPath.toString(),
126+
true
127+
);
71128

129+
} catch (IOException e) {
130+
log.error("Error al renombrar archivo de '{}' a '{}': {}", oldFileName, newFileName, e.getMessage());
131+
return new RenameResponse(
132+
"Error al renombrar archivo: " + e.getMessage(),
133+
oldFileName,
134+
newFileName,
135+
null,
136+
false
137+
);
138+
}
139+
}
140+
141+
// Listamos todos los archivos del directorio EFS
72142
public List<EfsFileInfo> listFiles() {
73143
List<EfsFileInfo> fileList = new ArrayList<>();
74144

@@ -97,7 +167,6 @@ public List<EfsFileInfo> listFiles() {
97167
}
98168

99169
// Descargar un archivo del EFS
100-
101170
public byte[] downloadFile(String fileName) {
102171
try {
103172
Path filePath = Paths.get(efsMountPath, fileName);
@@ -115,7 +184,6 @@ public byte[] downloadFile(String fileName) {
115184
}
116185

117186
// Eliminar un archivo del EFS
118-
119187
public boolean deleteFile(String fileName) {
120188
try {
121189
Path filePath = Paths.get(efsMountPath, fileName);
@@ -136,14 +204,12 @@ public boolean deleteFile(String fileName) {
136204
}
137205

138206
// Verifica si un archivo existe en EFS
139-
140207
public boolean fileExists(String fileName) {
141208
Path filePath = Paths.get(efsMountPath, fileName);
142209
return Files.exists(filePath) && Files.isRegularFile(filePath);
143210
}
144211

145212
// Obtiene información detallada de un archivo
146-
147213
public EfsFileInfo getFileInfo(String fileName) {
148214
try {
149215
Path filePath = Paths.get(efsMountPath, fileName);
@@ -161,7 +227,6 @@ public EfsFileInfo getFileInfo(String fileName) {
161227
}
162228

163229
// Verifica que el directorio EFS exista y sea accesible
164-
165230
public boolean isEfsAccessible() {
166231
try {
167232
Path efsPath = Paths.get(efsMountPath);
@@ -189,7 +254,6 @@ public boolean isEfsAccessible() {
189254
}
190255

191256
// Métodos auxiliares
192-
193257
private String generateUniqueFileName(String originalFileName) {
194258
String extension = "";
195259
if (originalFileName != null && originalFileName.contains(".")) {
@@ -213,4 +277,29 @@ private EfsFileInfo convertToEfsFileInfo(Path filePath) throws IOException {
213277
filePath.toString() // Path completo como URL
214278
);
215279
}
216-
}
280+
281+
// NUEVO MÉTODO: Sanitizar nombre de archivo
282+
private String sanitizeFileName(String fileName) {
283+
if (fileName == null) {
284+
return null;
285+
}
286+
287+
// Remover caracteres peligrosos para sistemas de archivos
288+
String sanitized = fileName.replaceAll("[\\\\/:*?\"<>|]", "_");
289+
290+
// Remover espacios del inicio y final
291+
sanitized = sanitized.trim();
292+
293+
// Limitar longitud del nombre (máximo 255 caracteres)
294+
if (sanitized.length() > 255) {
295+
sanitized = sanitized.substring(0, 255);
296+
}
297+
298+
// Evitar nombres vacíos
299+
if (sanitized.isEmpty()) {
300+
sanitized = "renamed_file_" + System.currentTimeMillis();
301+
}
302+
303+
return sanitized;
304+
}
305+
}

0 commit comments

Comments
 (0)