public XWikiAttachment(XWikiDocument doc, String filename) { this(); setDoc(doc); setFilename(filename); }
/** * Clone attachments from another document. This implementation expects that this document is the same as the other * document and thus attachments will be saved in the database in the same place as the ones which they are cloning. * * @param sourceDocument an XWikiDocument to copy attachments from */ private void cloneAttachments(final XWikiDocument sourceDocument) { this.getAttachmentList().clear(); for (XWikiAttachment attach : sourceDocument.getAttachmentList()) { XWikiAttachment newAttach = (XWikiAttachment) attach.clone(); // Document is set to this because if this document is renamed then the attachment will have a new id // and be saved somewhere different. newAttach.setDoc(this); this.getAttachmentList().add(newAttach); } }
@Override public XWikiAttachment getRevision(final XWikiAttachment attachment, final String rev, final XWikiContext context) { if (rev == null) { return null; } for (XWikiAttachment attach : this.revisions) { if (rev.equals(attach.getVersion())) { final XWikiAttachment out = cloneAttachment(attach); out.setAttachment_archive(this); // This is silly, we set the attachment document and passed value. // Keeping to maintain current behavior. out.setDoc(attachment.getDoc(), false); return out; } } return null; }
@Override public XWikiAttachment getRevision(final XWikiAttachment attachment, final String rev, final XWikiContext context) { if (rev == null) { return null; } for (XWikiAttachment attach : this.revisions) { if (rev.equals(attach.getVersion())) { final XWikiAttachment out = cloneAttachment(attach); out.setAttachment_archive(this); // This is silly, we set the attachment document and passed value. // Keeping to maintain current behavior. out.setDoc(attachment.getDoc()); return out; } } return null; }
/** * Copy attachments from one document to another. This implementation expects that you are copying the attachment * from one document to another and thus it should be saved seperately from the original in the database. * * @param sourceDocument an XWikiDocument to copy attachments from */ public void copyAttachments(XWikiDocument sourceDocument) { getAttachmentList().clear(); Iterator<XWikiAttachment> attit = sourceDocument.getAttachmentList().iterator(); while (attit.hasNext()) { XWikiAttachment attachment = attit.next(); XWikiAttachment newattachment = (XWikiAttachment) attachment.clone(); newattachment.setDoc(this); // TODO: Why must attachment content must be set dirty --cjdelisle if (newattachment.getAttachment_content() != null) { newattachment.getAttachment_content().setContentDirty(true); } getAttachmentList().add(newattachment); } setContentDirty(true); }
public XWikiAttachment addAttachment(String fileName, byte[] data, XWikiContext context) throws XWikiException { int i = fileName.indexOf('\\'); if (i == -1) { i = fileName.indexOf('/'); } String filename = fileName.substring(i + 1); // TODO : avoid name clearing when encoding problems will be solved // JIRA : http://jira.xwiki.org/jira/browse/XWIKI-94 filename = context.getWiki().clearName(filename, false, true, context); XWikiAttachment attachment = getAttachment(filename); if (attachment == null) { attachment = new XWikiAttachment(); // TODO: Review this code and understand why it's needed. // Add the attachment in the current doc getAttachmentList().add(attachment); } attachment.setContent(data); attachment.setFilename(filename); attachment.setAuthor(context.getUser()); // Add the attachment to the document attachment.setDoc(this); return attachment; }
/** * @param rcsArchive the RCS archive to import. * @throws Exception if getting a revision from the RCS archive or deserializing an attachment from XML fails */ private void fromRCS(final Archive rcsArchive) throws Exception { if (rcsArchive == null) { return; } final Node[] nodes = rcsArchive.changeLog(); for (int i = nodes.length - 1; i > -1; i--) { final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion()); final StringBuilder content = new StringBuilder(); for (int j = 0; j < lines.length; j++) { String line = lines[j].toString(); content.append(line); if (j != lines.length - 1) { content.append("\n"); } } final XWikiAttachment rev = new XWikiAttachment(); rev.fromXML(content.toString()); rev.setDoc(this.getAttachment().getDoc()); rev.setAttachment_archive(this); // this should not be necessary, keeping to maintain behavior. rev.setVersion(nodes[i].getVersion().toString()); revisions.add(rev); } }
/** * @param rcsArchive the RCS archive to import. * @throws Exception if getting a revision from the RCS archive or deserializing an attachment from XML fails */ private void fromRCS(final Archive rcsArchive) throws Exception { if (rcsArchive == null) { return; } final Node[] nodes = rcsArchive.changeLog(); for (int i = nodes.length - 1; i > -1; i--) { final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion()); final StringBuilder content = new StringBuilder(); for (int j = 0; j < lines.length; j++) { String line = lines[j].toString(); content.append(line); if (j != lines.length - 1) { content.append("\n"); } } final XWikiAttachment rev = new XWikiAttachment(); rev.fromXML(content.toString()); rev.setDoc(getAttachment().getDoc()); rev.setAttachment_archive(this); // this should not be necessary, keeping to maintain behavior. rev.setVersion(nodes[i].getVersion().toString()); revisions.add(rev); } }
attach.setContentStore(FileSystemStoreUtils.HINT); attach.setDoc(attachment.getDoc());
delAttach.getAttachment().setDoc(context.getWiki().getDocument(documentReference, context));
List<XWikiAttachment> list = query.list(); for (XWikiAttachment attachment : list) { attachment.setDoc(doc);
attachment.setDoc(doc);
/** * Restore a {@link XWikiAttachment} from a {@link DeletedAttachment}. Note that this method does not actually * restore the attachment to its owner document, it simply recomposes an {@link XWikiAttachment} object from the * saved data. * * @return restored attachment * @param attachment optional object where to put the attachment data, if not <code>null</code> * @param context the current {@link XWikiContext context} * @throws XWikiException If an exception occurs while the Attachment is restored from the XML. See * {@link XWikiAttachment#fromXML(String)}. */ public XWikiAttachment restoreAttachment(XWikiAttachment attachment, XWikiContext context) throws XWikiException { XWikiAttachment result = attachment; if (result == null) { result = new XWikiAttachment(); } result.fromXML(getXml()); if (result.getDoc() == null || !(this.getDocName().equals(result.getDoc().getFullName()))) { result.setDoc(context.getWiki().getDocument(this.getDocName(), context)); } return result; } }
attachment.setDoc(destinationDoc);
attachment.setDoc(doc); saveAttachment(attachment, false, context, false);
/** * {@inheritDoc} * * @see DocumentAccessBridge#setAttachmentContent(String, String, byte[]) */ @Deprecated public void setAttachmentContent(String documentReference, String attachmentFilename, byte[] attachmentData) throws Exception { XWikiContext xcontext = getContext(); XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext); XWikiAttachment attachment = doc.getAttachment(attachmentFilename); if (attachment == null) { attachment = new XWikiAttachment(); doc.getAttachmentList().add(attachment); doc.setComment("Add new attachment " + attachmentFilename); } else { doc.setComment("Update attachment " + attachmentFilename); } attachment.setContent(attachmentData); attachment.setFilename(attachmentFilename); attachment.setAuthor(getCurrentUser()); attachment.setDoc(doc); doc.setAuthor(getCurrentUser()); if (doc.isNew()) { doc.setCreator(getCurrentUser()); } doc.saveAttachmentContent(attachment, xcontext); }
attachment.setComment(getComment()); attachment.setDate(getDate()); attachment.setDoc(getDoc()); attachment.setFilename(getFilename()); attachment.setFilesize(getFilesize());
/** * {@inheritDoc} * * @see org.xwiki.bridge.DocumentAccessBridge#setAttachmentContent(org.xwiki.model.reference.AttachmentReference, * byte[]) */ public void setAttachmentContent(AttachmentReference attachmentReference, byte[] attachmentData) throws Exception { XWikiContext xcontext = getContext(); XWikiDocument doc = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext); XWikiAttachment attachment = doc.getAttachment(attachmentReference.getName()); if (attachment == null) { attachment = new XWikiAttachment(); doc.getAttachmentList().add(attachment); doc.setComment("Add new attachment " + attachmentReference.getName()); } else { doc.setComment("Update attachment " + attachmentReference.getName()); } attachment.setContent(attachmentData); attachment.setFilename(attachmentReference.getName()); attachment.setAuthor(getCurrentUser()); attachment.setDoc(doc); doc.setAuthor(getCurrentUser()); if (doc.isNew()) { doc.setCreator(getCurrentUser()); } doc.saveAttachmentContent(attachment, xcontext); }
xwikiAttachment.setAuthor(Utils.getXWikiUser(componentManager)); xwikiAttachment.setFilename(attachmentName); xwikiAttachment.setDoc(xwikiDocument);
@Override public XWikiAttachment restoreAttachment(final XWikiAttachment attachment, final XWikiContext context) throws XWikiException { XWikiAttachment result = attachment; if (result != null) { // TODO Add XWikiAttachment#clone(XWikiAttachment) // this toXML does not copy content. result.fromXML(this.attachment.toXML(context)); if (this.attachment.getAttachment_content() != null) { attachment.setAttachment_content((XWikiAttachmentContent) this.attachment.getAttachment_content() .clone()); attachment.getAttachment_content().setAttachment(attachment); } if (this.attachment.getAttachment_archive() != null) { result.setAttachment_archive((XWikiAttachmentArchive) this.attachment.getAttachment_archive().clone()); result.getAttachment_archive().setAttachment(result); } } else { result = (XWikiAttachment) this.attachment.clone(); } result.setDoc(context.getWiki().getDocument(this.attachment.getReference().getDocumentReference(), context)); return result; }