Search in sources :

Example 6 with BinaryAttachment

use of com.enonic.xp.node.BinaryAttachment in project xp by enonic.

the class DumpServiceImplTest method dumpAndLoadWithAttachments.

@Test
public void dumpAndLoadWithAttachments() throws Exception {
    NodeHelper.runAsAdmin(() -> {
        final Repository newRepo = NodeHelper.runAsAdmin(() -> doCreateRepository(RepositoryId.from("new-repo"), AccessControlList.create().add(AccessControlEntry.create().principal(RoleKeys.EVERYONE).allowAll().build()).build(), ChildOrder.manualOrder()));
        final PropertyTree data = new PropertyTree();
        data.addBinaryReference("attachmentName", BinaryReference.from("image.png"));
        final UpdateRepositoryEntryParams updateParams = UpdateRepositoryEntryParams.create().repositoryId(newRepo.getId()).repositoryData(data).attachments(ImmutableList.of(new BinaryAttachment(BinaryReference.from("image.png"), ByteSource.wrap("attachmentName".getBytes())))).build();
        repositoryEntryService.updateRepositoryEntry(updateParams);
    });
    NodeHelper.runAsAdmin(() -> this.dumpService.dump(SystemDumpParams.create().dumpName("testDump").build()));
    NodeHelper.runAsAdmin(() -> {
        dumpDeleteAndLoad(true);
        final AttachedBinaries attachedBinaries = repositoryEntryService.getRepositoryEntry(RepositoryId.from("new-repo")).getAttachments();
        assertEquals(1, attachedBinaries.getSize());
        assertNotNull(attachedBinaries.getByBinaryReference(BinaryReference.from("image.png")));
    });
}
Also used : Repository(com.enonic.xp.repository.Repository) PropertyTree(com.enonic.xp.data.PropertyTree) AttachedBinaries(com.enonic.xp.node.AttachedBinaries) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) UpdateRepositoryEntryParams(com.enonic.xp.repo.impl.repository.UpdateRepositoryEntryParams) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 7 with BinaryAttachment

use of com.enonic.xp.node.BinaryAttachment in project xp by enonic.

the class RepositoryServiceImplTest method update_attachment.

@Test
void update_attachment() throws Exception {
    final String repoId = "repo-with-attachment";
    doCreateRepo(repoId);
    final BinaryReference binaryRef = BinaryReference.from("image1.jpg");
    ByteSource binarySource = ByteSource.wrap("this-is-the-binary-data-for-image1".getBytes());
    Context mockCurrentContext = ContextBuilder.create().branch("master").repositoryId(repoId).authInfo(REPO_TEST_DEFAULT_USER_AUTHINFO).build();
    PropertyTree data = new PropertyTree();
    data.setBinaryReference("someIcon", binaryRef);
    mockCurrentContext.runWith(() -> repositoryService.updateRepository(UpdateRepositoryParams.create().repositoryId(RepositoryId.from(repoId)).editor(edit -> {
        edit.data = data;
        edit.binaryAttachments = ImmutableList.of(new BinaryAttachment(binaryRef, binarySource));
    }).build()));
    createAdminContext().runWith(() -> {
        repositoryService.invalidateAll();
    });
    ByteSource persistedAttachment = mockCurrentContext.callWith(() -> repositoryService.getBinary(RepositoryId.from(repoId), BinaryReference.from("image1.jpg")));
    assertTrue(binarySource.contentEquals(persistedAttachment));
}
Also used : Context(com.enonic.xp.context.Context) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IdProviderKey(com.enonic.xp.security.IdProviderKey) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) DeleteRepositoryParams(com.enonic.xp.repository.DeleteRepositoryParams) Node(com.enonic.xp.node.Node) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Branch(com.enonic.xp.branch.Branch) NodeHelper(com.enonic.xp.repo.impl.node.NodeHelper) RepositoryId(com.enonic.xp.repository.RepositoryId) ImmutableList(com.google.common.collect.ImmutableList) CreateBranchParams(com.enonic.xp.repository.CreateBranchParams) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ContextAccessor(com.enonic.xp.context.ContextAccessor) ContextBuilder(com.enonic.xp.context.ContextBuilder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) SystemConstants(com.enonic.xp.security.SystemConstants) PropertyTree(com.enonic.xp.data.PropertyTree) BinaryReference(com.enonic.xp.util.BinaryReference) DeleteBranchParams(com.enonic.xp.repository.DeleteBranchParams) User(com.enonic.xp.security.User) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) NodePath(com.enonic.xp.node.NodePath) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AccessControlList(com.enonic.xp.security.acl.AccessControlList) CreateRepositoryParams(com.enonic.xp.repository.CreateRepositoryParams) UpdateRepositoryParams(com.enonic.xp.repository.UpdateRepositoryParams) Test(org.junit.jupiter.api.Test) PrincipalKey(com.enonic.xp.security.PrincipalKey) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RoleKeys(com.enonic.xp.security.RoleKeys) Context(com.enonic.xp.context.Context) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) AccessControlEntry(com.enonic.xp.security.acl.AccessControlEntry) Repository(com.enonic.xp.repository.Repository) PropertyTree(com.enonic.xp.data.PropertyTree) ByteSource(com.google.common.io.ByteSource) BinaryReference(com.enonic.xp.util.BinaryReference) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 8 with BinaryAttachment

