/*
 * Copyright (c) 2017 Hazuki
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package jp.hazuki.yuzubrowser.download;

import android.util.Base64;
import android.webkit.MimeTypeMap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Pattern;

import jp.hazuki.yuzubrowser.settings.data.AppData;
import jp.hazuki.yuzubrowser.utils.FileUtils;

class DownloadUtils {

    static File saveBase64Image(String data) {
        Base64Image image = decodeBase64Image(data);
        if (image.isValid())
            return saveBase64Image(image, getFile(image));
        return null;
    }

    static File saveBase64Image(Base64Image imageData, File file) {
        if (imageData.isValid()) {
            byte[] image = Base64.decode(imageData.getData(), Base64.DEFAULT);

            if (file == null)
                file = getFile(imageData);

            if (file == null)
                return null;

            if (file.getParentFile() != null) {
                file.getParentFile().mkdirs();
            }

            try (OutputStream outputStream = new FileOutputStream(file)) {

                outputStream.write(image);
                outputStream.flush();

                return file;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    static File getFile(Base64Image image) {
        if (!image.isValid()) return null;

        String mimeType = image.getHeader().split(";")[0].substring(5);
        String name = System.currentTimeMillis() + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
        return FileUtils.createUniqueFile(AppData.download_folder.get(), name);
    }

    static Base64Image decodeBase64Image(String url) {
        return new Base64Image(url);
    }

    static class Base64Image {
        private final String[] data;

        private Base64Image(String url) {
            data = url.split(Pattern.quote(","));
        }

        boolean isValid() {
            return data.length >= 2;
        }

        private String getHeader() {
            return data[0];
        }

        private String getData() {
            return data[1];
        }
    }
}

@@ -1281,24 +1281,46 @@
 sValid()) %7B%0A
+            try %7B%0A    
             
@@ -1391,32 +1391,36 @@
 );%0A%0A            
+ 
+   
 if (file == null
@@ -1413,32 +1413,36 @@
  (file == null)%0A
+    
                 
@@ -1465,24 +1465,28 @@
 mageData);%0A%0A
+    
             
@@ -1511,32 +1511,36 @@
                 
+    
 return null;%0A%0A  
@@ -1529,32 +1529,36 @@
   return null;%0A%0A
+    
             if (
@@ -1601,24 +1601,28 @@
             
+   
+ 
 file.getPare
@@ -1644,35 +1644,43 @@
 ();%0A            
-%7D%0A%0A
+    %7D%0A%0A    
             try 
@@ -1747,32 +1747,36 @@
                 
+   
+ 
 outputStream.wri
@@ -1782,24 +1782,28 @@
 ite(image);%0A
+    
             
@@ -1841,24 +1841,28 @@
             
+    
 return file;
@@ -1874,16 +1874,20 @@
         
+  
+  
 %7D catch 
@@ -1900,24 +1900,134 @@
 eption e) %7B%0A
+                    e.printStackTrace();%0A                %7D%0A            %7D catch (IllegalArgumentException e) %7B%0A
             

fix bad image