Find the filesystem location of an attachment
Last modified by Vincent Massol on 2021/04/14 18:37
Find where an attachment on a wiki page is located on the filesystem |
Type | |
Category | Other |
Developed by | |
Rating | |
License | GNU Lesser General Public License 2.1 |
Table of contents
Description
Copy the following snippet in a page with a user having programming rights, and then follow the UI to get the path to the attachment:
{{velocity}}{{html}}
<form action="" method='get' class='xform full'>
<dl>
<dt><label for='wiki'>Wiki:</label></dt>
<dd><select name='wiki'>
#foreach($w in $services.wiki.getAllIds())
<option value='$w' #if($w == $request.wiki)selected='selected'#end>$w</option>
#end
</select></dd>
<dt><label for='pagename'>Page reference (as provided in the page information tab): </label></dt>
<dd><input type='text' name='pagename' value="$!request.pagename" /></dd>
<dt><label for='filename'>Filename: </label></dt>
<dd><input type='text' name='filename' value="$!request.filename" /></dd>
</dl>
<div class='buttonwrapper'>
<input type='submit' class='btn primary button' value='Get path'/>
</div>
</form>
{{/html}}{{/velocity}}
{{groovy}}
println "";
import org.xwiki.store.filesystem.internal.FilesystemStoreTools;
if(request.get('filename') != null && request.get('filename').size() > 0 && request.get('wiki') != null && request.get('wiki').size() > 0 && request.get('pagename') != null && request.get('pagename').size() > 0) {
def pageReference = services.model.resolveDocument(request.get('pagename'), services.model.createWikiReference(request.get('wiki')));
def attachmentReference = services.model.createAttachmentReference(pageReference, request.get('filename'));
def attachmentDoc = xwiki.getDocument(attachmentReference.getDocumentReference())
if (attachmentDoc != null && !attachmentDoc.isNew()) {
def attachment = attachmentDoc.getAttachment(attachmentReference.getName())
if (attachment != null) {
//println attachmentReference;
def comp = services.component.getInstance(FilesystemStoreTools.class);
println "File path is ";
println "";
println "{{box}}{{{ " + comp.getAttachmentFileProvider(attachmentReference).getAttachmentContentFile() + " }}}\\\\{{/box}}";
} else {
println "Page exists but attachment does not exist.";
}
} else {
println "Page does not exist.";
}
} else {
println "Please fill in a wiki, a page name and an attachment name.";
}
{{/groovy}}
<form action="" method='get' class='xform full'>
<dl>
<dt><label for='wiki'>Wiki:</label></dt>
<dd><select name='wiki'>
#foreach($w in $services.wiki.getAllIds())
<option value='$w' #if($w == $request.wiki)selected='selected'#end>$w</option>
#end
</select></dd>
<dt><label for='pagename'>Page reference (as provided in the page information tab): </label></dt>
<dd><input type='text' name='pagename' value="$!request.pagename" /></dd>
<dt><label for='filename'>Filename: </label></dt>
<dd><input type='text' name='filename' value="$!request.filename" /></dd>
</dl>
<div class='buttonwrapper'>
<input type='submit' class='btn primary button' value='Get path'/>
</div>
</form>
{{/html}}{{/velocity}}
{{groovy}}
println "";
import org.xwiki.store.filesystem.internal.FilesystemStoreTools;
if(request.get('filename') != null && request.get('filename').size() > 0 && request.get('wiki') != null && request.get('wiki').size() > 0 && request.get('pagename') != null && request.get('pagename').size() > 0) {
def pageReference = services.model.resolveDocument(request.get('pagename'), services.model.createWikiReference(request.get('wiki')));
def attachmentReference = services.model.createAttachmentReference(pageReference, request.get('filename'));
def attachmentDoc = xwiki.getDocument(attachmentReference.getDocumentReference())
if (attachmentDoc != null && !attachmentDoc.isNew()) {
def attachment = attachmentDoc.getAttachment(attachmentReference.getName())
if (attachment != null) {
//println attachmentReference;
def comp = services.component.getInstance(FilesystemStoreTools.class);
println "File path is ";
println "";
println "{{box}}{{{ " + comp.getAttachmentFileProvider(attachmentReference).getAttachmentContentFile() + " }}}\\\\{{/box}}";
} else {
println "Page exists but attachment does not exist.";
}
} else {
println "Page does not exist.";
}
} else {
println "Please fill in a wiki, a page name and an attachment name.";
}
{{/groovy}}
Example results:
/usr/local/xwiki-workdir/store/file/xwiki/8/3/927acf911df7547cf26345b2c3e89c/attachments/9/2/0bc685fa0da28168319c0126def81b/f.png
Note: this snippet is especially useful when trying to perform an import using filter streams application and not having access to the filesystem of the server: the file can be attached to a page and then the path on the filesystem can be fetched using this script and used in the Filter Streams Application .