Detect & improve broken files if possible

This commit is contained in:
Sylvain Berfini 2023-12-11 16:28:03 +01:00
parent c6b7ed0ef3
commit 54b9ae8cd4
5 changed files with 83 additions and 16 deletions

View file

@ -267,7 +267,7 @@ class NotificationsManager @MainThread constructor(private val context: Context)
@WorkerThread
override fun onChatRoomRead(core: Core, chatRoom: ChatRoom) {
Log.i(
"$TAG Conversation [$chatRoom] has been marked as read, removing notification if any"
"$TAG Conversation [${LinphoneUtils.getChatRoomId(chatRoom)}] has been marked as read, removing notification if any"
)
dismissChatNotification(chatRoom)
}

View file

@ -9,6 +9,7 @@ import org.linphone.utils.FileUtils
class FileModel @AnyThread constructor(
val file: String,
val fileName: String,
fileSize: Long,
val isWaitingToBeDownloaded: Boolean = false,
private val onClicked: ((model: FileModel) -> Unit)? = null
@ -17,8 +18,6 @@ class FileModel @AnyThread constructor(
private const val TAG = "[File Model]"
}
val fileName: String = FileUtils.getNameFromFilePath(file)
val formattedFileSize = MutableLiveData<String>()
val path = MutableLiveData<String>()

View file

@ -313,14 +313,19 @@ class MessageModel @WorkerThread constructor(
if (content.isFile) {
Log.d("$TAG Found file content with type [${content.type}/${content.subtype}]")
filesContentCount += 1
checkAndRepairFilePathIfNeeded(content)
val path = content.filePath ?: ""
val name = content.name ?: ""
if (path.isNotEmpty()) {
Log.d(
"$TAG Found file ready to be displayed [$path] with MIME [${content.type}/${content.subtype}] for message [${chatMessage.messageId}]"
)
when (content.type) {
"image", "video" -> {
val fileModel = FileModel(path, content.fileSize.toLong()) { model ->
val fileModel = FileModel(path, name, content.fileSize.toLong()) { model ->
onContentClicked?.invoke(model.file)
}
filesPath.add(fileModel)
@ -344,7 +349,7 @@ class MessageModel @WorkerThread constructor(
displayableContentFound = true
}
else -> {
val fileModel = FileModel(path, content.fileSize.toLong()) { model ->
val fileModel = FileModel(path, name, content.fileSize.toLong()) { model ->
onContentClicked?.invoke(model.file)
}
filesPath.add(fileModel)
@ -362,7 +367,7 @@ class MessageModel @WorkerThread constructor(
filesContentCount += 1
val name = content.name ?: ""
if (name.isNotEmpty()) {
val fileModel = FileModel(name, content.fileSize.toLong(), true) { model ->
val fileModel = FileModel(name, name, content.fileSize.toLong(), true) { model ->
Log.d("$TAG Starting downloading content for file [${model.fileName}]")
if (content.filePath.orEmpty().isEmpty()) {
@ -678,4 +683,33 @@ class MessageModel @WorkerThread constructor(
delay(50)
}
}
@WorkerThread
private fun checkAndRepairFilePathIfNeeded(content: Content): String {
val path = content.filePath ?: ""
if (path.isEmpty()) return ""
val name = content.name ?: ""
if (name.isEmpty()) return ""
val extension = FileUtils.getExtensionFromFileName(path)
if (extension.contains("/")) {
Log.w(
"$TAG Weird extension [$extension] found for file [$path], trying with file name [$name]"
)
val fileExtension = FileUtils.getExtensionFromFileName(name)
if (!fileExtension.contains("/")) {
Log.w("$TAG File extension [$fileExtension] seems better, renaming file")
val newPath = FileUtils.renameFile(path, name)
if (newPath.isNotEmpty()) {
content.filePath = newPath
Log.w("$TAG File [$path] has been renamed [${content.filePath}]")
return newPath
} else {
Log.e("$TAG Failed to rename file!")
}
}
}
return ""
}
}

View file

@ -310,9 +310,12 @@ class SendMessageInConversationViewModel @UiThread constructor() : ViewModel() {
fun addAttachment(file: String) {
val list = arrayListOf<FileModel>()
list.addAll(attachments.value.orEmpty())
val model = FileModel(file, 0) { model ->
val fileName = FileUtils.getNameFromFilePath(file)
val model = FileModel(file, fileName, 0) { model ->
removeAttachment(model.file)
}
list.add(model)
attachments.value = list

View file

@ -142,7 +142,7 @@ class FileUtils {
var prefix = 1
while (file.exists()) {
file = File(path, prefix.toString() + "_" + fileName)
Log.w("$TAG File with that name already exists, renamed to ${file.name}")
Log.w("$TAG File with that name already exists, renamed to [${file.name}]")
prefix += 1
}
}
@ -159,7 +159,7 @@ class FileUtils {
var prefix = 1
while (file.exists()) {
file = File(path, prefix.toString() + "_" + fileName)
Log.w("$TAG File with that name already exists, renamed to ${file.name}")
Log.w("$TAG File with that name already exists, renamed to [${file.name}]")
prefix += 1
}
}
@ -190,7 +190,7 @@ class FileUtils {
)
} catch (e: Exception) {
Log.e(
"$TAG Couldn't get URI for file $file using file provider ${context.getString(
"$TAG Couldn't get URI for file [$file] using file provider ${context.getString(
R.string.file_provider
)}"
)
@ -289,13 +289,13 @@ class FileUtils {
suspend fun copyFileTo(filePath: String, outputStream: OutputStream?): Boolean {
if (outputStream == null) {
Log.e("$TAG Can't copy file $filePath to given null output stream")
Log.e("$TAG Can't copy file [$filePath] to given null output stream")
return false
}
val file = File(filePath)
if (!file.exists()) {
Log.e("$TAG Can't copy file $filePath, it doesn't exists")
Log.e("$TAG Can't copy file [$filePath], it doesn't exists")
return false
}
@ -315,21 +315,52 @@ class FileUtils {
return false
}
fun renameFile(from: String, to: String): String {
val source = File(from)
val isImage = isExtensionImage(to)
val dest = getFileStoragePath(to, isImage = isImage, overrideExisting = true)
if (!source.exists()) {
if (dest.exists()) {
Log.w(
"$TAG Source file doesn't exists but destination does, considering renaming done"
)
return dest.absolutePath
}
Log.e("$TAG Can't rename file [${source.absoluteFile}], it doesn't exists!")
return ""
}
val destination = if (dest.exists()) {
getFileStoragePath(to, isImage = isImage, overrideExisting = false)
} else {
dest
}
if (source.renameTo(destination)) {
return destination.absolutePath
}
Log.e(
"$TAG Failed to rename file [${source.absoluteFile}] to [${destination.absoluteFile}]"
)
return ""
}
suspend fun deleteFile(filePath: String) {
withContext(Dispatchers.IO) {
val file = File(filePath)
if (file.exists()) {
try {
if (file.delete()) {
Log.i("$TAG Deleted $filePath")
Log.i("$TAG Deleted [$filePath]")
} else {
Log.e("$TAG Can't delete $filePath")
Log.e("$TAG Can't delete [$filePath]")
}
} catch (e: Exception) {
Log.e("$TAG Can't delete $filePath, exception: $e")
Log.e("$TAG Can't delete [$filePath], exception: $e")
}
} else {
Log.e("$TAG File $filePath doesn't exists")
Log.e("$TAG File [$filePath] doesn't exists")
}
}
}