/** * Creates an {@link AttachmentReference} from a file name and a reference to the document holding that file. * * @param documentReference a reference to the document the file is attached to * @param fileName the name of a file attached to a document * @return a reference to the specified attachment * @since 2.5M2 */ public AttachmentReference createAttachmentReference(DocumentReference documentReference, String fileName) { return new AttachmentReference(fileName, documentReference); }
private boolean attachmentExists(AttachmentReference attachment) { try { return this.bridge.getAttachmentReferences(attachment.getDocumentReference()).stream() .anyMatch(i -> StringUtils.equals(i.getName(), attachment.getName())); } catch (Exception e) { return false; } }
public DocumentReference getDocumentReference() { return (DocumentReference) extractReference(EntityType.DOCUMENT); } }
@Override public AttachmentReference replaceParent(EntityReference newParent) { if (newParent == getParent()) { return this; } return new AttachmentReference(this, newParent); } }
throws Exception AttachmentReference attachmentReference = new AttachmentReference(entityReference); XWikiDocument originalDocument = getDocument(attachmentReference.getDocumentReference()); XWikiAttachment attachment = originalDocument.getAttachment(attachmentReference.getName()); if (attachment == null) { return false;
private void migrateAttachments(File documentContentDirectory, DocumentReference documentReference) throws IOException { File attachmentsDirectory = new File(documentContentDirectory, FilesystemStoreTools.ATTACHMENTS_DIR_NAME); if (attachmentsDirectory.isDirectory()) { for (File oldAttachmentDirectory : attachmentsDirectory.listFiles()) { if (oldAttachmentDirectory.isDirectory()) { AttachmentReference attachmentReference = new AttachmentReference( FileSystemStoreUtils.decode(oldAttachmentDirectory.getName()), documentReference); File newAttachmentDirectory = this.fstools.getAttachmentDir(attachmentReference); this.logger.info("Moving attachment folder [{}] to new location [{}]", oldAttachmentDirectory, newAttachmentDirectory); FileUtils.moveDirectory(oldAttachmentDirectory, newAttachmentDirectory); migrateAttachmentFiles(newAttachmentDirectory, attachmentReference.getName()); } } } }
@Override protected boolean isFile(AttachmentReference attachmentReference) { File attachmentFolder = getAttachmentDir(attachmentReference); return new File(attachmentFolder, FileSystemStoreUtils.encode(attachmentReference.getName(), false)).exists(); } }
@Override public String view(AttachmentReference attachmentReference, Map<String, String> parameters) { // Clear previous caught exception. this.execution.getContext().removeProperty(OFFICE_VIEW_EXCEPTION); try { DocumentReference documentReference = attachmentReference.getDocumentReference(); // Check whether current user has view rights on the document containing the attachment. if (!this.documentAccessBridge.isDocumentViewable(documentReference)) { throw new RuntimeException("Inadequate privileges."); } // Create the view and render the result. Syntax fromSyntax = this.documentAccessBridge.getTranslatedDocumentInstance(documentReference).getSyntax(); Syntax toSyntax = Syntax.XHTML_1_0; return render(this.officeViewer.createView(attachmentReference, parameters), fromSyntax, toSyntax); } catch (Exception e) { // Save caught exception. this.execution.getContext().setProperty(OFFICE_VIEW_EXCEPTION, e); this.logger.error("Failed to view office document: " + attachmentReference, e); return null; } }
private void migrateDeletedAttachments(File documentContentDirectory, DocumentReference documentReference) throws IOException { File deletedAttachmentsDirectory = new File(documentContentDirectory, FilesystemStoreTools.DELETED_ATTACHMENTS_DIR_NAME); if (deletedAttachmentsDirectory.isDirectory()) { for (File oldDeletedAttachmentDirectory : deletedAttachmentsDirectory.listFiles()) { if (oldDeletedAttachmentDirectory.isDirectory()) { String folderName = FileSystemStoreUtils.decode(oldDeletedAttachmentDirectory.getName()); // Parse <attachmentName>-id<id> int index = folderName.lastIndexOf('-'); String attachmentName = folderName.substring(0, index); long id = Long.parseLong(folderName.substring(index + 3)); AttachmentReference attachmentReference = new AttachmentReference(attachmentName, documentReference); File newDeletedAttachmentDirectory = this.fstools.getDeletedAttachmentDir(attachmentReference, id); this.logger.info("Moving deleted attachment folder [{}] to new location [{}]", oldDeletedAttachmentDirectory, newDeletedAttachmentDirectory); FileUtils.moveDirectory(oldDeletedAttachmentDirectory, newDeletedAttachmentDirectory); migrateAttachmentFiles(newDeletedAttachmentDirectory, attachmentReference.getName()); } } } }
/** * Get an instance of AttachmentFileProvider which will save everything to do with an attachment in a separate * location which is repeatable only with the same attachment name, and containing document. * * @param attachmentReference the reference attachment to get a tools for. * @return a provider which will provide files with collision free path and repeatable with same inputs. * @since 9.10RC1 */ public AttachmentFileProvider getAttachmentFileProvider(final AttachmentReference attachmentReference) { return new DefaultAttachmentFileProvider(getAttachmentDir(attachmentReference), attachmentReference.getName()); }
if (attachment.getReference().getDocumentReference().getSpaceReferences().size() > 1 || !isValidZipURL(url, context.getAction().trim()))
@Override public AttachmentReference replaceParent(EntityReference oldParent, EntityReference newParent) { if (newParent == oldParent) { return this; } return new AttachmentReference(this, oldParent, newParent); }
/** * @param attachmentReference the attachment reference * @return the attachment directory * @since 11.0 */ public File getAttachmentDir(final AttachmentReference attachmentReference) { final File docDir = getDocumentContentDir(attachmentReference.getDocumentReference()); final File attachmentsDir = new File(docDir, ATTACHMENTS_DIR_NAME); return hashDirectory(attachmentsDir, attachmentReference.getName()); }
@Override protected boolean isFile(AttachmentReference attachmentReference) { return new DefaultAttachmentFileProvider(getAttachmentDir(attachmentReference), attachmentReference.getName()) .getAttachmentVersioningMetaFile().exists(); } }
@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; }
/** * @return the document reference contained in this attachment reference */ @Transient public DocumentReference getDocumentReference() { return (DocumentReference) extractReference(EntityType.DOCUMENT); }
/** * Creates an {@link AttachmentReference} from a file name and a reference to the document holding that file. * * @param documentReference a reference to the document the file is attached to * @param fileName the name of a file attached to a document * @return a reference to the specified attachment * @since 2.5M2 */ public AttachmentReference createAttachmentReference(DocumentReference documentReference, String fileName) { return new AttachmentReference(fileName, documentReference); }
protected File getAttachmentDir(final AttachmentReference attachmentReference) { final File docDir = getDocumentDir(attachmentReference.getDocumentReference()); final File attachmentsDir = new File(docDir, FilesystemStoreTools.ATTACHMENTS_DIR_NAME); try { return new File(attachmentsDir, URLEncoder.encode(attachmentReference.getName(), "UTF8")); } catch (UnsupportedEncodingException e) { throw new HibernateException("UTF8 is unknown", e); } } }
@Override protected boolean isFile(AttachmentReference attachmentReference) { return new DefaultAttachmentFileProvider(getAttachmentDir(attachmentReference), attachmentReference.getName()) .getAttachmentContentFile().exists(); } }
@Override public AttachmentReference resolve(EntityReference attachmentReferenceRepresentation, Object... parameters) { if (attachmentReferenceRepresentation instanceof AttachmentReference) { return (AttachmentReference) attachmentReferenceRepresentation; } return new AttachmentReference(this.entityReferenceResolver.resolve(attachmentReferenceRepresentation, EntityType.ATTACHMENT, parameters)); } }