Code source wiki de Service JSON des fichiers attachés
Modifié par Florent Charton le 2026/01/08 10:56
Masquer les derniers auteurs
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{template name="hierarchy_macros.vm" /}} |
| |
2.1 | 2 | {{template name="attachment_macros.vm" /}} |
| |
1.1 | 3 | |
| 4 | {{velocity wiki="false"}} | ||
| 5 | ## ======================================================================================== | ||
| 6 | ## This page is requested asynchronously by the Attachments live table and returns a set of | ||
| 7 | ## results serialized in JSON format. | ||
| 8 | ## ======================================================================================== | ||
| 9 | #if ($xcontext.action == 'get') | ||
| 10 | #set ($offset = $numbertool.toNumber($request.offset).intValue()) | ||
| 11 | ## The offset sent by the live table starts at 1. | ||
| 12 | #set ($offset = $offset - 1) | ||
| 13 | #if (!$offset || $offset < 0) | ||
| 14 | #set ($offset = 0) | ||
| 15 | #end | ||
| |
3.1 | 16 | #getAndValidateQueryLimitFromRequest('limit', 15, $limit) |
| |
1.1 | 17 | ## |
| 18 | ## Apply live table filters. | ||
| 19 | ## | ||
| 20 | #set ($constraints = []) | ||
| 21 | #set ($queryParameters = []) | ||
| 22 | #maybeApplyStringFilter('mimeType' 'prefix' $constraints $queryParameters) | ||
| 23 | #maybeApplyStringFilter('filename' 'partial' $constraints $queryParameters) | ||
| 24 | #maybeApplyStringFilter('doc.fullName' 'partial' $constraints $queryParameters) | ||
| 25 | #maybeApplyIntegerRangeFilter('filesize' $constraints $queryParameters) | ||
| 26 | #maybeApplyDateRangeFilter('date' $constraints $queryParameters) | ||
| 27 | #maybeApplyStringFilter('author' 'partial' $constraints $queryParameters) | ||
| 28 | #set ($whereClause = '') | ||
| 29 | #if ($constraints.size() > 0) | ||
| 30 | #set ($whereClause = 'where ' + $stringtool.join($constraints, ' and ')) | ||
| 31 | #end | ||
| 32 | ## | ||
| 33 | ## Determine the sort field and direction. | ||
| 34 | ## | ||
| 35 | #set ($validSortFields = ['mimeType', 'filename', 'doc.fullName', 'filesize', 'date', 'author']) | ||
| 36 | #set ($sortField = $request.sort) | ||
| 37 | #if (!$validSortFields.contains($sortField)) | ||
| 38 | #set ($sortField = 'filename') | ||
| 39 | #end | ||
| 40 | #set ($caseInsensitiveSort = $sortField != 'date' && $sortField != 'filesize') | ||
| 41 | #if (!$sortField.startsWith('doc.')) | ||
| 42 | #set ($sortField = "attachment.$sortField") | ||
| 43 | #end | ||
| 44 | #set ($direction = 'asc') | ||
| 45 | #if ("$!request.dir" == 'desc') | ||
| 46 | #set ($direction = 'desc') | ||
| 47 | #end | ||
| 48 | #if ($caseInsensitiveSort) | ||
| 49 | #set ($orderByClause = "order by lower($sortField) $direction, $sortField $direction") | ||
| 50 | #else | ||
| 51 | #set ($orderByClause = "order by $sortField $direction") | ||
| 52 | #end | ||
| 53 | ## | ||
| 54 | ## Compute the final query. | ||
| 55 | ## | ||
| 56 | #set ($query = $services.query.hql("$whereClause $orderByClause")) | ||
| 57 | #set ($discard = $query.addFilter('attachment').addFilter('hidden')) | ||
| 58 | #set ($discard = $query.setLimit($limit).setOffset($offset)) | ||
| 59 | #foreach ($queryParameter in $queryParameters) | ||
| 60 | #if ($queryParameter.match == 'exact') | ||
| 61 | #set ($discard = $query.bindValue($queryParameter.name, $queryParameter.value)) | ||
| 62 | #elseif ($queryParameter.match == 'prefix') | ||
| 63 | #set ($query = $query.bindValue($queryParameter.name).literal($queryParameter.value).anyChars().query()) | ||
| 64 | #else | ||
| 65 | ## Partial match. | ||
| 66 | #set ($query = $query.bindValue($queryParameter.name).anyChars().literal($queryParameter.value).anyChars().query()) | ||
| 67 | #end | ||
| 68 | #end | ||
| 69 | #set ($attachmentReferences = $query.execute()) | ||
| 70 | #set ($results = { | ||
| 71 | "totalrows": $query.count(), | ||
| 72 | "returnedrows": $mathtool.min($attachmentReferences.size(), $limit), | ||
| 73 | "offset": $mathtool.add($offset, 1), | ||
| 74 | "reqNo": $numbertool.toNumber($request.reqNo).intValue(), | ||
| 75 | "rows": [] | ||
| 76 | }) | ||
| 77 | #foreach ($attachmentReference in $attachmentReferences) | ||
| 78 | #set ($hasAccess = $services.security.authorization.hasAccess('view', $attachmentReference)) | ||
| 79 | #set ($location = "#hierarchy($attachmentReference.parent, { | ||
| 80 | 'limit': 5, | ||
| 81 | 'plain': $hasAccess.equals(false), | ||
| 82 | 'local': true, | ||
| 83 | 'displayTitle': false | ||
| 84 | })") | ||
| |
2.1 | 85 | ## The field "fullName" was used by Livetable and is needed for backwards-compatibility. |
| 86 | ## The field "doc.fullName" is needed by LiveData as doc.fullName is the actual field name. | ||
| |
1.1 | 87 | #set ($row = { |
| 88 | 'acclev': $hasAccess, | ||
| 89 | 'fullName': $location, | ||
| |
2.1 | 90 | 'doc.fullName': $location, |
| |
1.1 | 91 | 'filename': $attachmentReference.name, |
| 92 | 'fileurl': $xwiki.getURL($attachmentReference) | ||
| 93 | }) | ||
| 94 | #if ($hasAccess) | ||
| 95 | #set ($document = $xwiki.getDocument($attachmentReference.parent)) | ||
| 96 | #set ($attachment = $document.getAttachment($attachmentReference.name)) | ||
| 97 | #set ($authorReference = $services.model.resolveDocument($attachment.author)) | ||
| 98 | #set ($discard = $row.putAll({ | ||
| 99 | 'mimeType': "#displayAttachmentMimeType($attachment)", | ||
| |
2.1 | 100 | 'filesize': "#displayAttachmentSize($attachment.longSize)", |
| |
1.1 | 101 | 'date': $xwiki.formatDate($attachment.date), |
| |
2.1 | 102 | 'author': "#displayUserNameWithAvatar($attachment.author)" |
| |
1.1 | 103 | })) |
| 104 | #end | ||
| 105 | #set ($discard = $results.rows.add($row)) | ||
| 106 | #end | ||
| 107 | #jsonResponse($results) | ||
| 108 | #end | ||
| 109 | {{/velocity}} |