Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
- add support for Zimbra 8.7.1
Browse files Browse the repository at this point in the history
- remove preview using webODF as this is not good enough
- add preview using server side conversion to pdf (optional)
  (support docx,doc,xlsx,xls,pptx,ppt,odt,ods and odp)
- The document conversion is experimental and only for CentOS 7 atm.
  • Loading branch information
Barry de Graaff committed Oct 28, 2016
1 parent 5021cfe commit 187d953
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 91 deletions.
18 changes: 18 additions & 0 deletions bin/docconvert
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
# yum install -y libreoffice-headless.x86_64 libreoffice.x86_64
# adduser docconvert
# chmod +x /usr/local/sbin/docconvert
# echo "zimbra ALL=(docconvert) NOPASSWD: ALL" > /etc/sudoers.d/99_zimbra-docconvert
# usermod -a -G zimbra docconvert
# usermod -a -G docconvert zimbra

if ! [[ $1 ]]
then
echo "argument required"
exit 0;
fi

cd /tmp
sudo -u docconvert /usr/bin/soffice --convert-to pdf $1 --headless
sudo -u docconvert /usr/bin/chmod ugo+rwx /tmp/docconvert*.pdf

12 changes: 12 additions & 0 deletions bin/docconvertclean
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

if ! [[ $1 ]]
then
echo "argument required"
exit 0;
fi

cd /tmp
sudo -u docconvert rm -f $1


10 changes: 5 additions & 5 deletions extension/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Build file for the Zimbra Extension.
</description>

<property name="zal-version" value="1.10"/>
<property name="zal-version-extended" value="1.10.6"/>
<property name="zal-version" value="1.11"/>
<property name="zal-version-extended" value="1.11.2"/>
<!--
In order to build the extension, is not necessary the target zimbra version,
any version of the target jar is good for the build. Be sure to install the
correct version at runtime.
-->
<property name="zimbra-version" value="8.7.0"/>
<property name="zimbra-version" value="8.7.1"/>

