Search in sources :

Example 1 with DocReference

use of org.olat.core.util.openxml.DocReference in project OpenOLAT by OpenOLAT.

the class ForumDownloadResource method prepare.

@Override
public void prepare(HttpServletResponse hres) {
    try {
        hres.setCharacterEncoding("UTF-8");
    } catch (Exception e) {
        log.error("", e);
    }
    try (ZipOutputStream zout = new ZipOutputStream(hres.getOutputStream())) {
        String secureLabel = StringHelper.transformDisplayNameToFileSystemName(label);
        String file = secureLabel + ".zip";
        hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + StringHelper.urlEncodeUTF8(file));
        hres.setHeader("Content-Description", StringHelper.urlEncodeUTF8(label));
        ZipEntry test = new ZipEntry(secureLabel + ".docx");
        zout.putNextEntry(test);
        Map<File, DocReference> attachments = exportForum(zout);
        zout.closeEntry();
        if (attachments != null && attachments.size() > 0) {
            for (Map.Entry<File, DocReference> attachmentEntry : attachments.entrySet()) {
                File attachment = attachmentEntry.getKey();
                DocReference ref = attachmentEntry.getValue();
                zout.putNextEntry(new ZipEntry("attachments/" + ref.getFilename()));
                Files.copy(attachment.toPath(), new ShieldOutputStream(zout));
                zout.closeEntry();
            }
        }
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) Map(java.util.Map) DocReference(org.olat.core.util.openxml.DocReference)

Example 2 with DocReference

use of org.olat.core.util.openxml.DocReference in project openolat by klemens.

the class ForumDownloadResource method prepare.

@Override
public void prepare(HttpServletResponse hres) {
    try {
        hres.setCharacterEncoding("UTF-8");
    } catch (Exception e) {
        log.error("", e);
    }
    try (ZipOutputStream zout = new ZipOutputStream(hres.getOutputStream())) {
        String secureLabel = StringHelper.transformDisplayNameToFileSystemName(label);
        String file = secureLabel + ".zip";
        hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + StringHelper.urlEncodeUTF8(file));
        hres.setHeader("Content-Description", StringHelper.urlEncodeUTF8(label));
        ZipEntry test = new ZipEntry(secureLabel + ".docx");
        zout.putNextEntry(test);
        Map<File, DocReference> attachments = exportForum(zout);
        zout.closeEntry();
        if (attachments != null && attachments.size() > 0) {
            for (Map.Entry<File, DocReference> attachmentEntry : attachments.entrySet()) {
                File attachment = attachmentEntry.getKey();
                DocReference ref = attachmentEntry.getValue();
                zout.putNextEntry(new ZipEntry("attachments/" + ref.getFilename()));
                Files.copy(attachment.toPath(), new ShieldOutputStream(zout));
                zout.closeEntry();
            }
        }
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) Map(java.util.Map) DocReference(org.olat.core.util.openxml.DocReference)

Example 3 with DocReference

use of org.olat.core.util.openxml.DocReference in project OpenOLAT by OpenOLAT.

the class ForumOpenXMLFormatter method processAttachments.

private void processAttachments(VFSContainer attachmentsContainer) {
    List<VFSItem> attachments = new ArrayList<VFSItem>(attachmentsContainer.getItems(filter));
    for (VFSItem attachment : attachments) {
        if (attachment instanceof LocalFileImpl) {
            // add the text
            document.appendText(translator.translate("attachments"), true, Style.bold);
        }
    }
    for (VFSItem attachment : attachments) {
        if (attachment instanceof LocalFileImpl) {
            File file = ((LocalFileImpl) attachment).getBasefile();
            String filename = file.getName();
            int lastDot = filename.lastIndexOf('.');
            boolean attach = true;
            if (lastDot > 0) {
                String extension = filename.substring(lastDot + 1).toLowerCase();
                if ("jpeg".equals(extension) || "jpg".equals(extension) || "gif".equals(extension) || "png".equals(extension)) {
                    document.appendImage(file);
                    attach = false;
                }
            }
            if (attach) {
                StringBuilder attachSb = new StringBuilder(64);
                String uniqueFilename = getUniqueFilename(file);
                fileToAttachmentsMap.put(file, new DocReference("", uniqueFilename, null, file));
                attachSb.append(filename).append(": /attachments/").append(uniqueFilename);
                document.appendText(attachSb.toString(), true);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) File(java.io.File) DocReference(org.olat.core.util.openxml.DocReference)

Example 4 with DocReference

use of org.olat.core.util.openxml.DocReference in project openolat by klemens.

the class ForumOpenXMLFormatter method processAttachments.

private void processAttachments(VFSContainer attachmentsContainer) {
    List<VFSItem> attachments = new ArrayList<VFSItem>(attachmentsContainer.getItems(filter));
    for (VFSItem attachment : attachments) {
        if (attachment instanceof LocalFileImpl) {
            // add the text
            document.appendText(translator.translate("attachments"), true, Style.bold);
        }
    }
    for (VFSItem attachment : attachments) {
        if (attachment instanceof LocalFileImpl) {
            File file = ((LocalFileImpl) attachment).getBasefile();
            String filename = file.getName();
            int lastDot = filename.lastIndexOf('.');
            boolean attach = true;
            if (lastDot > 0) {
                String extension = filename.substring(lastDot + 1).toLowerCase();
                if ("jpeg".equals(extension) || "jpg".equals(extension) || "gif".equals(extension) || "png".equals(extension)) {
                    document.appendImage(file);
                    attach = false;
                }
            }
            if (attach) {
                StringBuilder attachSb = new StringBuilder(64);
                String uniqueFilename = getUniqueFilename(file);
                fileToAttachmentsMap.put(file, new DocReference("", uniqueFilename, null, file));
                attachSb.append(filename).append(": /attachments/").append(uniqueFilename);
                document.appendText(attachSb.toString(), true);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) File(java.io.File) DocReference(org.olat.core.util.openxml.DocReference)

Aggregations

File (java.io.File)4 DocReference (org.olat.core.util.openxml.DocReference)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ZipEntry (java.util.zip.ZipEntry)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 ShieldOutputStream (org.olat.core.util.io.ShieldOutputStream)2 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)2 VFSItem (org.olat.core.util.vfs.VFSItem)2