attachment.setAuthor(getAuthor()); attachment.setComment(getComment()); attachment.setDate(getDate()); attachment.setDoc(getDoc()); attachment.setFilename(getFilename()); attachment.setFilesize(getFilesize()); attachment.setRCSVersion(getRCSVersion()); if (getAttachment_content() != null) { attachment.setAttachment_content((XWikiAttachmentContent) getAttachment_content().clone()); attachment.getAttachment_content().setAttachment(attachment); if (getAttachment_archive() != null) { attachment.setAttachment_archive((XWikiAttachmentArchive) getAttachment_archive().clone()); attachment.getAttachment_archive().setAttachment(attachment);
@Override public boolean storeEntity(XWikiAttachment entity) { if (entity == null) { return true; } XWikiAttachment existing = new XWikiAttachment(entity.getDoc(), entity.getFilename()); try { this.store.loadAttachmentContent(existing, this.context.get(), false); // If loading succeeded, then the attachment already exists on the filesystem; // keep using the existing attachment version and discard the database one this.logger.debug("Skipped importing already existing attachment [{}]", entity.getReference()); return true; } catch (XWikiException e) { // No such attachment on the filesystem, continue storing it } try { this.store.saveAttachmentContent(entity, false, this.context.get(), false); // The archive is also automatically stored by the call above, no need to explicitly store the archive this.logger.debug("Imported attachment [{}] into the filesystem store", entity.getReference()); return true; } catch (XWikiException ex) { this.logger.error("Failed to store attachment into the filesystem store: {}", ex.getMessage(), ex); return false; } } }
@Override public InputStream getStream() throws XWikiException { return this.attachment.getContentInputStream(this.context); } }
public XWikiAttachment(XWikiDocument doc, String filename) { this(); setDoc(doc); setFilename(filename); }
@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; }
XWikiAttachment newAttachment = new XWikiAttachment(); newAttachment.setDoc(attachment.getDoc()); newAttachment.setAuthor(attachment.getAuthor()); newAttachment.setDate(attachment.getDate()); if (!isZipFile(attachment.getContentInputStream(context))) { return attachment; ZipInputStream zis = new ZipInputStream(attachment.getContentInputStream(context)); ZipEntry entry; newAttachment.setFilename(entryName); newAttachment.setContent(data); } else { newAttachment.setContent(zis, (int) entry.getSize());
if (attachment.getReference().getDocumentReference().getSpaceReferences().size() > 1 || !isValidZipURL(url, context.getAction().trim())) XWikiAttachment newAttachment = new XWikiAttachment(); newAttachment.setDoc(attachment.getDoc(), false); newAttachment.setAuthorReference(attachment.getAuthorReference()); newAttachment.setDate(attachment.getDate()); stream = new BufferedInputStream(attachment.getContentInputStream(context)); newAttachment.setFilename(entryName); newAttachment.setContent(data); } else { newAttachment.setContent(zis, (int) entry.getSize());
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; }
if (attachment.getAuthor() != null) { attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_AUTHOR, attachment.getAuthor()); attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_COMMENT, attachment.getComment()); attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_DATE, attachment.getDate()); attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION, attachment.getVersion()); archive = attachment.loadArchive(xcontext); if (archive != null) { attachmentParameters.put(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS, this.logger.error("Attachment [{}] has malformed history", attachment.getReference(), e); Long size; try { content = attachment.getContentInputStream(xcontext); size = Long.valueOf(attachment.getFilesize()); } catch (XWikiException e) { this.logger.error("Failed to get content of attachment [{}]", attachment.getReference(), e); attachmentFilter.onWikiAttachment(attachment.getFilename(), content, size, attachmentParameters);
solrDocument.addField(FieldUtils.FILENAME, attachment.getFilename()); solrDocument.addField(FieldUtils.MIME_TYPE, attachment.getMimeType(xcontext)); solrDocument.addField(FieldUtils.ATTACHMENT_DATE, attachment.getDate()); solrDocument.addField(FieldUtils.ATTACHMENT_SIZE, attachment.getLongSize()); String authorStringReference = entityReferenceSerializer.serialize(attachment.getAuthorReference()); solrDocument.addField(FieldUtils.ATTACHMENT_AUTHOR, authorStringReference); try { String authorDisplayName = xcontext.getWiki().getPlainUserName(attachment.getAuthorReference(), xcontext); solrDocument.addField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY, authorDisplayName); } catch (Exception e) { this.logger.error("Failed to get author display name for attachment [{}]", attachment.getReference(), e);
protected Attachment newAttachment(Attachment att, XWikiAttachment xAtt, XWikiContext context) { att.setAttDate(xAtt.getDate().getTime()); att.setAuthor(xAtt.getAuthor()); att.setFilename(xAtt.getFilename()); att.setId(xAtt.getId()); att.setImage(xAtt.isImage(context)); att.setMimeType(xAtt.getMimeType(context)); att.setLongSize(xAtt.getLongSize()); att.setDownloadUrl(context.getWiki().getExternalAttachmentURL(xAtt.getDoc().getFullName(), xAtt.getFilename(), context)); return att; }
public AttachmentData(XWikiAttachment attachment, XWikiContext context, boolean deleted) { super(LucenePlugin.DOCTYPE_ATTACHMENT, attachment.getDoc(), context, deleted); setModificationDate(attachment.getDate()); setAuthor(attachment.getAuthor()); setSize(attachment.getFilesize()); setFilename(attachment.getFilename()); setMimeType(attachment.getMimeType(context)); }
el.addText(getFilename()); wr.write(el); el.addText("" + getFilesize()); wr.write(el); el.addText(getAuthor()); wr.write(el); long d = getDate().getTime(); el = new DOMElement("date"); el.addText("" + d); el.addText(getVersion()); wr.write(el); el.addText(getComment()); wr.write(el); loadContent(context); XWikiAttachmentContent acontent = getAttachment_content(); if (acontent != null) { wr.writeBase64(el, getAttachment_content().getContentInputStream()); } else { el.addText(""); XWikiAttachmentArchive aarchive = loadArchive(context); if (aarchive != null) { el = new DOMElement("versions");
public void queueAttachment(XWikiAttachment attachment, XWikiContext context, boolean deleted) { if (attachment != null && context != null) { this.queue.add(new AttachmentData(attachment, context, deleted)); } else { LOGGER.error("Invalid parameters given to {} attachment [{}] of document [{}]", new Object[] { deleted ? "deleted" : "added", attachment == null ? null : attachment.getFilename(), attachment == null || attachment.getDoc() == null ? null : attachment.getDoc().getDocumentReference()}); } }
/** * @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); } }
XWikiAttachment xwikiAttachment = (XWikiAttachment) fields[3]; String mimeType = xwikiAttachment.getMimeType(Utils.getXWikiContext(componentManager)); attachment.setId(String.format("%s@%s", pageId, xwikiAttachment.getFilename())); attachment.setName(xwikiAttachment.getFilename()); attachment.setSize(xwikiAttachment.getFilesize()); attachment.setMimeType(mimeType); attachment.setAuthor(xwikiAttachment.getAuthor()); calendar.setTime(xwikiAttachment.getDate()); attachment.setDate(calendar); attachment.setVersion(xwikiAttachment.getVersion()); .getXWikiContext(componentManager) .getURLFactory() .createAttachmentURL(xwikiAttachment.getFilename(), pageSpace, pageName, "download", null, wikiName, Utils.getXWikiContext(componentManager)); attachment.setXwikiAbsoluteUrl(absoluteUrl.toString()); .build(wikiName, pageSpace, pageName, xwikiAttachment.getFilename()).toString(); Link attachmentLink = objectFactory.createLink(); attachmentLink.setHref(attachmentUri);
@Override public void serialize(final XWikiAttachment attach, final XMLWriter writer) throws IOException { final Element docel = new DOMElement(ROOT_ELEMENT_NAME); docel.addAttribute(SERIALIZER_PARAM, THIS_SERIALIZER); writer.writeOpen(docel); writer.write(new DOMElement(FILENAME).addText(attach.getFilename())); writer.write(new DOMElement(FILESIZE).addText(attach.getFilesize() + "")); writer.write(new DOMElement(AUTHOR).addText(attach.getAuthor())); writer.write(new DOMElement(VERSION).addText(attach.getVersion())); writer.write(new DOMElement(COMMENT).addText(attach.getComment())); writer.write(new DOMElement(DATE).addText(attach.getDate().getTime() + "")); writer.writeClose(docel); } }
@Override public XWikiAttachment parse(final Element docel) throws IOException { if (!ROOT_ELEMENT_NAME.equals(docel.getName())) { throw new IOException("XML not recognizable as attachment metadata, expecting <attachment> tag"); } if (docel.attribute(SERIALIZER_PARAM) == null || !THIS_SERIALIZER.equals(docel.attribute(SERIALIZER_PARAM).getValue())) { throw new IOException("Cannot parse this attachment metadata, it was saved with a different " + "serializer."); } final XWikiAttachment out = new XWikiAttachment(); out.setFilename(docel.element(FILENAME).getText()); out.setFilesize(Integer.parseInt(docel.element(FILESIZE).getText())); out.setAuthor(docel.element(AUTHOR).getText()); out.setVersion(docel.element(VERSION).getText()); out.setComment(docel.element(COMMENT).getText()); final String sdate = docel.element(DATE).getText(); final Date date = new Date(Long.parseLong(sdate)); out.setDate(date); return out; }
@Override public void serialize(final XWikiAttachment attach, final XMLWriter writer) throws IOException { final Element docel = new DOMElement(ROOT_ELEMENT_NAME); docel.addAttribute(SERIALIZER_PARAM, THIS_SERIALIZER); writer.writeOpen(docel); writer.write(new DOMElement(FILENAME).addText(attach.getFilename())); writer.write(new DOMElement(FILESIZE).addText(attach.getLongSize() + "")); writer.write(new DOMElement(AUTHOR).addText(attach.getAuthor())); writer.write(new DOMElement(VERSION).addText(attach.getVersion())); writer.write(new DOMElement(COMMENT).addText(attach.getComment())); writer.write(new DOMElement(DATE).addText(attach.getDate().getTime() + "")); writer.writeClose(docel); } }