EOD
This commit is contained in:
		
							parent
							
								
									bd797a5619
								
							
						
					
					
						commit
						4bdc25964b
					
				@ -13,7 +13,7 @@ import java.io.*;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
public class VCard {
 | 
			
		||||
public class Card {
 | 
			
		||||
    private static final Color WHITE = new Color(255, 255, 255);
 | 
			
		||||
    private static final Color GREEN = new Color(101, 142, 61);
 | 
			
		||||
 | 
			
		||||
@ -36,7 +36,7 @@ public class VCard {
 | 
			
		||||
    private final BufferedImage background;
 | 
			
		||||
    private final int size;
 | 
			
		||||
 | 
			
		||||
    public VCard(JsonObject json) {
 | 
			
		||||
    public Card(JsonObject json) {
 | 
			
		||||
        this.data[0] = json.get("name").getAsString(); //name
 | 
			
		||||
        this.data[1] = json.get("title").getAsString(); //title
 | 
			
		||||
        this.data[2] = json.get("email").getAsString(); //email
 | 
			
		||||
@ -62,7 +62,7 @@ public class VCard {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public VCard(String name, String title, String email, String locationId, String extension, String directNumber, String cellNumber, int size) {
 | 
			
		||||
    public Card(String name, String title, String email, String locationId, String extension, String directNumber, String cellNumber, int size) {
 | 
			
		||||
        this.data[0] = name; //name
 | 
			
		||||
        this.data[1] = title; //title
 | 
			
		||||
        this.data[2] = email; //email
 | 
			
		||||
@ -225,6 +225,9 @@ public class VCard {
 | 
			
		||||
        IIOMetadata metadata = reader.getImageMetadata(0);
 | 
			
		||||
        IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree("javax_imageio_png_1.0");
 | 
			
		||||
        IIOMetadataNode text = (IIOMetadataNode) root.getElementsByTagName("tEXt").item(0);
 | 
			
		||||
        if (text == null) {
 | 
			
		||||
            return "$null";
 | 
			
		||||
        }
 | 
			
		||||
        int numTextEntries = text.getLength();
 | 
			
		||||
 | 
			
		||||
        Map<String, String> keyValueMap = new HashMap<>();
 | 
			
		||||
							
								
								
									
										45
									
								
								src/main/java/tech/nevets/vcardgen/EditRoute.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/main/java/tech/nevets/vcardgen/EditRoute.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
			
		||||
package tech.nevets.vcardgen;
 | 
			
		||||
 | 
			
		||||
import spark.Request;
 | 
			
		||||
import spark.Response;
 | 
			
		||||
import spark.Route;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.MultipartConfigElement;
 | 
			
		||||
import javax.servlet.http.Part;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
public class EditRoute implements Route {
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object handle(Request req, Response res) {
 | 
			
		||||
        req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
 | 
			
		||||
        Part cardFile;
 | 
			
		||||
        try {
 | 
			
		||||
            cardFile = req.raw().getPart("card");
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            res.status(500);
 | 
			
		||||
            return "Error processing file: " + e.getMessage();
 | 
			
		||||
        }
 | 
			
		||||
        if (cardFile.getSize() > 1000000) {
 | 
			
		||||
            res.status(413);
 | 
			
		||||
            return "File too large";
 | 
			
		||||
        }
 | 
			
		||||
        if (!cardFile.getContentType().equalsIgnoreCase("image/png")) {
 | 
			
		||||
            res.status(415);
 | 
			
		||||
            return "Content is not of type image/png";
 | 
			
		||||
        }
 | 
			
		||||
        String cardData;
 | 
			
		||||
        try {
 | 
			
		||||
            cardData = Card.getDataFromVCard(cardFile.getInputStream());
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            res.status(500);
 | 
			
		||||
            return "Error retrieving data from card" + e.getMessage();
 | 
			
		||||
        }
 | 
			
		||||
        if (cardData.equals("$null")) {
 | 
			
		||||
            res.status(422);
 | 
			
		||||
            return "Card does not contain required metadata";
 | 
			
		||||
        }
 | 
			
		||||
        res.type("application/json");
 | 
			
		||||
        res.status(200);
 | 
			
		||||
        return cardData;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								src/main/java/tech/nevets/vcardgen/GenerateRoute.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/main/java/tech/nevets/vcardgen/GenerateRoute.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
package tech.nevets.vcardgen;
 | 
			
		||||
 | 
			
		||||
import com.google.gson.Gson;
 | 
			
		||||
import com.google.gson.JsonObject;
 | 
			
		||||
import com.google.gson.JsonSyntaxException;
 | 
			
		||||
import spark.Request;
 | 
			
		||||
import spark.Response;
 | 
			
		||||
import spark.Route;
 | 
			
		||||
 | 
			
		||||
public class GenerateRoute implements Route {
 | 
			
		||||
    private Card card;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object handle(Request req, Response res) {
 | 
			
		||||
        JsonObject data;
 | 
			
		||||
        try {
 | 
			
		||||
            data = new Gson().fromJson(req.body(), JsonObject.class);
 | 
			
		||||
        } catch (JsonSyntaxException e) {
 | 
			
		||||
            res.status(422);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        res.type("image/png");
 | 
			
		||||
        res.header("Access-Control-Expose-Headers", "Id");
 | 
			
		||||
        card = new Card(data);
 | 
			
		||||
        return new Card(data).toByteArray();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -3,7 +3,6 @@ package tech.nevets.vcardgen;
 | 
			
		||||
import com.google.gson.Gson;
 | 
			
		||||
import com.google.gson.JsonObject;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.MultipartConfigElement;
 | 
			
		||||
import java.io.*;
 | 
			
		||||
 | 
			
		||||
import static spark.Spark.*;
 | 
			
		||||
@ -39,19 +38,9 @@ public class Main {
 | 
			
		||||
                return new FileInputStream("locations.json");
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            post("/generate", (req, res) -> {
 | 
			
		||||
                res.type("image/png");
 | 
			
		||||
                res.header("Access-Control-Expose-Headers", "Id");
 | 
			
		||||
                JsonObject data = new Gson().fromJson(req.body(), JsonObject.class);
 | 
			
		||||
                return new VCard(data).toByteArray();
 | 
			
		||||
            });
 | 
			
		||||
            post("/generate", new GenerateRoute());
 | 
			
		||||
 | 
			
		||||
            post("/edit", (req, res) -> {
 | 
			
		||||
                req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
 | 
			
		||||
                res.status(200);
 | 
			
		||||
                res.type("application/json");
 | 
			
		||||
                return VCard.getDataFromVCard(req.raw().getPart("card").getInputStream());
 | 
			
		||||
            });
 | 
			
		||||
            post("/edit", new EditRoute());
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										6
									
								
								src/main/java/tech/nevets/vcardgen/TestMain.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								src/main/java/tech/nevets/vcardgen/TestMain.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
			
		||||
package tech.nevets.vcardgen;
 | 
			
		||||
 | 
			
		||||
public class TestMain {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user