Wiki source code of Locate an Attachment's File on Disk
Last modified by superadmin on 2026/11/25 21:29
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
![]() |
7.1 | 1 | The script below computes the hashed directory names that [[Filesystem Store Layout>>doc:documentation.xs.admin.attachments.store.file-layout.WebHome]] describes, and needs programming right to run, which is why it is given here rather than run on this Page. |
![]() |
3.1 | 2 | |
| 3 | 1. Note the Page the attachment is on and its file name, for instance ##Sandbox.WebHome## and ##XWikiLogo.png##. | ||
| 4 | 1. Create a Page on your own wiki and paste the script into it, in the wiki editor:((( | ||
| 5 | {{code language="groovy"}} | ||
| 6 | {{groovy}} | ||
| 7 | import org.apache.commons.codec.digest.DigestUtils | ||
| 8 | import org.xwiki.model.reference.DocumentReference | ||
| 9 | import org.xwiki.model.reference.EntityReferenceSerializer | ||
| 10 | |||
| 11 | // The attachment to locate, as "Space.Page@filename". | ||
| 12 | def target = "[email protected]" | ||
| 13 | |||
| 14 | def (page, filename) = target.split("@", 2) | ||
| 15 | // Resolve, then drop the locale: the store hashes the Page without it. | ||
| 16 | def docRef = new DocumentReference(services.model.resolveDocument(page), (Locale) null) | ||
| 17 | def localUid = services.component | ||
| 18 | .getInstance(EntityReferenceSerializer.TYPE_STRING, "local/uid") | ||
| 19 | .serialize(docRef) | ||
| 20 | |||
| 21 | def split = { s -> def h = DigestUtils.md5Hex(s); "${h.charAt(0)}/${h.charAt(1)}/${h.substring(2)}" } | ||
| 22 | def path = "${docRef.wikiReference.name}/${split(localUid)}/attachments/${split(filename)}" | ||
| 23 | |||
| 24 | println "|=Step|=Value" | ||
| 25 | println "|The Page, serialized with ##local/uid##|##${localUid}##" | ||
| 26 | println "|Its MD5|##${DigestUtils.md5Hex(localUid)}##" | ||
| 27 | println "|The MD5 of ##${filename}##|##${DigestUtils.md5Hex(filename)}##" | ||
| 28 | println "|The directory, under ##store/file##|##${path}##" | ||
| 29 | {{/groovy}} | ||
| 30 | {{/code}} | ||
| 31 | ))) | ||
| 32 | 1. Change the ##target## line to the Page and file name you noted, separated by ##@##, then save the Page. | ||
![]() |
5.2 | 33 | 1. Read the directory off the last row of the table and look for it under ##store/file## in the [[permanent directory>>doc:documentation.xs.admin.installation.directories.permanent-directory.WebHome]]. The file inside it is called ##f## plus the extension of the attachment.((( |
![]() |
3.1 | 34 | {{image reference="computed-path.png" size="extra" alt="The script's output table, its last row giving the directory under store/file"/}} |
| 35 | ))) |

