Skip to content

Commit

Permalink
Fileupload working, saving into s3 bucket also works, showing files a…
Browse files Browse the repository at this point in the history
…lso works
  • Loading branch information
Bernd Ritter committed May 2, 2024
1 parent a61805f commit f290b79
Show file tree
Hide file tree
Showing 26 changed files with 427 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ holarse-website.code-workspace
docs

*.log
nb-configuration.xml
onb-configuration.xml
.dev-environments
4 changes: 3 additions & 1 deletion doc/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ services:
- '../target/holarseweb.war:/usr/local/tomcat/webapps/holarseweb.war'
networks:
- holarse-dev

environment:
- OCI_ACCESSKEYID=${OCI_ACCESSKEYID}
- OCI_SECRETKEYID=${OCI_SECRETKEYID}
cache:
image: memcached:latest
container_name: cache
Expand Down
4 changes: 3 additions & 1 deletion doc/tomcat/conf/server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
URIEncoding="UTF-8"
maxPostSize="8388608"
/>

<Engine name="Catalina" defaultHost="localhost">
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
Expand Down
37 changes: 15 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,7 @@
<artifactId>hypersistence-utils-hibernate-62</artifactId>
<version>3.5.1</version>
</dependency>

<!-- <dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
</dependency> -->


<!-- <dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.2.1</version>
</dependency> -->


<!-- Logging via SLF4j auf Log4j -->
<dependency>
<groupId>org.slf4j</groupId>
Expand All @@ -248,13 +235,7 @@
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.32</version>
</dependency>

<!-- <dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency> -->


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down Expand Up @@ -282,7 +263,13 @@
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
<version>1.15</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.13.0</version>
</dependency>

<dependency>
Expand All @@ -296,6 +283,12 @@
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/de/holarse/backend/view/ArticleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ArticleView {

private List<AttachmentView> websiteLinks = new ArrayList<>();
private List<YoutubeView> youtubeVideos = new ArrayList<>();
private List<ScreenshotView> screenshotViews = new ArrayList<>();

public static ArticleView of(final ArticleRevision ar, final NodeSlug slug) {
final ArticleView av = new ArticleView();
Expand Down Expand Up @@ -208,4 +209,12 @@ public String getUrl() {
public void setUrl(String url) {
this.url = url;
}

public List<ScreenshotView> getScreenshots() {
return screenshotViews;
}

public void setScreenshots(List<ScreenshotView> screenshotViews) {
this.screenshotViews = screenshotViews;
}
}
17 changes: 17 additions & 0 deletions src/main/java/de/holarse/backend/view/ScreenshotView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.holarse.backend.view;

import de.holarse.backend.db.Attachment;
import org.springframework.stereotype.Component;

public class ScreenshotView extends AttachmentView {

public static ScreenshotView of(final Attachment attachment) {
final ScreenshotView view = new ScreenshotView();
view.setId(attachment.getId());
view.setData(attachment.getData());
view.setDescription(attachment.getDescription());
view.setWeight(attachment.getWeight());

return view;
}
}
8 changes: 8 additions & 0 deletions src/main/java/de/holarse/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.holarse.config;

import de.holarse.web.converters.StringToFilepondConverter;
import de.holarse.web.interceptors.RequestLoggingInterceptor;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
Expand All @@ -12,6 +13,7 @@
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.format.FormatterRegistry;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
Expand All @@ -38,6 +40,7 @@
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:credential.properties"),
@PropertySource("classpath:cloud.properties"),
@PropertySource("classpath:git.properties")})
public class AppConfig implements WebMvcConfigurer {

Expand Down Expand Up @@ -131,4 +134,9 @@ public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(requestLoggingInterceptor());
}

@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToFilepondConverter());
}

}
56 changes: 46 additions & 10 deletions src/main/java/de/holarse/web/controller/WikiController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.holarse.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.holarse.auth.web.HolarsePrincipal;
import de.holarse.backend.db.*;
import de.holarse.backend.db.repositories.*;
Expand All @@ -10,15 +12,15 @@
import static de.holarse.config.JmsQueueTypes.QUEUE_SEARCH;
import de.holarse.queues.commands.SearchRefresh;
import de.holarse.web.controller.commands.ArticleForm;
import de.holarse.web.controller.commands.FileUploadForm;
import de.holarse.web.defines.WebDefines;

