use of org.exist.util.MimeType in project exist by eXist-db.
the class GetThumbnailsFunction method eval.
/*
* (non-Javadoc)
*
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
* org.exist.xquery.value.Sequence)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
ValueSequence result = new ValueSequence();
// boolean isDatabasePath = false;
boolean isSaveToDataBase = false;
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
AnyURIValue picturePath = (AnyURIValue) args[0].itemAt(0);
if (picturePath.getStringValue().startsWith("xmldb:exist://")) {
picturePath = new AnyURIValue(picturePath.getStringValue().substring(14));
}
AnyURIValue thumbPath = null;
if (args[1].isEmpty()) {
thumbPath = new AnyURIValue(picturePath.toXmldbURI().append(THUMBPATH));
isSaveToDataBase = true;
} else {
thumbPath = (AnyURIValue) args[1].itemAt(0);
if (thumbPath.getStringValue().startsWith("file:")) {
isSaveToDataBase = false;
thumbPath = new AnyURIValue(thumbPath.getStringValue().substring(5));
} else {
isSaveToDataBase = true;
try {
XmldbURI thumbsURI = XmldbURI.xmldbUriFor(thumbPath.getStringValue());
if (!thumbsURI.isAbsolute())
thumbsURI = picturePath.toXmldbURI().append(thumbPath.toString());
thumbPath = new AnyURIValue(thumbsURI.toString());
} catch (URISyntaxException e) {
throw new XPathException(this, e.getMessage());
}
}
}
// result.add(new StringValue(picturePath.getStringValue()));
// result.add(new StringValue(thumbPath.getStringValue() + " isDB?= "
// + isSaveToDataBase));
int maxThumbHeight = MAXTHUMBHEIGHT;
int maxThumbWidth = MAXTHUMBWIDTH;
if (!args[2].isEmpty()) {
maxThumbHeight = ((IntegerValue) args[2].itemAt(0)).getInt();
if (args[2].hasMany())
maxThumbWidth = ((IntegerValue) args[2].itemAt(1)).getInt();
}
String prefix = THUMBPREFIX;
if (!args[3].isEmpty()) {
prefix = args[3].itemAt(0).getStringValue();
}
// result.add(new StringValue("maxThumbHeight = " + maxThumbHeight
// + ", maxThumbWidth = " + maxThumbWidth));
BrokerPool pool = null;
try {
pool = BrokerPool.getInstance();
} catch (Exception e) {
result.add(new StringValue(e.getMessage()));
return result;
}
final DBBroker dbbroker = context.getBroker();
// Start transaction
final TransactionManager transact = pool.getTransactionManager();
try (final Txn transaction = transact.beginTransaction()) {
Collection thumbCollection = null;
Path thumbDir = null;
if (isSaveToDataBase) {
try {
thumbCollection = dbbroker.getOrCreateCollection(transaction, thumbPath.toXmldbURI());
dbbroker.saveCollection(transaction, thumbCollection);
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
} else {
thumbDir = Paths.get(thumbPath.toString());
if (!Files.isDirectory(thumbDir))
try {
Files.createDirectories(thumbDir);
} catch (IOException e) {
throw new XPathException(this, e.getMessage());
}
}
Collection allPictures = null;
Collection existingThumbsCol = null;
List<Path> existingThumbsArray = null;
try {
allPictures = dbbroker.getCollection(picturePath.toXmldbURI());
if (allPictures == null) {
return Sequence.EMPTY_SEQUENCE;
}
if (isSaveToDataBase) {
existingThumbsCol = dbbroker.getCollection(thumbPath.toXmldbURI());
} else {
existingThumbsArray = FileUtils.list(thumbDir, path -> {
final String fileName = FileUtils.fileName(path);
return fileName.endsWith(".jpeg") || fileName.endsWith(".jpg");
});
}
} catch (PermissionDeniedException | IOException e) {
throw new XPathException(this, e.getMessage(), e);
}
DocumentImpl docImage = null;
BinaryDocument binImage = null;
@SuppressWarnings("unused") BufferedImage bImage = null;
@SuppressWarnings("unused") byte[] imgData = null;
Image image = null;
UnsynchronizedByteArrayOutputStream os = null;
try {
Iterator<DocumentImpl> i = allPictures.iterator(dbbroker);
while (i.hasNext()) {
docImage = i.next();
// is not already existing??
if (!((fileExist(context.getBroker(), existingThumbsCol, docImage, prefix)) || (fileExist(existingThumbsArray, docImage, prefix)))) {
if (docImage.getResourceType() == DocumentImpl.BINARY_FILE)
// TODO maybe extends for gifs too.
if (docImage.getMimeType().startsWith("image/jpeg")) {
binImage = (BinaryDocument) docImage;
try (final InputStream is = dbbroker.getBinaryResource(transaction, binImage)) {
image = ImageIO.read(is);
} catch (IOException ioe) {
throw new XPathException(this, ioe.getMessage());
}
try {
bImage = ImageModule.createThumb(image, maxThumbHeight, maxThumbWidth);
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
if (isSaveToDataBase) {
os = new UnsynchronizedByteArrayOutputStream();
try {
ImageIO.write(bImage, "jpg", os);
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
try (final StringInputSource sis = new StringInputSource(os.toByteArray())) {
thumbCollection.storeDocument(transaction, dbbroker, XmldbURI.create(prefix + docImage.getFileURI()), sis, new MimeType("image/jpeg", MimeType.BINARY));
} catch (final Exception e) {
throw new XPathException(this, e.getMessage());
}
// result.add(new
// StringValue(""+docImage.getFileURI()+"|"+thumbCollection.getURI()+THUMBPREFIX
// + docImage.getFileURI()));
} else {
try {
ImageIO.write(bImage, "jpg", Paths.get(thumbPath.toString() + "/" + prefix + docImage.getFileURI()).toFile());
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
// result.add(new StringValue(
// thumbPath.toString() + "/"
// + THUMBPREFIX
// + docImage.getFileURI()));
}
}
} else {
// result.add(new StringValue(""+docImage.getURI()+"|"
// + ((existingThumbsCol != null) ? ""
// + existingThumbsCol.getURI() : thumbDir
// .toString()) + "/" + prefix
// + docImage.getFileURI()));
result.add(new StringValue(docImage.getFileURI().toString()));
}
}
} catch (final PermissionDeniedException | LockException e) {
throw new XPathException(this, e.getMessage(), e);
}
try {
transact.commit(transaction);
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
}
final Optional<JournalManager> journalManager = pool.getJournalManager();
journalManager.ifPresent(j -> j.flush(true, false));
dbbroker.closeDocument();
return result;
}
use of org.exist.util.MimeType in project exist by eXist-db.
the class XmlrpcUpload method stream.
/**
* Write data from a (input)stream to the specified XMLRPC url and leave
* the input stream open.
*
* @param xmldbURL URL pointing to location on eXist-db server.
* @param is Document stream
* @throws IOException When something is wrong.
*/
public void stream(XmldbURL xmldbURL, InputStream is) throws IOException {
LOG.debug("Begin document upload");
try {
// Setup xmlrpc client
final XmlRpcClient client = new XmlRpcClient();
final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setEncoding("UTF-8");
config.setEnabledForExtensions(true);
config.setServerURL(new URL(xmldbURL.getXmlRpcURL()));
if (xmldbURL.hasUserInfo()) {
config.setBasicUserName(xmldbURL.getUsername());
config.setBasicPassword(xmldbURL.getPassword());
}
client.setConfig(config);
String contentType = MimeType.BINARY_TYPE.getName();
final MimeType mime = MimeTable.getInstance().getContentTypeFor(xmldbURL.getDocumentName());
if (mime != null) {
contentType = mime.getName();
}
// Initialize xmlrpc parameters
final List<Object> params = new ArrayList<>(5);
String handle = null;
// Copy data from inputstream to database
final byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) > 0) {
params.clear();
if (handle != null) {
params.add(handle);
}
params.add(buf);
params.add(len);
handle = (String) client.execute("upload", params);
}
// All data transported, now parse data on server
params.clear();
params.add(handle);
params.add(xmldbURL.getCollectionPath());
params.add(Boolean.TRUE);
params.add(contentType);
final Boolean result = (Boolean) client.execute("parseLocal", params);
// Check XMLRPC result
if (result) {
LOG.debug("Document stored.");
} else {
LOG.debug("Could not store document.");
throw new IOException("Could not store document.");
}
} catch (final IOException ex) {
LOG.debug(ex);
throw ex;
} catch (final Exception ex) {
LOG.debug(ex);
throw new IOException(ex.getMessage(), ex);
} finally {
LOG.debug("Finished document upload");
}
}
use of org.exist.util.MimeType in project exist by eXist-db.
the class ModificationTimeTest method storeBinary.
private BinaryDocument storeBinary(final DBBroker broker, final Txn transaction, final String name, final String data, final String mimeType) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
final Collection root = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI);
broker.saveCollection(transaction, root);
assertNotNull(root);
root.storeDocument(transaction, broker, XmldbURI.create(name), new StringInputSource(data.getBytes(UTF_8)), new MimeType(mimeType, MimeType.BINARY));
return (BinaryDocument) root.getDocument(broker, XmldbURI.create(name));
}
use of org.exist.util.MimeType in project exist by eXist-db.
the class AbstractExtractFunction method processCompressedEntry.
/**
* Processes a compressed entry from an archive
*
* @param name The name of the entry
* @param isDirectory true if the entry is a directory, false otherwise
* @param is an InputStream for reading the uncompressed data of the entry
* @param filterParam is an additional param for entry filtering function
* @param storeParam is an additional param for entry storing function
*
* @return the result of processing the compressed entry.
*
* @throws XPathException if a query error occurs
* @throws XMLDBException if a database error occurs
* @throws IOException if an I/O error occurs
*/
protected Sequence processCompressedEntry(String name, boolean isDirectory, InputStream is, Sequence filterParam, Sequence storeParam) throws IOException, XPathException, XMLDBException {
String dataType = isDirectory ? "folder" : "resource";
// call the entry-filter function
Sequence[] filterParams = new Sequence[3];
filterParams[0] = new StringValue(name);
filterParams[1] = new StringValue(dataType);
filterParams[2] = filterParam;
Sequence entryFilterFunctionResult = entryFilterFunction.evalFunction(contextSequence, null, filterParams);
if (BooleanValue.FALSE == entryFilterFunctionResult.itemAt(0)) {
return Sequence.EMPTY_SEQUENCE;
} else {
Sequence entryDataFunctionResult;
Sequence uncompressedData = Sequence.EMPTY_SEQUENCE;
if (entryDataFunction.getSignature().getReturnType().getPrimaryType() != Type.EMPTY && entryDataFunction.getSignature().getArgumentCount() == 3) {
Sequence[] dataParams = new Sequence[3];
System.arraycopy(filterParams, 0, dataParams, 0, 2);
dataParams[2] = storeParam;
entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
String path = entryDataFunctionResult.itemAt(0).getStringValue();
Collection root = new LocalCollection(context.getSubject(), context.getBroker().getBrokerPool(), new AnyURIValue("/db").toXmldbURI());
if (isDirectory) {
XMLDBAbstractCollectionManipulator.createCollection(root, path);
} else {
Resource resource;
Path file = Paths.get(path).normalize();
name = FileUtils.fileName(file);
path = file.getParent().toAbsolutePath().toString();
Collection target = (path == null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
// copy the input data
final byte[] entryData;
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
baos.write(is);
entryData = baos.toByteArray();
}
try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
NodeValue content = ModuleUtils.streamToXML(context, bis);
resource = target.createResource(name, "XMLResource");
ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
handler.startDocument();
content.toSAX(context.getBroker(), handler, null);
handler.endDocument();
} catch (SAXException e) {
resource = target.createResource(name, "BinaryResource");
resource.setContent(entryData);
}
if (resource != null) {
if (mime != null) {
((EXistResource) resource).setMimeType(mime.getName());
}
target.storeResource(resource);
}
}
} else {
// copy the input data
final byte[] entryData;
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
baos.write(is);
entryData = baos.toByteArray();
}
// try and parse as xml, fall back to binary
try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
uncompressedData = ModuleUtils.streamToXML(context, bis);
} catch (SAXException saxe) {
if (entryData.length > 0) {
try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
uncompressedData = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), bis);
}
}
}
// call the entry-data function
Sequence[] dataParams = new Sequence[4];
System.arraycopy(filterParams, 0, dataParams, 0, 2);
dataParams[2] = uncompressedData;
dataParams[3] = storeParam;
entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
}
return entryDataFunctionResult;
}
}
use of org.exist.util.MimeType in project exist by eXist-db.
the class ExistXmldbEmbeddedServer method storeResource.
public static void storeResource(final Collection collection, final String documentName, final byte[] content) throws XMLDBException {
final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentName);
final String type = mime.isXMLType() ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
final Resource resource = collection.createResource(documentName, type);
resource.setContent(content);
collection.storeResource(resource);
}
Aggregations