Skip to content
Permalink
Browse files

V1

Signed-off-by: 天空Blond <hurui200320@skyblond.info>
  • Loading branch information...
hurui200320 committed Mar 8, 2019
1 parent 6326816 commit 6229053d09e02c80d8a499f52eb7581f8c3ff0c1
@@ -0,0 +1,32 @@
<component name="ArtifactManager">
<artifact type="jar" name="Blake-Belladonna:jar, ALL-IN-ONE">
<output-path>$PROJECT_DIR$/out/artifacts/Blake_Belladonna_jar__ALL_IN_ONE</output-path>
<root id="archive" name="Blake-Belladonna.jar">
<element id="module-output" name="Blake-Belladonna" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/gson-2.8.5.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/slf4j-simple-1.7.25.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/slf4j-api-1.7.25.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/javalin-2.7.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/kotlin-stdlib-jdk8-1.2.71.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/kotlin-stdlib-1.2.71.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/kotlin-stdlib-common-1.2.71.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/annotations-13.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/kotlin-stdlib-jdk7-1.2.71.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-server-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/javax.servlet-api-3.1.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-http-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-util-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-io-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-webapp-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-xml-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-servlet-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-security-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/websocket-server-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/websocket-common-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/websocket-api-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/websocket-client-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jetty-client-9.4.15.v20190215.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/websocket-servlet-9.4.15.v20190215.jar" path-in-jar="/" />
</root>
</artifact>
</component>

Large diffs are not rendered by default.

363 README.md

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -1,9 +1,12 @@
#Blake Belladonna
#Thu Mar 07 22:23:51 CST 2019
message_expired_time=604800
create_theme=.\\create.html
port=7000
message_theme=.\\message.html
succeed_theme=.\\createSucceed.html
#Fri Mar 08 08:35:09 CST 2019
ip=127.0.0.1
data_directory=.\\messages
port=7000
data_directory=messages
message_theme=message.html
create_theme=create.html
succeed_theme=createSucceed.html
#7days, in unit of second
message_expired_time=604800
#allow at max of 500000 massage files on system
max_messages=500000
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: info.skyblond.Blake.Belladonna.Main

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: info.skyblond.Blake.Belladonna.Main

@@ -13,6 +13,7 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
@@ -28,18 +29,20 @@
import java.util.concurrent.TimeUnit;

