Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added attachment support

  • Loading branch information...
commit 52f00e3fb8902bd9049b80df33e5a28ee4f30dda 1 parent b32be91
@benfortuna benfortuna authored
View
15 pom.xml
@@ -141,6 +141,21 @@
<artifactId>juicer</artifactId>
<version>0.9.1</version>
</dependency>
+ <dependency>
+ <groupId>commons-fileupload</groupId>
+ <artifactId>commons-fileupload</artifactId>
+ <version>1.2.2</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.4</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.1</version>
+ </dependency>
</dependencies>
<repositories>
<repository>
View
69 src/main/webapp/attach.groovy
@@ -0,0 +1,69 @@
+import java.security.MessageDigest;
+
+import javax.jcr.Value;
+
+import org.apache.commons.codec.binary.Hex;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload
+
+if (request.method != 'POST') {
+ request.getRequestDispatcher("view.html").forward(request, response)
+}
+
+if (ServletFileUpload.isMultipartContent(request)) {
+ // Create a new file upload handler
+ ServletFileUpload upload = [new DiskFileItemFactory()]
+
+ def node
+ def items = upload.getItemIterator(request)
+ while (items.hasNext()) {
+ def item = items.next()
+ if (item.formField && item.fieldName == 'p') {
+ node = context.getAttribute('jcr').getNode(item.openStream().text)
+ }
+ else {
+ def digest = MessageDigest.getInstance('md5')
+ def binary = node.session.valueFactory.createBinary(item.openStream())
+// item.openStream().with {
+ binary.stream.with {
+ byte[] data = new byte[8192]
+ int bytesRead
+ while ((bytesRead = read(data)) >= 0) {
+ digest.update data, 0, bytesRead
+ }
+ }
+ def checksum = new String(Hex.encodeHex(digest.digest()))
+
+ def attachNode = node.session.rootNode << 'attachments'
+ checksum.split(/(?<=\G.{2})/).each {
+ attachNode = attachNode << it
+ }
+ if (attachNode.isNew()) {
+// println "Creating node: $attachNode.path"
+ attachNode.addMixin('mix:referenceable')
+ attachNode.session.save {
+// attachNode['jcr:content'] = attachNode.session.valueFactory.createBinary(item.openStream())
+ attachNode['jcr:content'] = binary
+ }
+ }
+
+ if (node) {
+ node.session.save {
+ def attachments = node << 'attachments'
+ attachments[item.name] = attachNode
+ }
+// attachments = node.attachments.values as List
+// }
+// else {
+// attachments = []
+// }
+// attachments << node.session.valueFactory.createValue(attachNode)
+//
+// node.session.save {
+// node['attachments'] = attachments as Value[]
+// }
+ }
+ }
+ }
+ response.sendRedirect("view.html?p=$node.path")
+}
View
7 src/main/webapp/download.groovy
@@ -0,0 +1,7 @@
+if (request.getParameter('p')) {
+ def node = context.getAttribute('jcr').getNode(request.getParameter('p'))
+// response.contentType = ''
+ response.setHeader('Content-Disposition', "attachment;filename=${request.getParameter('f')}")
+ sout << node['jcr:content'].binary.stream
+// sout << node['jcr:content'].binary.stream.bytes
+}
View
11 src/main/webapp/search.groovy
@@ -12,6 +12,17 @@ if (request.getParameter('q')) {
)
}
request.setAttribute('query', searchQuery)
+
+ def attachSearchQuery = new QueryBuilder(jcr.workspace.queryManager, jcr.valueFactory).with {
+ query(
+ source: selector(nodeType: 'nt:unstructured', name: 'items'),
+ constraint: and(
+ constraint1: descendantNode(selectorName: 'items', path: '/attachments'),
+ constraint2: fullTextSearch(selectorName: 'items', propertyName: 'jcr:content', searchTerms: request.getParameter('q'))
+ )
+ )
+ }
+ request.setAttribute('attachQuery', attachSearchQuery)
}
request.getRequestDispatcher("search.html").forward(request, response)
View
5 src/main/webapp/search.html
@@ -16,6 +16,11 @@
request.getAttribute('query').execute().nodes.each { %>
<a href='view.html?p=<% out << it.path%>'>${it.title.string}</a>
<% }} %>
+<%
+ if (request.getAttribute('attachQuery')) {
+ request.getAttribute('attachQuery').execute().nodes.each { %>
+ <a href='download.groovy?p=<% out << it.path%>&f=<% out << it.title.string %>'>${it.title.string}</a>
+<% }} %>
</body>
</html>
View
26 src/main/webapp/view.html
@@ -36,13 +36,33 @@ <h2 style="color:lightgray">
<a href="edit.html?p=<% out << node.path%>" style="color:gray;vertical-align:super;font-size:0.5em">[Edit]</a>
</h2>
<p><pre>${node.text ? node.text.string : 'Nothing to see here'}</pre></p>
-<% node.nodes.each { %>
- <a href='view.html?p=<% out << it.path%>'>${it.title.string}</a>
-<% } %>
+<% node.nodes.each {
+ if (it.name != 'attachments') { %>
+ <a href='view.html?p=<% out << it.path%>'>${it.title.string}</a>
+<% }
+ else { %>
+ <ul>
+ <%
+ it.properties.each { p ->
+ if (p.type == 9) { %>
+ <li><a href='download.groovy?p=<% out << p.getNode().path%>&f=<% out << p.name %>'>${p.name}</a></li>
+<%
+ }
+ }
+ %>
+ </ul>
+ <%
+ }
+ } %>
<form action="edit.html">
<input type="hidden" name="p" value="<% out << node.path%>"/>
<input type="text" name="c"/>
<input type="submit" value="Add"/>
</form>
+<form action="attach.groovy" enctype="multipart/form-data" method="post">
+ <input type="hidden" name="p" value="<% out << node.path%>"/>
+ <input type="file" name="datafile" size="40">
+ <input type="submit" value="Attach"/>
+</form>
</body>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.