Commit 0089c1ac authored by akwizgran's avatar akwizgran

Merge branch '1468-restrict-image-size' into 'master'

Fix first issues related to image size

See merge request !1018
parents a37b6d81 1d09a670
Pipeline #2913 passed with stage
in 10 minutes and 43 seconds
...@@ -6,6 +6,8 @@ import android.support.annotation.Nullable; ...@@ -6,6 +6,8 @@ import android.support.annotation.Nullable;
import android.support.media.ExifInterface; import android.support.media.ExifInterface;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import com.bumptech.glide.util.MarkEnforcingInputStream;
import org.briarproject.bramble.api.Pair; import org.briarproject.bramble.api.Pair;
import org.briarproject.bramble.api.db.DatabaseExecutor; import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
...@@ -44,6 +46,7 @@ class AttachmentController { ...@@ -44,6 +46,7 @@ class AttachmentController {
private static final Logger LOG = private static final Logger LOG =
getLogger(AttachmentController.class.getName()); getLogger(AttachmentController.class.getName());
private static final int READ_LIMIT = 1024 * 8192;
private final MessagingManager messagingManager; private final MessagingManager messagingManager;
private final int defaultSize; private final int defaultSize;
...@@ -128,8 +131,9 @@ class AttachmentController { ...@@ -128,8 +131,9 @@ class AttachmentController {
} }
Size size = new Size(); Size size = new Size();
InputStream is = new BufferedInputStream(a.getStream()); InputStream is = new MarkEnforcingInputStream(
is.mark(Integer.MAX_VALUE); new BufferedInputStream(a.getStream()));
is.mark(READ_LIMIT);
try { try {
// use exif to get size // use exif to get size
if (h.getContentType().equals("image/jpeg")) { if (h.getContentType().equals("image/jpeg")) {
...@@ -142,6 +146,8 @@ class AttachmentController { ...@@ -142,6 +146,8 @@ class AttachmentController {
// use BitmapFactory to get size // use BitmapFactory to get size
if (size.error) { if (size.error) {
is.reset(); is.reset();
// need to mark again to re-add read limit
is.mark(READ_LIMIT);
size = getSizeFromBitmap(is); size = getSizeFromBitmap(is);
} }
} catch (IOException e) { } catch (IOException e) {
...@@ -158,12 +164,11 @@ class AttachmentController { ...@@ -158,12 +164,11 @@ class AttachmentController {
} }
// get file extension // get file extension
String extension = getExtensionFromMimeType(size.mimeType); String extension = getExtensionFromMimeType(size.mimeType);
if (extension == null) { boolean hasError = extension == null || size.error;
return new AttachmentItem(messageId, 0, 0, "", "", 0, 0, true); if (extension == null) extension = "";
}
return new AttachmentItem(messageId, size.width, size.height, return new AttachmentItem(messageId, size.width, size.height,
size.mimeType, extension, thumbnailSize.width, size.mimeType, extension, thumbnailSize.width,
thumbnailSize.height, size.error); thumbnailSize.height, hasError);
} }
@Nullable @Nullable
......
...@@ -77,6 +77,7 @@ public class ImageFragment extends Fragment { ...@@ -77,6 +77,7 @@ public class ImageFragment extends Fragment {
viewModelFactory).get(ImageViewModel.class); viewModelFactory).get(ImageViewModel.class);
photoView = v.findViewById(R.id.photoView); photoView = v.findViewById(R.id.photoView);
photoView.setScaleLevels(1, 2, 4);
photoView.setOnClickListener(view -> viewModel.clickImage()); photoView.setOnClickListener(view -> viewModel.clickImage());
// Request Listener // Request Listener
...@@ -113,9 +114,10 @@ public class ImageFragment extends Fragment { ...@@ -113,9 +114,10 @@ public class ImageFragment extends Fragment {
// Load Image // Load Image
GlideApp.with(this) GlideApp.with(this)
.load(attachment) .load(attachment)
// TODO allow if size < maxTextureSize ?
// .override(SIZE_ORIGINAL)
.diskCacheStrategy(NONE) .diskCacheStrategy(NONE)
.error(R.drawable.ic_image_broken) .error(R.drawable.ic_image_broken)
.dontTransform()
.addListener(listener) .addListener(listener)
.into(photoView); .into(photoView);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment