start backend

This commit is contained in:
SlimusMinus
2026-03-26 00:21:30 +03:00
parent 611b1c82ad
commit 263242e469
56 changed files with 1524 additions and 23 deletions

View File

@@ -0,0 +1,42 @@
package com.krylov.refound.service;
import com.krylov.refound.enums.ErrorCode;
import com.krylov.refound.exception.ApiException;
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;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
public class FileStorageService {
@Value("${file.upload-dir}")
private String uploadDir;
public String save(MultipartFile file) {
try {
if (file.isEmpty()) {
throw new RuntimeException("File is empty");
}
String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
Path path = Paths.get(uploadDir).toAbsolutePath().normalize();
Files.createDirectories(path);
Path target = path.resolve(fileName);
Files.copy(file.getInputStream(), target, StandardCopyOption.REPLACE_EXISTING);
return "/files/" + fileName; // URL
} catch (IOException e) {
throw new ApiException(ErrorCode.BAD_REQUEST, "Only image files are allowed", HttpStatus.BAD_REQUEST);
}
}
}