Skip to content

Commit 8be6368

Browse files
author
Bernd Ritter
committed
Article Export now base64 and gzips the images and file attachments
1 parent f6dd11a commit 8be6368

6 files changed

Lines changed: 160 additions & 36 deletions

File tree

app/src/main/java/de/holarse/backend/api/Attachment.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
@JacksonXmlRootElement(localName="attachment")
88
public class Attachment implements Serializable {
99

10-
private static final long serialVersionUID = 1L;
10+
private static final long serialVersionUID = 3L;
11+
12+
@JacksonXmlProperty(isAttribute = true)
13+
private String name;
1114

1215
@JacksonXmlProperty(isAttribute = true)
1316
private Long prio;
@@ -24,6 +27,9 @@ public class Attachment implements Serializable {
2427
@JacksonXmlProperty
2528
private String description;
2629

30+
@JacksonXmlProperty(isAttribute = true)
31+
private Long fileSize;
32+
2733
public Long getPrio() {
2834
return prio;
2935
}
@@ -63,5 +69,28 @@ public String getGroup() {
6369
public void setGroup(String group) {
6470
this.group = group;
6571
}
72+
73+
public String getName() {
74+
return name;
75+
}
76+
77+
public void setName(String name) {
78+
this.name = name;
79+
}
80+
81+
82+
public Long getFileSize() {
83+
return fileSize;
84+
}
85+
86+
public void setFileSize(Long fileSize) {
87+
this.fileSize = fileSize;
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return "Attachment [name=" + name + ", prio=" + prio + ", type=" + type + ", group=" + group + ", description="
93+
+ description + ", fileSize=" + fileSize + "]";
94+
}
6695

6796
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
xml_dateformat=yyyy-MM-dd'T'HH:mm:ssZ
22
database_url=jdbc:mariadb://localhost:3306/holarse?user=export&password=geheim
33
export_path=/tmp/export
4+
media_base_dir=/var/www/www.holarse-linuxgaming.de/
5+
buffer_size=4096

tools/HolarseExport/pom.xml

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>de.holarse.tools</groupId>
77
<artifactId>HolarseExport</artifactId>
@@ -11,32 +11,39 @@
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1212
<java.version>25</java.version>
1313
</properties>
14-
14+
1515
<dependencies>
1616
<dependency>
1717
<groupId>org.mariadb.jdbc</groupId>
1818
<artifactId>mariadb-java-client</artifactId>
1919
<version>2.3.0</version>
20-
</dependency>
21-
20+
</dependency>
21+
2222
<dependency>
2323
<groupId>com.fasterxml.jackson.core</groupId>
2424
<artifactId>jackson-core</artifactId>
2525
<version>2.9.8</version>
26-
</dependency>
27-
26+
</dependency>
27+
2828
<dependency>
2929
<groupId>com.fasterxml.jackson.dataformat</groupId>
3030
<artifactId>jackson-dataformat-xml</artifactId>
3131
<version>2.9.8</version>
32-
</dependency>
33-
32+
</dependency>
33+
3434
<dependency>
3535
<groupId>org.apache.commons</groupId>
3636
<artifactId>commons-lang3</artifactId>
3737
<version>3.8.1</version>
38-
</dependency>
39-
38+
</dependency>
39+
40+
<!-- Source: https://mvnrepository.com/artifact/commons-codec/commons-codec -->
41+
<dependency>
42+
<groupId>commons-codec</groupId>
43+
<artifactId>commons-codec</artifactId>
44+
<version>1.22.0</version>
45+
</dependency>
46+
4047
<!-- Nötig ab Java 9 -->
4148
<dependency>
4249
<groupId>javax.xml.bind</groupId>
@@ -50,16 +57,16 @@
5057
<artifactId>slf4j-api</artifactId>
5158
<version>2.0.18</version>
5259
<scope>compile</scope>
53-
</dependency>
54-
60+
</dependency>
61+
5562
<!-- Source: https://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j -->
5663
<dependency>
5764
<groupId>org.slf4j</groupId>
5865
<artifactId>jul-to-slf4j</artifactId>
5966
<version>2.0.18</version>
6067
<scope>compile</scope>
61-
</dependency>
62-
68+
</dependency>
69+
6370
</dependencies>
6471
<build>
6572
<plugins>
@@ -75,9 +82,10 @@
7582
<arg>-Xlint:unchecked</arg>
7683
<arg>-Xlint:deprecation</arg>
7784
</compilerArgs>
78-
<!-- <outputDirectory>/home/comrad/Downloads/wlp/usr/servers/holarse/dropins</outputDirectory>-->
85+
<!--
86+
<outputDirectory>/home/comrad/Downloads/wlp/usr/servers/holarse/dropins</outputDirectory>-->
7987
</configuration>
80-
</plugin>
88+
</plugin>
8189
<plugin>
8290
<groupId>org.apache.maven.plugins</groupId>
8391
<artifactId>maven-assembly-plugin</artifactId>
@@ -102,8 +110,8 @@
102110
</configuration>
103111
</execution>
104112
</executions>
105-
</plugin>
106-
113+
</plugin>
114+
107115
</plugins>
108116
</build>
109117
</project>

tools/HolarseExport/src/main/java/de/holarse/backend/export/Attachment.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
@JacksonXmlRootElement(localName="attachment")
77
public class Attachment {
88

9+
private static final long serialVersionUID = 3L;
10+
11+
@JacksonXmlProperty(isAttribute = true)
12+
private String name;
13+
914
@JacksonXmlProperty(isAttribute = true)
1015
private Long prio;
1116

@@ -21,6 +26,9 @@ public class Attachment {
2126
@JacksonXmlProperty
2227
private String description;
2328

29+
@JacksonXmlProperty(isAttribute = true)
30+
private Long fileSize;
31+
2432
public Long getPrio() {
2533
return prio;
2634
}
@@ -61,10 +69,26 @@ public void setGroup(String group) {
6169
this.group = group;
6270
}
6371

72+
public String getName() {
73+
return name;
74+
}
75+
76+
public void setName(String name) {
77+
this.name = name;
78+
}
79+
80+
public Long getFileSize() {
81+
return fileSize;
82+
}
83+
84+
public void setFileSize(Long fileSize) {
85+
this.fileSize = fileSize;
86+
}
87+
6488
@Override
6589
public String toString() {
66-
return "Attachment{" + "prio=" + prio + ", type=" + type + ", group=" + group + ", content=" + content + ", description=" + description + '}';
90+
return "Attachment [name=" + name + ", prio=" + prio + ", type=" + type + ", group=" + group + ", description="
91+
+ description + ", fileSize=" + fileSize + "]";
6792
}
68-
69-
93+
7094
}

tools/HolarseExport/src/main/java/de/holarse/tools/holarseexport/ArticleExport.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import de.holarse.backend.export.Tag;
99
import de.holarse.backend.export.Title;
1010
import de.holarse.tools.helper.AttachmentHelper;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
1117
import java.sql.Connection;
1218
import java.sql.PreparedStatement;
1319
import java.sql.ResultSet;
@@ -298,13 +304,23 @@ public void export(Connection c) throws Exception {
298304
while (result.next()) {
299305
final String image = result.getString("filepath");
300306
if (StringUtils.isNotBlank(image)) {
301-
final Attachment att = new Attachment();
302-
att.setType("SCREENSHOT");
303-
att.setGroup("IMAGE");
304-
att.setContent(image);
305-
att.setPrio(prio++);
306-
307-
attachments.add(att);
307+
308+
try {
309+
final Path path = Path.of(Configuration.get("media_base_dir"), image);
310+
final String base64Image = MediaExporter.readAsBase64(path);
311+
312+
final Attachment att = new Attachment();
313+
att.setName(Path.of(image).getFileName().toString());
314+
att.setType("SCREENSHOT");
315+
att.setGroup("IMAGE");
316+
att.setContent(base64Image);
317+
att.setPrio(prio++);
318+
att.setFileSize(Files.size(path));
319+
320+
attachments.add(att);
321+
} catch (IOException ioex) {
322+
log.log(Level.WARNING, "Image IO error", ioex);
323+
}
308324
}
309325
}
310326
}
@@ -316,12 +332,22 @@ public void export(Connection c) throws Exception {
316332
while (result.next()) {
317333
final String file = result.getString("filepath");
318334
if (StringUtils.isNotBlank(file)) {
319-
final Attachment att = new Attachment();
320-
att.setType("FILE");
321-
att.setGroup("FILE");
322-
att.setContent(file);
335+
336+
try {
337+
final Path path = Path.of(Configuration.get("media_base_dir"), file);
338+
final String base64File = MediaExporter.readAsBase64(path);
323339

324-
attachments.add(att);
340+
final Attachment att = new Attachment();
341+
att.setName(path.getFileName().toString());
342+
att.setType("FILE");
343+
att.setGroup("FILE");
344+
att.setContent(base64File);
345+
att.setFileSize(Files.size(path));
346+
347+
attachments.add(att);
348+
} catch (IOException ioex) {
349+
log.log(Level.WARNING, "File IO error", ioex);
350+
}
325351
}
326352
}
327353
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package de.holarse.tools.holarseexport;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.nio.file.Path;
8+
import java.util.zip.GZIPOutputStream;
9+
10+
import org.apache.commons.codec.binary.Base64OutputStream;
11+
12+
public class MediaExporter {
13+
14+
private MediaExporter() {}
15+
16+
public static String readAsBase64(final Path path) throws Exception {
17+
final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 1024 * 1024); // 1 MB initial space
18+
19+
final int buffer_size = Integer.parseInt(Configuration.get("buffer_size"));
20+
21+
final byte[] buffer = new byte[buffer_size];
22+
int len = 0;
23+
24+
try (final GZIPOutputStream gzos = new GZIPOutputStream(new Base64OutputStream(baos), buffer_size);
25+
final FileInputStream fis = new FileInputStream(path.toFile())) {
26+
while ((len = fis.read(buffer)) != -1) {
27+
gzos.write(buffer, 0, len);
28+
}
29+
} catch (IOException e) {
30+
throw(e);
31+
}
32+
33+
return new String(baos.toByteArray());
34+
}
35+
}

0 commit comments

Comments
 (0)