Skip to content

Commit

Permalink
Fix upload retry on note editor;
Browse files Browse the repository at this point in the history
Fix missing image tags in nostr events;
Add runCatching to upload in PrimalFileUploader;
  • Loading branch information
AleksandarIlic committed May 20, 2024
1 parent f6d8331 commit 8b61847
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,29 +281,29 @@ class NoteEditorViewModel @AssistedInject constructor(
}

private suspend fun uploadAttachment(attachment: NoteAttachment, uploadId: UUID) {
var updatedAttachment = attachment
try {
setState { copy(uploadingAttachments = true) }
updateNoteAttachmentState(attachment = attachment.copy(uploadError = null))
updatedAttachment = updatedAttachment.copy(uploadError = null)
updateNoteAttachmentState(attachment = updatedAttachment)

val remoteUrl = withContext(dispatcherProvider.io()) {
attachmentRepository.uploadNoteAttachment(attachment = attachment, uploadId = uploadId)
}
updateNoteAttachmentState(attachment = attachment.copy(remoteUrl = remoteUrl))

if (attachment.isImageAttachment) {
val (mimeType, dimensions) = fileAnalyser.extractImageTypeAndDimensions(
attachment.localUri,
)
updateNoteAttachmentState(
attachment = attachment.copy(
mimeType = mimeType,
otherRelevantInfo = dimensions,
),
updatedAttachment = updatedAttachment.copy(remoteUrl = remoteUrl)
updateNoteAttachmentState(attachment = updatedAttachment)

val (mimeType, dimensions) = fileAnalyser.extractImageTypeAndDimensions(attachment.localUri)
if (mimeType != null && dimensions != null) {
updatedAttachment = updatedAttachment.copy(
mimeType = mimeType,
otherRelevantInfo = dimensions,
)
updateNoteAttachmentState(updatedAttachment)
}
} catch (error: UnsuccessfulFileUpload) {
Timber.w(error)
updateNoteAttachmentState(attachment = attachment.copy(uploadError = error))
updateNoteAttachmentState(attachment = updatedAttachment.copy(uploadError = error))
}
}

Expand Down Expand Up @@ -342,10 +342,11 @@ class NoteEditorViewModel @AssistedInject constructor(

private fun retryAttachmentUpload(attachmentId: UUID) =
viewModelScope.launch {
_state.value.attachments.firstOrNull { it.id == attachmentId }?.let {
val noteAttachment = _state.value.attachments.firstOrNull { it.id == attachmentId }
if (noteAttachment != null) {
val uploadId = UUID.randomUUID()
val job = viewModelScope.launch {
uploadAttachment(attachment = it, uploadId = uploadId)
uploadAttachment(attachment = noteAttachment, uploadId = uploadId)
}
attachmentUploads[attachmentId] = UploadJob(job = job, id = uploadId)
job.join()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class PrimalFileUploader @Inject constructor(
): String {
val fileDigest = MessageDigest.getInstance("SHA-256")
return withContext(dispatchers.io()) {
try {
val uploadResult = runCatching {
uploadsMap[uploadId] = UploadStatus.Uploading
val fileSizeInBytes = uri.readFileSizeInBytes()
val chunkSize = calculateChunkSize(fileSizeInBytes)
Expand Down Expand Up @@ -161,9 +161,14 @@ class PrimalFileUploader @Inject constructor(
)
uploadsMap[uploadId] = UploadStatus.UploadCompleted
remoteUrl
} catch (error: IOException) {
}

val remoteUrl = uploadResult.getOrNull()
if (remoteUrl != null) {
remoteUrl
} else {
uploadsMap[uploadId] = UploadStatus.UploadFailed
throw UnsuccessfulFileUpload(cause = error)
throw UnsuccessfulFileUpload(cause = uploadResult.exceptionOrNull())
}
}
}
Expand Down

0 comments on commit 8b61847

Please sign in to comment.