Code source wiki de DownloadAttachments
Version 7.7 par Paul Libbrecht (admin) le 2016/08/19 21:39
Masquer les derniers auteurs
author | version | line-number | content |
---|---|---|---|
![]() |
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 { | ||
![]() |
2.1 | 13 | pageDoc = xwiki.getDocument(page); |
![]() |
1.1 | 14 | } catch(Exception ex) {} |
15 | |||
16 | if(page && pageDoc) { | ||
17 | System.err.println("Should download attachments from page ${pageDoc}."); | ||
![]() |
3.1 | 18 | def uncompressedMimes = ["image/jpeg", "image/png", "application/zip"]; |
![]() |
2.1 | 19 | response.setContentType("application/zip"); |
![]() |
3.1 | 20 | String name = pageDoc.getName(); |
21 | if(name=="WebHome") name = pageDoc.getSpace(); | ||
22 | response.addHeader("Content-Disposition", "attachment; filename=${name}_attachments.zip"); | ||
![]() |
1.1 | 23 | |
24 | |||
25 | ZipOutputStream out = null; | ||
![]() |
2.1 | 26 | System.err.println("Attachments list's size: ${pageDoc.document.attachmentList.size()}."); |
![]() |
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); | ||
![]() |
7.6 | 32 | if(uncompressedMimes.contains(att.mimeType)) |
![]() |
7.7 | 33 | out.setLevel(0); |
![]() |
7.6 | 34 | else |
![]() |
7.7 | 35 | out.setLevel(9); |
![]() |
3.1 | 36 | entry.setTime(att.getDate().getTime()); |
![]() |
7.4 | 37 | System.err.println("Size: " + att.filesize); |
![]() |
3.1 | 38 | entry.setSize(att.filesize); |
![]() |
7.6 | 39 | |
![]() |
1.1 | 40 | out.putNextEntry(entry); |
![]() |
3.1 | 41 | IOUtils.copy(att.contentInputStream, out, 5*1024*1024); |
![]() |
1.1 | 42 | } |
43 | } catch (Exception ex) { | ||
44 | ex.printStackTrace(); | ||
45 | } finally { | ||
46 | try { | ||
47 | if(out!=null) out.close(); | ||
48 | } catch(Exception e) {} | ||
49 | } | ||
50 | |||
51 | } else { | ||
![]() |
3.1 | 52 | println(""" |
53 | = Download Attachments Macro = | ||
![]() |
7.3 | 54 | Use as ((({{downloadAttachments/}}))) so see a list of attachments. |
![]() |
3.1 | 55 | """); |
![]() |
1.1 | 56 | } |
57 | |||
58 | |||
59 | // {{/groovy}} |