@@ -46,18 +46,20 @@ internal class AssetSniffer(
46
46
source : Either <Resource , Container <Resource >>,
47
47
hints : FormatHints
48
48
): Try <Asset , SniffError > {
49
- val initialDescription = Format (
50
- specification = FormatSpecification (emptySet()),
51
- mediaType = MediaType .BINARY ,
52
- fileExtension = FileExtension (" " )
53
- )
49
+ val initialFormat = formatSniffer
50
+ .sniffHints(hints)
51
+ ? : Format (
52
+ specification = FormatSpecification (emptySet()),
53
+ mediaType = MediaType .BINARY ,
54
+ fileExtension = FileExtension (" " )
55
+ )
54
56
55
57
val cachingSource: Either <Readable , Container <Readable >> = when (source) {
56
58
is Either .Left -> Either .Left (CachingReadable (source.value))
57
59
is Either .Right -> Either .Right (CachingContainer (source.value))
58
60
}
59
61
60
- val asset = doSniff(initialDescription , source, cachingSource, hints)
62
+ val asset = sniffContent(initialFormat , source, cachingSource, hints, forceRefine = false )
61
63
.getOrElse { return Try .failure(SniffError .Reading (it)) }
62
64
63
65
return asset
@@ -66,45 +68,39 @@ internal class AssetSniffer(
66
68
? : Try .failure(SniffError .NotRecognized )
67
69
}
68
70
69
- private suspend fun doSniff (
71
+ private suspend fun sniffContent (
70
72
format : Format ,
71
73
source : Either <Resource , Container <Resource >>,
72
74
cache : Either <Readable , Container <Readable >>,
73
- hints : FormatHints
75
+ hints : FormatHints ,
76
+ forceRefine : Boolean
74
77
): Try <Asset , ReadError > {
75
- formatSniffer
76
- .sniffHints(format, hints)
77
- .takeIf { it.conformsTo(format) }
78
- ?.takeIf { it != format }
79
- ?.let { return doSniff(it, source, cache, hints) }
80
-
81
78
when (cache) {
82
79
is Either .Left ->
83
80
formatSniffer
84
81
.sniffBlob(format, cache.value)
85
82
.getOrElse { return Try .failure(it) }
86
- .takeIf { it.conformsTo(format) }
87
- ?.takeIf { it != format }
88
- ?.let { return doSniff(it, source, cache, hints) }
83
+ .takeIf { ! forceRefine || it.refines(format) }
84
+ ?.let { return sniffContent(it, source, cache, hints, forceRefine = true ) }
89
85
90
86
is Either .Right ->
91
87
formatSniffer
92
88
.sniffContainer(format, cache.value)
93
89
.getOrElse { return Try .failure(it) }
94
- .takeIf { it.conformsTo(format) }
95
- ?.takeIf { it != format }
96
- ?.let { return doSniff(it, source, cache, hints) }
90
+ .takeIf { ! forceRefine || it.refines(format) }
91
+ ?.let { return sniffContent(it, source, cache, hints, forceRefine = true ) }
97
92
}
98
93
99
94
if (source is Either .Left ) {
100
95
tryOpenArchive(format, source.value)
101
96
.getOrElse { return Try .failure(it) }
102
97
?.let {
103
- return doSniff (
98
+ return sniffContent (
104
99
it.format,
105
100
Either .Right (it.container),
106
101
Either .Right (CachingContainer (it.container)),
107
- hints
102
+ hints,
103
+ forceRefine = true
108
104
)
109
105
}
110
106
}
0 commit comments