Added some potential routes
This commit is contained in:
parent
ab44ecfef6
commit
7b8d4a7b4e
82
src/main/kotlin/uk/co/neviyn/booru/Controller.kt
Normal file
82
src/main/kotlin/uk/co/neviyn/booru/Controller.kt
Normal file
@ -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<String>?,
|
||||
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>
|
||||
): 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
|
||||
}
|
||||
}
|
@ -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<User, Long> {
|
||||
@ -8,6 +11,10 @@ interface UserRepository : CrudRepository<User, Long> {
|
||||
|
||||
interface RoleRepository : CrudRepository<Role, Long>
|
||||
|
||||
interface ImageRepository : CrudRepository<Image, Long>
|
||||
interface ImageRepository : JpaRepository<Image, Long> {
|
||||
fun findByTagsIsContaining(tags: List<Tag>, pageable: Pageable) : Page<Image>
|
||||
}
|
||||
|
||||
interface TagRepository : CrudRepository<Tag, Long>
|
||||
interface TagRepository : CrudRepository<Tag, Long> {
|
||||
fun findByTagIs(tag: String) : Tag?
|
||||
}
|
Loading…
Reference in New Issue
Block a user