Code source wiki de DownloadAttachments
Version 1.1 par Paul Libbrecht (admin) le 2016/08/18 23:48
Afficher les derniers auteurs
author | version | line-number | content |
---|---|---|---|
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 { | ||
13 | pageDoc = xwiki.getDocument(page); | ||
14 | } catch(Exception ex) {} | ||
15 | |||
16 | if(page && pageDoc) { | ||
17 | System.err.println("Should download attachments from page ${pageDoc}."); | ||
18 | //System.err.println("content: " + pageDoc.getContent()); | ||
19 | response.setContentType("application/zip; filename=${pageDoc.getName()}_attachments.zip"); | ||
20 | response.addHeader("Content-Disposition", "attachment"); | ||
21 | |||
22 | |||
23 | ZipOutputStream out = null; | ||
24 | System.err.println("Attachments list: ${pageDoc.document.attachmentList.size()}."); | ||
25 | try { | ||
26 | out = new ZipOutputStream(response.getOutputStream()); | ||
27 | for(Attachment att in pageDoc.getAttachmentList()) { | ||
28 | System.err.println("Outputting " + att.filename); | ||
29 | ZipEntry entry = new ZipEntry(att.filename); | ||
30 | out.putNextEntry(entry); | ||
31 | IOUtils.copy(att.contentInputStream, out); | ||
32 | } | ||
33 | } catch (Exception ex) { | ||
34 | ex.printStackTrace(); | ||
35 | } finally { | ||
36 | try { | ||
37 | if(out!=null) out.close(); | ||
38 | } catch(Exception e) {} | ||
39 | } | ||
40 | |||
41 | } else { | ||
42 | println(""" | ||
43 | = Download Attachments = | ||
44 | call this page with a parameter ``page`` set to the name of the page, it will put all attachments of that page in a single zip and download it. | ||
45 | """); | ||
46 | } | ||
47 | |||
48 | |||
49 | // {{/groovy}} |