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

Fixed model & layer download / upload

parent 83eec8d3
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,4 @@ public interface IUserManager {
User getUserByUsername(String username);
void setAdmin(Long id, User user) throws Exception;
boolean resetPassword(String email);
boolean changePassword(String token, String password);
}
\ No newline at end of file
package mozen.business;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
......@@ -37,13 +34,11 @@ public class LayerManager implements ILayerManager{
if (!UserHelper.isAuthor(model.getAuthor(), user))
throw new Exception("Not the author");
Checksum crc32 = new CRC32();
crc32.update(file.getBytes(), 0, file.getBytes().length);
CustomLayer layer = new CustomLayer();
layer.setName(name);
layer.setChecksum(Md5Utils.getMD5(file));
layer.setFile(file.getBytes());
layer.setFileName(file.getOriginalFilename());
layer.setFileType(file.getContentType());
layer.setModel(model);
......
......@@ -86,6 +86,7 @@ public class ModelManager implements IModelManager {
model.setChecksum(Md5Utils.getMD5(file));
model.setFile(file.getBytes());
model.setFileName(file.getOriginalFilename());
model.setFileType(file.getContentType());
modelRepo.save(model);
......
......@@ -54,18 +54,6 @@ public class UserManager implements IUserManager {
else return null;
}
@Override
public boolean resetPassword(String email) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean changePassword(String token, String password) {
// TODO Auto-generated method stub
return false;
}
@Override
public User getUserByUsername(String username) {
return repo.findByUsername(username);
......
......@@ -33,6 +33,10 @@ public class CustomLayer implements Serializable{
@Column(columnDefinition="BLOB")
private byte[] file;
@Basic
@Column
private String fileName;
@Basic
@Column
private String fileType;
......@@ -48,10 +52,11 @@ public class CustomLayer implements Serializable{
public CustomLayer() {
}
public CustomLayer(Long id, String name, byte[] file, String fileType, String checksum, Model model) {
public CustomLayer(Long id, String name, byte[] file, String fileName, String fileType, String checksum, Model model) {
this.id = id;
this.name = name;
this.file = file;
this.fileName = fileName;
this.fileType = fileType;
this.checksum = checksum;
this.model = model;
......@@ -108,4 +113,13 @@ public class CustomLayer implements Serializable{
this.checksum = checksum;
}
public String getFileName() {
return this.fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
\ No newline at end of file
......@@ -88,6 +88,10 @@ public class Model implements Serializable{
@Column
private String fileType;
@Basic
@Column
private String fileName;
@Basic
@Column
private String checksum;
......@@ -117,7 +121,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 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, byte[] file, String fileType, String fileName, String checksum, boolean isVerified, User author, Set<Tag> tags, Set<CustomLayer> customLayers, Set<Comment> comments) {
this.id = id;
this.name = name;
this.shortDescription = shortDescription;
......@@ -131,6 +135,7 @@ public class Model implements Serializable{
this.parameterCount = parameterCount;
this.file = file;
this.fileType = fileType;
this.fileName = fileName;
this.checksum = checksum;
this.isVerified = isVerified;
this.author = author;
......@@ -298,4 +303,13 @@ public class Model implements Serializable{
this.isVerified = isVerified;
}
public String getFileName() {
return this.fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
\ No newline at end of file
......@@ -5,9 +5,6 @@ import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -19,6 +16,7 @@ import mozen.business.IUserManager;
import mozen.model.CustomLayer;
import mozen.messages.ResponseMessage;
import mozen.model.User;
import mozen.utils.UserHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -46,9 +44,8 @@ public class CustomLayerController {
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
@PostMapping("/upload")
......@@ -57,33 +54,21 @@ public class CustomLayerController {
@RequestParam(value = "id", required = true) Long id,
@RequestParam(value = "name", required = true) String name) {
ResponseMessage response = new ResponseMessage(false, "");
User user = getCurrentUser();
User user = UserHelper.getCurrentUser(userManager);
if(user == null) {
response.setError(true);
response.setMessage("User unknown");
return ResponseEntity.ok(response);
return ResponseEntity.badRequest().body(response);
}
try {
System.err.println("[MODEL CONTROLLER] layer file upload f:"+file.getName()+" n:"+name);
layerManager.addLayerFile(file, id, name, user);
return ResponseEntity.ok(response);
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
}
return ResponseEntity.ok(response);
}
private User getCurrentUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof AnonymousAuthenticationToken)) {
if (auth.getPrincipal() instanceof org.springframework.security.core.userdetails.User) return null;
String username = (String) auth.getPrincipal();
return userManager.getUserByUsername(username);
} else {
return null;
return ResponseEntity.badRequest().body(response);
}
}
}
\ No newline at end of file
......@@ -63,13 +63,12 @@ public class ModelController {
try {
long modelId = modelManager.addModel(message, user).getId();
response.setMessage(Long.toString(modelId));
return ResponseEntity.ok(response);
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
@PutMapping("")
......@@ -84,13 +83,12 @@ public class ModelController {
try {
modelManager.updateModel(message, id, user);
return ResponseEntity.ok(response);
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
@DeleteMapping("")
......@@ -105,13 +103,12 @@ public class ModelController {
try {
modelManager.removeModel(id, user);
return ResponseEntity.ok(response);
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
@GetMapping("/setVerified")
......@@ -126,13 +123,12 @@ public class ModelController {
try {
modelManager.setVerified(id, user);
return ResponseEntity.ok(response);
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
@GetMapping("/list")
......@@ -153,13 +149,12 @@ public class ModelController {
try {
System.err.println("[MODEL CONTROLLER] model file upload n:"+file.getName()+" id:"+id);
modelManager.addModelFile(file, id, user);
return ResponseEntity.ok(response);
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
@GetMapping("/download")
......
......@@ -15,12 +15,7 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jackson.serialization.fail-on-empty-beans=false
spring.servlet.multipart.max-file-size=10GB
spring.servlet.multipart.max-request-size=10GB
spring.mail.host=ssl0.ovh.net
spring.mail.port=587
spring.mail.username=boobook@gltronic.ovh
spring.mail.password=123456789
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.test-connection=false
\ No newline at end of file
spring.jackson.serialization.fail-on-empty-beans=false
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment