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);
}
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;
}
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);
}
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);
}
Aggregations