Archive

Archive for the ‘javascript’ Category

Opera Unite shareFile problem and solution

WARNING: I’m not very experienced with Opera Unite, the technique presented in this post works for me and doesn’t has to be the best way possible. If you find a better way please let me know.

Opera Unite Version: first release

In Opera Unite all files are on the server and can not be accessed without specify allowing it. But when you share a file using the given APIs opera.webserver.webserver.shareFile(..) method this breaks and becomes unavailable. This seems to be somehow caused be loading the file again while the last download was still running.

As described on http://unitehowto.com/. (Thanks for the great work you are doing)
He found a way to solve this problem, he reloads the scripts on each request but I have thought there has to be an other way.

And I found one while looking through the different examples opera provides with unite. The solution for this problem seems that you have to share and unshare the file on each request. I don’t know if this is a bug or by indent. Opera can you tell us more?

But know I want you to show the solution I came up with.

Everything starts in the window.onload method, this method is called when the service is started. So everything the server has to initialize should be done here.

The first step is to get the webserver and filesystem object. This has nothing to-do specifically with this problem.

var webserver = opera.io.webserver;
var filesystem = opera.io.filesystem;

But the next line is important. You have to mount the the ‘application’ directory.So you have access to the application files.

this.application = filesystem.mountSystemDirectory('application');
webserver.addEventListener('_request', request, false);           

You also have to event Listener for the ‘_request’ event. This event will always be called when a site is requested. In this method we will do the work to make the files available.

And now lets start the most work.

function request(evt) {
    // make alias
    webserver = opera.io.webserver
    var request = evt.connection.request;
    var path = request.uri;

    path = path.substr(webserver.currentServicePath.length, path.length);
    path = path.replace(/\?.*$/, ''); // remove the query path
 
    if (path != "") {
        if (staticFiles[path]) {
            var file = self.application.resolve(staticFiles[path]);
            opera.io.webserver.shareFile(file, path);
            response.closeAndRedispatch();
            opera.io.webserver.unshareFile(file);
            return;
        }
    }

   if (response){
    response.closeAndRedispatch();
   }
}

This is a lot of code so lets get started. We begin the class by getting get webserver, request and the path which was requested.

We now have to remove the current service name from the string. The path has the format

/[serviceName]/[request].

After the service name and query path is removed we have to heck if the path is even contains any data now. If we call the default start site this string will be empty because we haven’t’ requested anything. If the file exists I check in the staticFiles directory if we have a static file defined with the given path. The directory looks like the following,

var staticFiles = { 'imgs/img.jpg': 'sharedFiles/imgs/img.jpg' }

The first element is the path the file should be accessed by the user and the value define the actual path of the file.

If we have defined a static file under the given path we will continue and call application.resolve(..) on the value of the staticFile directory. This gives us the the file we can now share.

for this we call webserver.shareFile(file, path) and feed in the file we just received and the path we generated before.

After the file is shared we call webserver.closeAndRedispatch(). This closes the connection and redispatch the request to the Web server.

After that we just have to unshare the file again. webserver.unshareFile(file) and call return;

But we also have to handle the case if we haven’t defined the static-file the user requested. This is done in the last if(response) block. Here we just webserver.closeAndRedispatch() and let the webserver handle what to do, normally show an 404 Not found error page.

This is everything necessary to get static-files shared without getting the resource broken after some time.

That’s it hope this information is helpful.

Categories: javascript, operaUnite