Reading files from file system -Enonic XP

Whether Enonic Xp provide any API to read data from file system ?
We have some pdf data stored in file system which we want to read and sent it to another system via Enonic XP

Hi Vishal,

Yes, we have io lib that can be used for that: http://repo.enonic.com/public/com/enonic/xp/docs/6.10.3/docs-6.10.3-libdoc.zip!/module-io.html

In a nutshell:

  1. You get a file pointer via getResource function
  2. You get a stream via getStream function
  3. You read from the file using either readLines or readText functions

Good luck!

Hi Alan,

Thanks for quick revert. If I understand correctly, it allows us to read data from folders which are inside Enonic Xp directory (from where it read the code)
For example resources within ‘site /site/lib/xp/examples/io/sample.txt’

but does it allow from location like “/users/abc/files/pdf” on machine where Enonic XP instance is running ?

Actually, the getResource only applies to resource files inside the current application if I remember correctly. We intentionally seek to avoid applications from accessing the file system due to security resons, and related to clustering.

Is it an option for you to place the file in a repo instead? Otherwise I guess you would need to use the Java IO libraries directly…

We have no such functions for this yet. But you could always use Java both outside your script and inside your script. What are you going to do? Can help you here if you can tell me the steps.

I need to read some pdf files from file system via service in Enonic XP which need to be served to Enonic CMS.
So need library to reach data from file system and server in pdf format.

Just as a side note, you could use a bash script via cron to post to a service.

Something like:

for f in *.pdf; do curl .... $f; done 

I have to return the pdf to browser after hitting the service in Enonic XP.

I have written the java program to read the file from file system :

            ****************************

package com.enonic.xp.sample.readunitpdf;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadUnitPdfFiles
{

public byte[] getPDfList(String reportType,String unitNumber)
{
    String fullPath="/var/nfs/post-pdf/"+reportType+"/"+reportType+'_'+unitNumber+ ".pdf";
    System.out.println(fullPath);
    System.out.println("HEY");
    byte[] fileBytes=null;
    try {
        File file = new File(fullPath);
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream inputStream = new BufferedInputStream(fis);
        fileBytes = new byte[(int) file.length()];
        inputStream.read(fileBytes);
        inputStream.close();

    } catch (IOException ex) {
        System.out.println(ex);
    }
    return fileBytes;

}

}


According to this link :-https://stackoverflow.com/questions/32476706/how-to-display-pdf-file-in-browser

I need to get response object , but in stand alone java program , I don’t get response object .

I have further done mapping of my java class to javascript code and able to get byte data of pdf from above java program but how to take this data to display it as PDF ?

1 Like

Can some one assist on this thread ?
Quick help is deeply appreciated.

Hey @vishal ,

I have created a small file system library that can get a file and stream the file out thru a service.

You can find the code, example in the README here: https://github.com/rbrastad/lib-filesystem

Please contact me if any problems with the library.

//runar

3 Likes

Hi Runar,

Thanks a lot for your help .

I managed to do it myself :slight_smile:

but this thing give an add on:
headers : {
“Content-Disposition”: ‘attachment; filename="’ + fileSource.name + ‘""’
}

so that I can set my pdf name