package com.mantacode.reactnativeparseimageupload;

import android.content.Context;
import android.net.Uri;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import java.net.URL;
import java.io.*;

public class FileHelpers {

    public static byte[] getJpgImageFromUri(Context context, String uri, int maxWidth, int maxHeight) throws Exception {
        byte[] result = null;
        InputStream inputStream = null;
        try {
            Uri contentUri = Uri.parse(uri);
            inputStream = context.getContentResolver().openInputStream(contentUri);
            result = getResizedImageFromStream(inputStream, contentUri.getPath(), maxWidth, maxHeight);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }

        return result;
    }

    public static byte[] getJpgImageFromRemoteUrl(String url, int maxWidth, int maxHeight) throws Exception {
        byte[] result = null;
        InputStream inputStream = null;
        try {
            URL fileUrl = new URL(url);
            inputStream = fileUrl.openStream();
            result = getResizedImageFromStream(inputStream, url, maxWidth, maxHeight);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }

        return result;
    }

    private static byte[] getResizedImageFromStream(InputStream inputStream, String path, int maxWidth, int maxHeight) {
        byte[] result = null;
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        Bitmap resizedImage;
        if (maxWidth > 0 || maxHeight > 0) {
            resizedImage = resizeImage(bitmap, maxWidth, maxHeight);
        } else {
            resizedImage = bitmap;
        }
        resizedImage = rotateImageIfRequired(resizedImage, path);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        resizedImage.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
        return outStream.toByteArray();
    }

    private static Bitmap resizeImage(Bitmap image, int maxWidth, int maxHeight) {
        if (image == null) {
            return null; // Can't load the image from the given path.
        }

        float width = image.getWidth();
        float height = image.getHeight();

        // do not upscale
        float widthRatio = maxWidth > 0 && maxWidth < width ? (float)maxWidth / width : 1.0f;
        float heightRatio = maxHeight > 0 && maxHeight < height ? (float)maxHeight / height : 1.0f;

        float ratio = Math.min(widthRatio, heightRatio);

        int finalWidth = (int) (width * ratio);
        int finalHeight = (int) (height * ratio);

        return Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
    }

    private static Bitmap rotateImageIfRequired(Bitmap img, String path) throws IOException {
        ExifInterface ei = new ExifInterface(path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return rotateImage(img, 90);
            case ExifInterface.ORIENTATION_ROTATE_180:
                return rotateImage(img, 180);
            case ExifInterface.ORIENTATION_ROTATE_270:
                return rotateImage(img, 270);
            default:
                return img;
        }
    }

    private static Bitmap rotateImage(Bitmap img, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
        img.recycle();
        return rotatedImg;
    }
}

@@ -239,16 +239,84 @@
 terface;
+%0Aimport android.provider.MediaStore;%0Aimport android.database.Cursor;
 %0A%0Aimport
@@ -707,24 +707,166 @@
 ontentUri);%0A
+            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);%0A            bitmap = rotateImageIfRequired(context, bitmap, contentUri);%0A
             
@@ -900,27 +900,22 @@
 mStream(
-inputStream
+bitmap
 , conten
@@ -1377,32 +1377,101 @@
 l.openStream();%0A
+            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);%0A
             resu
@@ -1501,27 +1501,22 @@
 mStream(
-inputStream
+bitmap
 , url, m
@@ -1739,39 +1739,29 @@
 mStream(
-InputStream inputStream
+Bitmap bitmap
 , String
@@ -1787,32 +1787,51 @@
 , int maxHeight)
+ throws IOException
  %7B%0A        byte%5B
@@ -1851,73 +1851,8 @@
 ll;%0A
-        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);%0A
     
@@ -2056,74 +2056,8 @@
   %7D%0A
-        resizedImage = rotateImageIfRequired(resizedImage, path);%0A
     
@@ -3033,16 +3033,33 @@
 equired(
+Context context, 
 Bitmap i
@@ -3062,27 +3062,23 @@
 ap img, 
-String path
+Uri uri
 ) throws
@@ -3104,302 +3104,423 @@
     
-ExifInterface ei = new ExifInterface(path);%0A        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);%0A
+int rotationAngle = getOrientation(context, uri);%0A        if (rotationAngle %3E 0) %7B%0A            return rotateImage(img, rotationAngle);%0A        %7D%0A%0A        return img;
 %0A    
+%7D%0A%0A
     
-switch (orientation) %7B%0A            case ExifInterface.ORIENTATION_ROTATE_90:%0A                return rotateImage(img, 90
+private static int getOrientation(Context context, Uri photoUri) %7B%0A        Cursor cursor = context.getContentResolver().query(photoUri,%0A                new String%5B%5D%7BMediaStore.Images.ImageColumns.ORIENTATION%7D, null, null, null
 );%0A
+%0A
         
     
@@ -3519,155 +3519,88 @@
     
-    case ExifInterface.ORIENTATION_ROTATE_180:%0A                return rotateImage(img, 180);%0A            case ExifInterface.ORIENTATION_ROTATE_270:
+if (cursor.getCount() != 1) %7B%0A            cursor.close();%0A            return -1;
 %0A   
@@ -3600,32 +3600,35 @@
 -1;%0A        
+%7D%0A%0A
         
 return rotat
@@ -3619,67 +3619,121 @@
     
-return rotateImage(img, 270);%0A            default:%0A        
+cursor.moveToFirst();%0A        int orientation = cursor.getInt(0);%0A        cursor.close();%0A        cursor = null;%0A
     
@@ -3747,22 +3747,20 @@
 urn 
-img;%0A        %7D
+orientation;
 %0A   

Use Android's mediastore metadata to get proper image orientation instead of reading EXIF