mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Use guessContentTypeFromStream as fallback for probeContentType method
Fixes #233 Fixes #109
This commit is contained in:
parent
e508fc50e9
commit
8115b16bfb
1 changed files with 16 additions and 4 deletions
|
@ -18,6 +18,7 @@ import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
|
||||||
import org.whispersystems.signalservice.api.util.StreamDetails;
|
import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||||
import org.whispersystems.signalservice.internal.util.Base64;
|
import org.whispersystems.signalservice.internal.util.Base64;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -29,6 +30,7 @@ import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.net.URLConnection;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
@ -59,13 +61,23 @@ class Utils {
|
||||||
return SignalServiceAttachments;
|
return SignalServiceAttachments;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
|
private static String getFileMimeType(File file) throws IOException {
|
||||||
InputStream attachmentStream = new FileInputStream(attachmentFile);
|
String mime = Files.probeContentType(file.toPath());
|
||||||
final long attachmentSize = attachmentFile.length();
|
if (mime == null) {
|
||||||
String mime = Files.probeContentType(attachmentFile.toPath());
|
try (InputStream bufferedStream = new BufferedInputStream(new FileInputStream(file))) {
|
||||||
|
mime = URLConnection.guessContentTypeFromStream(bufferedStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (mime == null) {
|
if (mime == null) {
|
||||||
mime = "application/octet-stream";
|
mime = "application/octet-stream";
|
||||||
}
|
}
|
||||||
|
return mime;
|
||||||
|
}
|
||||||
|
|
||||||
|
static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
|
||||||
|
InputStream attachmentStream = new FileInputStream(attachmentFile);
|
||||||
|
final long attachmentSize = attachmentFile.length();
|
||||||
|
final String mime = getFileMimeType(attachmentFile);
|
||||||
// TODO mabybe add a parameter to set the voiceNote, preview, width, height and caption option
|
// TODO mabybe add a parameter to set the voiceNote, preview, width, height and caption option
|
||||||
Optional<byte[]> preview = Optional.absent();
|
Optional<byte[]> preview = Optional.absent();
|
||||||
Optional<String> caption = Optional.absent();
|
Optional<String> caption = Optional.absent();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue