Is there a way to programmatically set metadata of an asset? - aem

I added a custom metadata in CQ5 with name ./dc:sample. Is there a way I can programmatically set this metadata for an asset?
I've written a workflow that intercepts the uploaded assets and replaces them with inputstream sent from the third party service. I accomplish this by doing the following in my workflow.
Rendition rendition = resource.adaptTo(Rendition.class);
Asset asset = rendition.getAsset();
InputStream newInputStream = myService.sendFile(is);
asset.addRendition(rendition.getName(),newInputStream,asset.getMimeType());
Question
At this time I would like to set the ./dc:sample metadata to a string like "test checking". Is this possible to do?

You can adapt the Asset to Resource, get its jcr:content/metadata grandchild and adapt it to ModifiableValueMap:
Resource metadataRes = asset.adaptTo(Resource.class).getChild("jcr:content/metadata");
ModifiableValueMap map = metadataRes.adaptTo(ModifiableValueMap.class);
map.put("dc:sample", "test checking");
resourceResolver.commit();

Related

Azure Media Services - Download Transient Error

I have a lot of audios in my database whose URLs are like:
https://mystorage.blob.core.windows.net/mycontainer/uploaded%2F735fe9dc-e568-4920-a3ed-67230ce01991%2F5998d1f8-1795-4776-a19c-f1bc4a0d4786%2F2020-08-13T13%3A09%3A13.0996703Z?sv=2020-02-10&se=2022-01-05T16%3A58%3A50Z&sr=b&sp=r&sig=hQBPyOE92%2F67MqU%2Fe5V2NsqGzgPxogVeXQT%2BOlvbayw%3D
I am using these URLs as my JobInput, and submitting a encoding job, because I want to migrate the audios distribution to a streaming approach.
However, every time I use this kind of URL, it fails with DownloadTransientError, and a message something like while trying to download the input files, the files were not acessible.
If I manually upload a file to the blob storage with a simpler URL (https://mystorage.blob.core.windows.net/mycontainer/my-audio.wav), and use it as the JobInput, it works seamlessly. I suspect it has something to do with the special characters on the bigger URL, but I am not sure. What could be the problem?
Here is the part of the code that submits the job:
var jobInput = new JobInputHttp(new[]
{
audio.AudioUrl.ToString()
});
JobOutput[] jobOutput =
{
new JobOutputAsset(outputAssetName),
};
var job = await client.Jobs.CreateAsync(
resourceGroupName: _azureMediaServicesSettings.ResourceGroup,
accountName: _azureMediaServicesSettings.AccountName,
transformName: TransformName,
jobName: jobName,
new Job
{
Input = jobInput,
Outputs = jobOutput
});
You need to include the file name in the URL you're providing. I'll use your URL as an example, but unescape it as well so that it is more clear. The URL should be something like https://mystorage.blob.core.windows.net/mycontainer/uploaded/735fe9dc-e568-4920-a3ed-67230ce01991/5998d1f8-1795-4776-a19c-f1bc4a0d4786/2020-08-13T13:09:13.0996703Z/my-audio.wav?sv=2020-02-10&se=2022-01-05T16:58:50Z&sr=b&sp=r&sig=hQBPyOE92/67MqU/e5V2NsqGzgPxogVeXQT+Olvbayw=
Just include the actual blob name of the input video or audio file with the associated file extension.

Return Email Message from Web Api

I'm not sure if this is possible, but I need to return an email message from a web api controller. Essentially, it would then allow the user the open the file (an eml or msg), make some changes and then send it to the relevant person.
Code wise I have a service that returns a MailMessage.
I have a controller that returns a pdf, using the file's byte array as it's content, but a mail message doesn't seem the easiest thing to convert.
Is this possible? I would rather not write the message to disk first if I can help it, but I could if this is the only solution.
I have just faced the same problem, and I resolved it like this:
var stream = new MemoryStream(); // this has to contain your file content
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(stream.GetBuffer())
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("message/rfc822");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "test.eml"
};
If you want to know how to generate emails and do not send the but save them to disk instead, you can check the solution proposed here: How to save MailMessage object to disk as *.eml or *.msg file

Manage Titles when Uploading Multiple Images

