diff --git a/mirai-core-mock/src/internal/serverfs/TmpResourceServerImpl.kt b/mirai-core-mock/src/internal/serverfs/TmpResourceServerImpl.kt index a705c690e..ce7f92f7c 100644 --- a/mirai-core-mock/src/internal/serverfs/TmpResourceServerImpl.kt +++ b/mirai-core-mock/src/internal/serverfs/TmpResourceServerImpl.kt @@ -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) } diff --git a/mirai-core-mock/test/ImageUploadTest.kt b/mirai-core-mock/test/ImageUploadTest.kt index 8cc0ceb53..ab0aeaf6f 100644 --- a/mirai-core-mock/test/ImageUploadTest.kt +++ b/mirai-core-mock/test/ImageUploadTest.kt @@ -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 { + Image.randomImageContent().toExternalResource().use { imgData -> + val img1 = bot.asFriend.uploadImage(imgData) + val img2 = bot.asFriend.uploadImage(imgData) + assertEquals(img1, img2) + } + } }