@Override public long getDocId() { // TODO deprecate me. if (this.attachment != null && this.attachment.getDoc() != null) { return this.attachment.getDocId(); } return 0; }
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()}); } }
public synchronized Version[] getVersions() { try { return getAttachment_archive().getVersions(); } catch (Exception ex) { LOG.warn(String.format("Cannot retrieve versions of attachment [%s@%s]: %s", getFilename(), getDoc() .getFullName(), ex.getMessage())); return new Version[] {new Version(this.getVersion())}; } }
/** * A constructor with all the information about the deleted attachment. * * @param attachment Deleted attachment. * @param deleter User which deleted the attachment. * @param deleteDate Date of delete action. * @param context The current context. Used for determining the encoding. * @throws XWikiException If the attachment cannot be exported to XML. */ public DeletedAttachment(XWikiAttachment attachment, String deleter, Date deleteDate, XWikiContext context) throws XWikiException { this.docId = attachment.getDocId(); this.docName = attachment.getDoc().getFullName(); this.filename = attachment.getFilename(); this.deleter = deleter; this.date = deleteDate; setAttachment(attachment, context); }
/** * Get the directory for storing files for an attachment. * This will look like storage/xwiki/Main/WebHome/~this/attachments/file.name/ * * @param attachment the attachment to get the directory for. * @return a File representing the directory. Note: The directory may not exist. */ private File getAttachmentDir(final XWikiAttachment attachment) { final XWikiDocument doc = attachment.getDoc(); if (doc == null) { throw new NullPointerException("Could not store attachment because it is not " + "associated with a document."); } final File docDir = getDocumentDir(doc.getDocumentReference(), this.storageDir, this.pathSerializer); final File attachmentsDir = new File(docDir, ATTACHMENT_DIR_NAME); return new File(attachmentsDir, GenericFileUtils.getURLEncoded(attachment.getFilename())); }
/** * Get a directory for storing the contentes of a deleted attachment. * The format is <document name>/~this/deleted-attachments/<attachment name>-<delete date>/ * <delete date> is expressed in "unix time" so it might look like: * WebHome/~this/deleted-attachments/file.txt-0123456789/ * * @param attachment the attachment to get the file for. * @param deleteDate the date the attachment was deleted. * @return a directory which will be repeatable only with the same inputs. */ private File getDeletedAttachmentDir(final XWikiAttachment attachment, final Date deleteDate) { final XWikiDocument doc = attachment.getDoc(); if (doc == null) { throw new NullPointerException("Could not store deleted attachment because " + "it is not attached to any document."); } final File docDir = getDocumentDir(doc.getDocumentReference(), this.storageDir, this.pathSerializer); final File deletedAttachmentsDir = new File(docDir, DELETED_ATTACHMENT_DIR_NAME); final String fileName = attachment.getFilename() + DELETED_ATTACHMENT_NAME_SEPARATOR + deleteDate.getTime(); return new File(deletedAttachmentsDir, GenericFileUtils.getURLEncoded(fileName)); }
/** * {@inheritDoc} */ public void deleteArchive(final XWikiAttachment attachment, final XWikiContext context, boolean bTransaction) throws XWikiException { try { executeWrite(context, bTransaction, new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException, XWikiException { XWikiAttachmentArchive archive = new XWikiAttachmentArchive(); archive.setAttachment(attachment); session.delete(archive); return null; } }); } catch (Exception e) { if (LOG.isWarnEnabled()) { LOG.warn(String.format("Error deleting attachment archive [%s] of doc [%s]", attachment.getFilename(), attachment.getDoc().getFullName()), e); } } } }
@Override public XWikiAttachmentArchive loadArchive(final XWikiAttachment attachment, final XWikiContext context, final boolean bTransaction) throws XWikiException { try { return this.loadArchive(attachment, this.fileTools.getAttachmentFileProvider(attachment)); } catch (Exception e) { if (e instanceof XWikiException) { throw (XWikiException) e; } final Object[] args = {attachment.getFilename(), UNKNOWN_NAME}; if (attachment.getDoc() != null) { args[1] = attachment.getDoc().getFullName(); } throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, "Exception while loading attachment archive {0} for document {1}", e, args); } }
@Override protected void onRun() throws Exception { // TODO: When the rest of storage is rewritten using TransactionRunnable, // this method should be disolved. final Session session = this.context.getWiki().getHibernateStore().getSession(this.context); // Delete the content from the attachment. // In case it was stored in the database by XWikiHibernateAttachmentStore. session.delete(new XWikiAttachmentContent(this.attachment)); // Update the document if required. if (this.updateDocument) { final String filename = this.attachment.getFilename(); final List<XWikiAttachment> list = attachment.getDoc().getAttachmentList(); for (int i = 0; i < list.size(); i++) { if (filename.equals(list.get(i).getFilename())) { list.remove(i); break; } } this.context.getWiki().getStore().saveXWikiDoc(this.attachment.getDoc(), this.context, false); } // Delete the attachment metadata. session.delete(this.attachment); } }
@Override public XWikiAttachmentArchive loadArchive(final XWikiAttachment attachment, final XWikiContext context, final boolean bTransaction) throws XWikiException { try { return this.loadArchive(attachment, this.fileTools.getAttachmentFileProvider(attachment.getReference())); } catch (Exception e) { if (e instanceof XWikiException) { throw (XWikiException) e; } final Object[] args = { attachment.getFilename(), UNKNOWN_NAME }; if (attachment.getDoc() != null) { args[1] = attachment.getDoc().getFullName(); } throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, "Exception while loading attachment archive {0} for document {1}", e, args); } }
@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 protected void onRun() throws Exception { // TODO: When the rest of storage is rewritten using TransactionRunnable, // this method should be disolved. final Session session = this.context.getWiki().getHibernateStore().getSession(this.context); // Delete the content from the attachment. // In case it was stored in the database by XWikiHibernateAttachmentStore. session.delete(new XWikiAttachmentContent(this.attachment)); // Update the document if required. if (this.updateDocument) { final String filename = this.attachment.getFilename(); final List<XWikiAttachment> list = attachment.getDoc().getAttachmentList(); for (int i = 0; i < list.size(); i++) { if (filename.equals(list.get(i).getFilename())) { list.remove(i); break; } } this.context.getWiki().getStore().saveXWikiDoc(this.attachment.getDoc(), this.context, false); } // Delete the attachment metadata. session.delete(this.attachment); } }
@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; }
/** * 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; } }
/** * Deletes the given attachment from it's document. * * @param attachment xwiki attachment. * @throws DavException if an error occurs while accessing the wiki. */ public void deleteAttachment(XWikiAttachment attachment) throws DavException { try { XWikiDocument document = attachment.getDoc(); document.removeAttachment(attachment); this.xwikiContext.getWiki().saveDocument(document, "Deleted attachment [" + attachment.getFilename() + "]", this.xwikiContext); } catch (XWikiException ex) { throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, ex); } }
@Override public void spool(OutputContext outputContext) throws IOException { // Protect against direct url referencing. if (!getContext().hasAccess("view", attachment.getDoc().getFullName())) { throw new IOException("Access rights violation."); } outputContext.setContentLanguage(attachment.getDoc().getLanguage()); outputContext.setContentLength(attachment.getLongSize()); outputContext.setContentType(getContext().getMimeType(attachment)); outputContext.setETag(DavConstants.modificationDateFormat.format(getModificationTime())); outputContext.setModificationTime(getModificationTime()); if (exists()) { OutputStream out = outputContext.getOutputStream(); if (null != out) { try { out.write(getContext().getContent(attachment)); out.flush(); } catch (DavException ex) { throw new IOException(ex.getMessage()); } } } }
/** * Set the locale to all the translations that the owning document has. This ensures that this entity is found for * all the translations of a document, not just the original document. * <p> * Also, index the content with each locale so that the right analyzer is used. * * @param attachment the attachment. * @param solrDocument the Solr document where to add the fields. * @throws Exception if problems occur. */ protected void setLocaleAndContentFields(XWikiAttachment attachment, SolrInputDocument solrDocument) throws Exception { String attachmentTextContent = getContentAsText(attachment); // Do the work for each locale. for (Locale documentLocale : getLocales(attachment.getDoc(), null)) { solrDocument.addField(FieldUtils.LOCALES, documentLocale.toString()); solrDocument.setField(FieldUtils.getFieldName(FieldUtils.ATTACHMENT_CONTENT, documentLocale), attachmentTextContent); } // We can't rely on the schema's copyField here because we would trigger it for each language. Doing the copy to // the text_general field manually. solrDocument.setField(FieldUtils.getFieldName(FieldUtils.ATTACHMENT_CONTENT, null), attachmentTextContent); } }
@Override public void move(DavResource destination) throws DavException { getContext().checkAccess("edit", attachment.getDoc().getFullName()); if (destination instanceof DavAttachment) { DavAttachment dAttachment = (DavAttachment) destination; // Check if this is a rename operation. if (dAttachment.getCollection().equals(getCollection())) { getContext().moveAttachment(attachment, attachment.getDoc(), dAttachment.getDisplayName()); } else if (dAttachment.getCollection() instanceof DavPage) { XWikiDocument dDoc = ((DavPage) dAttachment.getCollection()).getDocument(); getContext().moveAttachment(attachment, dDoc, dAttachment.getDisplayName()); } else { throw new DavException(DavServletResponse.SC_BAD_REQUEST); } } else { throw new DavException(DavServletResponse.SC_BAD_REQUEST); } clearCache(); }
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)); }
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; }