This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Link properties for archive entry metadata (#169)
- Loading branch information
1 parent
51603ef
commit 3010a60
Showing
6 changed files
with
213 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
r2-shared/src/main/java/org/readium/r2/shared/publication/archive/Properties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2021 Readium Foundation. All rights reserved. | ||
* Use of this source code is governed by the BSD-style license | ||
* available in the top-level LICENSE file of the project. | ||
*/ | ||
|
||
package org.readium.r2.shared.publication.archive | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
import org.json.JSONObject | ||
import org.readium.r2.shared.JSONable | ||
import org.readium.r2.shared.extensions.optNullableBoolean | ||
import org.readium.r2.shared.extensions.optNullableLong | ||
import org.readium.r2.shared.extensions.optNullableString | ||
import org.readium.r2.shared.publication.Properties | ||
import org.readium.r2.shared.publication.encryption.Encryption | ||
import org.readium.r2.shared.publication.encryption.encryption | ||
import org.readium.r2.shared.util.logging.WarningLogger | ||
import org.readium.r2.shared.util.logging.log | ||
|
||
// Archive Link Properties Extension | ||
|
||
/** | ||
* Holds information about how the resource is stored in the publication archive. | ||
* | ||
* @param entryLength The length of the entry stored in the archive. It might be a compressed length | ||
* if the entry is deflated. | ||
* @param isEntryCompressed Indicates whether the entry was compressed before being stored in the | ||
* archive. | ||
*/ | ||
@Parcelize | ||
data class ArchiveProperties( | ||
val entryLength: Long, | ||
val isEntryCompressed: Boolean | ||
) : JSONable, Parcelable { | ||
|
||
override fun toJSON(): JSONObject = JSONObject().apply { | ||
put("entryLength", entryLength) | ||
put("isEntryCompressed", isEntryCompressed) | ||
} | ||
|
||
companion object { | ||
fun fromJSON(json: JSONObject?, warnings: WarningLogger? = null): ArchiveProperties? { | ||
json ?: return null | ||
|
||
val entryLength = json.optNullableLong("entryLength") | ||
val isEntryCompressed = json.optNullableBoolean("isEntryCompressed") | ||
if (entryLength == null || isEntryCompressed == null) { | ||
warnings?.log(ArchiveProperties::class.java, "[entryLength] and [isEntryCompressed] are required", json) | ||
return null | ||
} | ||
|
||
return ArchiveProperties(entryLength = entryLength, isEntryCompressed = isEntryCompressed) | ||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Provides information about how the resource is stored in the publication archive. | ||
*/ | ||
val Properties.archive: ArchiveProperties? | ||
get() = (this["archive"] as? Map<*, *>) | ||
?.let { ArchiveProperties.fromJSON(JSONObject(it)) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
r2-shared/src/test/java/org/readium/r2/shared/publication/archive/PropertiesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.readium.r2.shared.publication.archive | ||
|
||
import org.json.JSONObject | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
import org.readium.r2.shared.assertJSONEquals | ||
import org.readium.r2.shared.publication.Properties | ||
|
||
class PropertiesTest { | ||
|
||
@Test | ||
fun `get no archive`() { | ||
assertNull(Properties().archive) | ||
} | ||
|
||
@Test | ||
fun `get full archive`() { | ||
assertEquals( | ||
ArchiveProperties(entryLength = 8273, isEntryCompressed = true), | ||
Properties(mapOf( | ||
"archive" to mapOf( | ||
"entryLength" to 8273, | ||
"isEntryCompressed" to true | ||
) | ||
)).archive | ||
) | ||
} | ||
|
||
@Test | ||
fun `get invalid archive`() { | ||
assertNull( | ||
Properties(mapOf( | ||
"archive" to mapOf( | ||
"foo" to "bar" | ||
) | ||
)).archive | ||
) | ||
} | ||
|
||
@Test | ||
fun `get incomplete archive`() { | ||
assertNull( | ||
Properties(mapOf( | ||
"archive" to mapOf( | ||
"isEntryCompressed" to true | ||
) | ||
)).archive | ||
) | ||
|
||
assertNull( | ||
Properties(mapOf( | ||
"archive" to mapOf( | ||
"entryLength" to 8273 | ||
) | ||
)).archive | ||
) | ||
} | ||
|
||
@Test | ||
fun `get archive JSON`() { | ||
assertJSONEquals( | ||
JSONObject(mapOf( | ||
"entryLength" to 8273L, | ||
"isEntryCompressed" to true | ||
)), | ||
ArchiveProperties(entryLength = 8273, isEntryCompressed = true).toJSON() | ||
) | ||
} | ||
|
||
} |