I am trying to access an attachment on the server. My use case involves uploading a CSV file and converting the entries into documents.
I have found several threads here regarding the attachment, but every response suggests that I need to download the file. I don’t understand this because I have just uploaded it and want to use it after document creation. All internal implementations capable of returning the file are either deprecated or unavailable, as far as I have found. 
The only method I’ve discovered to achieve what I want is to use the AttachmentAfterUpload event, but I can’t be certain that the user won’t cancel the operation at this point.
Is there a way to access the file and retrieve its contents?
If you want to access an already saved attachment in the backend using the content id this solution is probably the easiest way using non-internal A12 components.
But
My use case involves uploading a CSV file and converting the entries into documents.
sounds like not using the ContentStore but instead creating a custom REST endpoint for the upload may be a better solution. There you can parse the file & create the documents without needing to write and read the attachment to the filesystem
My ideal scenario is to use the DocumentAfterCreateEvent because we are also storing some metadata with the upload.
Certainly, a custom endpoint and a custom component could address that issue. However, we’re interested in knowing how to handle this in A12.
I already came across the post you linked, but it felt like more of a workaround. Is there another reason for the service? I’m curious why you would rename “attachment” to “content” if it’s an attachment service.
Moin @soeren-round-ridge,
if you want to access the attachment that has been created right before the DocumentAfterCreateEvent, you could try the following:
package com.mgmtp.a12.template.server.attachment;
import com.mgmtp.a12.contentstore.service.ContentStoreService;
import com.mgmtp.a12.dataservices.document.events.DocumentAfterCreateEvent;
import com.mgmtp.a12.dataservices.events.DataServicesEventListener;
import com.mgmtp.a12.kernel.md.document.api.IDocument;
import com.mgmtp.a12.kernel.md.document.api.IFieldInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@Component
public class AttachmentListener {
@Autowired
ContentStoreService contentStoreService;
@DataServicesEventListener
public void beforeDocumentCreate(DocumentAfterCreateEvent event) throws IOException {
String attachmentId = getFieldInstanceFromDocument(event.getDataServicesDocument().getKernelDocument(), "/Person/PersonalData/Photo/attachment_id").getValue().get().toString();
String contentUrl = contentStoreService.requestContentUrl(attachmentId, 60L);
String ticketId = contentUrl.substring(contentUrl.lastIndexOf("/") + 1);
byte[] bytes = contentStoreService.getContent(ticketId).getContentSupplier().get().readAllBytes();
String result = new String(bytes, StandardCharsets.UTF_8);
// Do something with the attachment content "result"
System.out.println(result);
}
private IFieldInstance getFieldInstanceFromDocument(IDocument document, String path) {
return document.getEntityInstances().stream()
.filter(instance -> instance.getPath().equals(path))
.findFirst()
.map(IFieldInstance.class::cast)
.orElseThrow();
}
}
Please also check if you have configured the content to be stored in the content store database. Per default, the attachment content is stored on file system (~/a12/dataservices/).
You could change the behaviour by configuring the following:
mgmtp.a12.dataservices.contentstore.storage.contentStorage=DB
Like @tim-steep-bit mentioned, otherwise it’s not possible to access the attachment’s content. The AttachmentService only provides functionality for retrieving either the attachment- or thumbnail url.
If you want to access the actual attachment’s content, you use the ContentStoreService.
(Depends on if you are using the content store, and have it configured)