CoolFace
Apppublic

sakthivikram/geo-location

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
FileStorageService.java113 linesDownload Raw Back to service
1package com.georeport.service;2 3import org.springframework.beans.factory.annotation.Value;4import org.springframework.stereotype.Service;5import org.springframework.util.StringUtils;6import org.springframework.web.multipart.MultipartFile;7 8import jakarta.annotation.PostConstruct;9import java.io.IOException;10import java.io.InputStream;11import java.nio.file.Files;12import java.nio.file.Path;13import java.nio.file.Paths;14import java.nio.file.StandardCopyOption;15import java.util.Arrays;16import java.util.List;17import java.util.UUID;18 19/**20 * Service for handling file storage operations.21 * Manages image uploads for issues.22 */23@Service24public class FileStorageService {25 26    @Value("${file.upload-dir}")27    private String uploadDir;28 29    @Value("${file.allowed-extensions}")30    private String allowedExtensions;31 32    private Path uploadPath;33    private List<String> allowedExtensionList;34 35    @PostConstruct36    public void init() {37        uploadPath = Paths.get(uploadDir).toAbsolutePath().normalize();38        allowedExtensionList = Arrays.asList(allowedExtensions.split(","));39 40        try {41            Files.createDirectories(uploadPath);42        } catch (IOException e) {43            throw new RuntimeException("Could not create upload directory", e);44        }45    }46 47    /**48     * Store a file and return the generated filename49     */50    public String storeFile(MultipartFile file) throws IOException {51        // Validate file52        String originalFilename = StringUtils.cleanPath(file.getOriginalFilename());53 54        if (originalFilename.contains("..")) {55            throw new IOException("Invalid file path: " + originalFilename);56        }57 58        // Check file extension59        String extension = getFileExtension(originalFilename);60        if (!allowedExtensionList.contains(extension.toLowerCase())) {61            throw new IOException("File type not allowed: " + extension);62        }63 64        // Generate unique filename65        String newFilename = UUID.randomUUID().toString() + "." + extension;66 67        // Store file68        Path targetLocation = uploadPath.resolve(newFilename);69        try (InputStream inputStream = file.getInputStream()) {70            Files.copy(inputStream, targetLocation, StandardCopyOption.REPLACE_EXISTING);71        }72 73        return newFilename;74    }75 76    /**77     * Delete a file78     */79    public boolean deleteFile(String filename) {80        try {81            Path filePath = uploadPath.resolve(filename).normalize();82            return Files.deleteIfExists(filePath);83        } catch (IOException e) {84            return false;85        }86    }87 88    /**89     * Get file path90     */91    public Path getFilePath(String filename) {92        return uploadPath.resolve(filename).normalize();93    }94 95    /**96     * Get file extension97     */98    private String getFileExtension(String filename) {99        int dotIndex = filename.lastIndexOf('.');100        if (dotIndex > 0 && dotIndex < filename.length() - 1) {101            return filename.substring(dotIndex + 1);102        }103        return "";104    }105 106    /**107     * Get upload path108     */109    public Path getUploadPath() {110        return uploadPath;111    }112}113