Fix the 6 issues reported by Sonar.

This commit is contained in:
Benoit Marty 2023-06-16 18:08:20 +02:00
parent 8b2121ae99
commit 2452d8aa91
7 changed files with 24 additions and 7 deletions

View file

@ -20,6 +20,7 @@ import android.net.Uri
import io.element.android.features.createroom.impl.configureroom.RoomPrivacy
import io.element.android.features.createroom.impl.di.CreateRoomScope
import io.element.android.features.createroom.impl.userlist.UserListDataStore
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.di.SingleIn
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.Flow
@ -36,7 +37,7 @@ class CreateRoomDataStore @Inject constructor(
private val createRoomConfigFlow: MutableStateFlow<CreateRoomConfig> = MutableStateFlow(CreateRoomConfig())
private var cachedAvatarUri: Uri? = null
set(value) {
field?.path?.let { File(it) }?.delete()
field?.path?.let { File(it) }?.safeDelete()
field = value
}

View file

@ -80,8 +80,7 @@ fun ConfirmAccountProviderView(
id = if (state.isAccountCreation) {
R.string.screen_account_provider_signup_subtitle
} else {
// Use same value for now.
R.string.screen_account_provider_signup_subtitle
R.string.screen_account_provider_signin_subtitle
},
)
)

View file

@ -6,6 +6,7 @@
<string name="screen_account_provider_form_notice">"Enter a search term or a domain address."</string>
<string name="screen_account_provider_form_subtitle">"Search for a company, community, or private server."</string>
<string name="screen_account_provider_form_title">"Find an account provider"</string>
<string name="screen_account_provider_signin_subtitle">"This is where you conversations will live — just like you would use an email provider to keep your emails."</string>
<string name="screen_account_provider_signin_title">"Youre about to sign in to %s"</string>
<string name="screen_account_provider_signup_subtitle">"This is where you conversations will live — just like you would use an email provider to keep your emails."</string>
<string name="screen_account_provider_signup_title">"Youre about to create an account on %s"</string>

View file

@ -35,6 +35,19 @@ fun File.safeDelete() {
)
}
fun File.safeRenameTo(dest: File) {
tryOrNull(
onError = {
Timber.e(it, "Error, unable to rename file $path to ${dest.path}")
},
operation = {
if (renameTo(dest).not()) {
Timber.w("Warning, unable to rename file $path to ${dest.path}")
}
}
)
}
fun Context.createTmpFile(baseDir: File = cacheDir, extension: String? = null): File {
val suffix = extension?.let { ".$extension" }
return File.createTempFile(UUID.randomUUID().toString(), suffix, baseDir).apply { mkdirs() }

View file

@ -24,6 +24,7 @@ import androidx.exifinterface.media.ExifInterface
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.androidutils.file.createTmpFile
import io.element.android.libraries.androidutils.file.getFileName
import io.element.android.libraries.androidutils.file.safeRenameTo
import io.element.android.libraries.androidutils.media.runAndRelease
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.data.tryOrNull
@ -133,7 +134,7 @@ class AndroidMediaPreProcessor @Inject constructor(
private fun MediaUploadInfo.postProcess(uri: Uri): MediaUploadInfo {
val name = context.getFileName(uri) ?: return this
val renamedFile = File(context.cacheDir, name).also {
file.renameTo(it)
file.safeRenameTo(it)
}
return when (this) {
is MediaUploadInfo.AnyFile -> copy(file = renamedFile)

View file

@ -21,6 +21,7 @@ import android.net.Uri
import com.otaliastudios.transcoder.Transcoder
import com.otaliastudios.transcoder.TranscoderListener
import io.element.android.libraries.androidutils.file.createTmpFile
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.di.ApplicationContext
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
@ -46,12 +47,12 @@ class VideoCompressor @Inject constructor(
}
override fun onTranscodeCanceled() {
tmpFile.delete()
tmpFile.safeDelete()
close()
}
override fun onTranscodeFailed(exception: Throwable) {
tmpFile.delete()
tmpFile.safeDelete()
close(exception)
}
})

View file

@ -18,6 +18,7 @@ package io.element.android.libraries.push.impl.notifications
import android.content.Context
import io.element.android.libraries.androidutils.file.EncryptedFileFactory
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.core.data.tryOrNull
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.di.ApplicationContext
@ -70,7 +71,7 @@ class NotificationEventPersistence @Inject constructor(
fun persistEvents(queuedEvents: NotificationEventQueue) {
Timber.tag(loggerTag.value).d("Serializing ${queuedEvents.rawEvents().size} NotifiableEvent(s)")
// Always delete file before writing, or encryptedFile.openFileOutput() will throw
file.delete()
file.safeDelete()
if (queuedEvents.isEmpty()) return
try {
encryptedFile.openFileOutput().use { fos ->