Search in sources :

Example 6 with TransferStatus

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

the class StatusServiceTestCase method testSimpleAddRemove.

public void testSimpleAddRemove() {
    TransferStatus status = service.createStreamStatus(player1);
    assertTrue("Wrong status.", status.isActive());
    assertEquals("Wrong list of statuses.", Arrays.asList(status), service.getAllStreamStatuses());
    assertEquals("Wrong list of statuses.", Arrays.asList(status), service.getStreamStatusesForPlayer(player1));
    service.removeStreamStatus(status);
    assertFalse("Wrong status.", status.isActive());
    assertEquals("Wrong list of statuses.", Arrays.asList(status), service.getAllStreamStatuses());
    assertEquals("Wrong list of statuses.", Arrays.asList(status), service.getStreamStatusesForPlayer(player1));
}
Also used : TransferStatus(org.libresonic.player.domain.TransferStatus)

Example 7 with TransferStatus

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

the class StatusServiceTestCase method testMultipleStreamsSamePlayer.

public void testMultipleStreamsSamePlayer() {
    TransferStatus statusA = service.createStreamStatus(player1);
    TransferStatus statusB = service.createStreamStatus(player1);
    assertEquals("Wrong list of statuses.", Arrays.asList(statusA, statusB), service.getAllStreamStatuses());
    assertEquals("Wrong list of statuses.", Arrays.asList(statusA, statusB), service.getStreamStatusesForPlayer(player1));
    // Stop stream A.
    service.removeStreamStatus(statusA);
    assertFalse("Wrong status.", statusA.isActive());
    assertTrue("Wrong status.", statusB.isActive());
    assertEquals("Wrong list of statuses.", Arrays.asList(statusB), service.getAllStreamStatuses());
    assertEquals("Wrong list of statuses.", Arrays.asList(statusB), service.getStreamStatusesForPlayer(player1));
    // Stop stream B.
    service.removeStreamStatus(statusB);
    assertFalse("Wrong status.", statusB.isActive());
    assertEquals("Wrong list of statuses.", Arrays.asList(statusB), service.getAllStreamStatuses());
    assertEquals("Wrong list of statuses.", Arrays.asList(statusB), service.getStreamStatusesForPlayer(player1));
    // Start stream C.
    TransferStatus statusC = service.createStreamStatus(player1);
    assertTrue("Wrong status.", statusC.isActive());
    assertEquals("Wrong list of statuses.", Arrays.asList(statusC), service.getAllStreamStatuses());
    assertEquals("Wrong list of statuses.", Arrays.asList(statusC), service.getStreamStatusesForPlayer(player1));
}
Also used : TransferStatus(org.libresonic.player.domain.TransferStatus)

Example 8 with TransferStatus

use of org.libresonic.player.domain.TransferStatus 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 9 with TransferStatus

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

the class NowPlayingController method handleRequestInternal.

@RequestMapping(method = RequestMethod.GET)
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Player player = playerService.getPlayer(request, response);
    List<TransferStatus> statuses = statusService.getStreamStatusesForPlayer(player);
    MediaFile current = statuses.isEmpty() ? null : mediaFileService.getMediaFile(statuses.get(0).getFile());
    MediaFile dir = current == null ? null : mediaFileService.getParentOf(current);
    String url;
    if (dir != null && !mediaFileService.isRoot(dir)) {
        url = "main.view?id=" + dir.getId();
    } else {
        url = "home.view";
    }
    return new ModelAndView(new RedirectView(url));
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) Player(org.libresonic.player.domain.Player) TransferStatus(org.libresonic.player.domain.TransferStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

TransferStatus (org.libresonic.player.domain.TransferStatus)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 File (java.io.File)2 MediaFile (org.libresonic.player.domain.MediaFile)2 Player (org.libresonic.player.domain.Player)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 Date (java.util.Date)1 HttpSession (javax.servlet.http.HttpSession)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 ZipFile (org.apache.tools.zip.ZipFile)1 JFreeChart (org.jfree.chart.JFreeChart)1 ValueAxis (org.jfree.chart.axis.ValueAxis)1 XYPlot (org.jfree.chart.plot.XYPlot)1 XYItemRenderer (org.jfree.chart.renderer.xy.XYItemRenderer)1 Range (org.jfree.data.Range)1 PlayStatus (org.libresonic.player.domain.PlayStatus)1 User (org.libresonic.player.domain.User)1 MonitoredDiskFileItemFactory (org.libresonic.player.upload.MonitoredDiskFileItemFactory)1