file: protocal

85 views
Skip to first unread message

ma...@jetsonsys.com

unread,
Jul 26, 2013, 8:54:23 PM7/26/13
to appj...@googlegroups.com
Hi all

Since I am building a desktop APP I was hoping to access files on the local filesystem via <img> tags, ie: file:///<path to file>.... I realize there is the disableSecurity option to createWindow to allow cross domain requests. Furthermore, there are the file access chromium options:

    b.file_access_from_file_urls_allowed      = !O_ENABLE(FileAccessFromFileURLs);
    b.universal_access_from_file_urls_allowed = !O_ENABLE(UniversalFileURLAccess);

In addtion, there is CefAddCrossOriginWhiteListEntry....

So, I'm a bit confused. I disabled security, and set the above file_access varilables to true (although I think I only need to turn on the first one). I still get the "access not allowed to local resource" errors.....

Anyone have any hints on how to allow local file system access?

Or do I really have to create a new scheme, such as appjs-file, in addition to appjs which we already have....

Marek

Kevin Ingwersen

unread,
Jul 27, 2013, 10:57:16 AM7/27/13
to appj...@googlegroups.com
Hey.

As far as I know, the file: protocoll is buggy.
Your best bet is to dynamically create symlinks into your webroot directory, I guess.
Another solution would be, but its more complicated, to allow PHP parsing in appJS by changing the appjs.router method a little. Then you would be able to use a PHP file. But since there is only UNIX based systems that by default ship with PHP, I wouldn't rely on that.
The last solution is to copy the needed file into your webroot using var fs=require("fs");

But are you sure you entered the URL right? I thought it was
...I am not entirely sure on the protocol. I myself use the PHP variant.

Regards, Ingwie
--
You received this message because you are subscribed to the Google Groups "appjs-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appjs-dev+...@googlegroups.com.
To post to this group, send email to appj...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

sihorton

unread,
Jul 31, 2013, 10:55:20 AM7/31/13
to appj...@googlegroups.com
Hi Marek,

As Ingwie said I think that using the file protocol to access local files in appjs is a hard road to travel. Since you will be accessing the files through chromium you will always be subject to various security restrictions and problems (even using flags to turn those restrictions off) since it is the last thing the browser developers really want you to do.

However in appjs you are already running on the users local machine rather than running from a remote server so actually direct access to files is best provided by nodejs. There are many ways to achieve this, one simple way is to add the require object from nodejs directly to the window object. So in app.js search for the "window.on('ready', function() {" line and add in a line afterwards:-

    window.require = require;

Then in the index.html page you now have a window.require object that can load any nodejs modules that you want access to. Hence you can access the file system object: http://nodejs.org/api/fs.html

    <script>
        var fs = window.require("fs");
        fs.writeFile("/tmp/test", "Hey there!", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
    </script>

The objects that are required are cached by nodejs so there is no problem to load or access them on each page.

/Simon

ma...@jetsonsys.com

unread,
Aug 22, 2013, 2:41:48 PM8/22/13
to appj...@googlegroups.com
Simon and Kevin

Thanks for the responses..... I think for me this was primarily an efficiency issue with trying to use file:. I wanted to avoid having node serve the files, but just have file: links in my HTML. In the end, I had no luck getting around the security restrictions you guys mentioned, even though the flags to do so seem to exist. What I did is this:

  * I have a cache of files which are generated locally or come in as attachments via DB sync in (using TouchDB, wish there was a way to truly embed that thing, but its running in a server sub-process, another topic to munch on). That cache is in :

  ~/Library/Caches/com.jetsonsystems.plm/media-manager/file/'

which seems like a sensible mac OSX's place to put that stuff.

  * I then have a sym link in my assets folder:

Mareks-MacBook-Pro:AppJS marekjulian$ ls -ls ~/Projects/PLM/DeskTopApp/PlmApp-work/app/assets/
total 16
8 lrwxr-xr-x   1 marekjulian  staff    74 Aug 15 10:50 mm-file-cache -> /Users/marekjulian/Library/Caches/com.jetsonsystems.plm/media-manager/file

  * The client side html then contains relative links: /mm-file-cache .....

  * I've been stubborn and trying to keep the client side code portable, so I've avoided "requiring" node code with the exception of a couple instances. All data is served via a node API layer. That API layer, has a "URL rewriting" hook, which rewrites URLs in the API responses based upon whether files are cached or not. So, urls could be HTTP to my TouchDB process, or /mm-file-cache pointing to the local file system.

This works and seems to be flexible.

But, I would of thought it would have been more efficient to go directly to the horses mouth if you will via file:// as opposed to talking to node to serve the files which would mean getting the cef scheme handlers involved and all that jazz. Not sure how much of a penalty all that is, but at least for now, the APP seems responsive enough.

Thought I'd share for the sake of the community.

Marek

Simon Horton

unread,
Aug 22, 2013, 2:55:57 PM8/22/13
to appj...@googlegroups.com
Hi Marek,

Good you found a flexible solution.

I would imagine the most efficient method would be to get hold of the nodejs fs library directly in the browser. If you then open a stream to a given file you will receive the chunks as they come off the disk. I would assume the file:// protocol would be much slower since there will be lots of security checking going on behind the scenes and translation of the data read so it can be accessed by javascript. Would be interesting to open a 1 gig file and stream it to the browser using the fs module from a web page and then using the file protocol (you might have to create the file first yourself from the webpage so you write to the sandbox).

You could have a little javascript api layer that you include via a script tag in the webpage, for the appjs / nodejs setup this can get hold of the nodejs fs module and grab files that way and then in pure browser mode include a different script with same api that pulls the files in using ajax or similar. Just my 2 cents worth!

/Simon


Reply all
Reply to author
Forward
0 new messages