<property name="lib" value="lib/"/>
<property name="build" value="build/"/>
Expand All @@ -35,8 +35,8 @@
includeantruntime="false"
debug="true"
debuglevel="lines,vars,source"
target="1.6"
source="1.6">
target="1.7"
source="1.7">
<classpath>
<path refid="libraries" />
</classpath>
Expand Down
Binary file not shown.
248 changes: 174 additions & 74 deletions extension/src/com/zextras/dav/DownloadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,192 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public class DownloadHandler implements HttpHandler
{
private final Map<String, DownloadJob> mDownloadJobMap;

public DownloadHandler(Map<String, DownloadJob> downloadJobMap)
{
mDownloadJobMap = downloadJobMap;
}

@Override
public void doGet(
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse
)
throws ServletException, IOException
{
Map<String, String> paramsMap = new HashMap<String, String>();

String[] params = httpServletRequest.getQueryString().split("&");
for (String param : params)
{
String[] subParam = param.split("=");
paramsMap.put(subParam[0], subParam[1]);
public class DownloadHandler implements HttpHandler {
private final Map<String, DownloadJob> mDownloadJobMap;

public DownloadHandler(Map<String, DownloadJob> downloadJobMap) {
mDownloadJobMap = downloadJobMap;
}
String token = paramsMap.get("token");
String inline = paramsMap.get("inline");

@Override
public void doGet(
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse
)
throws ServletException, IOException {
Map<String, String> paramsMap = new HashMap<String, String>();

if (mDownloadJobMap.containsKey(token)) {
DownloadJob job = mDownloadJobMap.get(token);
if (!job.expired()) {
if (paramsMap.containsKey("contentType"))
{
httpServletResponse.addHeader("Content-Type", paramsMap.get("contentType"));
String[] params = httpServletRequest.getQueryString().split("&");
for (String param : params) {
String[] subParam = param.split("=");
paramsMap.put(subParam[0], subParam[1]);
}
String token = paramsMap.get("token");
String inline = paramsMap.get("inline");

if(!"true".equals(inline)) {
httpServletResponse.addHeader("Content-Disposition", "attachment; filename=\""+paramsMap.get("name")+"\"");
}
else
{
httpServletResponse.addHeader("Content-Disposition", "inline; filename=\""+paramsMap.get("name")+"\"");
httpServletResponse.addHeader("Accept-Ranges", "none");

if (mDownloadJobMap.containsKey(token)) {
DownloadJob job = mDownloadJobMap.get(token);
if (!job.expired()) {

DavSoapConnector connector = job.getConnector();
InputStream fileStream = connector.getAsStream(job.getPath());
OutputStream outputStream = httpServletResponse.getOutputStream();

String extension;
extension = "false";
switch (paramsMap.get("contentType")) {
case "application/msword":
extension = "doc";
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
extension = "docx";
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
extension = "xlsx";
case "application/vnd.ms-excel":
extension = "xls";
case "application/vnd.ms-powerpoint":
extension = "ppt";
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
extension = "pptx";
case "application/vnd.oasis.opendocument.spreadsheet":
extension = "ods";
case "application/vnd.oasis.opendocument.presentation":
extension = "odp";
case "application/vnd.oasis.opendocument.text":
extension = "odt";
}

if (("true".equals(inline)) && (!"false".equals(extension))) {
//Check if conversion is available
File f = new File("/usr/local/sbin/docconvert");
if (f.exists()) {

//Convert to PDF
//Create a temporary filename
String tmpFileName = "/tmp/docconvert" + UUID.randomUUID().toString();

byte[] buffer = new byte[8 * 1024];

try {
OutputStream output = new FileOutputStream(tmpFileName + "." + extension);
try {
int bytesRead;
while ((bytesRead = fileStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
fileStream.close();
}

try {
this.runCommand("/usr/local/sbin/docconvert " + tmpFileName + "." + extension);

httpServletResponse.addHeader("Content-Type", "application/pdf");
httpServletResponse.addHeader("Content-Disposition", "attachment; filename=\"" + paramsMap.get("name") + ".pdf\"");
httpServletResponse.addHeader("Accept-Ranges", "none");

FileInputStream in = new FileInputStream(tmpFileName + ".pdf");

// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
outputStream.write(buf, 0, count);
}
outputStream.close();
in.close();
} finally {
outputStream.close();
}
try {
File fileorig = new File(tmpFileName + "." + extension);
fileorig.delete();
this.runCommand("/usr/local/sbin/docconvertclean " + tmpFileName + ".*");
} catch (Exception x) {

}
}
} else {
if (paramsMap.containsKey("contentType")) {
httpServletResponse.addHeader("Content-Type", paramsMap.get("contentType"));
}

if (!"true".equals(inline)) {
httpServletResponse.addHeader("Content-Disposition", "attachment; filename=\"" + paramsMap.get("name") + "\"");
} else {
httpServletResponse.addHeader("Content-Disposition", "inline; filename=\"" + paramsMap.get("name") + "\"");
httpServletResponse.addHeader("Accept-Ranges", "none");
}

byte[] buffer = new byte[64 * 1024];
int quantity;
try {
while (true) {
quantity = fileStream.read(buffer);
if (quantity <= 0) break;
outputStream.write(buffer, 0, quantity);
}
} finally {
mDownloadJobMap.remove(job.getToken());
outputStream.flush();
fileStream.close();
}
}
}
}
}

DavSoapConnector connector = job.getConnector();
InputStream fileStream = connector.getAsStream(job.getPath());
OutputStream outputStream = httpServletResponse.getOutputStream();
byte[] buffer = new byte[64 * 1024];
int quantity;
private String runCommand(String cmd) {
try {
while(true) {
quantity = fileStream.read(buffer);
if (quantity <= 0) break;
outputStream.write(buffer, 0, quantity);
}
} finally
{
mDownloadJobMap.remove(job.getToken());
outputStream.flush();
fileStream.close();
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
BufferedReader cmdOutputBuffer = new BufferedReader(new InputStreamReader(pr.getInputStream()));

StringBuilder builder = new StringBuilder();
String aux = "";
while ((aux = cmdOutputBuffer.readLine()) != null) {
builder.append(aux);
builder.append(';');
}
String cmdResult = builder.toString();
return cmdResult;

} catch (IOException e) {
{
return "IOException in dav_download/runCommand";
}
}
}
}
}

@Override
public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException
{}

@Override
public void doOptions(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException
{}

@Override
public String getPath()
{
return "dav_download";
}

@Override
public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
//This is used in the zimlet to determine if conversion if available, see OwnCloudListView.js
File f = new File("/usr/local/sbin/docconvert");
if (f.exists()) {
httpServletResponse.getOutputStream().print("true");
} else {
httpServletResponse.getOutputStream().print("false");
}
}

@Override
public void doOptions(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
}

@Override
public String getPath() {
return "dav_download";
}

}
4 changes: 2 additions & 2 deletions installer/install
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
shopt -s extglob

ZAL_VERSION="1.10"
ZAL_VERSION_EXTENDED="1.10.6"
ZAL_VERSION="1.11"
ZAL_VERSION_EXTENDED="1.11.2"

ZIMBRA_VERSION=$(sudo su - zimbra -c "zmcontrol -v" | tr -d '\n' | sed -r 's/.* ([0-9\.]+[0-9]).*/\1/')

Expand Down
23 changes: 21 additions & 2 deletions webdav-client-installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ if [ "$(id -u)" != "0" ]; then
exit 1
fi

echo ""
echo "Do you want to enable experimental document preview (CentOS7 only)? Y/n:"
read YN;

echo "Check if git and ant are installed."
set +e
Expand Down Expand Up @@ -58,6 +61,7 @@ TMPFOLDER="$(mktemp -d /tmp/webdav-client-installer.XXXXXXXX)"
echo "Download WebDAV Client to $TMPFOLDER"
cd $TMPFOLDER
git clone https://github.com/barrydegraaff/owncloud-zimlet
#cp -r /root/owncloud-zimlet $TMPFOLDER

echo "Compiling WebDAV Client"
cd owncloud-zimlet
Expand All @@ -68,8 +72,8 @@ make
echo "Installing server extension to /opt/zimbra/lib/ext/ownCloud"
cd $TMPFOLDER/owncloud-zimlet/dist/owncloud-extension/
shopt -s extglob
ZAL_VERSION="1.10"
ZAL_VERSION_EXTENDED="1.10.6"
ZAL_VERSION="1.11"
ZAL_VERSION_EXTENDED="1.11.2"
ZIMBRA_VERSION=$(sudo su - zimbra -c "zmcontrol -v" | tr -d '\n' | sed -r 's/.* ([0-9\.]+[0-9]).*/\1/')
echo "Downloading the correct ZAL Version (${ZAL_VERSION_EXTENDED} for zimbra ${ZIMBRA_VERSION})..."
wget "https://openzal.org/${ZAL_VERSION}/zal-${ZAL_VERSION_EXTENDED}-${ZIMBRA_VERSION}.jar" -O "zal-${ZAL_VERSION_EXTENDED}-${ZIMBRA_VERSION}.jar"
Expand All @@ -89,6 +93,21 @@ unzip $TMPFOLDER/owncloud-zimlet/zimlet/tk_barrydegraaff_owncloud_zimlet.zip -d
echo "Flushing Zimlet Cache"
su - zimbra -c "zmprov fc all"

if [ "$YN" == 'Y' ];
then
echo "Install LibreOffice"
cp -v $TMPFOLDER/owncloud-zimlet/bin/* /usr/local/sbin/
yum install -y libreoffice-headless.x86_64 libreoffice.x86_64

echo "Configure docconvert user and set up sudo in /etc/sudoers.d/99_zimbra-docconvert"
set +e
adduser docconvert
set -e
echo "zimbra ALL=(docconvert) NOPASSWD: ALL" > /etc/sudoers.d/99_zimbra-docconvert
usermod -a -G zimbra docconvert
usermod -a -G docconvert zimbra
fi

echo "--------------------------------------------------------------------------------------------------------------
Zimbra WebDAV Client installed successful
Expand Down
Loading

0 comments on commit 187d953

Please sign in to comment.