From 7c88e17d3ae5a5699542e7deb3536a80c64d9018 Mon Sep 17 00:00:00 2001 From: hb0 Date: Mon, 26 Aug 2024 14:56:34 +0200 Subject: [PATCH] [LEIP-223] Fix upload crash for json log files --- .../de/cyface/persistence/dao/AttachmentDao.kt | 3 +++ .../serialization/AttachmentSerializer.kt | 2 +- .../serialization/TransferFileSerializer.kt | 3 ++- .../de/cyface/synchronization/SyncAdapter.kt | 14 +++++++++++--- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/persistence/src/main/kotlin/de/cyface/persistence/dao/AttachmentDao.kt b/persistence/src/main/kotlin/de/cyface/persistence/dao/AttachmentDao.kt index 089113010..cf33d26d9 100644 --- a/persistence/src/main/kotlin/de/cyface/persistence/dao/AttachmentDao.kt +++ b/persistence/src/main/kotlin/de/cyface/persistence/dao/AttachmentDao.kt @@ -63,6 +63,9 @@ interface AttachmentDao { @Query("SELECT * FROM ${AttachmentTable.URI_PATH} WHERE ${BaseColumns.MEASUREMENT_ID} = :measurementId LIMIT 1") suspend fun loadOneByMeasurementId(measurementId: Long): Attachment? + @Query("SELECT * FROM ${AttachmentTable.URI_PATH} WHERE ${BaseColumns.MEASUREMENT_ID} = :measurementId AND ${AttachmentTable.COLUMN_TYPE} = :type ORDER BY ${BaseColumns.TIMESTAMP} ASC") + suspend fun loadOneByMeasurementIdAndType(measurementId: Long, type: FileType): Attachment? + @Query("SELECT * FROM ${AttachmentTable.URI_PATH} WHERE ${BaseColumns.MEASUREMENT_ID} = :measurementId AND ${AttachmentTable.COLUMN_STATUS} = :status ORDER BY ${BaseColumns.TIMESTAMP} ASC") suspend fun loadAllByMeasurementIdAndStatus(measurementId: Long, status: AttachmentStatus): List diff --git a/persistence/src/main/kotlin/de/cyface/persistence/serialization/AttachmentSerializer.kt b/persistence/src/main/kotlin/de/cyface/persistence/serialization/AttachmentSerializer.kt index 4224795e5..0f2f852b2 100644 --- a/persistence/src/main/kotlin/de/cyface/persistence/serialization/AttachmentSerializer.kt +++ b/persistence/src/main/kotlin/de/cyface/persistence/serialization/AttachmentSerializer.kt @@ -44,7 +44,7 @@ class AttachmentSerializer { */ fun readFrom(attachment: Attachment) { val builder = de.cyface.protos.model.File.newBuilder() - require(attachment.type == FileType.CSV || attachment.type == FileType.JPG) { "Unsupported type: ${attachment.type}" } + require(attachment.type == FileType.CSV || attachment.type == FileType.JSON || attachment.type == FileType.JPG) { "Unsupported type: ${attachment.type}" } // Ensure we only inject bytes from the correct file format version // The current version of the file format used to persist attachment data. It's stored in each attachment diff --git a/persistence/src/main/kotlin/de/cyface/persistence/serialization/TransferFileSerializer.kt b/persistence/src/main/kotlin/de/cyface/persistence/serialization/TransferFileSerializer.kt index 3eca3b3ca..3391fe593 100644 --- a/persistence/src/main/kotlin/de/cyface/persistence/serialization/TransferFileSerializer.kt +++ b/persistence/src/main/kotlin/de/cyface/persistence/serialization/TransferFileSerializer.kt @@ -277,7 +277,8 @@ object TransferFileSerializer { val builder = de.cyface.protos.model.Measurement.newBuilder() .setFormatVersion(MeasurementSerializer.TRANSFER_FILE_FORMAT_VERSION.toInt()) when (reference.type) { - FileType.CSV -> { + // TODO: zip all attachments + FileType.JSON, FileType.CSV -> { builder.capturingLog = attachment } diff --git a/synchronization/src/main/kotlin/de/cyface/synchronization/SyncAdapter.kt b/synchronization/src/main/kotlin/de/cyface/synchronization/SyncAdapter.kt index 526719896..15e5bbdf1 100644 --- a/synchronization/src/main/kotlin/de/cyface/synchronization/SyncAdapter.kt +++ b/synchronization/src/main/kotlin/de/cyface/synchronization/SyncAdapter.kt @@ -514,6 +514,9 @@ class SyncAdapter private constructor( FileType.CSV -> { Validate.isTrue(format == 1.toShort()) } + FileType.JSON -> { + Validate.isTrue(format == 1.toShort()) + } FileType.JPG -> { Validate.isTrue(format == 1.toShort()) } @@ -627,13 +630,18 @@ class SyncAdapter private constructor( return runBlocking { // Attachments - val logCount = persistence.attachmentDao!!.countByMeasurementIdAndType(measurement.id, FileType.CSV) + val csvCount = persistence.attachmentDao!!.countByMeasurementIdAndType(measurement.id, FileType.CSV) + val jsonCount = persistence.attachmentDao!!.countByMeasurementIdAndType(measurement.id, FileType.JSON) + val logCount = csvCount + jsonCount val imageCount = persistence.attachmentDao!!.countByMeasurementIdAndType(measurement.id, FileType.JPG) val allAttachments = persistence.attachmentDao!!.countByMeasurementId(measurement.id) val unsupportedAttachments = allAttachments - logCount - imageCount - require(unsupportedAttachments == 0) { "Number of unsupported attachments: $unsupportedAttachments" } + require(unsupportedAttachments == 0) { + "Number of unsupported attachments: $unsupportedAttachments" + } val filesSize = if (allAttachments > 0) { - val attachment = persistence.attachmentDao!!.loadOneByMeasurementId(measurement.id) + // TODO: support multiple attachments by zipping them + val attachment = persistence.attachmentDao!!.loadOneByMeasurementIdAndType(measurement.id, FileType.JSON) val folderPath = File(attachment!!.path.parent.toUri()) getFolderSize(folderPath) } else 0