UCO-PRE-PRODUCTION-2026-NAEEM
parent
0fa9899121
commit
ff93cba476
@ -0,0 +1,54 @@
|
|||||||
|
package com.mfsys.uco.service;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FileStorageService {
|
||||||
|
|
||||||
|
@Value("${file.upload-dir}")
|
||||||
|
private String uploadDir;
|
||||||
|
|
||||||
|
public String saveImage(String custcode, MultipartFile file) {
|
||||||
|
try {
|
||||||
|
Path uploadPath = Paths.get(uploadDir).toAbsolutePath().normalize();
|
||||||
|
Files.createDirectories(uploadPath);
|
||||||
|
|
||||||
|
String extension = getExtension(file.getOriginalFilename());
|
||||||
|
String filename = custcode + "_" + UUID.randomUUID() + "." + extension;
|
||||||
|
Path targetPath = uploadPath.resolve(filename);
|
||||||
|
|
||||||
|
Files.copy(file.getInputStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
|
return uploadDir + "/" + filename;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Failed to store image for custcode: " + custcode, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteImage(String imagePath) {
|
||||||
|
try {
|
||||||
|
if (imagePath != null) {
|
||||||
|
Path path = Paths.get(imagePath).toAbsolutePath().normalize();
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// log and continue — don't fail the update if old file delete fails
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getExtension(String filename) {
|
||||||
|
if (filename != null && filename.contains(".")) {
|
||||||
|
return filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
|
||||||
|
}
|
||||||
|
return "jpg";
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue