Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Fix download manager content type detection

AuthorDaveJarvis <email>
Date2023-02-20 17:46:24 GMT-0800
Commit55c334fe3ab43f75a0c636a43c3e941990284221
Parent19f70bf
Delta13 lines added, 8 lines removed, 5-line increase
src/main/java/com/keenwrite/io/downloads/DownloadManager.java
public static DownloadToken open( final URL url ) throws IOException {
final var conn = connect( url );
+ final var contentType = conn.getContentType();
- MediaType contentType;
+ MediaType remoteType;
try {
- contentType = MediaType.valueFrom( conn.getContentType() );
+ remoteType = MediaType.valueFrom( contentType );
} catch( final Exception ex ) {
// If the media type couldn't be detected, try using the stream.
- contentType = MediaType.UNDEFINED;
+ remoteType = MediaType.UNDEFINED;
}
final var input = open( conn );
// Peek at the magic header bytes to determine the media type.
final var magicType = MediaTypeSniffer.getMediaType( input );
// If the transport protocol's Content-Type doesn't align with the
- // media type for the magic header, defer to the transport protocol.
- final MediaType mediaType =
- !contentType.equals( magicType ) && !magicType.isUndefined()
- ? contentType
- : magicType;
+ // media type for the magic header, defer to the transport protocol (so
+ // long as the content type was sent from the remote side).
+ final MediaType mediaType = remoteType.equals( magicType )
+ ? remoteType
+ : contentType != null && !contentType.isBlank()
+ ? remoteType
+ : magicType.isUndefined()
+ ? remoteType
+ : magicType;
return new DownloadToken( conn, input, mediaType );