Search in sources :

Example 1 with Avatar

use of org.libresonic.player.domain.Avatar in project libresonic by Libresonic.

the class AvatarUploadController method createAvatar.

private void createAvatar(String fileName, byte[] data, String username, Map<String, Object> map) throws IOException {
    BufferedImage image;
    try {
        image = ImageIO.read(new ByteArrayInputStream(data));
        if (image == null) {
            throw new Exception("Failed to decode incoming image: " + fileName + " (" + data.length + " bytes).");
        }
        int width = image.getWidth();
        int height = image.getHeight();
        String mimeType = StringUtil.getMimeType(FilenameUtils.getExtension(fileName));
        // Scale down image if necessary.
        if (width > MAX_AVATAR_SIZE || height > MAX_AVATAR_SIZE) {
            double scaleFactor = (double) MAX_AVATAR_SIZE / (double) Math.max(width, height);
            height = (int) (height * scaleFactor);
            width = (int) (width * scaleFactor);
            image = CoverArtController.scale(image, width, height);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "jpeg", out);
            data = out.toByteArray();
            mimeType = StringUtil.getMimeType("jpeg");
            map.put("resized", true);
        }
        Avatar avatar = new Avatar(0, fileName, new Date(), mimeType, width, height, data);
        settingsService.setCustomAvatar(avatar, username);
        LOG.info("Created avatar '" + fileName + "' (" + data.length + " bytes) for user " + username);
    } catch (Exception x) {
        LOG.warn("Failed to upload personal image: " + x, x);
        map.put("error", x);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Avatar(org.libresonic.player.domain.Avatar) Date(java.util.Date)

Example 2 with Avatar

use of org.libresonic.player.domain.Avatar in project libresonic by Libresonic.

the class AvatarController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Avatar avatar = getAvatar(request);
    if (avatar == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return null;
    }
    response.setContentType(avatar.getMimeType());
    response.getOutputStream().write(avatar.getData());
    return null;
}
Also used : Avatar(org.libresonic.player.domain.Avatar) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Avatar

use of org.libresonic.player.domain.Avatar in project libresonic by Libresonic.

the class AvatarController method getLastModified.

public long getLastModified(HttpServletRequest request) {
    Avatar avatar = getAvatar(request);
    long result = avatar == null ? -1L : avatar.getCreatedDate().getTime();
    String username = request.getParameter("username");
    if (username != null) {
        UserSettings userSettings = settingsService.getUserSettings(username);
        result = Math.max(result, userSettings.getChanged().getTime());
    }
    return result;
}
Also used : UserSettings(org.libresonic.player.domain.UserSettings) Avatar(org.libresonic.player.domain.Avatar)

Aggregations

Avatar (org.libresonic.player.domain.Avatar)3 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Date (java.util.Date)1 UserSettings (org.libresonic.player.domain.UserSettings)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1