public class Main {
public static final boolean isDebug = false; // turn to false when make final artifacts

public static final String POST_TITLE_FIELD = "title";
public static final String POST_CONTENT_FIELD = "content";

public static void main(String[] args) {

PropertiesUtils.getProperties(); //read first
if(isDebug){
Share.filePrefix = ".//"; //using class path when debug.
}

PropertiesUtils.getProperties();

Javalin app = Javalin.create();
app.get("/", ctx -> {
ctx.redirect("https://github.com/hurui200320/Blake-Belladonna");
});

app.get("/create", ctx -> {
ctx.header("content-type","text/html; charset=UTF-8");
@@ -50,9 +53,9 @@ public static void main(String[] args) {
Messages messages;
if(true)
// if(PropertiesUtils.getProperties().getDataMode() == 0)
messages = Share.findMessageFile(ctx.pathParam("checksum").toUpperCase());
messages = Messages.findMessageFile(ctx.pathParam("checksum").toUpperCase());
else
messages = Share.findMessageMysql(ctx.pathParam("checksum").toUpperCase());
messages = Messages.findMessageMysql(ctx.pathParam("checksum").toUpperCase());
if(messages == null){
ctx.status(410);
return;
@@ -62,6 +65,14 @@ public static void main(String[] args) {
});

app.post("/new-message", ctx -> {
File[] list = PropertiesUtils.getProperties().getDataDirectory().toFile().listFiles();
if(list != null && list.length >= PropertiesUtils.getProperties().getMaxMessages()){
ctx.status(500);
ctx.result("Too many message files in data directory!");
Share.logger.error("Too many message files in data directory!");
return;
}

Map<String, List<String>> raw = ctx.formParamMap();
if(raw.keySet().containsAll(Arrays.asList(POST_TITLE_FIELD, POST_CONTENT_FIELD))){
Messages messages = new Messages();
@@ -94,33 +105,39 @@ public static void main(String[] args) {
ctx.result(String.join("\n", Files.readAllLines(PropertiesUtils.getProperties().getSucceedTheme())).replace("{{% code %}}", result));
}else {
ctx.status(400);
ctx.result("bad request");
}
});

app.get("/*", ctx -> {
ctx.redirect("https://github.com/hurui200320/Blake-Belladonna");
});

//period call gc and delete expired message.
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
Files.walkFileTree(PropertiesUtils.getProperties().getDataDirectory(), new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.requireNonNull(file);
Messages messages;
try{
messages = Share.gson.fromJson(String.join("",Files.readAllLines(file)), Messages.class);
}catch (Exception e){
Files.delete(file);
return FileVisitResult.TERMINATE;
if(PropertiesUtils.getProperties().getMessageExpiredTime() != 0)
Files.walkFileTree(PropertiesUtils.getProperties().getDataDirectory(), new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.requireNonNull(file);
Messages messages;
try{
messages = Share.gson.fromJson(String.join("",Files.readAllLines(file)), Messages.class);
}catch (Exception e){
Files.delete(file);
return FileVisitResult.TERMINATE;
}

if(messages.isExpired() && Files.exists(file))
Files.delete(file);

return super.visitFile(file, attrs);
}

if(messages.isExpired())
Files.delete(file);

return super.visitFile(file, attrs);
}
});
});
} catch (IOException e) {
e.printStackTrace();
} finally {
@@ -3,18 +3,15 @@
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.Timestamp;
import java.util.Base64;
import java.util.Random;
import java.util.*;
import java.util.zip.CRC32;

public class Messages {
private String title = "", content = "";
private Timestamp sendTime = new Timestamp(System.currentTimeMillis());
//0 for burn after reading or time up, default
//1 preserve forever, disabled when admin password is empty

public boolean isExpired(){
if(PropertiesUtils.getProperties().getMessageExpiredTime() == 0)
@@ -73,11 +70,48 @@ public String storeToFile(){
return name;
}

public static Messages findMessageFile(String name) throws IOException {
List<Messages> result = Collections.synchronizedList(new LinkedList<>());
result.clear();
Files.walkFileTree(PropertiesUtils.getProperties().getDataDirectory(), new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.requireNonNull(file);
if(file.getFileName().toString().toUpperCase().equals(name.toUpperCase())){
String read = String.join("",Files.readAllLines(file));
Messages messages;
try{
messages = Share.gson.fromJson(read, Messages.class);
}catch (Exception e){
System.out.println(read);
e.printStackTrace();
messages = null;
}finally {
if(Files.exists(file))
Files.delete(file);
}
if(messages != null)
result.add(messages);
return FileVisitResult.TERMINATE;
}
return super.visitFile(file, attrs);
}
});
if(result.size() == 0)
return null;
return result.get(0);
}

public String storeToMysql(){
//TODO
return null;
}

public static Messages findMessageMysql(String name){
// TODO
return null;
}

@Override
public String toString() {
return "Messages{" +
@@ -7,7 +7,7 @@
import java.util.Properties;

public class PropertiesUtils {
public static final String DEFAULT_PATH = ".//belladonna.properties";
public static final String DEFAULT_PATH = Share.filePrefix + ".//belladonna.properties";

private static final Properties properties = new Properties();

@@ -74,9 +74,25 @@ public static synchronized void readProperties(Path path){
Share.logger.warn("Empty " + Property.DATA_DIRECTORY_FIELD + ". Using default: " + Property.property.getDataDirectory());
needUpdate = true;
}else {
tmpString = Share.filePrefix + tmpString;
Property.property.setDataDirectory(Paths.get(tmpString));
}

if (Files.notExists(PropertiesUtils.getProperties().getDataDirectory())) {
try {
Files.createDirectories(PropertiesUtils.getProperties().getDataDirectory());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

try {
Property.property.setMaxMessage(Long.parseLong(properties.getProperty(Property.MAX_MESSAGES_FIELD)));
}catch (NumberFormatException e){
Share.logger.error("Invalidate " + Property.MAX_MESSAGES_FIELD + ". Using default: " + Property.property.getMaxMessages());
needUpdate = true;
}


}// else { // mysql
// Share.logger.info("Using MySQL mode.");
@@ -131,6 +147,7 @@ public static synchronized void readProperties(Path path){
Share.logger.warn("Empty " + Property.MESSAGE_THEME_FIELD + ". Using default: " + Property.property.getMessageTheme());
needUpdate = true;
}else {
tmpString = Share.filePrefix + tmpString;
Property.property.setMessageTheme(Paths.get(tmpString));
}

@@ -139,6 +156,7 @@ public static synchronized void readProperties(Path path){
Share.logger.warn("Empty " + Property.CREATE_THEME_FIELD + ". Using default: " + Property.property.getCreateTheme());
needUpdate = true;
}else {
tmpString = Share.filePrefix + tmpString;
Property.property.setCreateTheme(Paths.get(tmpString));
}

@@ -147,12 +165,12 @@ public static synchronized void readProperties(Path path){
Share.logger.warn("Empty " + Property.SUCCEED_THEME_FIELD + ". Using default: " + Property.property.getSucceedTheme());
needUpdate = true;
}else {
tmpString = Share.filePrefix + tmpString;
Property.property.setSucceedTheme(Paths.get(tmpString));
}

try {
tmpInt = Integer.parseInt(properties.getProperty(Property.MESSAGE_EXPIRED_TIME_FIELD));
Property.property.setMessageExpiredTime(tmpInt);
Property.property.setMessageExpiredTime(Long.parseLong(properties.getProperty(Property.MESSAGE_EXPIRED_TIME_FIELD)));
}catch (NumberFormatException e){
Share.logger.error("Invalidate " + Property.MESSAGE_EXPIRED_TIME_FIELD + ". Using default: " + Property.property.getMessageExpiredTime());
needUpdate = true;
@@ -173,6 +191,7 @@ public static synchronized void writeProperties(Path path){
properties.setProperty(Property.IP_FIELD, Property.property.getIP() + "");
// properties.setProperty(Property.DATA_MODE_FIELD, Property.property.getDataMode() + "");
properties.setProperty(Property.DATA_DIRECTORY_FIELD, Property.property.getDataDirectory() + "");
properties.setProperty(Property.MAX_MESSAGES_FIELD, Property.property.getMaxMessages() + "");
// properties.setProperty(Property.MYSQL_ADDR_FIELD, Property.property.getMysqlAddr() + "");
// properties.setProperty(Property.MYSQL_PORT_FIELD, Property.property.getMysqlPort() + "");
// properties.setProperty(Property.MYSQL_DATABASE_NAME_FIELD, Property.property.getMysqlDatabaseName() + "");
@@ -233,16 +252,19 @@ message expired(in second, 0 for no expired)
// private String mysqlPassword = "";
// public static final String MYSQL_PASSWORD_FIELD = "mysql_password";

private Path dataDirectory = Paths.get(".//messages");
private Path dataDirectory = Paths.get("messages");
public static final String DATA_DIRECTORY_FIELD = "data_directory";

private Path messageTheme = Paths.get(".//message.html");
private long maxMessage = 100000;
public static final String MAX_MESSAGES_FIELD = "max_messages";

private Path messageTheme = Paths.get("message.html");
public static final String MESSAGE_THEME_FIELD = "message_theme";

private Path createTheme = Paths.get(".//create.html");
private Path createTheme = Paths.get("create.html");
public static final String CREATE_THEME_FIELD = "create_theme";

private Path succeedTheme = Paths.get(".//createSucceed.html");
private Path succeedTheme = Paths.get("createSucceed.html");
public static final String SUCCEED_THEME_FIELD = "succeed_theme";

private long messageExpiredTime = 7*24*60*60L; //7days, in second
@@ -320,6 +342,14 @@ void setDataDirectory(Path dataDirectory) {
this.dataDirectory = dataDirectory;
}

public long getMaxMessages() {
return maxMessage;
}

public void setMaxMessage(long maxMessage) {
this.maxMessage = maxMessage;
}

public long getMessageExpiredTime() {
return messageExpiredTime;
}

0 comments on commit 6229053

Please sign in to comment.
You can’t perform that action at this time.