@@ -4,13 +4,17 @@ import io.minio.GetObjectArgs
4
4
import io.minio.MinioClient
5
5
import io.minio.PutObjectArgs
6
6
import io.minio.RemoveObjectArgs
7
+ import io.minio.errors.ErrorResponseException
7
8
import kotlinx.coroutines.Dispatchers
8
9
import kotlinx.coroutines.withContext
9
10
import org.apache.http.entity.ContentType
10
11
import org.springframework.beans.factory.annotation.Value
11
12
import org.springframework.stereotype.Repository
13
+ import ru.ifmo.se.dating.exception.NotFoundException
12
14
import ru.ifmo.se.dating.people.model.Picture
13
15
import ru.ifmo.se.dating.people.storage.PictureContentStorage
16
+ import ru.ifmo.se.dating.people.storage.minio.exception.NoSuchKeyException
17
+ import ru.ifmo.se.dating.people.storage.minio.exception.UnknownException
14
18
import java.io.ByteArrayInputStream
15
19
16
20
@Repository
@@ -22,23 +26,22 @@ class MinioPictureContentStorage(
22
26
) : PictureContentStorage {
23
27
private val contentType = ContentType .IMAGE_JPEG
24
28
25
- override suspend fun upload (id : Picture .Id , content : Picture .Content ): Unit =
26
- withContext(Dispatchers .IO ) {
27
- PutObjectArgs .builder()
28
- .bucket(bucket)
29
- .`object `(objectName(id))
30
- .stream(
31
- ByteArrayInputStream (content.bytes),
32
- content.bytes.size.toLong(),
33
- /* partSize = */ - 1 ,
34
- )
35
- .contentType(contentType.mimeType)
36
- .build()
37
- .let { minio.putObject(it) }
38
- }
29
+ override suspend fun upload (id : Picture .Id , content : Picture .Content ): Unit = wrapped {
30
+ PutObjectArgs .builder()
31
+ .bucket(bucket)
32
+ .`object `(objectName(id))
33
+ .stream(
34
+ ByteArrayInputStream (content.bytes),
35
+ content.bytes.size.toLong(),
36
+ /* partSize = */ - 1 ,
37
+ )
38
+ .contentType(contentType.mimeType)
39
+ .build()
40
+ .let { minio.putObject(it) }
41
+ }
39
42
40
- override suspend fun download (id : Picture .Id ): Picture .Content =
41
- withContext( Dispatchers . IO ) {
43
+ override suspend fun download (id : Picture .Id ): Picture .Content = try {
44
+ wrapped {
42
45
GetObjectArgs .builder()
43
46
.bucket(bucket)
44
47
.`object `(objectName(id))
@@ -47,15 +50,31 @@ class MinioPictureContentStorage(
47
50
.use { it.readBytes() }
48
51
.let { Picture .Content (it) }
49
52
}
53
+ } catch (e: NoSuchKeyException ) {
54
+ throw NotFoundException (e.message!! , e)
55
+ }
50
56
51
- override suspend fun remove (id : Picture .Id ) =
52
- withContext(Dispatchers .IO ) {
53
- RemoveObjectArgs .builder()
54
- .bucket(bucket)
55
- .`object `(objectName(id))
56
- .build()
57
- .let { minio.removeObject(it) }
58
- }
57
+ override suspend fun remove (id : Picture .Id ) = wrapped {
58
+ RemoveObjectArgs .builder()
59
+ .bucket(bucket)
60
+ .`object `(objectName(id))
61
+ .build()
62
+ .let { minio.removeObject(it) }
63
+ }
59
64
60
65
private fun objectName (id : Picture .Id ) = " $id .jpg"
66
+
67
+ private suspend fun <T > wrapped (action : () -> T ) = try {
68
+ withContext(Dispatchers .IO ) { action() }
69
+ } catch (e: ErrorResponseException ) {
70
+ val bucketName: String? = e.errorResponse().bucketName()
71
+ val objectName: String? = e.errorResponse().objectName()
72
+ when (e.errorResponse().code()) {
73
+ " NoSuchKey" ->
74
+ throw NoSuchKeyException (" Key (bucket: $bucketName , object: $objectName ) not found" )
75
+
76
+ else ->
77
+ throw UnknownException (" Unknown error: ${e.errorResponse().message()} " , e)
78
+ }
79
+ }
61
80
}
0 commit comments