<commit_before>package com.commontime.plugin;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.telecom.Call;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Base64;
import android.util.Log;
import android.webkit.MimeTypeMap;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class Thumbnail extends CordovaPlugin {

    private static final int MAX_IMAGE_DECODING_SIZE = 2000;
    private static final String LOG_TAG = "Thumbnail";

    private JSONArray savedArgs;
    private int savedMaxWidth;
    private int savedMaxHeight;
    private int savedQuality;
    private Map<String, CallbackContext> callbackContextList;
    private boolean wasDecrypted;
    private static final String DECRYPT_FILE_MSG_ID = "DECRYPT_FILE";
    private static final String DECRYPT_FILE_CALLBACK_MSG_ID = "DECRYPTION_RESPONSE";
    private static final String ENCRYPT_DECRYPT_FILE_ORIGINAL_URI_KEY = "originalUri";
    private static final String DECRYPT_FILE_URI_KEY = "uri";
    private static final String DECRYPT_FILE_CALLBACK_KEY = "cb";
    private static final String DECRYPT_TARGET_KEY = "target";
    private static final String ENCRYPTED_FILE_EXTENSION = ".encrypted";

    @Override
    public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException
    {
        if (callbackContextList == null)
            callbackContextList = new ArrayMap<String, CallbackContext>();

        //this.callbackContext = callbackContext;
        callbackContextList.put(args.getString(0), callbackContext);

        cordova.getThreadPool().execute(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    getThumbnail(args.getString(0), args.getInt(1), args.getInt(2), args.getInt(3), args, callbackContext);
                }
                catch (Exception e)
                {
                    callbackContext.error(e.getMessage());
                }
            }
        });

        return true;
    }

    @Override
    public Object onMessage(String id, Object data) {
        if (id.equals(DECRYPT_FILE_CALLBACK_MSG_ID)) {
            if (data != null) {
                try {
                    JSONObject jsonData = (JSONObject) data;
                    CallbackContext callbackContext = callbackContextList.get(((JSONObject) data).getString(ENCRYPT_DECRYPT_FILE_ORIGINAL_URI_KEY));
                    callbackContextList.remove(((JSONObject) data).getString(ENCRYPT_DECRYPT_FILE_ORIGINAL_URI_KEY));
                    getThumbnail(jsonData.getString(DECRYPT_FILE_URI_KEY), savedMaxWidth, savedMaxHeight, savedQuality, savedArgs, callbackContext);
                } catch (JSONException e) {
                    e.printStackTrace();
                    //callbackContext.error("Failed to make thumbnail");
                }
            } else {
                //callbackContext.error("Failed to make thumbnail");
            }
        }
        return super.onMessage(id, data);
    }

    private void getThumbnail(String path, int maxWidth, int maxHeight, int quality, JSONArray args, CallbackContext callbackContext) throws JSONException
    {
        if (path.contains(ENCRYPTED_FILE_EXTENSION))
        {
            if (webView.getPluginManager().getPlugin("FileEncryption") == null)
            {
                callbackContext.error("Unable to decrypt to make a thumbnail for the file at '" + path + "'. File encryption plugin not present.");
            }

            savedArgs = args;
            savedMaxWidth = maxWidth;
            savedMaxHeight = maxHeight;
            savedQuality = quality;
            wasDecrypted = true;
            JSONObject data = new JSONObject();
            data.put(DECRYPT_FILE_URI_KEY, path);
            data.put(DECRYPT_TARGET_KEY, cordova.getActivity().getCacheDir().getAbsolutePath());
            data.put(DECRYPT_FILE_CALLBACK_KEY, DECRYPT_FILE_CALLBACK_MSG_ID);
            webView.getPluginManager().postMessage(DECRYPT_FILE_MSG_ID, data);
            return;
        }

        if(path.contains("cdvfile://"))
        {
            CordovaResourceApi resourceApi = webView.getResourceApi();
            Uri fileURL = resourceApi.remapUri(Uri.parse(path));
            path = fileURL.getPath();
        }
        else
        {
            path = path.replace("file://", "");
            path = path.replace("file:", "");
        }

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        Bitmap original = null;

        if(path.contains("http://"))
        {
            try
            {
                path = path.replace(" ", "%20");<commit_msg>Now using a GUID to identify the correct callbackContext to use when the file have been decrypted to avoid potential issues with the same file name going through the process.<commit_after>package com.commontime.plugin;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.telecom.Call;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Base64;
import android.util.Log;
import android.webkit.MimeTypeMap;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

public class Thumbnail extends CordovaPlugin {

    private static final int MAX_IMAGE_DECODING_SIZE = 2000;
    private static final String LOG_TAG = "Thumbnail";

    private JSONArray savedArgs;
    private int savedMaxWidth;
    private int savedMaxHeight;
    private int savedQuality;
    private Map<String, CallbackContext> callbackContextList;
    private boolean wasDecrypted;
    private static final String DECRYPT_FILE_MSG_ID = "DECRYPT_FILE";
    private static final String DECRYPT_FILE_CALLBACK_MSG_ID = "DECRYPTION_RESPONSE";
    private static final String ENCRYPT_DECRYPT_FILE_ORIGINAL_URI_KEY = "originalUri";
    private static final String DECRYPT_FILE_URI_KEY = "uri";
    private static final String DECRYPT_FILE_CALLBACK_KEY = "cb";
    private static final String DECRYPT_TARGET_KEY = "target";
    private static final String ENCRYPT_DECRYPT_REQUEST_ID_KEY = "encryptDecryptRequestId";
    private static final String ENCRYPTED_FILE_EXTENSION = ".encrypted";

    @Override
    public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException
    {
        if (callbackContextList == null)
            callbackContextList = new ArrayMap<String, CallbackContext>();

        //this.callbackContext = callbackContext;

        cordova.getThreadPool().execute(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    getThumbnail(args.getString(0), args.getInt(1), args.getInt(2), args.getInt(3), args, callbackContext);
                }
                catch (Exception e)
                {
                    callbackContext.error(e.getMessage());
                }
            }
        });

        return true;
    }

    @Override
    public Object onMessage(String id, Object data) {
        if (id.equals(DECRYPT_FILE_CALLBACK_MSG_ID)) {
            if (data != null) {
                try {
                    JSONObject jsonData = (JSONObject) data;
                    CallbackContext callbackContext = callbackContextList.get(((JSONObject) data).getString(ENCRYPT_DECRYPT_REQUEST_ID_KEY));
                    callbackContextList.remove(((JSONObject) data).getString(ENCRYPT_DECRYPT_REQUEST_ID_KEY));
                    getThumbnail(jsonData.getString(DECRYPT_FILE_URI_KEY), savedMaxWidth, savedMaxHeight, savedQuality, savedArgs, callbackContext);
                } catch (JSONException e) {
                    e.printStackTrace();
                    //callbackContext.error("Failed to make thumbnail");
                }
            } else {
                //callbackContext.error("Failed to make thumbnail");
            }
        }
        return super.onMessage(id, data);
    }

    private void getThumbnail(String path, int maxWidth, int maxHeight, int quality, JSONArray args, CallbackContext callbackContext) throws JSONException
    {
        if (path.contains(ENCRYPTED_FILE_EXTENSION))
        {
            if (webView.getPluginManager().getPlugin("FileEncryption") == null)
            {
                callbackContext.error("Unable to decrypt to make a thumbnail for the file at '" + path + "'. File encryption plugin not present.");
            }

            String decryptRequestId = UUID.randomUUID().toString();

            callbackContextList.put(decryptRequestId, callbackContext);

            savedArgs = args;
            savedMaxWidth = maxWidth;
            savedMaxHeight = maxHeight;
            savedQuality = quality;
            wasDecrypted = true;
            JSONObject data = new JSONObject();
            data.put(ENCRYPT_DECRYPT_REQUEST_ID_KEY, decryptRequestId);
            data.put(DECRYPT_FILE_URI_KEY, path);
            data.put(DECRYPT_TARGET_KEY, cordova.getActivity().getCacheDir().getAbsolutePath());
            data.put(DECRYPT_FILE_CALLBACK_KEY, DECRYPT_FILE_CALLBACK_MSG_ID);
            webView.getPluginManager().postMessage(DECRYPT_FILE_MSG_ID, data);
            return;
        }

        if(path.contains("cdvfile://"))
        {
            CordovaResourceApi resourceApi = webView.getResourceApi();
            Uri fileURL = resourceApi.remapUri(Uri.parse(path));
            path = fileURL.getPath();
        }
        else
        {
            path = path.replace("file://", "");
            path = path.replace("file:", "");
        }

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        Bitmap original = null;

        if(path.contains("http://"))
        {
            try
            {
                path = path.replace(" ", "%20");
{'fraction_non_alphanumeric': 0.08999723934848625, 'fraction_numerical': 0.002300542928131039, 'mean_word_length': 2.5308641975308643, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3681677', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2830, 'n_words': 655}