admin管理员组文章数量:1432573
I have a certain JS File in DAM, that contains a JSON. I would like to access that file in the helper JS using any methods of JavaScript USE API in Sightly. I know it can be done using Java quite easily, but I would like to do it in a manner that I do not want to touch any Java Code.
I tried things like below. But after that, an input stream is unavailable to convert it to a stream data.
request.resourceResolver.getResource("/path/to/dam/file.js");
AND
request.resourceResolver.getResource("/path/to/dam/file.js").adaptTo(.adobe.granite.asset.api.Asset);
I have a certain JS File in DAM, that contains a JSON. I would like to access that file in the helper JS using any methods of JavaScript USE API in Sightly. I know it can be done using Java quite easily, but I would like to do it in a manner that I do not want to touch any Java Code.
I tried things like below. But after that, an input stream is unavailable to convert it to a stream data.
request.resourceResolver.getResource("/path/to/dam/file.js");
AND
request.resourceResolver.getResource("/path/to/dam/file.js").adaptTo(.adobe.granite.asset.api.Asset);
Share
Improve this question
edited Oct 9, 2019 at 4:44
Akshay Rathnavas
asked Oct 8, 2019 at 18:08
Akshay RathnavasAkshay Rathnavas
3685 silver badges16 bronze badges
2
-
1
Random tip: if your file contains JSON then it should be a
.json
file not a.js
;) – theopendle Commented Oct 9, 2019 at 8:26 - thanks for the tip. Even I was thinking about that, but it's getting rendered from a third-party. I'll pass it on to them to use .json next time :D – Akshay Rathnavas Commented Oct 10, 2019 at 12:34
3 Answers
Reset to default 3I saw the answer posted just now. But I had used another similar method before I saw this.
Here is it. It is very similar to the answer but with a few extra steps.
asset = request.resourceResolver.getResource(jsonPath).adaptTo(.day.cq.dam.api.Asset);
rend = asset.getOriginal().adaptTo(.day.cq.dam.api.Rendition);
OR DIRECTLY
rend= request.resourceResolver.getResource(jsonPath+"/jcr:content/renditions/original").adaptTo(.day.cq.dam.api.Rendition);
AND THEN
inputStream = rend.adaptTo(java.io.InputStream);
var is;
var c = '';
var flag = false;
try {
// reads till the end of the stream
while ((is = inputStream.read()) != -1) {
c = c + String.fromCharCode(is);
}
} catch (e) {
// if any I/O error occurs
log.debug("Input Stream Error " + e)
}
I am not sure if there are pure JS Use API methods which allow you to do it. However, since the JS Use API allows you to use Java classes and methods in it, you should be able to use them to fetch the information.
Since your file is stored as an asset in DAM, you need to access the data from the original rendition. One way of doing it is to use the .day.cq.dam.api.Asset
API to get the original rendition. The .adobe.granite.asset.api.Asset
doesn't have a direct way of accessing the original rendition, hence using the other one.
Working sample custom.js
use(function (data) {
var asset = request.resourceResolver.getResource("/content/dam/we-retail/en/data.js").adaptTo(.day.cq.dam.api.Asset);
var is = asset.getOriginal().adaptTo(java.io.InputStream);
var jsonData = JSON.parse(org.apache.mons.io.IOUtils.toString(is, "UTF-8"));
console.log(jsonData);
return jsonData;
});
The contents of the file in DAM
{
"fname": "abc",
"lname": "xyz"
}
The HTL file
<sly data-sly-use.custom="custom.js">
${custom.fname} --> ${custom.lname}
</sly>
While trying the existing solutions posted here, I had issues reading an arbitrary text file from the DAM; the resource didn't want to be adapted to Asset or Rendition.
After messing around a bit, I found that the resource could be adapted directly to an InputStream. Here's what I came up with:
function readFileToString(location)
{
var inputStream = request.resourceResolver.getResource(location).adaptTo(java.io.InputStream);
var data = org.apache.mons.io.IOUtils.toString(inputStream, "UTF-8");
return data + "";
}
本文标签: AEM Access JS File in DAM using JavaScript UseAPIStack Overflow
版权声明:本文标题:AEM: Access JS File in DAM using JavaScript Use-API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745582770a2664718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论