Search in sources :

Example 1 with ListAttachmentArchive

use of org.xwiki.store.legacy.doc.internal.ListAttachmentArchive in project xwiki-platform by xwiki.

the class FilesystemAttachmentVersioningStore method loadArchive.

/**
 * Load an attachment archive from a specified location.
 *
 * @param attachment the attachment to load the archive for.
 * @param provider a means of gaining access to the location where the archive is stored.
 * @return an XWikiAttachmentArchive for the given attachment.
 * @throws IOException if the metadata cannot be found or there is a failure while parsing it.
 */
XWikiAttachmentArchive loadArchive(final XWikiAttachment attachment, final AttachmentFileProvider provider) throws IOException {
    final File metaFile = provider.getAttachmentVersioningMetaFile();
    // If no meta file then assume no archive and return an empty archive.
    if (!metaFile.exists()) {
        return new ListAttachmentArchive(attachment);
    }
    final ReadWriteLock lock = this.fileTools.getLockForFile(metaFile);
    final List<XWikiAttachment> attachList;
    lock.readLock().lock();
    try {
        final InputStream is = new FileInputStream(metaFile);
        attachList = this.metaSerializer.parse(is);
        is.close();
    } finally {
        lock.readLock().unlock();
    }
    // Get the content file and lock for each revision.
    for (XWikiAttachment attach : attachList) {
        final File contentFile = provider.getAttachmentVersionContentFile(attach.getVersion());
        attach.setAttachment_content(new FilesystemAttachmentContent(contentFile, attach));
        attach.setContentStore(FileSystemStoreUtils.HINT);
        // Pass the document since it will be lost in the serialize/deserialize.
        attach.setDoc(attachment.getDoc());
    }
    final ListAttachmentArchive out = new ListAttachmentArchive(attachList);
    out.setAttachment(attachment);
    return out;
}
Also used : FilesystemAttachmentContent(org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) File(java.io.File) ListAttachmentArchive(org.xwiki.store.legacy.doc.internal.ListAttachmentArchive) FileInputStream(java.io.FileInputStream)

Example 2 with ListAttachmentArchive

use of org.xwiki.store.legacy.doc.internal.ListAttachmentArchive in project xwiki-platform by xwiki.

the class FilesystemAttachmentVersioningStoreTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    Utils.setComponentManager(this.getComponentManager());
    final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    this.storageLocation = new File(tmpDir, "test-storage-location");
    this.fileTools = new FilesystemStoreTools(new FileStringEntityReferenceSerializer(), storageLocation, new DummyLockProvider());
    final AttachmentListMetadataSerializer serializer = new AttachmentListMetadataSerializer(new AttachmentMetadataSerializer());
    this.versionStore = new FilesystemAttachmentVersioningStore();
    FieldUtils.writeDeclaredField(this.versionStore, "fileTools", this.fileTools, true);
    FieldUtils.writeDeclaredField(this.versionStore, "metaSerializer", serializer, true);
    final XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "Main", "WebHome"));
    final XWikiAttachment version1 = new XWikiAttachment();
    version1.setVersion("1.1");
    version1.setFilename("attachment.txt");
    version1.setDoc(doc);
    version1.setAttachment_content(new StringAttachmentContent("I am version 1.1"));
    final XWikiAttachment version2 = new XWikiAttachment();
    version2.setVersion("1.2");
    version2.setFilename("attachment.txt");
    version2.setDoc(doc);
    version2.setAttachment_content(new StringAttachmentContent("I am version 1.2"));
    final XWikiAttachment version3 = new XWikiAttachment();
    version3.setVersion("1.3");
    version3.setFilename("attachment.txt");
    version3.setDoc(doc);
    version3.setAttachment_content(new StringAttachmentContent("I am version 1.3"));
    this.provider = this.fileTools.getAttachmentFileProvider(version1.getReference());
    this.archive = new ListAttachmentArchive(new ArrayList<XWikiAttachment>() {

        {
            add(version1);
            add(version2);
            add(version3);
        }
    });
}
Also used : FileStringEntityReferenceSerializer(org.xwiki.store.internal.FileStringEntityReferenceSerializer) FilesystemStoreTools(org.xwiki.store.filesystem.internal.FilesystemStoreTools) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) AttachmentListMetadataSerializer(org.xwiki.store.serialization.xml.internal.AttachmentListMetadataSerializer) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DummyLockProvider(org.xwiki.store.locks.dummy.internal.DummyLockProvider) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference) ListAttachmentArchive(org.xwiki.store.legacy.doc.internal.ListAttachmentArchive) AttachmentMetadataSerializer(org.xwiki.store.serialization.xml.internal.AttachmentMetadataSerializer) Before(org.junit.Before)

Aggregations

XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 File (java.io.File)2 ListAttachmentArchive (org.xwiki.store.legacy.doc.internal.ListAttachmentArchive)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 Before (org.junit.Before)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 FilesystemStoreTools (org.xwiki.store.filesystem.internal.FilesystemStoreTools)1 FileStringEntityReferenceSerializer (org.xwiki.store.internal.FileStringEntityReferenceSerializer)1 FilesystemAttachmentContent (org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent)1 DummyLockProvider (org.xwiki.store.locks.dummy.internal.DummyLockProvider)1 AttachmentListMetadataSerializer (org.xwiki.store.serialization.xml.internal.AttachmentListMetadataSerializer)1 AttachmentMetadataSerializer (org.xwiki.store.serialization.xml.internal.AttachmentMetadataSerializer)1