Search code examples
codenameone

How to access files in FileSystemStorage in Browser component


I am trying to display a pdf saved in FileSystemStorage inside the browsercomponent, but it keeping giving me this error on the console: [0813/072549.347989:INFO:CONSOLE(37)] "Not allowed to load local resource: file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH", source: https://cn1app/streams/1 (37) [0813/072551.123557:INFO:CONSOLE(0)] "Not allowed to load local resource: file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH", source: https://cn1app/streams/1 (0) . Its like there is a restriction that I need to work around. How can I go around this?

Edits... Added information below.

This is the code I am using:

Form hi;
Container loadingMsg;

public void Home(){
    Form form = new Form("Display pdf", new BorderLayout());
    
    Button showPdf = new Button("Show Pdf");
    showPdf.addActionListener(l->{
        show();
    });
    
    form.add(BorderLayout.CENTER, showPdf);
    form.show();
}

private void show(){
 
    hi = new Form("PDF Viewer", new BorderLayout());
    Label loadingLabel = new Label("Loading PDF...");
    loadingMsg = FlowLayout.encloseCenter(loadingLabel);
    hi.add(BorderLayout.NORTH, loadingMsg);
    
    String pdfUrl = "https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
    String fileName = FileSystemStorage.getInstance().getAppHomePath() + "Chapter_11";
    
    if (!FileSystemStorage.getInstance().exists(fileName)) {
        Util.downloadUrlToFileSystemInBackground(pdfUrl, fileName);
    };
   
    hi.addShowListener(l -> {
        run1(fileName);
    });
    hi.show();
}

private void run1(String fileName) {
    BrowserComponent browser = new BrowserComponent();
    browser.setPage(getPdfViewerHtml(fileName), null);
    hi.add(BorderLayout.CENTER, browser);
    loadingMsg.remove();
    hi.revalidate();

}

private String getPdfViewerHtml(String fileName) {
      String html = "<!DOCTYPE html>\n"
            + "<html>\n"
            + "  <head>\n"
            + "    <title>PDF Viewer</title>\n"
            + "    <style>\n"
            + "    html{\n"
            + "      height: 100%;\n"
            + "      padding: 0;\n"
            + "    }\n"
            + "    body{\n"
            + "      height: 100%;\n"
            + "      overflow-y: hidden;\n"
            + "      position: fixed;\n"
            + "      width: 100%;\n"
            + "      padding: 0;\n"
            + " margin: 0;\n"
            + "    }\n"
            + "    </style>\n"
            + "  </head>\n"
            + "  <body>\n"
            + "\n"
            + "    <div style= \"height: 100%; margin: 0;\">\n"
            + "        <iframe\n"
            + "          src='"+fileName+"#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH'\n"
            + "          width=\"100%\"\n"
            + "          height=\"100%\"\n"
            + "        >\n"
            + "        <p>This browser does not support PDF!</p>\n"
            + "        </iframe>\n"
            + "\n"
            + "    </div>\n"
            + "\n"
            + "  </body>\n"
            + "</html>";
    return html;
}

So in the method String getPdfViewerHtml(String fileName),when i replace the fileName with a URL, thing works fine. But I want it to display a file from FileSystemStorage.


Solution

  • The code below should support the local embed tag correctly:

        private Container loadingMsg;
    
        public void start() {
            if (current != null) {
                current.show();
                return;
            }
            home();
        }
    
    
        public void home() {
            Form form = new Form("Display pdf", new BorderLayout());
    
            Button showPdf = new Button("Show Pdf");
            showPdf.addActionListener(l -> {
                showPdf.setText("Dowloading PDF...");
                showPdf.setEnabled(false);
                form.revalidate();
                show();
            });
    
            form.add(BorderLayout.CENTER, showPdf);
            form.show();
        }
    
        private void show() {
            String root = getAppHomePath() + "httpdocs/";
            mkdir(root);
            
            hi = new Form("PDF Viewer", new BorderLayout());
            Label loadingLabel = new Label("Loading PDF...");
            loadingMsg = FlowLayout.encloseCenter(loadingLabel);
            hi.add(BorderLayout.NORTH, loadingMsg);
    
            String pdfUrl = "https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
            String fileName = root + "/Chapter_11.pdf";
    
            if (!FileSystemStorage.getInstance().exists(fileName)) {
                Util.downloadUrlToFile(pdfUrl, fileName, false);
                try {
                    run1(docRoot, "Chapter_11.pdf");
                } catch (IOException ex) {
                    Log.e(ex);
                }
            } else {
                try {
                    run1(docRoot, fileName);
                } catch (IOException ex) {
                    Log.e(ex);
                }
            }
    
            hi.show();
        }
    
        private void run1(String docRoot, String fileName) throws IOException {
            
            BrowserComponent browser = new BrowserComponent();
            String localUrl = fileName;
            String htmlPage = getPdfViewerHtml(localUrl);
            File indexHtml = new File(docRoot, "index.html");
            writeStringToFile(indexHtml, htmlPage);
            browser.setURL(docRoot + "/index.html");
            hi.add(BorderLayout.CENTER, browser);
            loadingMsg.remove();
            hi.revalidate();
    
        }
    
        private String getPdfViewerHtml(String fileName) {
            String html = "<!DOCTYPE html>\n"
                    + "<html>\n"
                    + "  <head>\n"
                    + "    <title>PDF Viewer</title>\n"
                    + "    <style>\n"
                    + "    html{\n"
                    + "      height: 100%;\n"
                    + "      padding: 0;\n"
                    + "    }\n"
                    + "    body{\n"
                    + "      height: 100%;\n"
                    + "      overflow-y: hidden;\n"
                    + "      position: fixed;\n"
                    + "      width: 100%;\n"
                    + "      padding: 0;\n"
                    + " margin: 0;\n"
                    + "    }\n"
                    + "    </style>\n"
                    + "  </head>\n"
                    + "  <body>\n"
                    + "\n"
                    + "    <div style= \"height: 100%; margin: 0;\">\n"
                    + "        <iframe\n"
                    + "          src='" + fileName + "#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH'\n"
                    + "          width=\"100%\"\n"
                    + "          height=\"100%\"\n"
                    + "        >\n"
                    + "        <p>This browser does not support PDF!</p>\n"
                    + "        </iframe>\n"
                    + "\n"
                    + "    </div>\n"
                    + "\n"
                    + "  </body>\n"
                    + "</html>";
            return html;
        }
        
        private void writeStringToFile(File file, String content) throws IOException {
            FileSystemStorage fs = FileSystemStorage.getInstance();
            try (OutputStream os = fs.openOutputStream(file.getAbsolutePath())) {
                Util.copy(new ByteArrayInputStream(content.getBytes("UTF-8")), os);
            }
            
        }
    
    }