mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-12 17:08:59 -04:00
Merge pull request #3018 from kiwix/Issue#3017
Convert MetaLinkNetworkEntity.java to kotlin
This commit is contained in:
commit
0640b7e73a
@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Kiwix Android
|
||||
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package org.kiwix.kiwixmobile.core.entity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.simpleframework.xml.Attribute;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.ElementMap;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Text;
|
||||
|
||||
@Root(strict = false, name = "metalink")
|
||||
public class MetaLinkNetworkEntity {
|
||||
|
||||
@Element
|
||||
public FileElement file;
|
||||
|
||||
public List<Url> getUrls() {
|
||||
return file.urls;
|
||||
}
|
||||
|
||||
public Url getRelevantUrl() {
|
||||
return file.urls.get(0);
|
||||
}
|
||||
|
||||
public FileElement getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
@Root(strict = false)
|
||||
public static class FileElement {
|
||||
@Attribute
|
||||
public String name;
|
||||
@ElementList(inline = true, entry = "url")
|
||||
public List<Url> urls;
|
||||
@Element
|
||||
private long size;
|
||||
@ElementMap(entry = "hash", key = "type", attribute = true, inline = true, required = false)
|
||||
public Map<String, String> hashes;
|
||||
@Element(required = false)
|
||||
public Pieces pieces;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public int getPieceLength() {
|
||||
return pieces.length;
|
||||
}
|
||||
|
||||
public String getPieceHashType() {
|
||||
return pieces.hashType;
|
||||
}
|
||||
|
||||
public List<String> getPieceHashes() {
|
||||
return pieces.pieceHashes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file hash
|
||||
*
|
||||
* @param type Hash type as defined in metalink file
|
||||
* @return Hash value or {@code null}
|
||||
*/
|
||||
public String getHash(String type) {
|
||||
return hashes.get(type);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Pieces {
|
||||
@Attribute
|
||||
private int length;
|
||||
|
||||
@Attribute(name = "type")
|
||||
public String hashType;
|
||||
|
||||
@ElementList(inline = true, entry = "hash")
|
||||
public List<String> pieceHashes;
|
||||
}
|
||||
|
||||
public static class Url {
|
||||
@Attribute
|
||||
public String location;
|
||||
|
||||
@Attribute
|
||||
private int priority;
|
||||
|
||||
@Text
|
||||
public String value;
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Kiwix Android
|
||||
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package org.kiwix.kiwixmobile.core.entity
|
||||
|
||||
import org.simpleframework.xml.Attribute
|
||||
import org.simpleframework.xml.Element
|
||||
import org.simpleframework.xml.ElementList
|
||||
import org.simpleframework.xml.ElementMap
|
||||
import org.simpleframework.xml.Root
|
||||
import org.simpleframework.xml.Text
|
||||
|
||||
@Root(strict = false, name = "metalink")
|
||||
class MetaLinkNetworkEntity {
|
||||
@field:Element
|
||||
var file: FileElement? = null
|
||||
val urls: List<Url>?
|
||||
get() = file?.urls
|
||||
val relevantUrl: Url
|
||||
get() = file?.urls?.get(0) ?: Url()
|
||||
|
||||
@Root(strict = false)
|
||||
class FileElement {
|
||||
@field:Attribute
|
||||
var name: String? = null
|
||||
|
||||
@field:ElementList(inline = true, entry = "url")
|
||||
var urls: List<Url>? = null
|
||||
@field:Element var size: Long = 0
|
||||
|
||||
@field:ElementMap(
|
||||
entry = "hash",
|
||||
key = "type",
|
||||
attribute = true,
|
||||
inline = true,
|
||||
required = false
|
||||
)
|
||||
var hashes: Map<String, String>? = null
|
||||
|
||||
@field:Element(required = false)
|
||||
var pieces: Pieces? = null
|
||||
val pieceHashes: List<String>?
|
||||
get() = pieces?.pieceHashes
|
||||
|
||||
/**
|
||||
* Get file hash
|
||||
*
|
||||
* @param type Hash type as defined in metalink file
|
||||
* @return Hash value or `null`
|
||||
*/
|
||||
fun getHash(type: String): String? = hashes?.get(type)
|
||||
}
|
||||
|
||||
class Pieces {
|
||||
@field:Attribute
|
||||
var length = 0
|
||||
|
||||
@field:Attribute(name = "type")
|
||||
var hashType: String? = null
|
||||
|
||||
@field:ElementList(inline = true, entry = "hash")
|
||||
var pieceHashes: List<String>? = null
|
||||
}
|
||||
|
||||
class Url {
|
||||
@field:Attribute
|
||||
var location: String? = null
|
||||
|
||||
@field:Attribute
|
||||
var priority = 0
|
||||
|
||||
@field:Text
|
||||
var value: String? = null
|
||||
}
|
||||
}
|
@ -33,65 +33,63 @@ class MetaLinkNetworkEntityTest {
|
||||
"wikipedia_af_all_nopic_2016-05.zim.meta4"
|
||||
)
|
||||
)
|
||||
|
||||
MetaLinkNetworkEntityUrlAssert(result.urls).hasItems(
|
||||
listOf(
|
||||
DummyUrl(
|
||||
"us",
|
||||
1,
|
||||
"http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||
),
|
||||
DummyUrl(
|
||||
"gb",
|
||||
2,
|
||||
"http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" // ktlint-disable
|
||||
),
|
||||
DummyUrl(
|
||||
"us",
|
||||
3,
|
||||
"http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||
),
|
||||
DummyUrl(
|
||||
"de",
|
||||
4,
|
||||
"http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||
),
|
||||
DummyUrl(
|
||||
"fr",
|
||||
5,
|
||||
"http://mirror3.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||
result?.urls?.let {
|
||||
MetaLinkNetworkEntityUrlAssert(it).hasItems(
|
||||
listOf(
|
||||
DummyUrl(
|
||||
"us",
|
||||
1,
|
||||
"http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||
),
|
||||
DummyUrl(
|
||||
"gb",
|
||||
2,
|
||||
"http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" // ktlint-disable
|
||||
),
|
||||
DummyUrl(
|
||||
"us",
|
||||
3,
|
||||
"http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||
),
|
||||
DummyUrl(
|
||||
"de",
|
||||
4,
|
||||
"http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||
),
|
||||
DummyUrl(
|
||||
"fr",
|
||||
5,
|
||||
"http://mirror3.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
}
|
||||
// Basic file attributes
|
||||
assertThat(result.getFile().getName()).isEqualTo("wikipedia_af_all_nopic_2016-05.zim")
|
||||
assertThat(result.file?.name).isEqualTo("wikipedia_af_all_nopic_2016-05.zim")
|
||||
|
||||
assertThat(result.getFile().size).isEqualTo(63973123L)
|
||||
assertThat(result.file?.size).isEqualTo(63973123L)
|
||||
|
||||
// File hashes
|
||||
assertThat(result.getFile().getHash("md5")).isEqualTo("6f06866b61c4a921b57f28cfd4307220")
|
||||
assertThat(result.file?.getHash("md5")).isEqualTo("6f06866b61c4a921b57f28cfd4307220")
|
||||
assertThat(
|
||||
result.getFile().getHash("sha-1")
|
||||
result.file?.getHash("sha-1")
|
||||
).isEqualTo("8aac4c7f89e3cdd45b245695e19ecde5aac59593")
|
||||
assertThat(
|
||||
result.getFile().getHash("sha-256")
|
||||
result.file?.getHash("sha-256")
|
||||
).isEqualTo("83126775538cf588a85edb10db04d6e012321a2025278a08a084b258849b3a5c")
|
||||
|
||||
// Pieces
|
||||
assertThat(result.getFile().pieceHashType).isEqualTo("sha-1")
|
||||
assertThat(result.getFile().pieceLength).isEqualTo(1048576)
|
||||
assertThat(result.file?.pieces?.hashType).isEqualTo("sha-1")
|
||||
assertThat(result.file?.pieces?.length).isEqualTo(1048576)
|
||||
|
||||
// Check only the first and the last elements of the piece hashes
|
||||
assertThat(result.getFile().pieceHashes.size).isEqualTo(62)
|
||||
assertThat(result.file?.pieceHashes?.size).isEqualTo(62)
|
||||
assertThat(
|
||||
result.getFile().pieceHashes[0]
|
||||
)
|
||||
.isEqualTo("f36815d904d4fd563aaef4ee6ef2600fb1fd70b2")
|
||||
result.file?.pieceHashes?.get(0) ?: ""
|
||||
).isEqualTo("f36815d904d4fd563aaef4ee6ef2600fb1fd70b2")
|
||||
assertThat(
|
||||
result.getFile().pieceHashes[61]
|
||||
)
|
||||
.isEqualTo("8055e515aa6e78f2810bbb0e0cd07330838b8920")
|
||||
result.file?.pieceHashes?.get(61) ?: ""
|
||||
).isEqualTo("8055e515aa6e78f2810bbb0e0cd07330838b8920")
|
||||
}
|
||||
|
||||
data class DummyUrl(val location: String, val priority: Int, val value: String)
|
||||
|
Loading…
x
Reference in New Issue
Block a user