Page created:
Jun 17, 2009 (? ago)
Last modified ? ago
To main page | Opera Unite HowTo's

Static images, client-side scripts


You can create a "magic" directory:
./public_html
everything inside that directory is given out as a static file (@ Artemy Tregubenko )

Other Methods

1. You need to use opera.io.webserver.shareFile(file, path) from here .

2. Uniteness has a easier way to deal with this.
URLs([
   'js','static'
]);
3. Yusef has something called sharedDirectory (unfortunately didn't figure out how to properly call it and it seems like there's a bug inside, so I converted it into a function):
// From Yusef (modified)
function shareDirectory( dir, rootPath )
{
    dir.refresh();

    for( var i = 0, file; file = dir[i]; i++ )
    {
        if( file.path[file.path.lastIndexOf('/')+1]==='.' )
        {
            continue;
        }
        if( file.isDirectory )
        {
            shareDirectory( file, rootPath );
        }
        else if( file.isFile )
        {
            var path = file.path.slice( rootPath.length );
            opera.io.webserver.shareFile( file, 
                  file.path.replace(/^\/[^\/]+/, '') );
        }
    }
}

function shareStatic(dir) {
    public_html = opera.io.filesystem.
        mountSystemDirectory('application').resolve( '/'+dir+'/' );
    shareDirectory( public_html, public_html.path );
};	

Using Yusef's function:

Define it, when you define URLs:
window.onload = function () {
    webserver = opera.io.webserver
    if (webserver) {
        webserver.addEventListener('_index', start_page, false);
        staticDirectory('imgs');
        staticDirectory('docs/js');
    };
};
Now ./imgs/ and ./docs/js directory (and any subdirectories) will be served as-is (imgs as imgs).

Serving Images

If your URLs are always http://...operaunite/appname/smth (if you don't know - then you probably are using URLs this way):
function start_page(r) {
    var o = r.connection.response;
    o.write('<img src="imgs/my_image.jpg">');

Serving Images (more complex way)

If you use deeper URLs, then you need to use webserver.currentServicePath to form image URLs like so "/service_name/imgs/my_image.jpg":
function start_page(r) {
    var o = r.connection.response;
    o.write('<img src="' + webserver.currentServicePath + 
               'imgs/my_image.jpg">');
This will serve image "my_image.jpg" from your "imgs" directory under root directory (where your config.xml is).
./config.xml
./index.html
./imgs/my_image.jpg

Serving client-side scripts

If you have:
./config.xml
./js/jquery.js
and you want to make sure user uses "/js/jquery.js" script, you need to:
function start_page(r) {
    var o = r.connection.response;
    o.write('<script src="' + webserver.currentServicePath + 
               'scripts/jquery.js">');


Explanation

There is a variable:
opera.io.webserver.currentServicePath
If your app name is "/application/" this variable contains a relative path to your app, like so:
/application/

Slava V. [about me]


main page



Last updated


  1. Markuper (HTML templates)
  2. .ua
  3. Opera Unite HowTo's
  4. .us (files)
  5. Distribute Your Application
  6. 24/7 sites (permanent applications - idea) [stub]
  7. Issues
  8. HTTP Connections (AJAX/REST)
  9. Cookies
  10. file_wrap.js - File Wrapper
  11. Basic HowTo: Simple app (tutorial)
  12. Static images, client-side scripts
  13. Application Examples
  14. How to Debug Opera Unite apps
  15. Persistence & databases
  16. Opera Unite benchmark
  17. From PHP to Opera Unite
  18. Uniteness (Framework)
  19. Config.xml
  20. Key-value storage
  21. Widget Object
  22. Notifications (Growl'esque)
  23. Reset (debug)
  24. Cron example
  25. What I meant by CNAMEs
  26. Wish List
  27. Device Unavailable
  28. StopLorem (Opera Unite blogging)
  29. uniteness-0.11
  30. GET/POST data
  31. CRUD And Static (example)
  32. Opera object
  33. URLs
  34. Headers & Redirects
  35. Error Console
  36. JSON State (storing data)
  37. Security
  38. /storage/ (in fileio)
  39. Yusef library
  40. unite_info (a-la php_info)
  41. Javascript Imports
  42. onunload / _close
  43. fileio: Sandboxed Filesystem
  44. Request Hierarchy (like php_info)
  45. Intro: Web Apps with Opera Unite