Search in sources :

Example 31 with User

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

the class TopController method handleRequestInternal.

@RequestMapping(method = RequestMethod.GET)
protected ModelAndView handleRequestInternal(HttpServletRequest request) throws Exception {
    Map<String, Object> map = new HashMap<>();
    User user = securityService.getCurrentUser(request);
    UserSettings userSettings = settingsService.getUserSettings(user.getUsername());
    map.put("user", user);
    map.put("showSideBar", userSettings.isShowSideBar());
    map.put("showAvatar", userSettings.getAvatarScheme() != AvatarScheme.NONE);
    return new ModelAndView("top", "model", map);
}
Also used : User(org.libresonic.player.domain.User) HashMap(java.util.HashMap) UserSettings(org.libresonic.player.domain.UserSettings) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 32 with User

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

the class UploadController method handleRequestInternal.

@RequestMapping(method = { RequestMethod.POST })
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map<String, Object> map = new HashMap<>();
    List<File> uploadedFiles = new ArrayList<>();
    List<File> unzippedFiles = new ArrayList<>();
    TransferStatus status = null;
    try {
        status = statusService.createUploadStatus(playerService.getPlayer(request, response, false, false));
        status.setBytesTotal(request.getContentLength());
        request.getSession().setAttribute(UPLOAD_STATUS, status);
        // Check that we have a file upload request
        if (!ServletFileUpload.isMultipartContent(request)) {
            throw new Exception("Illegal request.");
        }
        File dir = null;
        boolean unzip = false;
        UploadListener listener = new UploadListenerImpl(status);
        FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<?> items = upload.parseRequest(request);
        // First, look for "dir" and "unzip" parameters.
        for (Object o : items) {
            FileItem item = (FileItem) o;
            if (item.isFormField() && "dir".equals(item.getFieldName())) {
                dir = new File(item.getString());
            } else if (item.isFormField() && "unzip".equals(item.getFieldName())) {
                unzip = true;
            }
        }
        if (dir == null) {
            throw new Exception("Missing 'dir' parameter.");
        }
        // Look for file items.
        for (Object o : items) {
            FileItem item = (FileItem) o;
            if (!item.isFormField()) {
                String fileName = item.getName();
                if (fileName.trim().length() > 0) {
                    File targetFile = new File(dir, new File(fileName).getName());
                    if (!securityService.isUploadAllowed(targetFile)) {
                        throw new Exception("Permission denied: " + StringUtil.toHtml(targetFile.getPath()));
                    }
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    item.write(targetFile);
                    uploadedFiles.add(targetFile);
                    LOG.info("Uploaded " + targetFile);
                    if (unzip && targetFile.getName().toLowerCase().endsWith(".zip")) {
                        unzip(targetFile, unzippedFiles);
                    }
                }
            }
        }
    } catch (Exception x) {
        LOG.warn("Uploading failed.", x);
        map.put("exception", x);
    } finally {
        if (status != null) {
            statusService.removeUploadStatus(status);
            request.getSession().removeAttribute(UPLOAD_STATUS);
            User user = securityService.getCurrentUser(request);
            securityService.updateUserByteCounts(user, 0L, 0L, status.getBytesTransfered());
        }
    }
    map.put("uploadedFiles", uploadedFiles);
    map.put("unzippedFiles", unzippedFiles);
    return new ModelAndView("upload", "model", map);
}
Also used : User(org.libresonic.player.domain.User) ModelAndView(org.springframework.web.servlet.ModelAndView) UploadListener(org.libresonic.player.upload.UploadListener) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) MonitoredDiskFileItemFactory(org.libresonic.player.upload.MonitoredDiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) TransferStatus(org.libresonic.player.domain.TransferStatus) MonitoredDiskFileItemFactory(org.libresonic.player.upload.MonitoredDiskFileItemFactory) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 33 with User

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

the class UserChartController method createDataset.