use of com.enonic.xp.node.BinaryAttachment in project xp by enonic.

the class BinaryAttachmentsParser method handleValue.

private void handleValue(final Object value) {
    if (value instanceof BinaryAttachment) {
        final BinaryAttachment binaryAttachment = (BinaryAttachment) value;
        this.binaryAttachmentsBuilder.add(new BinaryAttachment(binaryAttachment.getReference(), binaryAttachment.getByteSource()));
    }
}
Also used : BinaryAttachment(com.enonic.xp.node.BinaryAttachment)

Example 9 with BinaryAttachment

use of com.enonic.xp.node.BinaryAttachment in project xp by enonic.

the class ProjectServiceImpl method createProjectIcon.

private BinaryAttachment createProjectIcon(final CreateAttachment icon, final int size) throws IOException {
    if (icon != null) {
        final ByteSource source = icon.getByteSource();
        if ("image/svg+xml".equals(icon.getMimeType())) {
            return new BinaryAttachment(BinaryReference.from(icon.getName()), icon.getByteSource());
        }
        try (InputStream inputStream = source.openStream()) {
            final BufferedImage bufferedImage = ImageIO.read(inputStream);
            if (size > 0 && (bufferedImage.getWidth() >= size)) {
                final BufferedImage scaledImage = scaleWidth(bufferedImage, size);
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                ImageHelper.writeImage(out, scaledImage, ImageHelper.getFormatByMimeType(icon.getMimeType()), -1);
                return new BinaryAttachment(BinaryReference.from(icon.getName()), ByteSource.wrap(out.toByteArray()));
            }
            return new BinaryAttachment(BinaryReference.from(icon.getName()), icon.getByteSource());
        }
    }
    return null;
}
Also used : InputStream(java.io.InputStream) ByteSource(com.google.common.io.ByteSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) BufferedImage(java.awt.image.BufferedImage)

Example 10 with BinaryAttachment

use of com.enonic.xp.node.BinaryAttachment in project xp by enonic.

the class CreateNodeCommand method storeAndAttachBinaries.

private AttachedBinaries storeAndAttachBinaries() {
    final PropertyTree data = params.getData();
    final AttachedBinaries.Builder builder = AttachedBinaries.create();
    final ImmutableList<Property> binaryReferences = data.getProperties(ValueTypes.BINARY_REFERENCE);
    for (final Property binaryRef : binaryReferences) {
        final BinaryAttachment binaryAttachment = this.params.getBinaryAttachments().get(binaryRef.getBinaryReference());
        if (binaryAttachment == null) {
            throw new NodeBinaryReferenceException("No binary with reference " + binaryRef + " attached in createNodeParams");
        }
        final RepositoryId repositoryId = ContextAccessor.current().getRepositoryId();
        final AttachedBinary attachedBinary = this.binaryService.store(repositoryId, binaryAttachment);
        builder.add(attachedBinary);
    }
    return builder.build();
}
Also used : NodeBinaryReferenceException(com.enonic.xp.node.NodeBinaryReferenceException) PropertyTree(com.enonic.xp.data.PropertyTree) AttachedBinaries(com.enonic.xp.node.AttachedBinaries) Property(com.enonic.xp.data.Property) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) RepositoryId(com.enonic.xp.repository.RepositoryId) AttachedBinary(com.enonic.xp.node.AttachedBinary)

Aggregations

BinaryAttachment (com.enonic.xp.node.BinaryAttachment)11 Test (org.junit.jupiter.api.Test)5 PropertyTree (com.enonic.xp.data.PropertyTree)4 BinaryReference (com.enonic.xp.util.BinaryReference)4 ByteSource (com.google.common.io.ByteSource)4 UpdateRepositoryParams (com.enonic.xp.repository.UpdateRepositoryParams)3 AttachedBinaries (com.enonic.xp.node.AttachedBinaries)2 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)2 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)2 EditableRepository (com.enonic.xp.repository.EditableRepository)2 Repository (com.enonic.xp.repository.Repository)2 RepositoryId (com.enonic.xp.repository.RepositoryId)2 Application (com.enonic.xp.app.Application)1 Branch (com.enonic.xp.branch.Branch)1 ContentInheritType (com.enonic.xp.content.ContentInheritType)1 ContentPath (com.enonic.xp.content.ContentPath)1 Context (com.enonic.xp.context.Context)1 ContextAccessor (com.enonic.xp.context.ContextAccessor)1 ContextBuilder (com.enonic.xp.context.ContextBuilder)1 Property (com.enonic.xp.data.Property)1