Example #1
0
  // remove photo api
  @Override
  @DELETE
  @Path("/remove")
  @Produces(MediaType.APPLICATION_XML)
  @Transactional(propagation = Propagation.NESTED)
  public Response removePhoto(Integer photoId, @Context HttpServletRequest request) {

    try {

      Photo photo = photoDao.load(photoId);
      Token tokenObj = (Token) request.getAttribute("token");
      User user = tokenObj.getUser();
      if (user.equals(photo.getUser())) {

        new File(photo.getPath()).delete(); // delete the file
        photoDao.delete(photo);
      }
      GetPhotoResponseDTO dto = buildDTO(200, null);
      dto.setImageId(photoId);
      return Response.status(200).entity(dto).build();
    } catch (HibernateException e) {

      e.printStackTrace();
      return Response.status(500).entity(buildDTO(500, null)).build();
    }
  }
Example #2
0
  private String getPath(Photo photo) throws IOException {

    InputStream is = new FileInputStream(servletContext.getRealPath("/WEB_INF/config.properties"));
    Properties properties = new Properties();
    properties.load(is);
    String rootPath = properties.getProperty("path");
    is.close();
    return rootPath + photo.getPath();
  }