mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 01:40:07 +01:00
Add upload/download size restrictions for attachments based on remote config.
This commit is contained in:
@@ -11,11 +11,14 @@ import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.attachments.Attachment;
|
||||
import org.thoughtcrime.securesms.jobs.AttachmentUploadJob;
|
||||
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
import org.thoughtcrime.securesms.util.FeatureFlags;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
import org.thoughtcrime.securesms.util.MemoryFileDescriptor;
|
||||
import org.whispersystems.signalservice.api.crypto.AttachmentCipherOutputStream;
|
||||
import org.whispersystems.signalservice.internal.crypto.PaddingInputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -50,31 +53,39 @@ public abstract class MediaConstraints {
|
||||
*/
|
||||
public abstract int[] getImageDimensionTargets(Context context);
|
||||
|
||||
public abstract int getGifMaxSize(Context context);
|
||||
public abstract int getVideoMaxSize(Context context);
|
||||
public abstract long getGifMaxSize(Context context);
|
||||
public abstract long getVideoMaxSize(Context context);
|
||||
|
||||
public @IntRange(from = 0, to = 100) int getImageCompressionQualitySetting(@NonNull Context context) {
|
||||
return 70;
|
||||
}
|
||||
|
||||
public int getUncompressedVideoMaxSize(Context context) {
|
||||
public long getUncompressedVideoMaxSize(Context context) {
|
||||
return getVideoMaxSize(context);
|
||||
}
|
||||
|
||||
public int getCompressedVideoMaxSize(Context context) {
|
||||
public long getCompressedVideoMaxSize(Context context) {
|
||||
return getVideoMaxSize(context);
|
||||
}
|
||||
|
||||
public abstract int getAudioMaxSize(Context context);
|
||||
public abstract int getDocumentMaxSize(Context context);
|
||||
public abstract long getAudioMaxSize(Context context);
|
||||
public abstract long getDocumentMaxSize(Context context);
|
||||
|
||||
public long getMaxAttachmentSize() {
|
||||
return AttachmentUploadJob.getMaxPlaintextSize();
|
||||
}
|
||||
|
||||
public boolean isSatisfied(@NonNull Context context, @NonNull Attachment attachment) {
|
||||
try {
|
||||
return (MediaUtil.isGif(attachment) && attachment.getSize() <= getGifMaxSize(context) && isWithinBounds(context, attachment.getUri())) ||
|
||||
(MediaUtil.isImage(attachment) && attachment.getSize() <= getImageMaxSize(context) && isWithinBounds(context, attachment.getUri())) ||
|
||||
(MediaUtil.isAudio(attachment) && attachment.getSize() <= getAudioMaxSize(context)) ||
|
||||
(MediaUtil.isVideo(attachment) && attachment.getSize() <= getVideoMaxSize(context)) ||
|
||||
(MediaUtil.isFile(attachment) && attachment.getSize() <= getDocumentMaxSize(context));
|
||||
long size = attachment.getSize();
|
||||
if (size > getMaxAttachmentSize()) {
|
||||
return false;
|
||||
}
|
||||
return (MediaUtil.isGif(attachment) && size <= getGifMaxSize(context) && isWithinBounds(context, attachment.getUri())) ||
|
||||
(MediaUtil.isImage(attachment) && size <= getImageMaxSize(context) && isWithinBounds(context, attachment.getUri())) ||
|
||||
(MediaUtil.isAudio(attachment) && size <= getAudioMaxSize(context)) ||
|
||||
(MediaUtil.isVideo(attachment) && size <= getVideoMaxSize(context)) ||
|
||||
(MediaUtil.isFile(attachment) && size <= getDocumentMaxSize(context));
|
||||
} catch (IOException ioe) {
|
||||
Log.w(TAG, "Failed to determine if media's constraints are satisfied.", ioe);
|
||||
return false;
|
||||
@@ -83,6 +94,9 @@ public abstract class MediaConstraints {
|
||||
|
||||
public boolean isSatisfied(@NonNull Context context, @NonNull Uri uri, @NonNull String contentType, long size) {
|
||||
try {
|
||||
if (size > getMaxAttachmentSize()) {
|
||||
return false;
|
||||
}
|
||||
return (MediaUtil.isGif(contentType) && size <= getGifMaxSize(context) && isWithinBounds(context, uri)) ||
|
||||
(MediaUtil.isImageType(contentType) && size <= getImageMaxSize(context) && isWithinBounds(context, uri)) ||
|
||||
(MediaUtil.isAudioType(contentType) && size <= getAudioMaxSize(context)) ||
|
||||
|
||||
@@ -43,27 +43,27 @@ final class MmsMediaConstraints extends MediaConstraints {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGifMaxSize(Context context) {
|
||||
public long getGifMaxSize(Context context) {
|
||||
return getMaxMessageSize(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVideoMaxSize(Context context) {
|
||||
public long getVideoMaxSize(Context context) {
|
||||
return getMaxMessageSize(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUncompressedVideoMaxSize(Context context) {
|
||||
public long getUncompressedVideoMaxSize(Context context) {
|
||||
return Math.max(getVideoMaxSize(context), 15 * 1024 * 1024);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAudioMaxSize(Context context) {
|
||||
public long getAudioMaxSize(Context context) {
|
||||
return getMaxMessageSize(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDocumentMaxSize(Context context) {
|
||||
public long getDocumentMaxSize(Context context) {
|
||||
return getMaxMessageSize(context);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public class PushMediaConstraints extends MediaConstraints {
|
||||
|
||||
@Override
|
||||
public int getImageMaxSize(Context context) {
|
||||
return currentConfig.maxImageFileSize;
|
||||
return (int) Math.min(currentConfig.maxImageFileSize, getMaxAttachmentSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,35 +49,35 @@ public class PushMediaConstraints extends MediaConstraints {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGifMaxSize(Context context) {
|
||||
return 25 * MB;
|
||||
public long getGifMaxSize(Context context) {
|
||||
return Math.min(25 * MB, getMaxAttachmentSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVideoMaxSize(Context context) {
|
||||
return 100 * MB;
|
||||
public long getVideoMaxSize(Context context) {
|
||||
return getMaxAttachmentSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUncompressedVideoMaxSize(Context context) {
|
||||
public long getUncompressedVideoMaxSize(Context context) {
|
||||
return isVideoTranscodeAvailable() ? 500 * MB
|
||||
: getVideoMaxSize(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCompressedVideoMaxSize(Context context) {
|
||||
public long getCompressedVideoMaxSize(Context context) {
|
||||
return Util.isLowMemory(context) ? 30 * MB
|
||||
: 50 * MB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAudioMaxSize(Context context) {
|
||||
return 100 * MB;
|
||||
public long getAudioMaxSize(Context context) {
|
||||
return getMaxAttachmentSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDocumentMaxSize(Context context) {
|
||||
return 100 * MB;
|
||||
public long getDocumentMaxSize(Context context) {
|
||||
return getMaxAttachmentSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user