import static de.holarse.web.defines.WebDefines.WIKI_ARTICLES_DEFAULT_PAGE_SIZE;
import de.holarse.web.renderer.Renderer;
import de.holarse.web.services.AttachmentService;
import de.holarse.web.services.SlugService;
import de.holarse.web.services.TagService;
import de.holarse.web.services.*;
import jakarta.persistence.EntityNotFoundException;
import java.security.Principal;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -79,6 +81,12 @@ public class WikiController {
@Autowired
private AttachmentService attachmentService;

@Autowired
private ObjectStorageService objectStorageService;

@Autowired
private FileUploadService fileUploadService;

@Autowired
private Renderer renderer;

Expand Down Expand Up @@ -128,7 +136,8 @@ public ModelAndView show(@PathVariable("slug") final String slug, final ModelAnd

final List<Attachment> websiteLinks = attachmentService.getAttachments(article, attachmentGroupRepository.findByCode(AttachmentGroupType.website.name()));
final List<Attachment> videos = attachmentService.getAttachments(article, attachmentGroupRepository.findByCode(AttachmentGroupType.video.name()));

final List<Attachment> screenshots = attachmentService.getAttachments(article, attachmentGroupRepository.findByCode(AttachmentGroupType.image.name()));

// View zusammenstellen
final ArticleView view = ArticleView.of(articleRevision, mainSlug);
view.setNodeId(article.getNodeId());
Expand All @@ -137,6 +146,7 @@ public ModelAndView show(@PathVariable("slug") final String slug, final ModelAnd
//view.setSlug(mainSlug.getName());
view.setWebsiteLinks(websiteLinks.stream().map(AttachmentView::of).toList());
view.setYoutubeVideos(videos.stream().map(YoutubeView::of).toList());
view.setScreenshots(screenshots.stream().map(ScreenshotView::of).map(ssv -> objectStorageService.patchUrl(ssv)).toList());

mv.addObject("view", view);

Expand Down Expand Up @@ -178,7 +188,7 @@ public ModelAndView edit(@PathVariable final Integer nodeId, final ModelAndView
form.setContent(articleRevision.getContent());

// Attachments
final List<Attachment> websiteLinks = attachmentService.getAttachments(article, attachmentGroupRepository.findByCode("website"));
final List<Attachment> websiteLinks = attachmentService.getAttachments(article, attachmentGroupRepository.findByCode(AttachmentGroupType.website.name()));
logger.debug("Links: {}", websiteLinks);
form.setWebsiteLinks(websiteLinks.stream().map(AttachmentView::of).toList());

Expand All @@ -192,26 +202,49 @@ public ModelAndView edit(@PathVariable final Integer nodeId, final ModelAndView

@Transactional
@PostMapping("{nodeId}")
public ModelAndView update(@PathVariable final int nodeId, @ModelAttribute("form") final ArticleForm form, final ModelAndView mv, final Authentication authentication) {
public ModelAndView update(@PathVariable final int nodeId, @ModelAttribute("form") final ArticleForm form, final ModelAndView mv, final Authentication authentication) throws JsonProcessingException {
logger.debug("Updating by form {}", form);

final Article article = articleRepository.findByNodeId(nodeId).orElseThrow(EntityNotFoundException::new);

final User author = userRepository.findById( ((HolarsePrincipal)authentication.getPrincipal()).getUser().getId() ).orElseThrow(EntityNotFoundException::new);

final AttachmentType attScreenshot = attachmentTypeRepository.findByCode("screenshot");
final AttachmentType attLink = attachmentTypeRepository.findByCode("link");

List<Attachment> addedScreenShots = new ArrayList<>();
final List<FileUploadForm> screenshots = fileUploadService.readFileUpload(form);
for (FileUploadForm screenshot : screenshots) {
final String hashedFilename = objectStorageService.writeToCloud(screenshot);
Attachment attachment = new Attachment();
attachment.setData(hashedFilename);
attachment.setAttachmentType(attScreenshot);
attachment.setWeight(0);
attachment.setNodeId(nodeId);
attachment.setCreated(OffsetDateTime.now());
addedScreenShots.add(attachment);
}

// Aktualisieren und als neue Revision speichern
final int revision = articleRevisionRepository.nextRevision();

ArticleRevision articleRevision = new ArticleRevision();
articleRevision.setNodeId(nodeId);
articleRevision.setRevision(revision);
articleRevision.setTitle1(form.getTitle1());
articleRevision.setTitle1Lang(WebDefines.TITLE_LANG_GERMAN);
articleRevision.setTitle2(form.getTitle2());
articleRevision.setTitle2Lang(WebDefines.TITLE_LANG_GERMAN);
articleRevision.setTitle3(form.getTitle3());
articleRevision.setTitle3Lang(WebDefines.TITLE_LANG_GERMAN);
articleRevision.setTitle4(form.getTitle4());
articleRevision.setTitle4Lang(WebDefines.TITLE_LANG_GERMAN);
articleRevision.setTitle5(form.getTitle5());
articleRevision.setTitle5Lang(WebDefines.TITLE_LANG_GERMAN);
articleRevision.setTitle6(form.getTitle6());
articleRevision.setTitle6Lang(WebDefines.TITLE_LANG_GERMAN);
articleRevision.setTitle7(form.getTitle7());
articleRevision.setTitle7Lang(WebDefines.TITLE_LANG_GERMAN);
articleRevision.setContent(form.getContent());
articleRevision.setChangelog("TODO Changelog im Form"); // TODO Changelog im Article-Form
articleRevision.setAuthor(author);
Expand All @@ -225,16 +258,16 @@ public ModelAndView update(@PathVariable final int nodeId, @ModelAttribute("form
// Website Links
//
logger.debug("Link-Attachments: {}", form.getWebsiteLinks());
final Map<Boolean, List<AttachmentView>> websiteLinksMap = form.getWebsiteLinks().stream().collect(Collectors.partitioningBy(av -> av.isMarkAsDeleted()));
final Map<Boolean, List<AttachmentView>> websiteLinksMap = form.getWebsiteLinks().stream().collect(Collectors.partitioningBy(AttachmentView::isMarkAsDeleted));
// Die zu Löschenden verarbeiten
attachmentRepository.deleteAllById(websiteLinksMap.get(Boolean.TRUE).stream().map(av -> av.getId()).toList());
attachmentRepository.deleteAllById(websiteLinksMap.get(Boolean.TRUE).stream().map(AttachmentView::getId).toList());

final List<Attachment> createdAndUpdatedAttachments = new ArrayList<>();
// Die neuen Entities umwandeln und speichern
createdAndUpdatedAttachments.addAll(websiteLinksMap.get(Boolean.FALSE).stream()
.filter(av -> av.getId() == null)
.filter(av -> StringUtils.isNotBlank(av.getData()))
.map(av -> Attachment.build(av, nodeId, attachmentTypeRepository.findByCode("link")))
.map(av -> Attachment.build(av, nodeId, attLink))
.toList());
// Die bestehenden finden und updaten
for (final AttachmentView av : websiteLinksMap.get(Boolean.FALSE).stream().filter(av -> av.getId() != null).toList()) {
Expand All @@ -246,7 +279,10 @@ public ModelAndView update(@PathVariable final int nodeId, @ModelAttribute("form
createdAndUpdatedAttachments.add(att);
}
attachmentRepository.saveAllAndFlush(createdAndUpdatedAttachments);
logger.debug("Saved attachments");
logger.debug("Saved attachments (links)");

attachmentRepository.saveAllAndFlush(addedScreenShots);
logger.debug("Saved attachments (screenshots");

// Status
final NodeStatus nodeStatus = article.getNodeStatus();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.holarse.web.controller.commands;

import de.holarse.backend.view.AttachmentView;
import de.holarse.backend.view.ScreenshotView;
import de.holarse.backend.view.SettingsView;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
Expand All @@ -16,6 +17,7 @@ public abstract class AbstractNodeForm {
private String content;

private List<AttachmentView> websiteLinks = new ArrayList<>();
private List<ScreenshotView> screenshots = new ArrayList<>();

private SettingsView settings = new SettingsView();

Expand Down Expand Up @@ -51,4 +53,7 @@ public void setSettings(SettingsView settings) {
this.settings = settings;
}

public void setScreenshots(List<ScreenshotView> screenshots) { this.screenshots = screenshots ;}
public List<ScreenshotView> getScreenshots() { return screenshots; };

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import de.holarse.backend.view.SettingsView;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.io.File;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -20,6 +23,8 @@ public class ArticleForm extends AbstractNodeForm {
@NotBlank
private String tags;

private List<FileUploadForm> filepond = new ArrayList<>();

public String getTitle1() {
return title1;
}
Expand Down Expand Up @@ -83,5 +88,12 @@ public String getTags() {
public void setTags(String tags) {
this.tags = tags;
}


public List<FileUploadForm> getFilepond() {
return filepond;
}

public void setFilepond(List<FileUploadForm> filepond) {
this.filepond = filepond;
}
}
Loading

0 comments on commit f290b79

Please sign in to comment.