Search in sources :

Example 1 with MonitoredDiskFileItemFactory

use of org.libresonic.player.upload.MonitoredDiskFileItemFactory 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)

Aggregations

File (java.io.File)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 TransferStatus (org.libresonic.player.domain.TransferStatus)1 User (org.libresonic.player.domain.User)1 MonitoredDiskFileItemFactory (org.libresonic.player.upload.MonitoredDiskFileItemFactory)1 UploadListener (org.libresonic.player.upload.UploadListener)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1