Image uploads now get a generated thumbnail version

This commit is contained in:
neviyn 2021-05-09 15:44:05 +01:00
parent bd09ba1fe0
commit 87303d1ad7
5 changed files with 12 additions and 3 deletions

View File

@ -101,6 +101,12 @@
<artifactId>bootstrap-icons</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
</dependencies>
<build>

View File

@ -136,7 +136,7 @@ class UploadController
if (!imageConfigurationProperties.types.contains(extension)) return "upload" // TODO: Show error on page, unrecognised file type
val user = memberRepository.findByName(userDetails.username)!!
val hash: String = generateFileHash(file)
val tagData = tags.split(" ").map { tagRepository.findByTagIs(it) ?: tagRepository.save(Tag(it)) }.toMutableSet()
val tagData = tags.split(" ").map { tagRepository.findByTagIs(it) ?: tagRepository.save(Tag(tag = it)) }.toMutableSet()
storage.addImageFile(hash, extension, file.bytes)
imageRepository.save(Image("$hash.$extension", user, tagData))
return "upload" // TODO: Show success on page

View File

@ -1,5 +1,7 @@
package uk.co.neviyn.booru
import net.coobird.thumbnailator.Thumbnails
import net.coobird.thumbnailator.name.Rename
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.io.File
@ -22,6 +24,7 @@ class FileSystemStorageService
override fun addImageFile(name: String, extension: String, data: ByteArray) {
val outputFile = File(imageConfigurationProperties.directory, "$name.$extension")
outputFile.writeBytes(data)
Thumbnails.of(outputFile).size(200, 200).toFiles(Rename.PREFIX_DOT_THUMBNAIL)
}
}

View File

@ -25,7 +25,7 @@
<div class="row" th:if="${imagePage != null}">
<div class="col-2" th:each="image : ${imagePage.content}">
<!--/*@thymesVar id="image" type="uk.co.neviyn.booru.Image"*/-->
<img alt="" class="img-thumbnail" th:src="'/i/' + ${image.filename}"/>
<img alt="" class="img-thumbnail" th:src="'/i/thumbnail.' + ${image.filename}"/>
</div>
</div>
<div class="row" th:if="${imagePage == null}">

View File

@ -13,7 +13,7 @@
</div>
</div>
<div class="row justify-content-center">
<form th:action="${#mvc.url('UC#uploadFile').build()}" class="mb-3 col-6 text-center" enctype="multipart/form-data" method="POST">
<form action="/upload" class="mb-3 col-6 text-center" enctype="multipart/form-data" method="POST">
<input th:name="${_csrf.parameterName}" type="hidden" th:value="${_csrf.token}"/>
<label class="form-label" for="formFile">Image file</label>
<input class="form-control" id="formFile" name="file" type="file" th:accept="${@imageConfigurationProperties.typeListForFormFilter()}">