From 4e3c60925cd8284427c9edfe3ad2bdd65373f4dc Mon Sep 17 00:00:00 2001 From: neviyn Date: Sun, 9 May 2021 21:06:46 +0100 Subject: [PATCH] Images can now be deleted --- .../kotlin/uk/co/neviyn/booru/Controller.kt | 23 ++++++++++++++++++- .../uk/co/neviyn/booru/FileSystemStorage.kt | 9 ++++++++ .../kotlin/uk/co/neviyn/booru/Repository.kt | 2 ++ src/main/resources/application.properties | 1 + src/main/resources/templates/single.html | 3 +++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index 4aa7498..5db5c50 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -6,6 +6,7 @@ import org.springframework.data.domain.PageRequest import org.springframework.security.core.annotation.AuthenticationPrincipal import org.springframework.stereotype.Controller import org.springframework.ui.Model +import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.ModelAttribute import org.springframework.web.bind.annotation.PathVariable @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.multipart.MultipartFile import java.security.MessageDigest import java.util.* +import javax.transaction.Transactional import javax.validation.Valid import javax.validation.constraints.NotEmpty @@ -38,12 +40,13 @@ class BaseController } @GetMapping("/view/{id}") - fun viewSingleImage(@PathVariable id: Long, model: Model) : String { + fun viewSingleImage(@PathVariable id: Long, model: Model, @AuthenticationPrincipal userDetails: CustomUserDetails?) : String { val image = imageRepository.findById(id).get() val tagData = image.tags.sortedBy { it.tag } model.addAttribute("image", image) model.addAttribute("tags", tagData) model.addAttribute("title", tagData.joinToString { it.tag }) + model.addAttribute("isUploader", (userDetails != null && (userDetails.authorities.any { it.authority == "ADMIN" } || userDetails.getId() == image.uploader.id))) return "single" } } @@ -152,6 +155,24 @@ class UploadController imageRepository.save(Image("$hash.$extension", user, tagData)) return "upload" // TODO: Show success on page } + + @DeleteMapping("/d/{imageID}") + @Transactional + fun deleteUpload(@PathVariable imageID: Long, @AuthenticationPrincipal userDetails: CustomUserDetails) : String { + val target = imageRepository.findById(imageID) + if(target.isEmpty) return "gallery" // No image with ID + val rTarget = target.get() + return if(userDetails.authorities.any { it.authority == "ADMIN" } || rTarget.uploader.id == userDetails.getId()) { + rTarget.tags.map { it.amount = it.amount - 1 } + tagRepository.saveAll(rTarget.tags) + imageRepository.deleteTagAssociations(rTarget.id) + storage.deleteImageFile(rTarget.filename) + imageRepository.delete(rTarget) + "gallery" // Success + } else { + "gallery" // Can only delete someone elses images if you are an admin + } + } } val digest: MessageDigest = MessageDigest.getInstance("SHA-512/256") diff --git a/src/main/kotlin/uk/co/neviyn/booru/FileSystemStorage.kt b/src/main/kotlin/uk/co/neviyn/booru/FileSystemStorage.kt index b8c0b4b..344164f 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/FileSystemStorage.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/FileSystemStorage.kt @@ -12,6 +12,7 @@ interface FileSystemStorage { * Add a file to the filesystem with [name] and file [extension] */ fun addImageFile(name: String, extension: String, data: ByteArray) + fun deleteImageFile(name: String) } @@ -27,4 +28,12 @@ class FileSystemStorageService Thumbnails.of(outputFile).size(200, 200).toFiles(Rename.PREFIX_DOT_THUMBNAIL) } + override fun deleteImageFile(name: String) { + File(imageConfigurationProperties.directory, name).delete() + File(imageConfigurationProperties.directory, "thumbnail.$name").delete() + + } + + + } \ 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 a666fab..ab458c4 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt @@ -3,6 +3,7 @@ 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.jpa.repository.Modifying import org.springframework.data.jpa.repository.Query import org.springframework.data.repository.CrudRepository @@ -23,6 +24,7 @@ interface ImageRepository : JpaRepository { fun findAllByTagsContainingOrderById(tag: Tag, pageable: Pageable): Page @Suppress("SqlResolve") + @Modifying @Query(value = "DELETE FROM BOORU.TAG_IMAGE WHERE IMAGE_ID = ?1", nativeQuery = true) fun deleteTagAssociations(imagesID: Long) } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a2a60e6..577dfc8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,5 @@ spring.jpa.hibernate.ddl-auto=validate spring.datasource.initialization-mode=always spring.jpa.properties.hibernate.default_schema=booru +spring.mvc.hiddenmethod.filter.enabled=true images.types=jpg,png,gif \ No newline at end of file diff --git a/src/main/resources/templates/single.html b/src/main/resources/templates/single.html index 3061fc0..df7aff7 100644 --- a/src/main/resources/templates/single.html +++ b/src/main/resources/templates/single.html @@ -27,6 +27,9 @@ +
+ +