Esempio n. 1
0
  private HttpEntity<LoginStatus> entity(LoginStatus status) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Access-Control-Allow-Origin", siteConfig.getMainUrlWithoutSlash());
    headers.add("Access-Control-Allow-Credentials", "true");

    return new HttpEntity<>(status, headers);
  }
Esempio n. 2
0
  @RequestMapping(value = "/addphoto.jsp", method = RequestMethod.POST)
  @PreAuthorize("hasRole('ROLE_ANONYMOUS')")
  public ModelAndView addPhoto(
      @RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {

    if (file == null || file.isEmpty()) {
      return new ModelAndView("addphoto", "error", "изображение не задано");
    }

    try {
      File uploadedFile =
          File.createTempFile(
              "userpic", "", new File(siteConfig.getPathPrefix() + "/linux-storage/tmp/"));

      file.transferTo(uploadedFile);

      ImageParam param = userService.checkUserPic(uploadedFile);
      String extension = param.getExtension();

      Random random = new Random();

      String photoname;
      File photofile;

      do {
        photoname =
            Integer.toString(AuthUtil.getCurrentUser().getId())
                + ':'
                + random.nextInt()
                + '.'
                + extension;
        photofile = new File(siteConfig.getHTMLPathPrefix() + "/photos", photoname);
      } while (photofile.exists());

      if (!uploadedFile.renameTo(photofile)) {
        logger.warn("Can't move photo to " + photofile);
        throw new ScriptErrorException("Can't move photo: internal error");
      }

      userDao.setPhoto(AuthUtil.getCurrentUser(), photoname);

      logger.info("Установлена фотография пользователем " + AuthUtil.getCurrentUser().getNick());

      return new ModelAndView(
          new RedirectView(
              UriComponentsBuilder.fromUri(
                      PROFILE_NOCACHE_URI_TEMPLATE.expand(AuthUtil.getCurrentUser().getNick()))
                  .queryParam("nocache", Integer.toString(random.nextInt()) + '=')
                  .build()
                  .encode()
                  .toString()));
    } catch (IOException ex) {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return new ModelAndView("addphoto", "error", ex.getMessage());
    } catch (BadImageException ex) {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return new ModelAndView("addphoto", "error", ex.getMessage());
    } catch (UserErrorException ex) {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return new ModelAndView("addphoto", "error", ex.getMessage());
    }
  }