It would be great if we could manage the titles of each image that we upload when uploading multiple images. This way I could select each image that I want to upload, title them, then hit the upload button. Right now one must either upload one by one or have all the selected images have the same title.
Kinda like Facebook or Panoramio where it's easy to manage the titles of the images before uploading.
This isn't natively supported in Fine Uploader at the moment, but I've opened up a feature request and tentatively scheduled it for the 3.7 milestone. In the meantime, you can certainly provide your own UI elements to allow users to provide alternate names for each upload-able item and pass these new names as a parameter. Server-side, you would have to parse this parameter and associate it with the uploaded item. Fine Uploader will have to adopt a parameter name that contains the user-supplied alternate filename anyway (and the server will have to be aware of this convention and parse this parameter), since we won't be able to change the value file input field sent along with the multipart encoded request.
use this:
var uploader = $('.uploader'),
titleBox = $('input[type=text]');
uploader.fineUploader({
request: {
endpoint: 'path/to/url'
},
formatFileName: function (name) {
var title = titleBox.val() + ' - ' + name + '';
titleBox.val('');
return title;
},
});
uploader.on('submit', function (event, id, name) {
uploader.fineUploader('setParams', {title: titleBox.val()}, id);
});

From CMIS how do I get the thumbnail placeholder for Alfresco?

I understand that the following URL will give me the thumbnail of a document or the placeholder image, if there isn't a thumbnail. This works because of the ph=true at the end.
http://cms.mydomain.com:8080/share/proxy/alfresco/api/node/workspace/SpacesStore/" + childId + "/content/thumbnails/doclib?ph=true
I have also found that the following server side code will get me the ContentStream of the thumbnail image.
OperationContext context = session.createOperationContext();
context.setRenditionFilterString("cmis:thumbnail");
CmisObject doc = session.getObject(id, context);
List<Rendition> renditions = doc.getRenditions();
if (renditions.size() > 0) {
Rendition rend = renditions.get(0);
cs = rend.getContentStream();
}
The problem with this is it does not return the placeholder image if there isn't a thumbnail, like the first URL.
For the server side how would I retrieve the appropriate placeholder image when there isn't a thumbnail? For example for docx and xlsx files.
Thanks,
Jon
I think in this case the most effective way to retrieve thumbnails (doclib or a placeholder) is to call directly the REST Alfresco Service.
I'm not 100% sure but CMIS rendition response from an Alfresco Server contains only existing renditions. There's no placeholder provided in CMIS renditions part like the one provided by the Alfresco REST url.
The key here is in if (renditions.size() > 0) -> this is exactly what the webscript does.
But what the REST webscript also does is to ad an else -> so if there aren't renditions so far, it fill determine mimetype and fetch the corresponding placeholder.
You should do the same in your code to make it fetch placeholder -> add an else and then find the placeholder for that image.

Playframework, binary pdf file attachment issue

I´ve got a pdf file as ByteArray and I want to know if there´s a way to attach it without creating the main file on the server.
The code provided by the Play documentation only allows real files to be attached.
EmailAttachment attachment = new EmailAttachment();
attachment.setDescription("A pdf document");
attachment.setPath(Play.getFile("rules.pdf").getPath());
I´m using the Playframework Mail module.
Thanks!
Since Play 1.x uses the Apache Commons Email library under the hood, you could use the MultiPartEmail#attach(DataSource ds, String name, String description) method:
import org.apache.commons.mail.*;
// create the mail
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe#somewhere.org", "John Doe");
email.setFrom("me#apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// get your inputstream from your db
InputStream is = new BufferedInputStream(MyUtils.getBlob());
DataSource source = new ByteArrayDataSource(is, "application/pdf");
// add the attachment
email.attach(source, "somefile.pdf", "Description of some file");
// send the email
email.send();
Upcoming Play Version 1.3 will introduce a method attachDataSource(), which can be called from within a Mailer class. This will allow you to attach a ByteArray as an attachment to emails easily without the need to save them to the disk first or without the need to use the Apache Commons Emails. You can then use the "standard" Play way.
Here is the corresponding feature request in the play bugtracker:
http://play.lighthouseapp.com/projects/57987/tickets/1500-adding-maillerattachdatasource-functionality