private CategoryDataset createDataset(String type) {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    List<User> users = securityService.getAllUsers();
    for (User user : users) {
        double value;
        if ("stream".equals(type)) {
            value = user.getBytesStreamed();
        } else if ("download".equals(type)) {
            value = user.getBytesDownloaded();
        } else if ("upload".equals(type)) {
            value = user.getBytesUploaded();
        } else if ("total".equals(type)) {
            value = user.getBytesStreamed() + user.getBytesDownloaded() + user.getBytesUploaded();
        } else {
            throw new RuntimeException("Illegal chart type: " + type);
        }
        value /= BYTES_PER_MB;
        dataset.addValue(value, "Series", user.getUsername());
    }
    return dataset;
}
Also used : User(org.libresonic.player.domain.User) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset)

Example 34 with User

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

the class UserSettingsController method displayForm.

@RequestMapping(method = RequestMethod.GET)
protected String displayForm(HttpServletRequest request, Model model) throws Exception {
    UserSettingsCommand command;
    if (!model.containsAttribute("command")) {
        command = new UserSettingsCommand();
        User user = getUser(request);
        if (user != null) {
            command.setUser(user);
            command.setEmail(user.getEmail());
            UserSettings userSettings = settingsService.getUserSettings(user.getUsername());
            command.setTranscodeSchemeName(userSettings.getTranscodeScheme().name());
            command.setAllowedMusicFolderIds(Util.toIntArray(getAllowedMusicFolderIds(user)));
        } else {
            command.setNewUser(true);
            command.setStreamRole(true);
            command.setSettingsRole(true);
        }
    } else {
        command = (UserSettingsCommand) model.asMap().get("command");
    }
    command.setAdmin(User.USERNAME_ADMIN.equals(command.getUsername()));
    command.setUsers(securityService.getAllUsers());
    command.setTranscodingSupported(transcodingService.isDownsamplingSupported(null));
    command.setTranscodeDirectory(transcodingService.getTranscodeDirectory().getPath());
    command.setTranscodeSchemes(TranscodeScheme.values());
    command.setLdapEnabled(settingsService.isLdapEnabled());
    command.setAllMusicFolders(settingsService.getAllMusicFolders());
    model.addAttribute("command", command);
    return "userSettings";
}
Also used : User(org.libresonic.player.domain.User) UserSettings(org.libresonic.player.domain.UserSettings) UserSettingsCommand(org.libresonic.player.command.UserSettingsCommand) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with User

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

the class RESTController method stream.

@RequestMapping(value = "/rest/stream", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView stream(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request = wrapRequest(request);
    User user = securityService.getCurrentUser(request);
    if (!user.isStreamRole()) {
        error(request, response, ErrorCode.NOT_AUTHORIZED, user.getUsername() + " is not authorized to play files.");
        return null;
    }
    streamController.handleRequest(request, response);
    return null;
}
Also used : User(org.libresonic.player.domain.User) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (org.libresonic.player.domain.User)52 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)33 ModelAndView (org.springframework.web.servlet.ModelAndView)10 HashMap (java.util.HashMap)8 Test (org.junit.Test)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 Share (org.libresonic.player.domain.Share)6 UserSettings (org.libresonic.player.domain.UserSettings)6 MusicFolder (org.libresonic.player.domain.MusicFolder)5 Player (org.libresonic.player.domain.Player)4 UserSettingsCommand (org.libresonic.player.command.UserSettingsCommand)3 Playlist (org.libresonic.player.domain.Playlist)3 org.libresonic.restapi (org.libresonic.restapi)3 RedirectView (org.springframework.web.servlet.view.RedirectView)3 File (java.io.File)2 Date (java.util.Date)2 MediaFile (org.libresonic.player.domain.MediaFile)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 LinkedHashMap (java.util.LinkedHashMap)1 ReCaptcha (net.tanesha.recaptcha.ReCaptcha)1