From 9b89833b90d06fca0a072b5afa3e7976f7c07f31 Mon Sep 17 00:00:00 2001
From: Thomas <gltron3000@gmail.com>
Date: Wed, 27 May 2020 15:54:15 +0200
Subject: [PATCH] Added content store

---
 mozen/pom.xml                                 |  5 ++
 .../java/mozen/business/IModelManager.java    |  3 +
 .../java/mozen/business/ModelManager.java     | 67 ++++++++++++++-----
 mozen/src/main/java/mozen/model/Model.java    | 46 ++++++++-----
 .../src/main/java/mozen/repos/ModelStore.java |  9 +++
 .../main/java/mozen/web/ModelController.java  |  4 +-
 .../src/main/resources/application.properties | 10 +--
 7 files changed, 103 insertions(+), 41 deletions(-)
 create mode 100644 mozen/src/main/java/mozen/repos/ModelStore.java

diff --git a/mozen/pom.xml b/mozen/pom.xml
index dfe5cd8..fdf4046 100644
--- a/mozen/pom.xml
+++ b/mozen/pom.xml
@@ -46,6 +46,11 @@
 			<artifactId>jjwt</artifactId>
 			<version>0.9.1</version>
 		</dependency>
+		<dependency>
+			<groupId>com.github.paulcwarren</groupId>
+			<artifactId>spring-content-fs-boot-starter</artifactId>
+			<version>1.0.0.M10</version>
+		</dependency>
 
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
diff --git a/mozen/src/main/java/mozen/business/IModelManager.java b/mozen/src/main/java/mozen/business/IModelManager.java
index 61de3c2..50e8f3e 100644
--- a/mozen/src/main/java/mozen/business/IModelManager.java
+++ b/mozen/src/main/java/mozen/business/IModelManager.java
@@ -1,5 +1,6 @@
 package mozen.business;
 
+import java.io.InputStream;
 import java.util.Collection;
 
 import org.springframework.web.multipart.MultipartFile;
@@ -20,5 +21,7 @@ public interface IModelManager {
   Model getModel(long id);
   Collection<Model> getModels();
 
+  InputStream getModelFile(long id) throws Exception;
+
   SearchResult findModel(String name, int page, int size, String sort, Collection<String> tagsName);
 }
\ No newline at end of file
diff --git a/mozen/src/main/java/mozen/business/ModelManager.java b/mozen/src/main/java/mozen/business/ModelManager.java
index cdb7f5f..963a66a 100644
--- a/mozen/src/main/java/mozen/business/ModelManager.java
+++ b/mozen/src/main/java/mozen/business/ModelManager.java
@@ -1,5 +1,6 @@
 package mozen.business;
 
+import java.io.InputStream;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
@@ -20,6 +21,7 @@ import mozen.messages.SearchResult;
 import mozen.model.Tag;
 import mozen.model.User;
 import mozen.repos.ModelRepository;
+import mozen.repos.ModelStore;
 import mozen.repos.TagRepository;
 import mozen.utils.Md5Utils;
 import mozen.utils.UserHelper;
