Masquer les derniers auteurs
Paul Libbrecht (admin) 1.1 1 // {{groovy}}
2
3
4 import com.xpn.xwiki.api.Attachment
5 import com.xpn.xwiki.api.Document
6 import org.apache.commons.io.IOUtils
7 import java.util.zip.ZipOutputStream
8 import java.util.zip.ZipEntry
9
10 def page = request.getParameter("page");
11 Document pageDoc = null;
12 try {
Paul Libbrecht (admin) 2.1 13 pageDoc = xwiki.getDocument(page);
Paul Libbrecht (admin) 1.1 14 } catch(Exception ex) {}
15
16 if(page && pageDoc) {
17 System.err.println("Should download attachments from page ${pageDoc}.");
Paul Libbrecht (admin) 3.1 18 def uncompressedMimes = ["image/jpeg", "image/png", "application/zip"];
Paul Libbrecht (admin) 2.1 19 response.setContentType("application/zip");
Paul Libbrecht (admin) 3.1 20 String name = pageDoc.getName();
21 if(name=="WebHome") name = pageDoc.getSpace();
22 response.addHeader("Content-Disposition", "attachment; filename=${name}_attachments.zip");
Paul Libbrecht (admin) 1.1 23
24
25 ZipOutputStream out = null;
Paul Libbrecht (admin) 2.1 26 System.err.println("Attachments list's size: ${pageDoc.document.attachmentList.size()}.");
Paul Libbrecht (admin) 1.1 27 try {
28 out = new ZipOutputStream(response.getOutputStream());
29 for(Attachment att in pageDoc.getAttachmentList()) {
30 System.err.println("Outputting " + att.filename);
31 ZipEntry entry = new ZipEntry(att.filename);
Paul Libbrecht (admin) 3.1 32 entry.setTime(att.getDate().getTime());
33 entry.setSize(att.filesize);
34 if(uncompressedMimes.contain(att.mimeType))
35 entry.setMethod(ZipEntry.STORED);
36 else
37 entry.setMethod(ZipEntry.DEFLATED);
Paul Libbrecht (admin) 1.1 38 out.putNextEntry(entry);
Paul Libbrecht (admin) 3.1 39 IOUtils.copy(att.contentInputStream, out, 5*1024*1024);
Paul Libbrecht (admin) 1.1 40 }
41 } catch (Exception ex) {
42 ex.printStackTrace();
43 } finally {
44 try {
45 if(out!=null) out.close();
46 } catch(Exception e) {}
47 }
48
49 } else {
Paul Libbrecht (admin) 3.1 50 println("""
51 = Download Attachments Macro =
Paul Libbrecht (admin) 5.1 52 Use as ~{~{downloadAttachments/~}~} to see a link to a zip of all attachments.
Paul Libbrecht (admin) 3.1 53 """);
Paul Libbrecht (admin) 1.1 54 }
55
56
57 // {{/groovy}}
This wiki is licensed under a Creative Commons 2.0 license
XWiki 13.10 - Documentation - Conditions