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")));
});
}
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));
}
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()));
}
}
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;
}
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();
}
Aggregations