From 7b8d4a7b4ee8b18910d78bf582e626d9db61e0e2 Mon Sep 17 00:00:00 2001 From: neviyn Date: Thu, 29 Apr 2021 20:22:00 +0100 Subject: [PATCH] Added some potential routes --- .../kotlin/uk/co/neviyn/booru/Controller.kt | 82 +++++++++++++++++++ .../kotlin/uk/co/neviyn/booru/Repository.kt | 11 ++- 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/uk/co/neviyn/booru/Controller.kt diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt new file mode 100644 index 0000000..aff234b --- /dev/null +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -0,0 +1,82 @@ +package uk.co.neviyn.booru + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.data.domain.PageRequest +import org.springframework.security.core.annotation.AuthenticationPrincipal +import org.springframework.security.crypto.codec.Hex +import org.springframework.stereotype.Controller +import org.springframework.ui.Model +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.multipart.MultipartFile +import java.io.File +import java.security.MessageDigest + +@Controller +class BaseController { + + @GetMapping("/") + fun landingPage(): String = "landing" + +} + +@Controller("/gallery") +class ImageController +@Autowired constructor( + val imageRepository: ImageRepository, + val tagRepository: TagRepository +) { + + @GetMapping + fun getGalleryPage( + @RequestParam(defaultValue = "1") pageNumber: Int, + @RequestParam tags: List?, + model: Model + ): String { + val page = PageRequest.of(pageNumber - 1, 20) + if (tags != null) { + val tagData = tags.mapNotNull { tagRepository.findByTagIs(it) } + val images = imageRepository.findByTagsIsContaining(tagData, page) + model.addAttribute("images", images) + } else { // If no tag data supplied, just return all images + val images = imageRepository.findAll(page) + model.addAttribute("images", images) + } + return "gallery" + } + +} + +@Controller("/upload") +class UploadController +@Autowired constructor( + val imageRepository: ImageRepository, + val tagRepository: TagRepository, + val userRepository: UserRepository, + val imageConfigurationProperties: ImageConfigurationProperties +) { + + val digest: MessageDigest = MessageDigest.getInstance("SHA-512/256") + + @GetMapping + fun showUploadPage(): String = "upload" + + @PostMapping + fun uploadFile( + @AuthenticationPrincipal userDetails: CustomUserDetails, + @RequestParam file: MultipartFile, + @RequestParam tags: List + ): String { + if (file.isEmpty) return "upload" // TODO: Show error on page, can't upload nothing + val extension = File(file.originalFilename!!).extension + if (!imageConfigurationProperties.types.contains(extension)) return "upload" // TODO: Show error on page, unrecognised file type + val user = userRepository.findByName(userDetails.username)!! + val hash: String = Hex.encode(digest.digest(file.bytes)).toString() + val tagData = tags.map { tagRepository.findByTagIs(it) ?: tagRepository.save(Tag(it)) }.toMutableSet() + val outputFile = File(imageConfigurationProperties.directory, "$hash.$extension") + outputFile.writeBytes(file.bytes) + imageRepository.save(Image("$hash.$extension", user, tagData)) + return "upload" // TODO: Show success on page + } +} \ No newline at end of file diff --git a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt index 37f985a..7d2939a 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt @@ -1,5 +1,8 @@ package uk.co.neviyn.booru +import org.springframework.data.domain.Page +import org.springframework.data.domain.Pageable +import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.repository.CrudRepository interface UserRepository : CrudRepository { @@ -8,6 +11,10 @@ interface UserRepository : CrudRepository { interface RoleRepository : CrudRepository -interface ImageRepository : CrudRepository +interface ImageRepository : JpaRepository { + fun findByTagsIsContaining(tags: List, pageable: Pageable) : Page +} -interface TagRepository : CrudRepository \ No newline at end of file +interface TagRepository : CrudRepository { + fun findByTagIs(tag: String) : Tag? +} \ No newline at end of file