@Secured(value = {"ROLE_USER", "ROLE_ADMIN", "ROLE_MANAGER"}) @RequestMapping(method = RequestMethod.GET, value = "profile-pic") public @ResponseBody FileSystemResource getFile( @RequestParam(required = false, value = "user") String userid, Principal principal, HttpSession session) throws IOException { if (documentFolder.exists()) { String uuid; if (userid != null) uuid = userid; else { PipUser user = PipUser.findPipUsersByEmailEquals(principal.getName()).getSingleResult(); uuid = user.getUuid(); } File folder = new File(documentFolder.getFile(), "user-data/" + uuid); File file; if (!uuid.isEmpty() && folder.exists() && folder.listFiles().length > 0) file = folder.listFiles()[0]; else { file = new ServletContextResource(session.getServletContext(), "/images/profile.jpg") .getFile(); } return new FileSystemResource(file); } return null; }
public Resource getResource(String resourceName) { String location = System.getProperty(PROP_KEY); if (StringUtils.isBlank(location)) { return new ClassPathResource(resourceName); } FileSystemResource resource = new FileSystemResource(location + SYSTEM_FILE_SEPARATOR + resourceName); return resource.exists() ? resource : null; }
/** * Closure that returns a Spring Resource - either from $GRAILS_HOME if that is set, or from the * classpath. */ public Resource grailsResource(String path) { if (grailsHome != null) { FileSystemResource resource = new FileSystemResource(grailsHome + "/" + path); if (!resource.exists()) { resource = new FileSystemResource(grailsHome + "/grails-resources/" + path); } return resource; } return new ClassPathResource(path); }
@Secured(value = {"ROLE_USER", "ROLE_ADMIN", "ROLE_MANAGER"}) @RequestMapping(method = RequestMethod.POST, value = "upload-profile-pic") public @ResponseBody ResponseEntity<ResponseObject> uploadProfilePic( @RequestParam("file") List<MultipartFile> files, Principal principal, @RequestParam(value = "userid", required = false) String userid) { if (documentFolder.exists()) { PipUser user; PipUser principalUser = PipUser.findPipUsersByEmailEquals(principal.getName()).getSingleResult(); if (userid != null) { user = PipUser.findPipUsersByUuidEquals(userid).getSingleResult(); if (!PipRole.ADMIN.getName().equals(principalUser.getRole()) && !principalUser.equals(user)) return new ResponseEntity<ResponseObject>(HttpStatus.FORBIDDEN); } else user = principalUser; File directory = new File(documentFolder.getPath() + "/user-data/" + user.getUuid()); directory.mkdirs(); for (File file : directory.listFiles()) { file.delete(); } for (MultipartFile multiPartfile : files) { File file = new File(directory, multiPartfile.getOriginalFilename()); try { multiPartfile.transferTo(file); } catch (IllegalStateException e) { e.printStackTrace(); return new ResponseEntity<ResponseObject>(HttpStatus.INTERNAL_SERVER_ERROR); } catch (IOException e) { e.printStackTrace(); return new ResponseEntity<ResponseObject>(HttpStatus.INTERNAL_SERVER_ERROR); } } return new ResponseEntity<ResponseObject>(HttpStatus.OK); } return new ResponseEntity<ResponseObject>(HttpStatus.INTERNAL_SERVER_ERROR); }