Skip to content
Snippets Groups Projects
Commit 9b89833b authored by Thomas's avatar Thomas
Browse files

Added content store

parent 0d586967
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
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
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) {
......@@ -154,11 +164,20 @@ public class ModelManager implements IModelManager {
// 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;
......@@ -186,12 +205,24 @@ public class ModelManager implements IModelManager {
@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
......@@ -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() {
......
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
......@@ -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());
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment