Skip to content

Commit

Permalink
chore: Take into account children of a file to update its size
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Nov 13, 2024
1 parent a2a77fd commit f7f10c6
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers

import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.FileUi

interface File {
val containerUUID: String
val uuid: String
Expand All @@ -37,27 +35,54 @@ interface File {
val parent: File?
val children: List<File>


/**
* Recursively searches for a child with the given name.
*
* @param targetName The name of the child to search for.
* @param fileName The name of the child to search for.
* @return The [File] object representing the found child, or null if not found.
*/
fun findChildByName(fileName: String): File? {
if (this.fileName == fileName) return this

children.forEach { child ->
child.findChildByName(fileName)?.let {
return it
}
}

return null
}

/**
* Recursively searches for a child with the given UUID.
*
* @param uuid The UUID of the child to search for.
* @return The [File] object representing the found child, or null if not found.
*/
fun findChildByName(targetName: String): File? {
if (fileName == targetName) return this
fun findChildByUuid(uuid: String): File? {
if (this.uuid == uuid) return this

children.forEach { child ->
child.findChildByName(targetName)?.let {
child.findChildByUuid(uuid)?.let {
return it
}
}

return null
}

fun treeLines(): List<String> {
return listOf(fileName) + children.flatMap { it.treeLines() }.map { " $it" }
companion object {

fun List<File>.findFirstChildByUuid(uuid: String): File? {
forEach { file ->
val foundChild = file.findChildByUuid(uuid)
if (foundChild != null) {
foundChild.children.forEach { child -> println("children ${child.fileName}") }
return foundChild
}
}
return null
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ package com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.RemoteUploadFile
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadFileSession
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

@OptIn(ExperimentalUuidApi::class)
data class FileUi(
val uid: String,
val fileName: String,
val isFolder: Boolean,
val isFolder: Boolean? = false,
val fileSize: Long,
val mimeType: String?,
val localPath: String?,
Expand All @@ -38,15 +35,15 @@ data class FileUi(
constructor(file: File) : this(
uid = file.uuid,
fileName = file.fileName,
isFolder = false,
isFolder = file.isFolder,
fileSize = file.receivedSizeInBytes,
mimeType = file.mimeType,
localPath = null,
)

// Init for folder
constructor(folderName: String) : this(
uid = Uuid.random().toString(),
uid = folderName,
fileName = folderName,
isFolder = true,
fileSize = 0L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.infomaniak.multiplatform_swisstransfer.managers
import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.exceptions.UnknownException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Transfer
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.FileUi
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadSession
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection
Expand Down Expand Up @@ -85,6 +86,17 @@ class TransferManager internal constructor(
return transferController.getTransfer(transferUUID)?.let(::TransferUi)
}

/**
* Retrieves a flow of files from a transfer, filtered by link UUID and folder UUID.
*
* @param linkUUID The UUID of the transfer link.
* @param folderUuid The UUID of the folder within the transfer.
* @return A flow of lists of [FileUi] objects representing the files in the transfer.
*/
fun getFilesFromTransfer(linkUUID: String, folderUuid: String): Flow<List<FileUi>?> {
return transferController.getFilesFromTransfer(linkUUID, folderUuid)
}

/**
* Retrieves a transfer using the provided link UUID and saves it to the database.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@
package com.infomaniak.multiplatform_swisstransfer.database.controllers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File.Companion.findFirstChildByUuid
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Transfer
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.FileUi
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadSession
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferStatus
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB
import com.infomaniak.multiplatform_swisstransfer.database.utils.FileUtils
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.toRealmList
import io.realm.kotlin.query.RealmResults
import io.realm.kotlin.query.Sort
import io.realm.kotlin.query.TRUE_PREDICATE
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.mapLatest
import kotlin.coroutines.cancellation.CancellationException

Expand Down Expand Up @@ -60,13 +65,24 @@ class TransferController(private val realmProvider: RealmProvider) {
fun getTransfer(linkUUID: String): Transfer? = runThrowingRealm {
return realm?.query<TransferDB>("${TransferDB::linkUUID.name} == '$linkUUID'")?.first()?.find()
}

@Throws(RealmException::class)
fun getFilesFromTransfer(linkUUID: String, folderUuid: String): Flow<List<FileUi>?> = runThrowingRealm {
return flow {
getTransfer(linkUUID)?.container?.files?.let { transferFiles ->
emit(transferFiles.findFirstChildByUuid(folderUuid)?.children?.map(::FileUi))
}
}
}
//endregion

//region Upsert data
@Throws(RealmException::class, CancellationException::class)
suspend fun upsert(transfer: Transfer, transferDirection: TransferDirection) = runThrowingRealm {
realm?.write {
this.copyToRealm(TransferDB(transfer, transferDirection), UpdatePolicy.ALL)
val transferDB = TransferDB(transfer, transferDirection)
transferDB.container?.files?.let { transferDB.container?.files = FileUtils.getFileDBTree(it).toRealmList() }
this.copyToRealm(transferDB, UpdatePolicy.ALL)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.infomaniak.multiplatform_swisstransfer.database.models.transfers

import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Container
import com.infomaniak.multiplatform_swisstransfer.database.utils.FileUtils.getFileDBTree
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadContainer
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadFileSession
import io.realm.kotlin.ext.realmListOf
Expand Down Expand Up @@ -60,8 +59,7 @@ class ContainerDB() : Container, RealmObject {
this.swiftVersion = container.swiftVersion
this.downloadLimit = container.downloadLimit
this.source = container.source
this.files = getFileDBTree(container.files).mapTo(realmListOf(), ::FileDB)
// Retourner une liste de FileDb, rajouter children et isFolder dans FileDb et FileUi, enlever parent, déplacer FileUtils et les tests dans Database
this.files = container.files.mapTo(realmListOf(), ::FileDB)
}

constructor(uploadContainer: UploadContainer, uploadFileSessionList: List<UploadFileSession>) : this() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
package com.infomaniak.multiplatform_swisstransfer.database.models.transfers

import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadContainer
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadFileSession
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
import kotlinx.datetime.Clock
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadContainer
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadFileSession
import kotlinx.datetime.Clock

@OptIn(ExperimentalUuidApi::class)
class FileDB() : File, RealmObject {
@PrimaryKey
override var containerUUID: String = ""
override var uuid: String = ""
override var containerUUID: String = ""
override var fileName: String = ""
override var isFolder: Boolean = false
override var fileSizeInBytes: Long = 0L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ object FileUtils {
val parent = result.second
fileDB.parent = parent
parent?.children?.add(fileDB)
parent?.fileSizeInBytes = parent?.children?.sumOf { it.fileSizeInBytes } ?: 0L
parent?.receivedSizeInBytes = parent?.children?.sumOf { it.receivedSizeInBytes } ?: 0L

tree = result.first.toMutableList()
}
}

return tree
}

fun findFolder(pathComponents: List<String>, tree: List<FileDB>): Pair<List<FileDB>, FileDB?> {
private fun findFolder(pathComponents: List<String>, tree: List<FileDB>): Pair<List<FileDB>, FileDB?> {
var result: FileDB? = null
var modifiedTree = tree.toMutableList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ class FileUtilsTest {

private fun List<FileDB>.getFileDB(name: String) = find { it.fileName == name }

private fun findFirstChildByNameInList(tree: List<FileDB>, targetName: String): File? {
private fun findFirstChildByNameInList(tree: List<FileDB>, fileName: String): File? {
tree.forEach { file ->
val foundChild = file.findChildByName(targetName)
val foundChild = file.findChildByName(fileName)
if (foundChild != null) {
return foundChild
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class FileApi : File {
@SerialName("containerUUID")
override var containerUUID: String = ""
override var fileName: String = ""
@Transient
override var isFolder: Boolean = false
override var fileSizeInBytes: Long = 0L
override var downloadCounter: Int = 0
@SerialName("createdDate")
Expand All @@ -47,4 +49,8 @@ class FileApi : File {
override var path: String? = null
@Transient
override var thumbnailPath: String? = null
@Transient
override var parent: File? = null
@Transient
override var children: List<File> = emptyList()
}

0 comments on commit f7f10c6

Please sign in to comment.