Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ class MindRecordFlowAndroidTest {
DailyQuestionWriteViewModel(
savedStateHandle = SavedStateHandle(emptyMap()),
repository = repository,
photoUploadRepository = FakePhotoUploadRepository("https://cdn.test/question.jpg"),
photoUploadRepository =
FakePhotoUploadRepository(
uploadedUrl = "https://cdn.test/question.jpg",
uploadedKey = "mindrecords/1/question.jpg",
),
draftLoader = MindRecordDraftLoader(FakeDiaryRepository(), repository),
)
composeRule.setContent { AfternoteTheme {} }
Expand Down Expand Up @@ -164,7 +168,11 @@ class MindRecordFlowAndroidTest {
),
),
repository = repository,
photoUploadRepository = FakePhotoUploadRepository("https://cdn.test/image.jpg"),
photoUploadRepository =
FakePhotoUploadRepository(
uploadedUrl = "https://cdn.test/image.jpg",
uploadedKey = "mindrecords/1/image.jpg",
),
userRepository = appTestUserRepository(),
draftLoader = MindRecordDraftLoader(repository, FakeDailyQuestionRepository()),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.afternote.afternote_fe.test.FailureArtifactRule
import com.afternote.afternote_fe.test.appTestUserRepository
import com.afternote.core.domain.model.UploadedFile
import com.afternote.core.domain.testing.FakePhotoUploadRepository
import com.afternote.core.ui.theme.AfternoteTheme
import com.afternote.feature.timeletter.domain.model.NewTimeLetterBlock
Expand Down Expand Up @@ -121,7 +122,15 @@ class TimeLetterFlowAndroidTest {
val resolver =
ResolveTimeLetterBlocksUseCase(
FakePhotoUploadRepository(
onUpload = { uri, _ -> Result.success("https://cdn.test/${uri.substringAfterLast('/')}") },
onUpload = { uri, _ ->
val name = uri.substringAfterLast('/')
Result.success(
UploadedFile(
fileUrl = "https://cdn.test/$name",
fileKey = "timeletters/1/$name",
),
)
},
),
)
return TimeLetterWriteViewModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.webkit.MimeTypeMap
import androidx.core.net.toUri
import com.afternote.core.common.di.IoDispatcher
import com.afternote.core.common.result.runCatchingCancellable
import com.afternote.core.domain.model.UploadedFile
import com.afternote.core.domain.repository.PhotoUploadRepository
import com.afternote.core.network.dto.PresignedUrlRequestDto
import com.afternote.core.network.model.requireData
Expand Down Expand Up @@ -73,7 +74,7 @@ internal class PhotoUploadRepositoryImpl
override suspend fun upload(
uriString: String,
directory: String,
): Result<String> =
): Result<UploadedFile> =
runCatchingCancellable {
val uri = uriString.toUri()
val mime = context.contentResolver.getType(uri)
Expand Down Expand Up @@ -127,7 +128,7 @@ internal class PhotoUploadRepositoryImpl
}
}

presigned.fileUrl
UploadedFile(fileUrl = presigned.fileUrl, fileKey = presigned.fileKey)
} finally {
tempFile.delete()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.net.Uri
import androidx.core.net.toUri
import com.afternote.core.common.di.IoDispatcher
import com.afternote.core.common.result.runCatchingCancellable
import com.afternote.core.domain.model.UploadedFile
import com.afternote.core.domain.repository.VideoUploadRepository
import com.afternote.core.network.dto.PresignedUrlRequestDto
import com.afternote.core.network.model.requireData
Expand Down Expand Up @@ -34,7 +35,7 @@ internal class VideoUploadRepositoryImpl
override suspend fun upload(
uriString: String,
directory: String,
): Result<String> =
): Result<UploadedFile> =
runCatchingCancellable {
val uri = uriString.toUri()
val extension = videoExtensionFromUri(uri)
Expand Down Expand Up @@ -85,7 +86,7 @@ internal class VideoUploadRepositoryImpl
}
}

