Export All Attachments

Last modified by Ramona Conoro on 2021/03/18 11:28

page_white_goExports all attachments found in the wiki (v2).
TypeSnippet
Category
Developed by

Raluca Stavro

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

This code snippet has been converted to 2.0 syntax.

You can choose the database and the space name to export from. 

The attachments will be saved on the server, so you also need to insert the path to the destination directory. 

=Export Attachments=

{{info}}
By default, the export is made from the **current database** and from **all spaces**.
{{/info}}

{{velocity}}
  {{html}}
   <form action="$doc.getURL()">
     ; Database:
     : <input type="text" name="dbName" size="30" value="$!request.dbName" />
     ; Space name:
     : <input type="text" name="spaceName" size="30" value="$!request.spaceName" />
     ; Path to directory to write to:
     : <input type="text" name="dirpath" size="60" />
     ; <input type="checkbox" name="confirm" /> Confirm
     : <input type="submit" value="Submit" />
   </form>
  {{/html}}
{{/velocity}}

{{groovy}}
 import com.xpn.xwiki.*;
 import com.xpn.xwiki.api.*;
 import com.xpn.xwiki.doc.*;

 import org.apache.commons.io.FileUtils;

 import java.io.File;
 import java.net.URL;

 class ExportedAttachments {
    static attachmentsNo = 0;
  }

 // Export attachments
 def void exportAttachments(XWikiContext context) {
   def dirPath = request.dirpath + "/";
   def dir = new File(dirPath);

    String hqlDocCount = "select distinct count(doc.name) from XWikiDocument as doc";
    String hqlDocList = "select doc.fullName from XWikiDocument as doc";

   if(request.spaceName && request.spaceName != ''){
      hqlDocCount += " where doc.web='" + request.spaceName + "'";
      hqlDocList += " where doc.web='" + request.spaceName + "'";
    }

    XWiki wiki = context.getWiki();

   int total = wiki.getStore().search(hqlDocCount, 0, 0, context).get(0);
    println "**" + total + "** documents to search for attachments";

    List documentNames = null;
   int offset = 0;
   while (documentNames == null || documentNames.size() == 100) {
      documentNames = wiki.getStore().search(hqlDocList, 100, offset, context);

     for (docName in documentNames) {
       try {
          XWikiDocument doc = context.getWiki().getDocument(docName, context);
          exportDocAttachments(doc, dir, context);
        } catch (Exception e) {
          println "{{error}} Error : " + e + "{{/error}}";
        }
      }
 
      offset += documentNames.size();
    }

    println "";
    println "((({{info}}Export done.{{/info}})))";
    println "**" + ExportedAttachments.attachmentsNo + "** attachments exported.";
  }

 // Export attachments for the given document
def void exportDocAttachments(XWikiDocument doc, File dir, XWikiContext context) {
  if(doc.getAttachmentList().size() > 0){
      println "* **" + doc.fullName + "**";

    for (XWikiAttachment attach in doc.getAttachmentList()) {
        println "** " + attach.filename;

       try {
         // Create file
          File spaceDir = new File(dir, java.net.URLEncoder.encode(doc.web));
          File docDir = new File(spaceDir, java.net.URLEncoder.encode(doc.name));
          File destinationFile = new File(docDir, java.net.URLEncoder.encode(attach.filename));
         def attachmentURL = doc.getExternalAttachmentURL(attach.filename, 'download', context);

         // Copy attachment from URL
         URL url = new URL(attachmentURL);
         FileUtils.copyURLToFile(url, destinationFile);

         ExportedAttachments.attachmentsNo++;

        }catch(Exception e){
          println "{{error}} Error: Could not create file: " + e + "{{/error}}";
        }
      }
    }
  }

 // Handle request
 if(request.confirm && request.dirpath){
   // Set database
   def currentDatabase  = xcontext.getDatabase();
   def newDatabase = currentDatabase;
   if(request.dbName && request.dbName != ''){
      newDatabase = request.dbName
    }
    xcontext.setDatabase(newDatabase);

    println "=Exporting attachments from **" + newDatabase + "** wiki=";

   // Export
    exportAttachments(xcontext.context);

   // Reset database
    xcontext.setDatabase(currentDatabase);
  }
{{/groovy}}

Get Connected