mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Extract MimeUtils
This commit is contained in:
parent
b9eee539bd
commit
a8e68dce3a
5 changed files with 59 additions and 25 deletions
|
@ -10,6 +10,7 @@ import org.asamk.signal.manager.storage.recipients.Contact;
|
|||
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
||||
import org.asamk.signal.manager.util.AttachmentUtils;
|
||||
import org.asamk.signal.manager.util.IOUtils;
|
||||
import org.asamk.signal.manager.util.MimeUtils;
|
||||
import org.signal.libsignal.protocol.IdentityKey;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -105,7 +106,7 @@ public class SyncHelper {
|
|||
try (var groupsFileStream = new FileInputStream(groupsFile)) {
|
||||
var attachmentStream = SignalServiceAttachment.newStreamBuilder()
|
||||
.withStream(groupsFileStream)
|
||||
.withContentType("application/octet-stream")
|
||||
.withContentType(MimeUtils.OCTET_STREAM)
|
||||
.withLength(groupsFile.length())
|
||||
.build();
|
||||
|
||||
|
@ -173,7 +174,7 @@ public class SyncHelper {
|
|||
try (var contactsFileStream = new FileInputStream(contactsFile)) {
|
||||
var attachmentStream = SignalServiceAttachment.newStreamBuilder()
|
||||
.withStream(contactsFileStream)
|
||||
.withContentType("application/octet-stream")
|
||||
.withContentType(MimeUtils.OCTET_STREAM)
|
||||
.withLength(contactsFile.length())
|
||||
.build();
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@ public record DataURI(String mediaType, Map<String, String> parameter, byte[] da
|
|||
Pattern.CASE_INSENSITIVE);
|
||||
public static final Pattern PARAMETER_PATTERN = Pattern.compile("\\G;(?<key>.+)=(?<value>.+)",
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
public static final String DEFAULT_TYPE = "text/plain";
|
||||
|
||||
/**
|
||||
* Generates a new {@link DataURI} object that follows
|
||||
|
@ -32,7 +31,7 @@ public record DataURI(String mediaType, Map<String, String> parameter, byte[] da
|
|||
* The {@code <mediatype>} is an Internet media type specification (with
|
||||
* optional parameters.) The appearance of ";base64" means that the data
|
||||
* is encoded as base64. Without ";base64", the data is represented using (ASCII) URL Escaped encoding.
|
||||
* If {@code <mediatype>} is omitted, it defaults to {@link DataURI#DEFAULT_TYPE}.
|
||||
* If {@code <mediatype>} is omitted, it defaults to {@link MimeUtils#PLAIN_TEXT}.
|
||||
* Parameter values should use the URL Escaped encoding.
|
||||
*
|
||||
* @param dataURI the data URI
|
||||
|
@ -65,6 +64,6 @@ public record DataURI(String mediaType, Map<String, String> parameter, byte[] da
|
|||
data = URLDecoder.decode(matcher.group("data"), StandardCharsets.UTF_8).getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
return new DataURI(Optional.ofNullable(matcher.group("type")).orElse(DEFAULT_TYPE), parameters, data);
|
||||
return new DataURI(Optional.ofNullable(matcher.group("type")).orElse(MimeUtils.PLAIN_TEXT), parameters, data);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package org.asamk.signal.manager.util;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MimeUtils {
|
||||
|
||||
public static final String LONG_TEXT = "text/x-signal-plain";
|
||||
public static final String PLAIN_TEXT = "text/plain";
|
||||
public static final String OCTET_STREAM = "application/octet-stream";
|
||||
|
||||
public static Optional<String> getFileMimeType(final File file) throws IOException {
|
||||
var mime = Files.probeContentType(file.toPath());
|
||||
if (mime != null) {
|
||||
return Optional.of(mime);
|
||||
}
|
||||
|
||||
try (final InputStream bufferedStream = new BufferedInputStream(new FileInputStream(file))) {
|
||||
return getStreamMimeType(bufferedStream);
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<String> getStreamMimeType(final InputStream inputStream) throws IOException {
|
||||
return Optional.ofNullable(URLConnection.guessContentTypeFromStream(inputStream));
|
||||
}
|
||||
|
||||
public static Optional<String> guessExtensionFromMimeType(String mimeType) {
|
||||
return Optional.ofNullable(switch (mimeType) {
|
||||
case "application/vnd.android.package-archive" -> "apk";
|
||||
case "application/json" -> "json";
|
||||
case "image/png" -> "png";
|
||||
case "image/jpeg" -> "jpg";
|
||||
case "image/heic" -> "heic";
|
||||
case "image/heif" -> "heif";
|
||||
case "image/webp" -> "webp";
|
||||
case "image/gif" -> "gif";
|
||||
case "audio/aac" -> "aac";
|
||||
case "video/mp4" -> "mp4";
|
||||
case "text/x-vcard" -> "vcf";
|
||||
case PLAIN_TEXT, LONG_TEXT -> "txt";
|
||||
case OCTET_STREAM -> "bin";
|
||||
default -> null;
|
||||
});
|
||||
}
|
||||
}
|
|
@ -12,7 +12,6 @@ import java.io.File;
|
|||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
import java.util.zip.ZipFile;
|
||||
|
@ -118,11 +117,11 @@ public class StickerUtils {
|
|||
if (zip != null) {
|
||||
final var entry = zip.getEntry(subfile);
|
||||
try (InputStream bufferedStream = new BufferedInputStream(zip.getInputStream(entry))) {
|
||||
return URLConnection.guessContentTypeFromStream(bufferedStream);
|
||||
return MimeUtils.getStreamMimeType(bufferedStream).orElse(null);
|
||||
}
|
||||
} else {
|
||||
final var file = new File(rootPath, subfile);
|
||||
return Utils.getFileMimeType(file, null);
|
||||
return MimeUtils.getFileMimeType(file).orElse(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,16 +9,13 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
@ -34,19 +31,6 @@ public class Utils {
|
|||
|
||||
private final static Logger logger = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
public static String getFileMimeType(final File file, final String defaultMimeType) throws IOException {
|
||||
var mime = Files.probeContentType(file.toPath());
|
||||
if (mime == null) {
|
||||
try (final InputStream bufferedStream = new BufferedInputStream(new FileInputStream(file))) {
|
||||
mime = URLConnection.guessContentTypeFromStream(bufferedStream);
|
||||
}
|
||||
}
|
||||
if (mime == null) {
|
||||
return defaultMimeType;
|
||||
}
|
||||
return mime;
|
||||
}
|
||||
|
||||
public static Pair<StreamDetails, Optional<String>> createStreamDetailsFromDataURI(final String dataURI) {
|
||||
final DataURI uri = DataURI.of(dataURI);
|
||||
|
||||
|
@ -57,7 +41,7 @@ public class Utils {
|
|||
public static StreamDetails createStreamDetailsFromFile(final File file) throws IOException {
|
||||
final InputStream stream = new FileInputStream(file);
|
||||
final var size = file.length();
|
||||
final var mime = getFileMimeType(file, "application/octet-stream");
|
||||
final var mime = MimeUtils.getFileMimeType(file).orElse(MimeUtils.OCTET_STREAM);
|
||||
return new StreamDetails(stream, mime, size);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue