|
@@ -6,7 +6,6 @@ import net.coobird.thumbnailator.geometry.Positions
|
|
|
import org.noear.solon.annotation.Component
|
|
import org.noear.solon.annotation.Component
|
|
|
import org.noear.solon.annotation.Inject
|
|
import org.noear.solon.annotation.Inject
|
|
|
import org.noear.solon.core.handle.Context
|
|
import org.noear.solon.core.handle.Context
|
|
|
-import org.noear.solon.net.http.HttpUtils
|
|
|
|
|
import java.awt.Color
|
|
import java.awt.Color
|
|
|
import java.io.*
|
|
import java.io.*
|
|
|
import java.net.URL
|
|
import java.net.URL
|
|
@@ -34,7 +33,6 @@ class ThumbnailService {
|
|
|
* @param relativePath 相对文件路径
|
|
* @param relativePath 相对文件路径
|
|
|
* @param width 目标宽度
|
|
* @param width 目标宽度
|
|
|
* @param height 目标高度
|
|
* @param height 目标高度
|
|
|
- * @param outputStream 输出流
|
|
|
|
|
*/
|
|
*/
|
|
|
fun generateThumbnailToStream(relativePath: String, width: Int, height: Int, type: String) { // 构建完整的文件路径
|
|
fun generateThumbnailToStream(relativePath: String, width: Int, height: Int, type: String) { // 构建完整的文件路径
|
|
|
val fullPath = filePrefix + relativePath
|
|
val fullPath = filePrefix + relativePath
|
|
@@ -61,10 +59,10 @@ class ThumbnailService {
|
|
|
"8" -> this.sourceRegion(Positions.BOTTOM_CENTER, width, height)
|
|
"8" -> this.sourceRegion(Positions.BOTTOM_CENTER, width, height)
|
|
|
"9" -> this.sourceRegion(Positions.BOTTOM_RIGHT, width, height)
|
|
"9" -> this.sourceRegion(Positions.BOTTOM_RIGHT, width, height)
|
|
|
}
|
|
}
|
|
|
- }.size(width,height).outputFormat("jpg").outputQuality(quality!!) // 先保存到文件(用于缓存)
|
|
|
|
|
|
|
+ }.size(width, height).outputFormat("jpg").outputQuality(quality!!) // 先保存到文件(用于缓存)
|
|
|
thumbnailBuilder.toFile(thumbnailFile)
|
|
thumbnailBuilder.toFile(thumbnailFile)
|
|
|
}
|
|
}
|
|
|
- var context = Context.current()
|
|
|
|
|
|
|
+ val context = Context.current()
|
|
|
context.headerAdd("content-Type", "image/jpeg")
|
|
context.headerAdd("content-Type", "image/jpeg")
|
|
|
context.outputStream().use {
|
|
context.outputStream().use {
|
|
|
Files.copy(thumbnailFile.toPath(), it)
|
|
Files.copy(thumbnailFile.toPath(), it)
|
|
@@ -82,22 +80,6 @@ class ThumbnailService {
|
|
|
return File(directory, thumbnailName).absolutePath
|
|
return File(directory, thumbnailName).absolutePath
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 验证图片文件路径
|
|
|
|
|
- */
|
|
|
|
|
- private fun validateImagePath(path: String) {
|
|
|
|
|
- val file = File(path)
|
|
|
|
|
- require(file.exists()) { "文件不存在: $path" }
|
|
|
|
|
- require(file.isFile) { "路径不是一个文件: $path" }
|
|
|
|
|
- require(file.length() <= 50 * 1024 * 1024) { "文件大小不能超过50MB" }
|
|
|
|
|
-
|
|
|
|
|
- val allowedExtensions = setOf("jpg", "jpeg", "png", "webp", "bmp", "gif")
|
|
|
|
|
- val extension = file.extension.lowercase()
|
|
|
|
|
- require(extension in allowedExtensions) {
|
|
|
|
|
- "不支持的文件类型: $extension,支持的类型: ${allowedExtensions.joinToString()}"
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 生成缩略图
|
|
* 生成缩略图
|
|
|
* @param inputStream 原始图片输入流
|
|
* @param inputStream 原始图片输入流
|