improved archive: storage dir cannow be configured. also, mails are now displayed in send-time order
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
.idea
|
||||
archive
|
||||
target
|
||||
config/config.json
|
||||
*.sqlite3
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>Widerhall</artifactId>
|
||||
<version>0.2.1</version>
|
||||
<version>0.2.2</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -59,6 +59,7 @@ public class Configuration {
|
||||
dbFile();
|
||||
baseUrl();
|
||||
serverPort();
|
||||
archiveDir();
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -92,6 +93,13 @@ public class Configuration {
|
||||
return this;
|
||||
}
|
||||
|
||||
public File archiveDir() {
|
||||
var locations = locations();
|
||||
if (!locations.containsKey(ARCHIVE)) locations.put(ARCHIVE, String.join(File.separator,baseDir(),"archive"));
|
||||
return new File((String) locations.get(ARCHIVE));
|
||||
}
|
||||
|
||||
|
||||
public int serverPort() {
|
||||
if (!data.containsKey(PORT)) data.put(PORT,80L);
|
||||
var o = data.get(PORT);
|
||||
@@ -107,4 +115,5 @@ public class Configuration {
|
||||
public File file() {
|
||||
return file;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.srsoftware.widerhall;
|
||||
|
||||
public class Constants {
|
||||
public static final String ARCHIVE = "archive";
|
||||
public static final String BASE = "base";
|
||||
public static final String BASE_URL = "base_url";
|
||||
public static final String CONFIG = "configuration";
|
||||
|
||||
@@ -93,6 +93,7 @@ public class Database {
|
||||
private final StringBuilder sql; // buffer the sql to be built
|
||||
private final HashMap<String, List<Object>> where = new HashMap<>(); // buffer condition statements for select
|
||||
private final HashMap<String,Object> values = new HashMap<>(); // buffer values for insert/update statements
|
||||
private final List<String> sortFields = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Start to create a new request with the initial SQL
|
||||
@@ -126,6 +127,10 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
private void applySorting(){
|
||||
if (!sortFields.isEmpty()) sql.append(" ORDER BY ").append(String.join(", ",sortFields));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Request clone() {
|
||||
Request clone = new Request(sql);
|
||||
@@ -142,6 +147,7 @@ public class Database {
|
||||
var args = new ArrayList<>();
|
||||
applyValues(args);
|
||||
applyConditions(args);
|
||||
applySorting();
|
||||
return new CompiledRequest(sql.toString(),args);
|
||||
}
|
||||
|
||||
@@ -245,6 +251,11 @@ public class Database {
|
||||
list.add(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Request sort(String field) {
|
||||
sortFields.add(field);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public Database(Connection connection) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.srsoftware.widerhall.data;
|
||||
|
||||
import de.srsoftware.widerhall.Configuration;
|
||||
import de.srsoftware.widerhall.Util;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -11,8 +12,10 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.sql.Array;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
@@ -59,7 +62,9 @@ public class Post {
|
||||
var time = message.getSentDate().getTime();
|
||||
|
||||
Post post = new Post(id,list,fromEmail,fromName,subject,time);
|
||||
Files.writeString(post.file().toPath(),text, StandardCharsets.UTF_8);
|
||||
var file = post.file();
|
||||
file.getParentFile().mkdirs();
|
||||
Files.writeString(file.toPath(),text, StandardCharsets.UTF_8);
|
||||
return post.save();
|
||||
} catch (MessagingException | IOException | SQLException e) {
|
||||
LOG.warn("Failed to create post from {}",message);
|
||||
@@ -93,10 +98,10 @@ public class Post {
|
||||
return new File(filename);
|
||||
}
|
||||
|
||||
public static HashSet<Post> find(MailingList list) throws SQLException {
|
||||
var rs = Database.open().select(TABLE_NAME).where(LIST,list.email()).compile().exec();
|
||||
public static ArrayList<Post> find(MailingList list) throws SQLException {
|
||||
var rs = Database.open().select(TABLE_NAME).where(LIST,list.email()).sort(DATE).compile().exec();
|
||||
try {
|
||||
var result = new HashSet<Post>();
|
||||
var result = new ArrayList<Post>();
|
||||
while (rs.next()) result.add(Post.from(rs));
|
||||
return result;
|
||||
} finally {
|
||||
@@ -111,6 +116,7 @@ public class Post {
|
||||
if (post == null) {
|
||||
var list = MailingList.load(rs.getString(LIST));
|
||||
post = new Post(id, list, rs.getString(FROM_ADDR), rs.getString(FROM_NAME), rs.getString(SUBJECT), rs.getLong(DATE));
|
||||
post.filename = rs.getString(FILE);
|
||||
cache.put(id,post);
|
||||
}
|
||||
return post;
|
||||
@@ -122,7 +128,7 @@ public class Post {
|
||||
|
||||
|
||||
private String generateFilename() {
|
||||
return "/tmp/"+id+".json";
|
||||
return Configuration.instance().archiveDir()+File.separator+id+".txt";
|
||||
}
|
||||
|
||||
public String id() {
|
||||
|
||||
@@ -9,19 +9,15 @@ import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static de.srsoftware.widerhall.Constants.*;
|
||||
import static de.srsoftware.widerhall.Util.t;
|
||||
@@ -148,17 +144,17 @@ public class Rest extends HttpServlet {
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Long, Object> archive(HttpServletRequest req) {
|
||||
private List<? extends Object> archive(HttpServletRequest req) {
|
||||
var list = Util.getMailingList(req);
|
||||
if (list != null){
|
||||
try {
|
||||
return Post.find(list).stream().collect(Collectors.toMap(Post::timestamp,Post::safeMap));
|
||||
return Post.find(list).stream().map(Post::safeMap).toList();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
LOG.debug("list: {}",list.email());
|
||||
return Map.of();
|
||||
return List.of();
|
||||
}
|
||||
|
||||
public String handlePost(HttpServletRequest req, HttpServletResponse resp){
|
||||
|
||||
@@ -28,7 +28,6 @@ public class Web extends TemplateServlet {
|
||||
private static final String CONFIRM = "confirm";
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Web.class);
|
||||
private static final String ADMIN = "admin";
|
||||
private static final String ARCHIVE = "archive";
|
||||
private static final String INSPECT = "inspect";
|
||||
private static final String LOGIN = "login";
|
||||
private static final String LOGOUT = "logout";
|
||||
|
||||
Reference in New Issue
Block a user