Skip to content

Commit

Permalink
Merge branch 'spark-migration'
Browse files Browse the repository at this point in the history
  • Loading branch information
freyacodes committed Dec 2, 2016
2 parents 582ebd6 + 3001c50 commit b63599e
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 228 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/target/
/data/
/dependency-reduced-pom.xml
*.moe.sublime-workspace
/.idea/
*.iml
/logs/
/data/
19 changes: 6 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
<artifactId>logback-core</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -38,16 +33,14 @@
<version>20160810</version>
</dependency>
<dependency>
<!-- Used by Spring Boot -->
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<!-- Used by Spring Boot -->
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.14</version>
</dependency>
</dependencies>
<repositories>
Expand Down
8 changes: 6 additions & 2 deletions public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
<html manifest="/main.appcache">
<head>
<link rel="stylesheet" href="/main.css">
<link rel="icon"
type="image/png"
href="/favicon.ico?v=2">
<title>Fred.moe~</title>
</head>
<body>
<center>
<div style="text-align: center;">
<div class="title">
Error 404
</div>
<img src="/img/404.png" class="logo">
<div class="title">
Not found
</div>
</center>
</div>
</body>
</html>
8 changes: 6 additions & 2 deletions public/500.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
<html manifest="/main.appcache">
<head>
<link rel="stylesheet" href="/main.css">
<link rel="icon"
type="image/png"
href="/favicon.ico?v=2">
<title>Fred.moe~</title>
</head>
<body>
<center>
<div style="text-align: center;">
<div class="title">
Error 500
</div>
<img src="/img/500.png" class="logo">
<div class="title">
Internal server error
</div>
</center>
</div>
</body>
</html>
Binary file added public/favicon.ico
Binary file not shown.
8 changes: 6 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
<html manifest="/main.appcache">
<head>
<link rel="stylesheet" href="main.css">
<link rel="icon"
type="image/png"
href="/favicon.ico?v=2">
<title>Fred.moe~</title>
</head>
<body>
<center>
<div style="text-align: center;">
<img src="img/tenshi.png" class="logo">
<div class="title">
Fred.moe~
</div>
</center>
</div>
</body>
</html>
</hml>
1 change: 1 addition & 0 deletions public/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

body {
background-image: url(img/concrete_seamless.png);
background-color: #F5F5F5;
}

.logo {
Expand Down
37 changes: 0 additions & 37 deletions src/main/java/com/frederikam/fred/moe/ErrorHandler.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.frederikam.fred.moe;

public class FileTooBigException extends RuntimeException {
}
156 changes: 14 additions & 142 deletions src/main/java/com/frederikam/fred/moe/FredDotMoe.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
package com.frederikam.fred.moe;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.io.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.json.JSONArray;

import org.apache.tika.exception.TikaException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerMapping;
import spark.Spark;
import spark.route.RouteOverview;

@Configuration
@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class})
@Controller
@ComponentScan
public class FredDotMoe {

private static final Logger log = LoggerFactory.getLogger(FredDotMoe.class);


private static String baseUrl;
protected static String baseUrl;

public static final long MAX_UPLOAD_SIZE = 128 * 1000000;
private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("(\\.\\w+)$");

public static void main(String[] args) throws FileNotFoundException {
public static void main(String[] args) throws IOException, TikaException {
InputStream is = new FileInputStream(new File("./config.json"));
Scanner scanner = new Scanner(is);
JSONObject config = new JSONObject(scanner.useDelimiter("\\A").next());
Expand All @@ -56,114 +29,13 @@ public static void main(String[] args) throws FileNotFoundException {
scanner.close();
ResourceManager.dataDir.mkdirs();

ApplicationContext ctx = SpringApplication.run(FredDotMoe.class, args);
}

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
private static void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
String path = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);

log.info("GET "+path);

if (path.equals("/")) {
path = "/index.html";
}

File f = ResourceManager.getResource(path.substring(1));
boolean isInPublic = f.getAbsolutePath().startsWith(ResourceManager.PUBLIC_DIR.getAbsolutePath());
//Verify that the file requested is in a public directory
if (!f.getParentFile().getAbsolutePath().equals(ResourceManager.dataDir.getAbsolutePath())
&& !isInPublic) {
response.sendError(400);
return;
}

if (!f.exists()) {
response.sendError(404);
return;
}

if(isInPublic && f.getName().endsWith(".css")){
response.setContentType("text/css");
}

if(isInPublic && f.getName().endsWith(".appcache")){
response.setContentType("text/cache-manifest");
}

InputStream is = new FileInputStream(f);
IOUtils.copy(is, response.getOutputStream());
}

@PostMapping(path = "/upload", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
private static String upload(HttpServletRequest request,
HttpServletResponse response,
@ModelAttribute("file") UploadedFile uploadedFile
) throws IOException, NoSuchAlgorithmException {
MultipartFile file = uploadedFile.getFile();

String path = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);

log.info("POST "+path);

//Check if the file limit is reached
if (file.getSize() > MAX_UPLOAD_SIZE) {
response.sendError(413);
return "";
}

String filename = file.getOriginalFilename();
if (request.getParameter("name") != null) {
filename = request.getParameter("name");
}

//No .exe files please
if (filename.toLowerCase().endsWith(".exe")) {
response.sendError(400);
return "";
}

String extension = "";
Matcher m = FILE_EXTENSION_PATTERN.matcher(filename);
if (m.find()) {
extension = m.group(1);
}

String storeName = ResourceManager.getUniqueName(extension);
File f = ResourceManager.getResource(storeName);

MessageDigest md = MessageDigest.getInstance("md5");
String hash = Base64.getEncoder().encodeToString(md.digest(file.getBytes()));

//Now generate a response
JSONObject root = new JSONObject();
root.put("success", true);

JSONArray files = new JSONArray();
JSONObject arrayInner = new JSONObject();
arrayInner.put("hash", hash);
arrayInner.put("name", filename);
arrayInner.put("url", baseUrl + storeName);
arrayInner.put("size", file.getSize());

files.put(arrayInner);
root.put("files", files);

file.transferTo(f);//Finally move the file

return root.toString();
}
Spark.port(8080);
RouteOverview.enableRouteOverview();

/* Configuration */
@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolverBean() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize(Long.MAX_VALUE);
return resolver;
/* handlers */
Spark.get("/*", Routes.onGet());
Spark.post("/upload", Routes.upload());
Spark.exception(Exception.class, Routes.exceptionHandler());
}

}
Loading

0 comments on commit b63599e

Please sign in to comment.