Skip to content
Snippets Groups Projects

Endre kontroller-type for filhåndtering

Merged Jakob Karevold Grønhaug requested to merge Filopplasting into main
2 files
+ 9
6
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -14,13 +14,14 @@ import org.springframework.http.HttpStatus;
@@ -14,13 +14,14 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestParam;
 
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@@ -34,7 +35,8 @@ import edu.ntnu.idatt210602.matsvinnbackend.repo.ProfileRepository;
@@ -34,7 +35,8 @@ import edu.ntnu.idatt210602.matsvinnbackend.repo.ProfileRepository;
import java.nio.file.Path;
import java.nio.file.Path;
@Controller
@RestController
 
@CrossOrigin
@RequestMapping(path = "/img")
@RequestMapping(path = "/img")
public class FileController {
public class FileController {
@@ -50,7 +52,7 @@ public class FileController {
@@ -50,7 +52,7 @@ public class FileController {
ProfileRepository profileRepo;
ProfileRepository profileRepo;
@PostMapping("")
@PostMapping("")
public ResponseEntity<String> uploadProfilePicture(@RequestParam("file") MultipartFile file,
public Profile uploadProfilePicture(@RequestParam("file") MultipartFile file,
@RequestParam("profileId") Integer profileId) {
@RequestParam("profileId") Integer profileId) {
String authenticatedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
String authenticatedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
Account loggedInAccount = accountRepo.findByEmail(authenticatedUsername).orElseThrow();
Account loggedInAccount = accountRepo.findByEmail(authenticatedUsername).orElseThrow();
@@ -90,10 +92,12 @@ public class FileController {
@@ -90,10 +92,12 @@ public class FileController {
.path(profileId.toString())
.path(profileId.toString())
.toUriString();
.toUriString();
return ResponseEntity.ok(fileDownloadUri);
profile.setProfileImageUrl(fileDownloadUri);
 
 
return profileRepo.save(profile);
}
}
@GetMapping("/{profileId:.+}")
@GetMapping("/{profileId}")
public ResponseEntity<Resource> get(@PathVariable Integer profileId) {
public ResponseEntity<Resource> get(@PathVariable Integer profileId) {
String authenticatedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
String authenticatedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
Account loggedInAccount = accountRepo.findByEmail(authenticatedUsername).orElseThrow();
Account loggedInAccount = accountRepo.findByEmail(authenticatedUsername).orElseThrow();
Loading