package io.github.ppalazon.bildstod;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.FileUpload;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;

/**
 * User: ppalazon
 * Date: 10/21/16
 * Time: 2:01 PM
 */
public class BildstodVerticle extends AbstractVerticle
{
    @Override
    public void start() throws Exception
    {
        Config conf = ConfigFactory.load();

        HttpServer server = vertx.createHttpServer();
        EventBus eventBus = vertx.eventBus();

        Router router = Router.router(vertx);

        router.getWithRegex("\\/(\\w{8}?).(png|jpg|gif)").handler(routingContext ->
        {
            String code = routingContext.request().getParam("param0");
            String extension = routingContext.request().getParam("param1");
            String filename = code+"."+extension;
            if(conf.getString("storage.type").contentEquals("local")) {
                File storagePath = new File(conf.getString("storage.local.path"));
                if(!storagePath.exists())
                {
                    throw new RuntimeException("Local storage "+conf.getString("storage.local.path")+" doesn't exists");
                }
                File imageFile = new File(storagePath, filename);
                if(!imageFile.exists())
                {
                    throw new RuntimeException("File "+imageFile+" doesn't exists");
                }

                routingContext.response()
                        .sendFile(imageFile.getPath());
            }

        });

        router.put("/upload").handler(BodyHandler.create().setMergeFormAttributes(true));
        router.put("/upload").blockingHandler(routingContext ->
        {
            Set<FileUpload> fileUploadSet = routingContext.fileUploads();
            for (FileUpload fileUpload : fileUploadSet) {
                // To get the uploaded file do
                if(fileUpload.name().contentEquals("file")) {
                    String code = RandomStringUtils.random(8, true, true);
                    String extension = ".png";
                    try {
                        extension = URLDecoder.decode(fileUpload.fileName(), "UTF-8").split("\\.")[1].toLowerCase();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    String filename = code+"."+extension;

                    if(conf.getString("storage.type").contentEquals("local"))
                    {
                        File storagePath = new File(conf.getString("storage.local.path"));
                        if(!storagePath.exists())
                        {
                            storagePath.mkdir();
                        }
                        File imageFile = new File(storagePath, filename);
                        try {
                            FileUtils.copyFile(new File(fileUpload.uploadedFileName()), imageFile);
                        } catch (IOException e) {
                            throw new RuntimeException("Cannot copy files "+e.getMessage());
                        }

                        routingContext.response()
                                .setStatusCode(201)
                                .putHeader("Location", "/"+code);
                    }

                }

                // Use the Event Bus to dispatch the file now
                // Since Event Bus does not support POJOs by default so we need to create a MessageCodec implementation
                // and provide methods for encode and decode the bytes
            }
        });

        server.requestHandler(router::accept).listen(8080);
    }
}

@@ -1213,37 +1213,41 @@
  filename = code
-+%22.%22+
+ + %22.%22 + 
 extension;%0A     
@@ -1247,32 +1247,33 @@
 ;%0A            if
+ 
 (conf.getString(
@@ -1407,32 +1407,33 @@
               if
+ 
 (!storagePath.ex
@@ -1439,32 +1439,16 @@
 xists())
-%0A               
  %7B%0A     
@@ -1505,17 +1505,19 @@
 torage %22
-+
+ + 
 conf.get
@@ -1544,17 +1544,19 @@
 l.path%22)
-+
+ + 
 %22 doesn'
@@ -1669,16 +1669,17 @@
       if
+ 
 (!imageF
@@ -1691,32 +1691,16 @@
 xists())
-%0A               
  %7B%0A     
@@ -1748,17 +1748,19 @@
 (%22File %22
-+
+ + 
 imageFil
@@ -1760,17 +1760,19 @@
 mageFile
-+
+ + 
 %22 doesn'
@@ -2289,16 +2289,17 @@
       if
+ 
 (fileUpl
@@ -2775,13 +2775,17 @@
 code
-+%22.%22+
+ + %22.%22 + 
 exte
@@ -2814,16 +2814,17 @@
       if
+ 
 (conf.ge
@@ -2870,36 +2870,16 @@
 local%22))
-%0A                   
  %7B%0A     
@@ -2990,16 +2990,17 @@
       if
+ 
 (!storag
@@ -3018,32 +3018,8 @@
 s())
-%0A                       
  %7B%0A 
@@ -3421,17 +3421,19 @@
  files %22
-+
+ + 
 e.getMes
@@ -3628,17 +3628,19 @@
 on%22, %22/%22
-+
+ + 
 code);%0A 

Format code