223 lines
9.7 KiB
Java
223 lines
9.7 KiB
Java
package com.discord.utilities.attachments;
|
|
|
|
import android.content.ClipDescription;
|
|
import android.content.ContentResolver;
|
|
import android.graphics.Bitmap;
|
|
import android.net.Uri;
|
|
import androidx.core.view.inputmethod.InputContentInfoCompat;
|
|
import com.discord.api.message.LocalAttachment;
|
|
import com.discord.utilities.string.StringUtilsKt;
|
|
import com.lytefast.flexinput.model.Attachment;
|
|
import d0.g;
|
|
import d0.g0.t;
|
|
import d0.t.u;
|
|
import d0.z.d.m;
|
|
import g0.e;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import kotlin.Lazy;
|
|
import kotlin.ranges.IntRange;
|
|
/* compiled from: AttachmentUtils.kt */
|
|
public final class AttachmentUtilsKt {
|
|
private static final Lazy REGEX_FILE_NAME_PATTERN$delegate = g.lazy(AttachmentUtilsKt$REGEX_FILE_NAME_PATTERN$2.INSTANCE);
|
|
private static final int UTF_8_RANGE_END_EXCLUSIVE = 126;
|
|
private static final IntRange UTF_8_RANGE_EXCLUSIVE = new IntRange(32, 126);
|
|
private static final int UTF_8_RANGE_START_EXCLUSIVE = 32;
|
|
|
|
public final /* synthetic */ class WhenMappings {
|
|
public static final /* synthetic */ int[] $EnumSwitchMapping$0 = new int[Bitmap.CompressFormat.values().length];
|
|
public static final /* synthetic */ int[] $EnumSwitchMapping$1;
|
|
|
|
static {
|
|
int[] iArr = new int[Bitmap.CompressFormat.values().length];
|
|
$EnumSwitchMapping$1 = iArr;
|
|
iArr[Bitmap.CompressFormat.PNG.ordinal()] = 1;
|
|
iArr[Bitmap.CompressFormat.WEBP.ordinal()] = 2;
|
|
iArr[Bitmap.CompressFormat.JPEG.ordinal()] = 3;
|
|
}
|
|
}
|
|
|
|
public static final /* synthetic */ Pattern access$getREGEX_FILE_NAME_PATTERN$p() {
|
|
return getREGEX_FILE_NAME_PATTERN();
|
|
}
|
|
|
|
public static final String appendLinks(String str, List<? extends Attachment<?>> list) {
|
|
m.checkNotNullParameter(str, "content");
|
|
m.checkNotNullParameter(list, "links");
|
|
return str + "\n" + u.joinToString$default(list, "\n", null, null, 0, null, AttachmentUtilsKt$appendLinks$1.INSTANCE, 30, null);
|
|
}
|
|
|
|
public static final List<Attachment<?>> extractLinks(List<? extends Attachment<?>> list, ContentResolver contentResolver) {
|
|
m.checkNotNullParameter(list, "$this$extractLinks");
|
|
m.checkNotNullParameter(contentResolver, "contentResolver");
|
|
ArrayList arrayList = new ArrayList();
|
|
for (Object obj : list) {
|
|
Attachment attachment = (Attachment) obj;
|
|
Object data = attachment.getData();
|
|
if (!(data instanceof InputContentInfoCompat)) {
|
|
data = null;
|
|
}
|
|
InputContentInfoCompat inputContentInfoCompat = (InputContentInfoCompat) data;
|
|
boolean z2 = true;
|
|
if (inputContentInfoCompat == null || inputContentInfoCompat.getLinkUri() == null || !m.areEqual(getMimeType(attachment, contentResolver), "image/gif")) {
|
|
z2 = false;
|
|
}
|
|
if (z2) {
|
|
arrayList.add(obj);
|
|
}
|
|
}
|
|
return arrayList;
|
|
}
|
|
|
|
private static final String getExtension(Bitmap.CompressFormat compressFormat) {
|
|
int i = WhenMappings.$EnumSwitchMapping$1[compressFormat.ordinal()];
|
|
return i != 1 ? i != 2 ? "jpg" : "webp" : "png";
|
|
}
|
|
|
|
public static final String getMimeType(ContentResolver contentResolver, Uri uri, String str) {
|
|
m.checkNotNullParameter(uri, "uri");
|
|
String str2 = null;
|
|
String type = contentResolver != null ? contentResolver.getType(uri) : null;
|
|
if (type != null) {
|
|
return type;
|
|
}
|
|
AttachmentUtilsKt$getMimeType$1 attachmentUtilsKt$getMimeType$1 = AttachmentUtilsKt$getMimeType$1.INSTANCE;
|
|
try {
|
|
String invoke = attachmentUtilsKt$getMimeType$1.invoke(String.valueOf(uri));
|
|
str2 = invoke != null ? invoke : attachmentUtilsKt$getMimeType$1.invoke(String.valueOf(str));
|
|
} catch (Exception unused) {
|
|
}
|
|
return str2 != null ? str2 : "application/octet-stream";
|
|
}
|
|
|
|
public static final String getMimeType(Attachment<?> attachment, ContentResolver contentResolver) {
|
|
ClipDescription description;
|
|
m.checkNotNullParameter(attachment, "$this$getMimeType");
|
|
Object data = attachment.getData();
|
|
String str = null;
|
|
if (!(data instanceof InputContentInfoCompat)) {
|
|
data = null;
|
|
}
|
|
InputContentInfoCompat inputContentInfoCompat = (InputContentInfoCompat) data;
|
|
if (!(inputContentInfoCompat == null || (description = inputContentInfoCompat.getDescription()) == null)) {
|
|
str = description.getMimeType(0);
|
|
}
|
|
return str != null ? str : getMimeType(contentResolver, attachment.getUri(), attachment.getDisplayName());
|
|
}
|
|
|
|
public static /* synthetic */ String getMimeType$default(ContentResolver contentResolver, Uri uri, String str, int i, Object obj) {
|
|
if ((i & 4) != 0) {
|
|
str = null;
|
|
}
|
|
return getMimeType(contentResolver, uri, str);
|
|
}
|
|
|
|
private static final Pattern getREGEX_FILE_NAME_PATTERN() {
|
|
return (Pattern) REGEX_FILE_NAME_PATTERN$delegate.getValue();
|
|
}
|
|
|
|
public static final String getSanitizedFileName(String str, Bitmap.CompressFormat compressFormat) {
|
|
m.checkNotNullParameter(str, "fileName");
|
|
if (compressFormat != null) {
|
|
Matcher matcher = getREGEX_FILE_NAME_PATTERN().matcher(str);
|
|
if (matcher.matches()) {
|
|
str = matcher.group(1);
|
|
}
|
|
str = str + '.' + getExtension(compressFormat);
|
|
}
|
|
return toHumanReadableAscii(StringUtilsKt.stripAccents(str));
|
|
}
|
|
|
|
public static final boolean isGif(Attachment<?> attachment, ContentResolver contentResolver) {
|
|
m.checkNotNullParameter(attachment, "$this$isGif");
|
|
return m.areEqual(getMimeType(attachment, contentResolver), "image/gif");
|
|
}
|
|
|
|
public static final boolean isImage(ContentResolver contentResolver, Uri uri, String str) {
|
|
m.checkNotNullParameter(uri, "uri");
|
|
return t.startsWith$default(getMimeType(contentResolver, uri, str), "image", false, 2, null);
|
|
}
|
|
|
|
public static final boolean isImage(Attachment<?> attachment, ContentResolver contentResolver) {
|
|
m.checkNotNullParameter(attachment, "$this$isImage");
|
|
return t.startsWith$default(getMimeType(attachment, contentResolver), "image", false, 2, null);
|
|
}
|
|
|
|
public static /* synthetic */ boolean isImage$default(ContentResolver contentResolver, Uri uri, String str, int i, Object obj) {
|
|
if ((i & 4) != 0) {
|
|
str = null;
|
|
}
|
|
return isImage(contentResolver, uri, str);
|
|
}
|
|
|
|
public static final boolean isImageAttachment(LocalAttachment localAttachment, ContentResolver contentResolver) {
|
|
m.checkNotNullParameter(localAttachment, "$this$isImageAttachment");
|
|
m.checkNotNullParameter(contentResolver, "contentResolver");
|
|
Uri parse = Uri.parse(localAttachment.c());
|
|
m.checkNotNullExpressionValue(parse, "Uri.parse(uriString)");
|
|
return isImage(contentResolver, parse, localAttachment.a());
|
|
}
|
|
|
|
public static final boolean isVideo(ContentResolver contentResolver, Uri uri, String str) {
|
|
m.checkNotNullParameter(uri, "uri");
|
|
return t.startsWith$default(getMimeType(contentResolver, uri, str), "video", false, 2, null);
|
|
}
|
|
|
|
public static final boolean isVideo(Attachment<?> attachment, ContentResolver contentResolver) {
|
|
m.checkNotNullParameter(attachment, "$this$isVideo");
|
|
return t.startsWith$default(getMimeType(attachment, contentResolver), "video", false, 2, null);
|
|
}
|
|
|
|
public static /* synthetic */ boolean isVideo$default(ContentResolver contentResolver, Uri uri, String str, int i, Object obj) {
|
|
if ((i & 4) != 0) {
|
|
str = null;
|
|
}
|
|
return isVideo(contentResolver, uri, str);
|
|
}
|
|
|
|
public static final boolean isVideoAttachment(LocalAttachment localAttachment, ContentResolver contentResolver) {
|
|
m.checkNotNullParameter(localAttachment, "$this$isVideoAttachment");
|
|
m.checkNotNullParameter(contentResolver, "contentResolver");
|
|
Uri parse = Uri.parse(localAttachment.c());
|
|
m.checkNotNullExpressionValue(parse, "Uri.parse(uriString)");
|
|
return isVideo(contentResolver, parse, localAttachment.a());
|
|
}
|
|
|
|
public static final Attachment toAttachment(LocalAttachment localAttachment) {
|
|
m.checkNotNullParameter(localAttachment, "$this$toAttachment");
|
|
long b = localAttachment.b();
|
|
Uri parse = Uri.parse(localAttachment.c());
|
|
m.checkNotNullExpressionValue(parse, "Uri.parse(uriString)");
|
|
return new Attachment(b, parse, localAttachment.a(), null);
|
|
}
|
|
|
|
private static final String toHumanReadableAscii(String str) {
|
|
int i = 0;
|
|
while (i < str.length()) {
|
|
int codePointAt = str.codePointAt(i);
|
|
if (UTF_8_RANGE_EXCLUSIVE.contains(codePointAt)) {
|
|
i += Character.charCount(codePointAt);
|
|
} else {
|
|
e eVar = new e();
|
|
eVar.c0(str, 0, i);
|
|
while (i < str.length()) {
|
|
int codePointAt2 = str.codePointAt(i);
|
|
eVar.e0(UTF_8_RANGE_EXCLUSIVE.contains(codePointAt2) ? codePointAt2 : 63);
|
|
i += Character.charCount(codePointAt2);
|
|
}
|
|
return eVar.G();
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
|
|
public static final LocalAttachment toLocalAttachment(Attachment<?> attachment) {
|
|
m.checkNotNullParameter(attachment, "$this$toLocalAttachment");
|
|
long id2 = attachment.getId();
|
|
String uri = attachment.getUri().toString();
|
|
m.checkNotNullExpressionValue(uri, "uri.toString()");
|
|
return new LocalAttachment(id2, uri, attachment.getDisplayName());
|
|
}
|
|
}
|