public void loadContent(XWikiContext context) throws XWikiException { if (this.attachment_content == null) { try { context.getWiki().getAttachmentStore().loadAttachmentContent(this, context, true); } catch (Exception ex) { LOG.warn(String.format("Failed to load content for attachment [%s@%s]. " + "This attachment is broken, please consider re-uploading it. " + "Internal error: %s", getFilename(), (this.doc != null) ? this.doc.getFullName() : "<unknown>", ex.getMessage())); } } }
@Override public XWikiAttachment next() { Object[] item = this.data.next(); try { XWikiDocument doc = new XWikiDocument(HibernateAttachmentsReader.this.resolver.resolve(String.valueOf(item[0]))); XWikiAttachment att = new XWikiAttachment(doc, String.valueOf(item[1])); HibernateAttachmentsReader.this.store.loadAttachmentContent(att, HibernateAttachmentsReader.this.context.get(), true); HibernateAttachmentsReader.this.archiveStore.loadArchive(att, HibernateAttachmentsReader.this.context.get(), true); HibernateAttachmentsReader.this.logger.debug("Loaded [{}] from the database", att.getReference()); return att; } catch (Exception ex) { HibernateAttachmentsReader.this.logger.error("Failed to read attachment from the database store: {}", ex.getMessage(), ex); } return null; }
@Override public XWikiAttachment next() { Object[] item = this.data.next(); try { XWikiDocument doc = new XWikiDocument(HibernateAttachmentsReader.this.resolver.resolve(String.valueOf(item[0]))); XWikiAttachment att = new XWikiAttachment(doc, String.valueOf(item[1])); HibernateAttachmentsReader.this.store.loadAttachmentContent(att, HibernateAttachmentsReader.this.context.get(), true); HibernateAttachmentsReader.this.archiveStore.loadArchive(att, HibernateAttachmentsReader.this.context.get(), true); HibernateAttachmentsReader.this.logger.debug("Loaded [{}] from the database", att.getReference()); return att; } catch (Exception ex) { HibernateAttachmentsReader.this.logger.error("Failed to read attachment from the database store: {}", ex.getMessage(), ex); } return null; }
@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 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; } } }
public void loadAttachmentContent(XWikiAttachment attachment, XWikiContext context) throws XWikiException { String database = context.getDatabase(); try { // We might need to switch database to // get the translated content if (getDatabase() != null) { context.setDatabase(getDatabase()); } context.getWiki().getAttachmentStore().loadAttachmentContent(attachment, context, true); } finally { if (database != null) { context.setDatabase(database); } } }