Search in sources :

Example 1 with ProgressInputStream

use of org.opencastproject.util.ProgressInputStream in project opencast by opencast.

the class IngestServiceImpl method addContentToRepo.

private URI addContentToRepo(MediaPackage mp, String elementId, String filename, InputStream file) throws IOException {
    ProgressInputStream progressInputStream = new ProgressInputStream(file);
    progressInputStream.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            long totalNumBytesRead = (Long) evt.getNewValue();
            long oldTotalNumBytesRead = (Long) evt.getOldValue();
            ingestStatistics.add(totalNumBytesRead - oldTotalNumBytesRead);
        }
    });
    return workingFileRepository.put(mp.getIdentifier().compact(), elementId, filename, progressInputStream);
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) ProgressInputStream(org.opencastproject.util.ProgressInputStream)

Example 2 with ProgressInputStream

use of org.opencastproject.util.ProgressInputStream in project opencast by opencast.

the class StaticFileServiceImpl method storeFile.

@Override
public String storeFile(String filename, InputStream inputStream) throws IOException {
    notNull(filename, "filename");
    notNull(inputStream, "inputStream");
    final String uuid = UUID.randomUUID().toString();
    final String org = securityService.getOrganization().getId();
    Path file = getTemporaryStorageDir(org).resolve(Paths.get(uuid, filename));
    try (ProgressInputStream progressInputStream = new ProgressInputStream(inputStream)) {
        progressInputStream.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                long totalNumBytesRead = (Long) evt.getNewValue();
                long oldTotalNumBytesRead = (Long) evt.getOldValue();
                staticFileStatistics.add(totalNumBytesRead - oldTotalNumBytesRead);
            }
        });
        Files.createDirectories(file.getParent());
        Files.copy(progressInputStream, file);
    } catch (IOException e) {
        logger.error("Unable to save file '{}' to {} because: {}", filename, file, ExceptionUtils.getStackTrace(e));
        throw e;
    }
    return uuid;
}
Also used : Path(java.nio.file.Path) PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) ProgressInputStream(org.opencastproject.util.ProgressInputStream) IOException(java.io.IOException)

Example 3 with ProgressInputStream

use of org.opencastproject.util.ProgressInputStream in project opencast by opencast.

the class StaticFileRestService method postStaticFile.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path("")
@RestQuery(name = "postStaticFile", description = "Post a new static resource", bodyParameter = @RestParameter(description = "The static resource file", isRequired = true, name = "BODY", type = RestParameter.Type.FILE), reponses = { @RestResponse(description = "Returns the id of the uploaded static resource", responseCode = HttpServletResponse.SC_CREATED), @RestResponse(description = "No filename or file to upload found", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "The upload size is too big", responseCode = HttpServletResponse.SC_BAD_REQUEST) }, returnDescription = "")
public Response postStaticFile(@Context HttpServletRequest request) {
    if (maxUploadSize > 0 && request.getContentLength() > maxUploadSize) {
        logger.warn("Preventing upload of static file as its size {} is larger than the max size allowed {}", request.getContentLength(), maxUploadSize);
        return Response.status(Status.BAD_REQUEST).build();
    }
    ProgressInputStream inputStream = null;
    try {
        String filename = null;
        if (ServletFileUpload.isMultipartContent(request)) {
            boolean isDone = false;
            for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
                FileItemStream item = iter.next();
                if (item.isFormField()) {
                    continue;
                } else {
                    logger.debug("Processing file item");
                    filename = item.getName();
                    inputStream = new ProgressInputStream(item.openStream());
                    inputStream.addPropertyChangeListener(new PropertyChangeListener() {

                        @Override
                        public void propertyChange(PropertyChangeEvent evt) {
                            long totalNumBytesRead = (Long) evt.getNewValue();
                            if (totalNumBytesRead > maxUploadSize) {
                                logger.warn("Upload limit of {} bytes reached, returning a bad request.", maxUploadSize);
                                throw new WebApplicationException(Status.BAD_REQUEST);
                            }
                        }
                    });
                    isDone = true;
                }
                if (isDone)
                    break;
            }
        } else {
            logger.warn("Request is not multi part request, returning a bad request.");
            return Response.status(Status.BAD_REQUEST).build();
        }
        if (filename == null) {
            logger.warn("Request was missing the filename, returning a bad request.");
            return Response.status(Status.BAD_REQUEST).build();
        }
        if (inputStream == null) {
            logger.warn("Request was missing the file, returning a bad request.");
            return Response.status(Status.BAD_REQUEST).build();
        }
        String uuid = staticFileService.storeFile(filename, inputStream);
        try {
            return Response.created(getStaticFileURL(uuid)).entity(uuid).build();
        } catch (NotFoundException e) {
            logger.error("Previous stored file with uuid {} couldn't beren found: {}", uuid, ExceptionUtils.getStackTrace(e));
            return Response.serverError().build();
        }
    } catch (WebApplicationException e) {
        return e.getResponse();
    } catch (Exception e) {
        logger.error("Unable to store file because: {}", ExceptionUtils.getStackTrace(e));
        return Response.serverError().build();
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) PropertyChangeListener(java.beans.PropertyChangeListener) WebApplicationException(javax.ws.rs.WebApplicationException) ProgressInputStream(org.opencastproject.util.ProgressInputStream) FileItemStream(org.apache.commons.fileupload.FileItemStream) NotFoundException(org.opencastproject.util.NotFoundException) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) ConfigurationException(org.osgi.service.cm.ConfigurationException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

PropertyChangeEvent (java.beans.PropertyChangeEvent)3 PropertyChangeListener (java.beans.PropertyChangeListener)3 ProgressInputStream (org.opencastproject.util.ProgressInputStream)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 NotFoundException (org.opencastproject.util.NotFoundException)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1