Code source wiki de FileUploader
Version 1.1 par superadmin le 2022/06/20 08:49
Afficher les derniers auteurs
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity output="false"}} | ||
| 2 | #macro (createAttachment $fileName) | ||
| 3 | #set ($document = $doc) | ||
| 4 | #if ("$!request.document" != '') | ||
| 5 | #set ($document = $xwiki.getDocument($request.document)) | ||
| 6 | #end | ||
| 7 | #if ($document && $document.hasAccessLevel('edit')) | ||
| 8 | ## If the attachment is uploaded to a new document then make sure we create the document with the right default | ||
| 9 | ## locale. See CKEDITOR-316: Document translations are created when uploading a file from the WYSIWYG editor in a | ||
| 10 | ## multi-language wiki. The following code replicates the behavior of the Upload action, but it has the desired | ||
| 11 | ## effect only on XWiki 11.9+ | ||
| 12 | #if ($document.isNew()) | ||
| 13 | #set ($rootLocale = $services.localization.toLocale('')) | ||
| 14 | #set ($discard = $document.setLocale($rootLocale)) | ||
| 15 | #if ($rootLocale.equals($document.getDefaultLocale())) | ||
| 16 | #set ($discard = $document.setDefaultLocale($xwiki.getLocalePreference())) | ||
| 17 | #end | ||
| 18 | ## We also need to make sure the new document is created with the right syntax (e.g. the one coming from the | ||
| 19 | ## template used to create the document rather than the default wiki syntax). | ||
| 20 | #if ("$!request.syntax" != '') | ||
| 21 | #set ($discard = $document.setSyntaxId($request.syntax)) | ||
| 22 | #end | ||
| 23 | #end | ||
| 24 | ## FIXME: Get the file content as an input stream instead of a byte array, but for this we need to expose | ||
| 25 | ## getFileItemInputStream in the public API of the fileupload plugin (or use a script service). | ||
| 26 | #set ($bytes = $xwiki.fileupload.getFileItemData('upload')) | ||
| 27 | #if (!$bytes) | ||
| 28 | ## Empty file (unfortunately getFileItemData returns null instead of an empty byte array). | ||
| 29 | #set ($bytes = []) | ||
| 30 | #end | ||
| 31 | #set ($discard = $document.addAttachment($fileName, $bytes)) | ||
| 32 | #set ($discard = $document.save($services.localization.render('core.comment.uploadAttachmentComment', [$fileName]))) | ||
| 33 | #sendSuccess($document $fileName) | ||
| 34 | #else | ||
| 35 | ## Forbidden | ||
| 36 | #sendError(403 'You are not allowed to perform this action.') | ||
| 37 | #end | ||
| 38 | #end | ||
| 39 | |||
| 40 | ## Old way of uploading, with a save of the document for each upload. | ||
| 41 | #macro (handleUploadRequest) | ||
| 42 | #set ($fileName = $xwiki.fileupload.getFileName('upload')) | ||
| 43 | #if ("$!fileName" != '') | ||
| 44 | #if ($services.csrf.isTokenValid($request.form_token)) | ||
| 45 | #if ($fileName.startsWith('__fileCreatedFromDataURI__.')) | ||
| 46 | ## We need to generate a new name so that we don't overwrite existing attachments. | ||
| 47 | #set ($extension = $stringtool.substringAfter($fileName, '.')) | ||
| 48 | #set ($fileName = "${datetool.date.time}-${mathtool.random(100, 1000)}.$extension") | ||
| 49 | #end | ||
| 50 | #createAttachment($fileName) | ||
| 51 | #else | ||
| 52 | #sendError(403 "$services.localization.render('ckeditor.upload.error.csrf')") | ||
| 53 | #end | ||
| 54 | #else | ||
| 55 | ## Bad Request | ||
| 56 | #sendError(400 "$services.localization.render('ckeditor.upload.error.emptyReturn')") | ||
| 57 | #end | ||
| 58 | #end | ||
| 59 | |||
| 60 | ## New way of handling attachment without saving immediately the document. | ||
| 61 | #macro (handleTemporaryUploadRequest) | ||
| 62 | #if ($services.csrf.isTokenValid($request.form_token)) | ||
| 63 | #set ($document = $doc) | ||
| 64 | #if ("$!request.document" != '') | ||
| 65 | #set ($document = $xwiki.getDocument($request.document)) | ||
| 66 | #end | ||
| 67 | #set ($reference = $document.documentReference) | ||
| 68 | #set ($attachment = $services.temporaryAttachments.uploadTemporaryAttachment($reference, 'upload')) | ||
| 69 | #if ($attachment) | ||
| 70 | #sendSuccess($document, $attachment.filename) | ||
| 71 | #else | ||
| 72 | #sendError(400 "$services.localization.render('ckeditor.upload.error.emptyreturn')") | ||
| 73 | #end | ||
| 74 | #else | ||
| 75 | #sendError(403 "$services.localization.render('ckeditor.upload.error.csrf')") | ||
| 76 | #end | ||
| 77 | #end | ||
| 78 | |||
| 79 | #macro (sendSuccess $document $fileName) | ||
| 80 | #set ($url = $document.getAttachmentURL($fileName)) | ||
| 81 | #set ($attachmentReference = $services.model.createAttachmentReference($document.documentReference, $fileName)) | ||
| 82 | #set ($resourceReference = { | ||
| 83 | 'type': 'attach', | ||
| 84 | 'reference': $services.model.serialize($attachmentReference, $document.documentReference) | ||
| 85 | }) | ||
| 86 | #set ($discard = $response.setContentType('application/json')) | ||
| 87 | #if ($request.initiator == 'filetools') | ||
| 88 | $jsontool.serialize({ | ||
| 89 | 'uploaded': 1, | ||
| 90 | 'fileName': $fileName, | ||
| 91 | 'url': $url, | ||
| 92 | 'resourceReference': $resourceReference | ||
| 93 | }) | ||
| 94 | #else | ||
| 95 | ## JSON expected by the filebrowser plugin. | ||
| 96 | $jsontool.serialize({ | ||
| 97 | 'uploaded': 1, | ||
| 98 | 'url': $url, | ||
| 99 | 'fileName': $fileName, | ||
| 100 | 'message': { | ||
| 101 | 'resourceReference': $resourceReference | ||
| 102 | } | ||
| 103 | }) | ||
| 104 | #end | ||
| 105 | #end | ||
| 106 | |||
| 107 | #macro (sendError $code $message) | ||
| 108 | #set ($discard = $response.setContentType('application/json')) | ||
| 109 | ## The filetools plugin doesn't display the proper message if we call sendError() or setStatus() on the response. | ||
| 110 | #if ($request.initiator != 'filetools') | ||
| 111 | #set ($discard = $response.setStatus($code)) | ||
| 112 | #end | ||
| 113 | $jsontool.serialize({ | ||
| 114 | 'uploaded': 0, | ||
| 115 | 'error': { | ||
| 116 | 'number': $code, | ||
| 117 | 'message': $message | ||
| 118 | } | ||
| 119 | }) | ||
| 120 | #end | ||
| 121 | {{/velocity}} | ||
| 122 | |||
| 123 | {{velocity wiki="false"}} | ||
| 124 | #if ($xcontext.action == 'get') | ||
| 125 | #if ($request.getHeader('X-XWiki-Temporary-Attachment-Support') == 'true') | ||
| 126 | #handleTemporaryUploadRequest | ||
| 127 | #else | ||
| 128 | #handleUploadRequest | ||
| 129 | #end | ||
| 130 | #end | ||
| 131 | {{/velocity}} |