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);
}
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;
}
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);
}
}
Aggregations