Skip to content

Commit

Permalink
[LEIP-223] Fix upload crash for json log files
Browse files Browse the repository at this point in the history
[LEIP-223] Fix upload crash for json log files
  • Loading branch information
hb0 authored Aug 26, 2024
2 parents cdb5e33 + 7c88e17 commit 58b5cc6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attachment>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 58b5cc6

Please sign in to comment.