@@ -33,6 +35,9 @@ public class ModelManager implements IModelManager {
   @Autowired
   private TagRepository tagRepo;
 
+  @Autowired
+  private ModelStore modelStore;
+
   @Override
   public Model addModel(ModelMessage message, User user) throws Exception {
     Model model = new Model();
@@ -48,7 +53,8 @@ public class ModelManager implements IModelManager {
     model.setParameterCount(message.getParameterCount());
 
     // Standardise la mesure de performance.
-    // C'est bancale mais difficile de trier autrement en mélangeant les unitées de performances
+    // C'est bancale mais difficile de trier autrement en mélangeant les unitées de
+    // performances
     if (message.getPerformanceLowerIsBetter()) {
       model.setPerformanceIndex(message.getPerformance());
     } else {
@@ -85,7 +91,7 @@ public class ModelManager implements IModelManager {
       throw new Exception("Not the author");
 
     model.setChecksum(Md5Utils.getMD5(file));
-    model.setFile(file.getBytes());
+    modelStore.setContent(model, file.getInputStream());
     model.setFileName(file.getOriginalFilename());
     model.setFileType(file.getContentType());
 
@@ -101,12 +107,16 @@ public class ModelManager implements IModelManager {
     if (!UserHelper.isAuthor(model.getAuthor(), user))
       throw new Exception("Not the author");
 
-    if (message.getName() != null) model.setName(message.getName());
-    if (message.getShortDescription() != null) model.setShortDescription(message.getShortDescription());
-    if (message.getLongDescription() != null) model.setLongDescription(message.getLongDescription());
-    
+    if (message.getName() != null)
+      model.setName(message.getName());
+    if (message.getShortDescription() != null)
+      model.setShortDescription(message.getShortDescription());
+    if (message.getLongDescription() != null)
+      model.setLongDescription(message.getLongDescription());
+
     model.setPerformance(message.getPerformance());
-    if(message.getPerformanceUnit() != null) model.setPerformanceUnit(message.getPerformanceUnit());
+    if (message.getPerformanceUnit() != null)
+      model.setPerformanceUnit(message.getPerformanceUnit());
     model.setParameterCount(message.getParameterCount());
 
     if (message.getTags() != null) {
@@ -151,16 +161,25 @@ public class ModelManager implements IModelManager {
   @Override
   public SearchResult findModel(String name, int page, int size, String sort, Collection<String> tagsName) {
     Pageable paging;
-    
+
     // Tri dans le bon sens selon l'attribut
     switch (sort) {
-      case "votes": paging = PageRequest.of(page - 1, size, Sort.by(sort).descending()); break;
-      case "lastModified": paging = PageRequest.of(page - 1, size, Sort.by(sort).descending()); break;
-      case "performance": paging = PageRequest.of(page - 1, size, Sort.by("performanceIndex")); break;
-      case "parameterCount": paging = PageRequest.of(page - 1, size, Sort.by(sort)); break;
-      default: paging = PageRequest.of(page - 1, size, Sort.by("name"));
+      case "votes":
+        paging = PageRequest.of(page - 1, size, Sort.by(sort).descending());
+        break;
+      case "lastModified":
+        paging = PageRequest.of(page - 1, size, Sort.by(sort).descending());
+        break;
+      case "performance":
+        paging = PageRequest.of(page - 1, size, Sort.by("performanceIndex"));
+        break;
+      case "parameterCount":
+        paging = PageRequest.of(page - 1, size, Sort.by(sort));
+        break;
+      default:
+        paging = PageRequest.of(page - 1, size, Sort.by("name"));
     }
-    
+
     Page<Model> resultPage;
     if (tagsName == null) {
       resultPage = modelRepo.findByNameContainingIgnoreCase(name, paging);
@@ -168,7 +187,7 @@ public class ModelManager implements IModelManager {
       Collection<Tag> tags = tagRepo.findByNameIn(tagsName);
       resultPage = modelRepo.findDistinctByNameContainingIgnoreCaseAndTagsIn(name, tags, paging);
     }
-    
+
     SearchResult result = new SearchResult();
     if (resultPage.hasContent()) {
       result.setTotalResult((int) resultPage.getTotalElements());
@@ -182,16 +201,28 @@ public class ModelManager implements IModelManager {
     }
     return result;
   }
-  
+
   @Override
   public Model getModel(long id) {
     Optional<Model> model = modelRepo.findById(id);
-    if (model.isPresent()) return model.get();
-    else return null;
+    if (model.isPresent())
+      return model.get();
+    else
+      return null;
   }
 
   @Override
   public Collection<Model> getModels() {
     return modelRepo.findAll();
   }
+
+  @Override
+  public InputStream getModelFile(long id) throws Exception {
+    Model model = getModel(id);
+
+    if (model == null)
+      throw new Exception("Unknown model");
+
+    return modelStore.getContent(model);
+  }
 }
\ No newline at end of file
diff --git a/mozen/src/main/java/mozen/model/Model.java b/mozen/src/main/java/mozen/model/Model.java
index 077b0ba..1be7cc0 100644
--- a/mozen/src/main/java/mozen/model/Model.java
+++ b/mozen/src/main/java/mozen/model/Model.java
@@ -13,7 +13,6 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinTable;
-import javax.persistence.Lob;
 import javax.persistence.ManyToMany;
 import javax.persistence.ManyToOne;
 import javax.persistence.OneToMany;
@@ -25,6 +24,10 @@ import javax.validation.constraints.Size;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
 
+import org.springframework.content.commons.annotations.ContentId;
+import org.springframework.content.commons.annotations.ContentLength;
+import org.springframework.content.commons.annotations.MimeType;
+
 @Entity
 @Table
 public class Model implements Serializable{
@@ -80,18 +83,19 @@ public class Model implements Serializable{
 	@Column
   private int parameterCount;
 
-  @Lob
-	@Column(columnDefinition="BLOB")
-  private byte[] file;
-
-  @Basic
-	@Column
-  private String fileType;
-
   @Basic
 	@Column
   private String fileName;
 
+  @ContentId
+  private String contentId;
+
+  @ContentLength
+  private long contentLength;
+
+  @MimeType
+  private String fileType;
+
   @Basic
 	@Column
   private String checksum;
@@ -121,7 +125,7 @@ public class Model implements Serializable{
   public Model() {
   }
 
-  public Model(Long id, String name, String shortDescription, String longDescription, Date added, Date lastModified, int votes, double performance, String performanceUnit, double performanceIndex, int parameterCount, byte[] file, String fileType, String fileName, String checksum, boolean isVerified, User author, Set<Tag> tags, Set<CustomLayer> customLayers, Set<Comment> comments) {
+  public Model(Long id, String name, String shortDescription, String longDescription, Date added, Date lastModified, int votes, double performance, String performanceUnit, double performanceIndex, int parameterCount, String fileName, String contentId, long contentLength, String fileType, String checksum, boolean isVerified, User author, Set<Tag> tags, Set<CustomLayer> customLayers, Set<Comment> comments) {
     this.id = id;
     this.name = name;
     this.shortDescription = shortDescription;
@@ -133,9 +137,10 @@ public class Model implements Serializable{
     this.performanceUnit = performanceUnit;
     this.performanceIndex = performanceIndex;
     this.parameterCount = parameterCount;
-    this.file = file;
-    this.fileType = fileType;
     this.fileName = fileName;
+    this.contentId = contentId;
+    this.contentLength = contentLength;
+    this.fileType = fileType;
     this.checksum = checksum;
     this.isVerified = isVerified;
     this.author = author;
@@ -210,12 +215,21 @@ public class Model implements Serializable{
   }
 
   @JsonIgnore
-  public byte[] getFile() {
-    return this.file;
+  public String getContentId() {
+    return this.contentId;
+  }
+
+  public void setContentId(String contentId) {
+    this.contentId = contentId;
+  }
+
+  @JsonIgnore
+  public long getContentLength() {
+    return this.contentLength;
   }
 
-  public void setFile(byte[] file) {
-    this.file = file;
+  public void setContentLength(long contentLength) {
+    this.contentLength = contentLength;
   }
 
   public User getAuthor() {
diff --git a/mozen/src/main/java/mozen/repos/ModelStore.java b/mozen/src/main/java/mozen/repos/ModelStore.java
new file mode 100644
index 0000000..e660a90
--- /dev/null
+++ b/mozen/src/main/java/mozen/repos/ModelStore.java
@@ -0,0 +1,9 @@
+package mozen.repos;
+
+import org.springframework.content.commons.repository.ContentStore;
+
+import mozen.model.Model;
+
+public interface ModelStore extends ContentStore<Model, String>{
+  
+}
\ No newline at end of file
diff --git a/mozen/src/main/java/mozen/web/ModelController.java b/mozen/src/main/java/mozen/web/ModelController.java
index 69e8522..559bf7e 100644
--- a/mozen/src/main/java/mozen/web/ModelController.java
+++ b/mozen/src/main/java/mozen/web/ModelController.java
@@ -5,7 +5,7 @@ import java.util.Collection;
 import javax.validation.Valid;
 
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.InputStreamResource;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
@@ -166,7 +166,7 @@ public class ModelController {
       return ResponseEntity.ok()
         .contentType(MediaType.parseMediaType(model.getFileType()))
         .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + model.getName() + "\"")
-        .body(new ByteArrayResource(model.getFile()));
+        .body(new InputStreamResource(modelManager.getModelFile(id)));
     } catch (Exception e) {
       response.setError(true);
       response.setMessage(e.getMessage());
diff --git a/mozen/src/main/resources/application.properties b/mozen/src/main/resources/application.properties
index a848592..4d10c89 100644
--- a/mozen/src/main/resources/application.properties
+++ b/mozen/src/main/resources/application.properties
@@ -22,11 +22,11 @@ server.port=8181
 #spring.jpa.hibernate.ddl-auto=update
 
 # DOCKER + MARIADB
-#spring.datasource.url=jdbc:mariadb://mozen_mariadb:3306/MOZEN_DB
-#spring.datasource.username=mozen_user
-#spring.datasource.password=1234
-#spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
-#spring.jpa.hibernate.ddl-auto=update
+spring.datasource.url=jdbc:mariadb://mozen_mariadb:3306/MOZEN_DB
+spring.datasource.username=mozen_user
+spring.datasource.password=1234
+spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
+spring.jpa.hibernate.ddl-auto=update
 
 spring.servlet.multipart.max-file-size=10GB
 spring.servlet.multipart.max-request-size=10GB
-- 
GitLab