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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@


- `POST /images/upload` Upload image
- `GET /images/{id}` Get image link
- `GET /images/my` My uploaded images
- `GET /images` Home / search images **_Public_**
- `GET /images/{id}/content` Display image **_Public_**
- `DELETE /images/{id}` Delete my image
15 changes: 11 additions & 4 deletions src/main/java/dev/barboud/taglens/controller/ImageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.UUID;

@RestController
Expand All @@ -33,10 +34,16 @@ public ResponseEntity<ImageResponseDto> uploadImage(
.body(imageResponseDto);
}

// @GetMapping("/link")
// public ResponseEntity<Map<String, String>> getImageLink(@RequestParam String key) {
// return ResponseEntity.ok(Map.of("url", imageService.getImageLink(key, 15)));
// }
@GetMapping("/{id}")
public ResponseEntity<Map<String, String>> getImageLink(
@PathVariable UUID id,
@CookieValue("session_id") UUID sessionId
) {
String url = imageService.getImageLink(id, sessionId);
return ResponseEntity.ok(Map.of("url", url));
}


//
// @DeleteMapping
// public ResponseEntity<Void> deleteImage(@RequestParam String key) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/dev/barboud/taglens/repository/ImageRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
@RequiredArgsConstructor
public class ImageRepository {
Expand All @@ -26,4 +30,26 @@ public void saveImage(Image image) {
image.getFileSize()
);
}

public Optional<Image> findImageById(UUID imageId) {
String sql = """
SELECT id, user_id, storage_key, content_type, file_size, created_at
FROM images
WHERE id = ?
""";

List<Image> results = jdbcTemplate.query(sql, (rs, rowNum) -> {
Image image = new Image();
image.setId(UUID.fromString(rs.getString("id")));
image.setUserId(UUID.fromString(rs.getString("user_id")));
image.setStorageKey(rs.getString("storage_key"));
image.setContentType(rs.getString("content_type"));
image.setFileSize(rs.getLong("file_size"));
image.setCreatedAt(rs.getTimestamp("created_at").toInstant());
return image;
}, imageId);

return results.stream().findFirst();
}

}
19 changes: 17 additions & 2 deletions src/main/java/dev/barboud/taglens/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ImageService {

private final SessionRepository sessionRepository;
private final ImageRepository imageRepository;
private final StorageService fileService;
private final StorageService storageService;

private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB

Expand Down Expand Up @@ -52,7 +52,7 @@ public ImageResponseDto uploadImage(

// 5. Upload image to Object Storage
String storageKey =
fileService.upload(file);
storageService.upload(file);

// 6. Create Image object
Image image = new Image();
Expand All @@ -73,4 +73,19 @@ public ImageResponseDto uploadImage(
image.getStorageKey()
);
}


public String getImageLink(UUID imageId, UUID sessionId) {
Session session = sessionRepository.findValidSession(sessionId)
.orElseThrow(() -> new RuntimeException("Unauthorized"));

Image image = imageRepository.findImageById(imageId)
.orElseThrow(() -> new RuntimeException("Not found"));

if (!image.getUserId().equals(session.getUserId())) {
throw new RuntimeException("Not found");
}

return storageService.getLink(image.getStorageKey(), 15);
}
}
Loading