presigned.fileUrl
UploadedFile(fileUrl = presigned.fileUrl, fileKey = presigned.fileKey)
} finally {
tempFile.delete()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.afternote.core.domain.model

/**
* 업로드가 끝난 파일. presigned 응답이 내려주는 두 값을 그대로 들고 있는다.
*
* 종전에는 [fileUrl] 하나만 돌려줘, 키가 필요한 호출부가 URL 앞을 잘라 역산했다. 그 역산은
* `fileUrl == "<스킴>://<호스트>/" + fileKey` 를 가정하는데, 경로 프리픽스나 쿼리스트링이
* 붙는 순간 조용히 어긋난 키가 나간다 (#1017). 두 값을 다 실어 보내 추측을 없앤다.
*/
data class UploadedFile(
/** 화면에 그대로 띄울 수 있는 전체 URL. */
val fileUrl: String,
/** 서버가 파일을 식별하는 키. */
val fileKey: String,
)
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.afternote.core.domain.repository

import com.afternote.core.domain.model.UploadedFile

/**
* Core photo upload: content URI → presigned URL → S3 PUT → file URL.
* Core photo upload: content URI → presigned URL → S3 PUT → uploaded file.
* Reusable for profile image, memorial photo, and any other image upload that uses
* POST /files/presigned-url and the same S3 flow.
*/
fun interface PhotoUploadRepository {
/**
* @param uriString content URI of the selected image (e.g. from gallery).
* @param directory target directory for the file (e.g. "profiles", "afternotes").
* @return Success with image URL (https) or failure.
* @return Success with the uploaded file — full URL and file key both — or failure.
*/
suspend fun upload(
uriString: String,
directory: String,
): Result<String>
): Result<UploadedFile>
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.afternote.core.domain.repository

import com.afternote.core.domain.model.UploadedFile

/**
* Core video upload: content URI → presigned URL → S3 PUT → file URL.
* Core video upload: content URI → presigned URL → S3 PUT → uploaded file.
* Reusable for memorial video, and any other video upload that uses
* POST /files/presigned-url and the same S3 flow.
*/
fun interface VideoUploadRepository {
/**
* @param uriString content URI of the selected video (e.g. from gallery / Photo Picker).
* @param directory target directory for the file (e.g. "afternotes").
* @return Success with video URL (https) or failure.
* @return Success with the uploaded file — full URL and file key both — or failure.
*/
suspend fun upload(
uriString: String,
directory: String,
): Result<String>
): Result<UploadedFile>
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package com.afternote.core.domain.testing

import com.afternote.core.domain.model.UploadedFile
import com.afternote.core.domain.repository.PhotoUploadRepository
import java.util.concurrent.CopyOnWriteArrayList

/** [PhotoUploadRepository] fake 정본. 업로드 요청을 기록하고 기본 CDN URL을 돌려준다. */
/** [PhotoUploadRepository] fake 정본. 업로드 요청을 기록하고 기본 업로드 결과를 돌려준다. */
class FakePhotoUploadRepository(
var uploadedUrl: String = DEFAULT_UPLOADED_URL,
var onUpload: (suspend (String, String) -> Result<String>)? = null,
var uploadedKey: String = DEFAULT_UPLOADED_KEY,
var onUpload: (suspend (String, String) -> Result<UploadedFile>)? = null,
) : PhotoUploadRepository {
val uploads = CopyOnWriteArrayList<Pair<String, String>>()

override suspend fun upload(
uriString: String,
directory: String,
): Result<String> {
): Result<UploadedFile> {
uploads += uriString to directory
onUpload?.let { return it(uriString, directory) }
return Result.success(uploadedUrl)
return Result.success(UploadedFile(fileUrl = uploadedUrl, fileKey = uploadedKey))
}

companion object {
const val DEFAULT_UPLOADED_URL = "https://cdn.test/uploaded"
const val DEFAULT_UPLOADED_KEY = "uploads/test/uploaded"

fun strict(): FakePhotoUploadRepository =
FakePhotoUploadRepository(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.afternote.core.network.dto

import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

Expand Down Expand Up @@ -30,6 +31,27 @@ class PresignedUrlContractTest {
assertTrue("파일 크기가 빠지면 서버가 400 으로 거절한다", encoded.contains("\"contentLength\":357"))
}

@Test
fun `응답의 fileKey 와 fileUrl 을 둘 다 읽는다`() {
// 업로드 결과를 쓰는 쪽은 미리보기용 전체 URL 과 파일 키를 둘 다 필요로 한다. 한쪽만 읽으면
// 나머지를 문자열로 역산하게 되고, 호스트 뒤에 경로가 더 붙는 순간 어긋난다 (#1017).
val decoded =
json.decodeFromString(
PresignedUrlDto.serializer(),
"""
{
"presignedUrl": "https://s3.example/put?sig=abc",
"fileKey": "mindrecords/staging/13/uuid.png",
"fileUrl": "https://cdn.example/assets/mindrecords/staging/13/uuid.png",
"contentType": "image/png"
}
""",
)

assertEquals("mindrecords/staging/13/uuid.png", decoded.fileKey)
assertEquals("https://cdn.example/assets/mindrecords/staging/13/uuid.png", decoded.fileUrl)
}

@Test
fun `크기가 0 이어도 필드는 생략되지 않는다`() {
// 기본값을 두면 kotlinx 가 기본값과 같은 값을 생략할 수 있다 — 크기는 언제나 명시한다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class MemorialMediaUploadRepositoryImpl

is MediaInput.Local -> {
when (kind) {
MediaKind.PHOTO -> photoUploadRepository.upload(input.uri, DIRECTORY_AFTERNOTES)
MediaKind.VIDEO -> videoUploadRepository.upload(input.uri, DIRECTORY_AFTERNOTES)
MediaKind.PHOTO -> photoUploadRepository.upload(input.uri, DIRECTORY_AFTERNOTES).map { it.fileUrl }
MediaKind.VIDEO -> videoUploadRepository.upload(input.uri, DIRECTORY_AFTERNOTES).map { it.fileUrl }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ class DailyQuestionWriteViewModel
_uiState.update { it.copy(isUploadingImage = true, imageUploadError = null) }
return photoUploadRepository
.upload(uriString = uriString, directory = MIND_RECORD_UPLOAD_DIRECTORY)
.onSuccess { url ->
.onSuccess { uploaded ->
// 계약에 imageUrl 이 없어 상태로 들지 않는다 — 본문 img 로 들어가고,
// 제출 직전 fileKey 로 바뀔 수 있게 기억만 해 둔다 (#549).
uploadedImageUrls += url
uploadedImageUrls += uploaded.fileUrl
_uiState.update { it.copy(isUploadingImage = false) }
}.onFailure {
// null 로 흡수하면 사용자는 이미지가 붙은 줄 알고 저장한다 (#716).
Expand All @@ -214,6 +214,7 @@ class DailyQuestionWriteViewModel
)
}
}.getOrNull()
?.fileUrl
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ class DiaryWriteViewModel
_uiState.update { it.copy(isUploadingImage = true, imageUploadError = null) }
return photoUploadRepository
.upload(uriString = uriString, directory = MIND_RECORD_UPLOAD_DIRECTORY)
.onSuccess { url ->
.onSuccess { uploaded ->
// 제출 직전 fileKey 로 바꿀 대상이다 (#1016).
uploadedImageUrls += url
uploadedImageUrls += uploaded.fileUrl
_uiState.update {
val withUrl = if (it.imageUrl == null) it.copy(imageUrl = url) else it
val withUrl = if (it.imageUrl == null) it.copy(imageUrl = uploaded.fileUrl) else it
withUrl.copy(isUploadingImage = false)
}
}.onFailure {
Expand All @@ -132,6 +132,7 @@ class DiaryWriteViewModel
)
}
}.getOrNull()
?.fileUrl
}

fun submit(isDraft: Boolean = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class DailyQuestionResumeDraftTest {
DailyQuestionWriteViewModel(
savedStateHandle = SavedStateHandle(emptyMap()),
repository = repository,
photoUploadRepository = FakePhotoUploadRepository(uploadedUrl = ""),
photoUploadRepository = FakePhotoUploadRepository(uploadedUrl = "", uploadedKey = ""),
// 툴바 카운트는 이 테스트의 관심사가 아니다 — 같은 저장소를 넘겨 0건으로 둔다 (#769).
draftLoader =
MindRecordDraftLoader(
Expand Down Expand Up @@ -151,7 +151,7 @@ class DailyQuestionResumeDraftTest {
DailyQuestionWriteViewModel(
savedStateHandle = SavedStateHandle(emptyMap()),
repository = repository,
photoUploadRepository = FakePhotoUploadRepository(uploadedUrl = ""),
photoUploadRepository = FakePhotoUploadRepository(uploadedUrl = "", uploadedKey = ""),
// 툴바 카운트는 이 테스트의 관심사가 아니다 — 같은 저장소를 넘겨 0건으로 둔다 (#769).
draftLoader =
MindRecordDraftLoader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ class DailyQuestionWriteViewModelTest {
DailyQuestionWriteViewModel(
SavedStateHandle(emptyMap()),
repository,
FakePhotoUploadRepository(uploadedUrl = "https://cdn/just-picked.jpg"),
FakePhotoUploadRepository(
uploadedUrl = "https://cdn/just-picked.jpg",
uploadedKey = "mindrecords/1/just-picked.jpg",
),
noopDraftLoader(),
)

Expand All @@ -135,7 +138,10 @@ class DailyQuestionWriteViewModelTest {
DailyQuestionWriteViewModel(
SavedStateHandle(emptyMap()),
repository,
FakePhotoUploadRepository(uploadedUrl = "https://cdn/picked.jpg"),
FakePhotoUploadRepository(
uploadedUrl = "https://cdn/picked.jpg",
uploadedKey = "mindrecords/staging/13/picked.jpg",
),
noopDraftLoader(),
)

Expand Down Expand Up @@ -225,7 +231,10 @@ class DailyQuestionWriteViewModelTest {
DailyQuestionWriteViewModel(
SavedStateHandle(emptyMap()),
repository,
FakePhotoUploadRepository(uploadedUrl = "https://cdn.example.net/mindrecords/staging/13/a.png"),
FakePhotoUploadRepository(
uploadedUrl = "https://cdn.example.net/mindrecords/staging/13/a.png",
uploadedKey = "mindrecords/staging/13/a.png",
),
noopDraftLoader(),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@ class DiaryWriteImageKeyTest {
DiaryWriteViewModel(
savedStateHandle = SavedStateHandle(emptyMap()),
repository = RecordingDiaryRepository(onCreate),
photoUploadRepository = FakePhotoUploadRepository(uploadedUrl = UPLOADED_URL),
photoUploadRepository =
FakePhotoUploadRepository(
uploadedUrl = UPLOADED_URL,
uploadedKey = UPLOADED_KEY,
),
userRepository = noReceiverUserRepository(),
draftLoader = MindRecordDraftLoader(RecordingDiaryRepository {}, NoDailyQuestionRepository),
)

private companion object {
const val UPLOADED_URL = "https://cdn.example.net/mindrecords/staging/13/a.png"
const val UPLOADED_KEY = "mindrecords/staging/13/a.png"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.afternote.feature.mindrecord.presentation.viewmodel

import androidx.lifecycle.SavedStateHandle
import com.afternote.core.domain.model.UploadedFile
import com.afternote.core.domain.testing.FakePhotoUploadRepository
import com.afternote.feature.mindrecord.domain.model.DailyQuestion
import com.afternote.feature.mindrecord.domain.model.DailyQuestionCreatePayload
Expand Down Expand Up @@ -107,7 +108,7 @@ class MindRecordFailureRecoveryTest {
runTest(dispatcher) {
// 툴바 임시저장은 `enabled` 없는 clickable 이라 canSubmit 을 우회한다 — 그래서
// 상태만으로는 부족하고 submit() 이 직접 막아야 한다 (리뷰 지적).
val uploadGate = CompletableDeferred<Result<String>>()
val uploadGate = CompletableDeferred<Result<UploadedFile>>()
val repository = NeverSubmittingRepository()
val viewModel =
DailyQuestionWriteViewModel(
Expand Down Expand Up @@ -137,7 +138,14 @@ class MindRecordFailureRecoveryTest {
)

// 업로드를 끝내 코루틴을 정리한다.
uploadGate.complete(Result.success("https://cdn.example.com/a.png"))
uploadGate.complete(
Result.success(
UploadedFile(
fileUrl = "https://cdn.example.com/a.png",
fileKey = "mindrecords/staging/13/a.png",
),
),
)
uploading.join()
}
}
Expand Down
Loading
Loading