@Override public boolean discardEntity(XWikiAttachment entity) { boolean transaction = false; try { transaction = ((XWikiHibernateBaseStore) this.store).beginTransaction(this.context.get()); Session session = ((XWikiHibernateBaseStore) this.store).getSession(this.context.get()); session.delete(entity.getAttachment_content()); session.delete(entity.getAttachment_archive()); this.logger.debug("Deleted attachment [{}] from the database", entity.getReference()); } catch (XWikiException ex) { this.logger.warn("Failed to cleanup attachment from the database: {}", ex.getMessage()); return false; } finally { if (transaction) { ((XWikiHibernateBaseStore) this.store).endTransaction(this.context.get(), transaction); } } return true; }
@Override public boolean discardEntity(XWikiAttachment entity) { boolean transaction = false; try { transaction = ((XWikiHibernateBaseStore) this.store).beginTransaction(this.context.get()); Session session = ((XWikiHibernateBaseStore) this.store).getSession(this.context.get()); session.delete(entity.getAttachment_content()); session.delete(entity.getAttachment_archive()); this.logger.debug("Deleted attachment [{}] from the database", entity.getReference()); } catch (XWikiException ex) { this.logger.warn("Failed to cleanup attachment from the database: {}", ex.getMessage()); return false; } finally { if (transaction) { ((XWikiHibernateBaseStore) this.store).endTransaction(this.context.get(), transaction); } } return true; }
/** * 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); }
private void resizeImageToAttachment(InputStream imageFileInputStream, int width, int height, XWikiAttachment outputAttachment) throws IOException { OutputStream attachmentOutputStream = null; try { Image originalImage = imageProcessor.readImage(imageFileInputStream); RenderedImage resizedImage = imageProcessor.scaleImage(originalImage, width, height); attachmentOutputStream = outputAttachment.getAttachment_content().getContentOutputStream(); imageProcessor.writeImage(resizedImage, "image/png", 1.0f, attachmentOutputStream); } finally { // Close the attachment output stream since we are done writing to it. if (attachmentOutputStream != null) { IOUtils.closeQuietly(attachmentOutputStream); } } } }
@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; }
attachment.setFilesize(getFilesize()); attachment.setRCSVersion(getRCSVersion()); if (getAttachment_content() != null) { attachment.setAttachment_content((XWikiAttachmentContent) getAttachment_content().clone()); attachment.getAttachment_content().setAttachment(attachment);
/** * Get a TransactionRunnable for saving the attachment content. If {@link XWikiAttachment#getAttachment_content()} * yields null, this runnable will do nothing. * * @param attachment the XWikiAttachment whose content should be saved. * @param updateDocument whether or not to update the document at the same time. * @param context the XWikiContext for the request. * @return a TransactionRunnable for saving the attachment content in an XWikiHibernateTransaction. * @throws XWikiException if thrown by AttachmentSaveTransactionRunnable() */ private TransactionRunnable<XWikiHibernateTransaction> getAttachmentContentSaveRunnable( final XWikiAttachment attachment, final boolean updateDocument, final XWikiContext context) throws XWikiException { final XWikiAttachmentContent content = attachment.getAttachment_content(); if (content == null) { // If content does not exist we should not blank the attachment. return new TransactionRunnable<>(); } // This is the permanent location where the attachment content will go. final File attachFile = this.fileTools.getAttachmentFileProvider(attachment.getReference()).getAttachmentContentFile(); return new AttachmentSaveTransactionRunnable(attachment, updateDocument, context, attachFile, this.fileTools.getTempFile(attachFile), this.fileTools.getBackupFile(attachFile), this.fileTools.getLockForFile(attachFile)); }
loadAttachmentContent(attachment, context, false); try { session.delete(attachment.getAttachment_content()); } catch (Exception e) { if (log.isWarnEnabled()) {
oldAttachmentRevision.getAttachment_content().setContentDirty(true); replaceAttachmentInPlace(rolledbackDoc, oldAttachmentRevision); restoredAttachmentRevision.setVersion(restoredAttachment.getVersion()); restoredAttachmentRevision.setMetaDataDirty(true); restoredAttachmentRevision.getAttachment_content().setContentDirty(true); replaceAttachmentInPlace(rolledbackDoc, restoredAttachmentRevision); } else {
throws XWikiException final XWikiAttachmentContent content = attachment.getAttachment_content();
attachment.getAttachment_content().setContentDirty(false); xwiki.getAttachmentStore().saveAttachmentContent(attachment, false, context, true);
XWikiAttachmentContent acontent = getAttachment_content(); if (acontent != null) { wr.writeBase64(el, getAttachment_content().getContentInputStream()); } else { el.addText("");
attachment.getAttachment_content().setContentDirty(false);
XWikiAttachmentContent content = attachment.getAttachment_content(); if (bTransaction) { checkHibernate(context);
xa.getAttachment_content().setContentDirty(false);