I ended up with a work around as I could not find how to create a File object 
from an object of type Asset (the code I posted previously did break when I was 
receiving a AssetWrapper).

Note that this work around is probably not needed for Magnolia 5 since the DAM 
module (version 1.0-alpha2-3 at the time of writing [july 2013]) provides an 
Asset interface that has a [quote]getContentStream()[/quote] method returning 
an [quote]InputStream[/quote]. See 
[url=http://nexus.magnolia-cms.com/content/sites/magnolia.public.sites/modules/magnolia-module-dam/1.0-alpha2-3/magnolia-module-dam-parent/magnolia-module-dam/apidocs/index.html]http://nexus.magnolia-cms.com/content/sites/magnolia.public.sites/modules/magnolia-module-dam/1.0-alpha2-3/magnolia-module-dam-parent/magnolia-module-dam/apidocs/index.html[/url]

[code]...
Asset asset = stkTemplatingFunctions.getAsset(node, "pdf");
String assetLink = stkTemplatingFunctions.getAssetLink(node, "pdf");

if (assetLink!=null){
        log.info("asset found");
        
        String urlStringForAsset = getUrlString(assetLink);
        log.info("url link to asset: " + urlStringForAsset);

        try {
                URL url = new URL(urlStringForAsset);

                String tDir = System.getProperty("java.io.tmpdir");
                String path = tDir + "tmp"; // + ".pdf";
                // file = File.createTempFile("tmp", "");
                file = new File(path);
                file.deleteOnExit();
                
                // This will only work in PUBLIC instance (files are accessible 
by "not-logged-in" users)
                FileUtils.copyURLToFile(url, file);
                log.info("File exists? " + file.exists());
                
        } catch (MalformedURLException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }
}
...

private String getUrlString(String pathname) {
        ExtendedAggregationState eState = (ExtendedAggregationState) 
MgnlContext.getAggregationState();
        String domainName = eState.getDomainName();
        return "http://"; + domainName + pathname;
}[/code]

So this work around simply creates a file from a URL. Which as mentioned in the 
comment would only work in PUBLIC instance as files on your Author instance are 
not accessible by users who are not logged in. Again, not the way I wished to 
go for but that's the only way I managed to get this working fine.

Looking forward to move on to Magnolia 5 & use the DAM module API :)

-- 
Context is everything: 
http://forum.magnolia-cms.com/forum/thread.html?threadId=1f484c58-604b-4b08-86f6-947080d7e2a8


----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------

Reply via email to