Search in sources :

Example 1 with FilesystemAttachmentContent

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

the class FilesystemAttachmentStore method loadAttachmentContent.

@Override
public void loadAttachmentContent(final XWikiAttachment attachment, final XWikiContext context, final boolean bTransaction) throws XWikiException {
    final File attachFile = this.fileTools.getAttachmentFileProvider(attachment.getReference()).getAttachmentContentFile();
    if (!attachFile.exists()) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_FILENOTFOUND, "The attachment could not be found in the filesystem attachment store (" + attachFile + ").\n");
    }
    FilesystemAttachmentContent content = new FilesystemAttachmentContent(attachFile);
    content.setContentDirty(false);
    attachment.setAttachment_content(content);
    attachment.setContentStore(FileSystemStoreUtils.HINT);
}
Also used : FilesystemAttachmentContent(org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent) File(java.io.File) XWikiException(com.xpn.xwiki.XWikiException)

Example 2 with FilesystemAttachmentContent

use of org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent 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 3 with FilesystemAttachmentContent

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

the class FilesystemAttachmentStoreTest method loadContentTest.

@Test
public void loadContentTest() throws Exception {
    this.storeFile.getParentFile().mkdirs();
    OutputStream os = new FileOutputStream(this.storeFile, false);
    IOUtils.copy(HELLO_STREAM, os);
    os.close();
    getMockery().checking(new Expectations() {

        {
            oneOf(mockAttach).setAttachment_content(with(any(FilesystemAttachmentContent.class)));
            will(new CustomAction("Check to make sure the attachment content is correct.") {

                @Override
                public Object invoke(final Invocation invoc) {
                    final FilesystemAttachmentContent content = (FilesystemAttachmentContent) invoc.getParameter(0);
                    try {
                        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        IOUtils.copy(content.getContentInputStream(), baos);
                        final String output = new String(baos.toByteArray(), "UTF-8");
                        Assert.assertEquals("Not the same attachment content.", HELLO, output);
                        return null;
                    } catch (IOException e) {
                        throw new RuntimeException("Exception getting attachment content.", e);
                    }
                }
            });
            oneOf(mockAttach).setContentStore("file");
        }
    });
    this.attachStore.loadAttachmentContent(this.mockAttach, this.mockContext, false);
}
Also used : Expectations(org.jmock.Expectations) FilesystemAttachmentContent(org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with FilesystemAttachmentContent

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

the class FilesystemAttachmentRecycleBinContentStore method deletedAttachmentContentFromProvider.

/**
 * Get a deleted attachment content by it's filesystem location. This returns a DeletedAttachmentContent which is
 * not attached to any document! It is the job of the caller to get the attachment and any version of it and attach
 * them to a document.
 *
 * @param provider a means to get the files which store the deleted attachment content and metadata.
 * @return the deleted attachment for that directory.
 * @throws IOException if deserialization fails or there is a problem loading the archive.
 */
private DeletedAttachmentContent deletedAttachmentContentFromProvider(final DeletedAttachmentFileProvider provider) throws IOException {
    final File deletedMeta = provider.getDeletedAttachmentMetaFile();
    // No metadata, no deleted attachment.
    if (!deletedMeta.exists()) {
        return null;
    }
    final XWikiAttachment attachment;
    ReadWriteLock lock = this.fileTools.getLockForFile(deletedMeta);
    lock.readLock().lock();
    try {
        attachment = this.metaSerializer.parse(new FileInputStream(deletedMeta));
    } finally {
        lock.readLock().unlock();
    }
    final File contentFile = provider.getAttachmentContentFile();
    attachment.setAttachment_content(new FilesystemAttachmentContent(contentFile, attachment));
    attachment.setContentStore(FileSystemStoreUtils.HINT);
    attachment.setAttachment_archive(((FilesystemAttachmentVersioningStore) this.attachmentVersionStore).loadArchive(attachment, provider));
    attachment.setArchiveStore(FileSystemStoreUtils.HINT);
    return new FileDeletedAttachmentContent(attachment);
}
Also used : FilesystemAttachmentContent(org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

FilesystemAttachmentContent (org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent)4 File (java.io.File)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 FileInputStream (java.io.FileInputStream)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 XWikiException (com.xpn.xwiki.XWikiException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Expectations (org.jmock.Expectations)1 Invocation (org.jmock.api.Invocation)1 CustomAction (org.jmock.lib.action.CustomAction)1 Test (org.junit.Test)1 ListAttachmentArchive (org.xwiki.store.legacy.doc.internal.ListAttachmentArchive)1