[mock] Fix image multi-uploading; fix #2401

This commit is contained in:
Karlatemp 2022-12-24 23:29:13 +08:00
parent 60dd4b5be6
commit c957d008fb
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59
2 changed files with 24 additions and 1 deletions

View File

@ -68,7 +68,20 @@ internal class TmpResourceServerImpl(
override suspend fun uploadResourceAsImage(resource: ExternalResource): URI {
val imgId = generateUUID(resource.md5)
val resId = uploadResource(resource)
images.resolve(imgId).createLinkPointingTo(storage.resolve(resId))
val imgPath = images.resolve(imgId)
val storagePath = storage.resolve(resId).toAbsolutePath()
if (imgPath.exists()) {
return resolveImageUrl(imgId)
}
kotlin.runCatching {
imgPath.createLinkPointingTo(storagePath)
}.recoverCatchingSuppressed {
imgPath.createSymbolicLinkPointingTo(storagePath)
}.getOrThrow()
return resolveImageUrl(imgId)
}

View File

@ -19,6 +19,7 @@ import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.net.URL
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
@ -44,4 +45,13 @@ internal class ImageUploadTest {
data.contentEquals(URL(img.queryUrl()).readBytes())
}
}
@Test
fun testSameImageMultiUpload() = runBlocking<Unit> {
Image.randomImageContent().toExternalResource().use { imgData ->
val img1 = bot.asFriend.uploadImage(imgData)
val img2 = bot.asFriend.uploadImage(imgData)
assertEquals(img1, img2)
}
}
}