Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/annaojdowska/kiohub
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskier07 committed Dec 1, 2018
2 parents c9d3a9d + c49058f commit e0e15b7
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 188 deletions.
2 changes: 1 addition & 1 deletion kiohub-client/src/app/services/attachment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class AttachmentService {

getPhotoAttachment(id: number) {
const params = new HttpParams().set('id', id.toString());
return this.http.get(address + '/attachment/downloadPhoto', { responseType: 'blob', params: params });
return this.http.get(address + '/attachment/download', { responseType: 'blob', params: params });
}

getAttachment(id: number) {
Expand Down
246 changes: 132 additions & 114 deletions kiohub/src/main/java/pg/eti/kiohub/controller/AttachmentControler.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.stereotype.Controller;
import pg.eti.kiohub.entity.repository.AttachmentFileRepository;
import pg.eti.kiohub.entity.repository.AttachmentRepository;
import pg.eti.kiohub.entity.repository.LicenceRepository;
import pg.eti.kiohub.entity.repository.NoteRepository;
Expand Down Expand Up @@ -65,9 +64,6 @@ public class MainController {
@Autowired
protected AttachmentRepository attachmentRepository;

@Autowired
protected AttachmentFileRepository attachmentFileRepository;

@Autowired
protected UserEmailRepository userEmailRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package pg.eti.kiohub.controller;


import java.io.IOException;
import lombok.extern.jbosslog.JBossLog;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -28,6 +29,9 @@
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import pg.eti.kiohub.entity.model.Attachment;

@JBossLog
@CrossOrigin
Expand Down Expand Up @@ -128,9 +132,15 @@ public ResponseEntity setRelatedProjects(@PathVariable("id") Long id,
public ResponseEntity delete(@PathVariable("id") Long id, HttpServletRequest request) {
Optional<Project> projectToDelete = this.projectRepository.findById(id);
if (projectToDelete.isPresent()) {
projectToDelete.get().getAttachments().forEach((att) -> {
this.attachmentFileRepository.deleteById(att.getId());
});
for (Attachment attachment : projectToDelete.get().getAttachments()) {
try {
this.attachmentService.remove(attachment);
} catch (IOException ex) {
Logger.getLogger(ProjectController.class.getName()).log(Level.SEVERE, null, ex);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

};
this.collaboratorsRepository.deleteAllCollaborators(id);
this.userPinnedProjectRepository.deleteAllPinnedProject(id);
this.projectRepository.deleteAllRelatedProjects(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ public class Attachment {

@Column(name = "is_main_photo")
private Boolean mainPhoto;

public String getFullPath() {
return fileLocation + id.toString();
}
}

This file was deleted.

This file was deleted.

39 changes: 19 additions & 20 deletions kiohub/src/main/java/pg/eti/kiohub/service/AttachmentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pg.eti.kiohub.entity.model.Attachment;
import pg.eti.kiohub.entity.model.AttachmentFile;
import pg.eti.kiohub.entity.repository.AttachmentRepository;
import pg.eti.kiohub.utils.FileUtils;

Expand All @@ -31,30 +30,15 @@ public class AttachmentService {
@Autowired
AttachmentRepository attachmentFileRepository;

public void prepareAndSaveAttachment(Optional<Attachment> attachmentOpt,
Optional<AttachmentFile> attachmentFileOpt,
HttpServletResponse response) throws SQLException, IOException {
Attachment attachment = attachmentOpt.get();
AttachmentFile attachmentFile = attachmentFileOpt.get();

String name = attachment.getFileName();
String extension = FilenameUtils.getExtension(name);
Blob blob = attachmentFile.getFile();

InputStream in = blob.getBinaryStream();
response.setContentType(FileUtils.getMimeType(extension));
IOUtils.copy(in, response.getOutputStream());
}

public boolean attachmentExists(Optional<AttachmentFile> attachmentFile, Optional<Attachment> attachment) {
return attachmentFile.isPresent() && attachment.isPresent();
public boolean attachmentExists(Optional<Attachment> attachment) {
Attachment att = attachment.get();
return attachment.isPresent() && new File(att.getFullPath()).exists();
}

public void rollbackSaveAttachment(Attachment attachment) {
if (attachment != null) {
if (attachment.getId() != null) {
attachmentRepository.deleteById(attachment.getId());
attachmentFileRepository.deleteById(attachment.getId());
}
}
}
Expand Down Expand Up @@ -85,7 +69,7 @@ public boolean saveAttachmentToDisk(InputStream inputStream, String filepath) th
}

public InputStream getAttachmentFromDisk(Attachment attachment) throws FileNotFoundException {
String path = "/home/attachments/wyjscie.txt"; // attachment.getFilePath();
String path = attachment.getFullPath();
String name = attachment.getFileName();
String extension = FilenameUtils.getExtension(name);

Expand All @@ -95,4 +79,19 @@ public InputStream getAttachmentFromDisk(Attachment attachment) throws FileNotFo
InputStream inputStream = new FileInputStream(fileToDownload);
return inputStream;
}

private void deleteAttachmentFromDisk(Attachment attachment) throws IOException {
File file = new File(attachment.getFullPath());
if (!file.delete()) {
throw new IOException();
}
}

public void remove(Attachment attachment) throws IOException {
deleteAttachmentFromDisk(attachment);
attachmentRepository.deleteById(attachment.getId());
}



}

0 comments on commit e0e15b7

Please sign in to comment.