Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ public ResponseEntity<Category> deleteCategory(
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
})
@PostMapping(path = "/{id}/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Void> uploadImage(
public ResponseEntity<Image> uploadImage(
@Parameter(
description = "Файл фотографии",
required = true,
content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)
) @RequestParam("file") MultipartFile file,
@PathVariable
@Parameter(description = "id категории", example = "10") long id) {
categoryService.uploadImage(file, id);
return ResponseEntity.ok().build();
Image image = categoryService.uploadImage(file, id);
return ResponseEntity.ok(image);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ public class FacilityController {
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
})
@PostMapping(path = "/{id}/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Void> uploadImage(
public ResponseEntity<Image> uploadImage(
@Parameter(
description = "Файл фотографии",
required = true,
content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)
) @RequestParam("file") MultipartFile file,
@PathVariable
@Parameter(description = "id категории", example = "10") long id) {
facilityService.uploadImage(file, id);
return ResponseEntity.ok().build();

return ResponseEntity.ok(facilityService.uploadImage(file, id));
}

@Operation(summary = "Получение всех удобств", description = "Получение всех удобств")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,63 @@
import kattsyn.dev.rentplace.entities.Image;
import kattsyn.dev.rentplace.enums.ImageType;
import kattsyn.dev.rentplace.services.ImageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
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.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("${api.path}/images")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "ImageController", description = "Для взаимодействия с фотографиями")
public class ImageController {

private final ImageService imageService;
private final Path rootLocation;

public ImageController(ImageService imageService, @Value("${upload.path}") String uploadPath) {
this.imageService = imageService;
this.rootLocation = Paths.get(uploadPath).toAbsolutePath().normalize();
}

@GetMapping("/{entityType}/{entityId}/{filename:.+}")
public ResponseEntity<Resource> getImage(
@PathVariable String entityType,
@PathVariable Long entityId,
@PathVariable String filename) {

Path filePath = rootLocation.resolve(entityType)
.resolve(entityId.toString())
.resolve(filename)
.normalize();

log.info(filePath.toString());

try {
Resource resource = new UrlResource(filePath.toUri());

if (resource.exists() || resource.isReadable()) {
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, Files.probeContentType(filePath))
.body(resource);
}
return ResponseEntity.notFound().build();
} catch (IOException e) {
return ResponseEntity.internalServerError().build();
}
}

@Operation(
summary = "Загрузка фотографии",
Expand All @@ -49,7 +88,7 @@ public ResponseEntity<Image> uploadImage(
required = true,
schema = @Schema(
implementation = ImageType.class)
) @RequestParam ImageType imageType) {
) @RequestParam ImageType imageType) { //todo: delete method
return ResponseEntity.ok(imageService.uploadImage(file, imageType));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import kattsyn.dev.rentplace.dtos.PropertyDTO;
import kattsyn.dev.rentplace.entities.Image;
import kattsyn.dev.rentplace.entities.Property;
import kattsyn.dev.rentplace.enums.ImageType;
import kattsyn.dev.rentplace.services.ImageService;
import kattsyn.dev.rentplace.services.PropertyService;
import kattsyn.dev.rentplace.utils.PathResolver;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -30,7 +27,6 @@
public class PropertyController {

private final PropertyService propertyService;
private final ImageService imageService;

@PostMapping(value = "/{id}/images", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(
Expand All @@ -56,14 +52,6 @@ public ResponseEntity<List<Image>> uploadMultipleImages(
return ResponseEntity.ok(savedImages);
}

@PostMapping("/{id}/image")
public Image uploadPropertyImage(
@PathVariable Long id,
@RequestParam MultipartFile file) {

String path = PathResolver.resolvePath(ImageType.PROPERTY, id);
return imageService.uploadImage(file, path);
}

@Operation(
summary = "Получение всех объявлений",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Image {

@Column(name = "file_name", length = 400)
private String fileName;
@Column(name = "url", length = 1000)
private String url;
@Column(name = "original_file_name")
private String originalFileName;
@Column(name = "content_type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kattsyn.dev.rentplace.dtos.CategoryDTO;
import kattsyn.dev.rentplace.entities.Category;
import kattsyn.dev.rentplace.entities.Image;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
Expand All @@ -14,6 +15,6 @@ public interface CategoryService {
Category update(Long id, CategoryDTO categoryDTO);
Category deleteById(Long id);

void uploadImage(MultipartFile file, long id);
Image uploadImage(MultipartFile file, long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kattsyn.dev.rentplace.dtos.FacilityDTO;
import kattsyn.dev.rentplace.entities.Facility;
import kattsyn.dev.rentplace.entities.Image;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
Expand All @@ -14,5 +15,5 @@ public interface FacilityService {
Facility update(Long id, FacilityDTO facilityDTO);
Facility deleteById(Long id);

void uploadImage(MultipartFile file, long id);
Image uploadImage(MultipartFile file, long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Category deleteById(Long id) {

@Transactional
@Override
public void uploadImage(MultipartFile file, long id) {
public Image uploadImage(MultipartFile file, long id) {
Category category = findById(id);
String path = PathResolver.resolvePath(ImageType.CATEGORY, id);

Expand All @@ -71,5 +71,7 @@ public void uploadImage(MultipartFile file, long id) {
Image image = imageService.uploadImage(file, path);
category.setImage(image);
categoryRepository.save(category);

return image;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Facility deleteById(Long id) {

@Transactional
@Override
public void uploadImage(MultipartFile file, long id) {
public Image uploadImage(MultipartFile file, long id) {
Facility facility = findById(id);
String path = PathResolver.resolvePath(ImageType.FACILITY, id);

Expand All @@ -71,5 +71,7 @@ public void uploadImage(MultipartFile file, long id) {
Image image = imageService.uploadImage(file, path);
facility.setImage(image);
facilityRepository.save(facility);

return image;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import kattsyn.dev.rentplace.services.ImageService;
import kattsyn.dev.rentplace.services.StorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -18,6 +19,11 @@ public class ImageServiceImpl implements ImageService {
private final ImageRepository imageRepository;
private final StorageService storageService;

@Value("${app.base-url:http://localhost:8080}")
private String baseUrl;
@Value("${api.path}")
private String apiPath;

private void validateImage(MultipartFile file) {
String contentType = file.getContentType();
if (file.isEmpty()) {
Expand All @@ -42,10 +48,16 @@ public Image uploadImage(MultipartFile file) {
image.setOriginalFileName(file.getOriginalFilename());
image.setContentType(file.getContentType());
image.setSize(file.getSize());
image.setAdditionalPath("");
image.setUrl(generateImageUrl("", filename));

return imageRepository.save(image);
}

private String generateImageUrl(String relativePath, String fileName) {
return String.format("%s/%s/images%s%s", baseUrl, apiPath, relativePath, fileName);
}

@Override
public Image uploadImage(MultipartFile file, ImageType type) {
try {
Expand All @@ -59,6 +71,7 @@ public Image uploadImage(MultipartFile file, ImageType type) {
image.setContentType(file.getContentType());
image.setAdditionalPath(type.additionalPath);
image.setSize(file.getSize());
image.setUrl(generateImageUrl(type.additionalPath, filename));

return imageRepository.save(image);
} catch (Exception e) {
Expand All @@ -80,6 +93,7 @@ public Image uploadImage(MultipartFile file, String relativePath) {
image.setContentType(file.getContentType());
image.setAdditionalPath(relativePath);
image.setSize(file.getSize());
image.setUrl(generateImageUrl(relativePath, filename));

return imageRepository.save(image);
} catch (Exception e) {
Expand Down
7 changes: 5 additions & 2 deletions rentplace/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ spring:
generate-ddl: true
hibernate:
ddl-auto: update
open-in-view: false
api:
path: api/v1
upload:
path: ./uploads
path: C:/Users/Kattsyn/TP-PROJECT/Backend/rentplace/uploads
storage:
location: ./uploads/
location: ./uploads/
app:
base-url: http://localhost:8080
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE images (
image_id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
file_name VARCHAR(400) NOT NULL,
url VARCHAR(1000) NOT NULL,
original_file_name VARCHAR(255) NOT NULL,
additional_path VARCHAR(255) NOT NULL,
size BIGINT NOT NULL,
Expand Down