Code source wiki de DownloadAttachments
Version 2.1 par Paul Libbrecht (admin) le 2016/08/18 23:58
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 | response.setContentType("application/zip"); | ||
19 | response.addHeader("Content-Disposition", "attachment; filename=${pageDoc.getName()}_attachments.zip"); | ||
20 | |||
21 | |||
22 | ZipOutputStream out = null; | ||
23 | System.err.println("Attachments list's size: ${pageDoc.document.attachmentList.size()}."); | ||
24 | try { | ||
25 | out = new ZipOutputStream(response.getOutputStream()); | ||
26 | for(Attachment att in pageDoc.getAttachmentList()) { | ||
27 | System.err.println("Outputting " + att.filename); | ||
28 | ZipEntry entry = new ZipEntry(att.filename); | ||
29 | out.putNextEntry(entry); | ||
30 | IOUtils.copy(att.contentInputStream, out); | ||
31 | } | ||
32 | } catch (Exception ex) { | ||
33 | ex.printStackTrace(); | ||
34 | } finally { | ||
35 | try { | ||
36 | if(out!=null) out.close(); | ||
37 | } catch(Exception e) {} | ||
38 | } | ||
39 | |||
40 | } else { | ||
41 | println("Sorry, I need a page parameter."); | ||
42 | } | ||
43 | |||
44 | |||
45 | // {{/groovy}} |