Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active June 22, 2016 18:01
Show Gist options
  • Save andersevenrud/e66d6bb0fbc8aa8b0e2fc5a1b91aafea to your computer and use it in GitHub Desktop.
Save andersevenrud/e66d6bb0fbc8aa8b0e2fc5a1b91aafea to your computer and use it in GitHub Desktop.
OS.js HTTP VFS Module
/*!
* OS.js - JavaScript Cloud/Web Desktop Platform
*
* Copyright (c) 2011-2016, Anders Evenrud <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Anders Evenrud <[email protected]>
* @licence Simplified BSD License
*/
(function(Utils, API) {
'use strict';
window.OSjs = window.OSjs || {};
OSjs.VFS = OSjs.VFS || {};
OSjs.VFS.Transports = OSjs.VFS.Transports || {};
/*
* THIS IS AN EXPERIMENTAL WEB TRANSPORT MODULE FOR OS.js VFS
*
* IT IS READ-ONLY!
*
* To make this work you *will need* CORS support!
*
* Scandir() works by loading a file named `_scandir.json` in the
* requested folder.
*
* Below you can find the mountpoint map to URL;
*
* Example _scandir.json file:
*/
/*
[
{
"filename": "subdir",
"path": "/subdir",
"size": 0,
"type": "dir"
},
{
"filename": "README.md",
"path": "/README.md",
"size": 2762,
"type": "file",
"mime": "text/plain"
},
{
"filename": "screenshot.png",
"path": "/screenshot.png",
"size": 139204,
"type": "file",
"mime": "image/png"
}
]
*/
var BASE_MAP = {
'home:///': 'http://10.0.0.11:3000'
};
/////////////////////////////////////////////////////////////////////////////
// HELPERS
/////////////////////////////////////////////////////////////////////////////
function httpCall(func, item, callback) {
var url = makePath(item);
if ( func === 'scandir' ) {
url += '/_scandir.json';
}
var args = {
method: func === 'exists' ? 'HEAD' : 'GET',
url: url,
onerror: function(error) {
callback(error);
},
onsuccess: function(response) {
callback(false, response);
}
};
if ( func === 'read' ) {
args.responseType = 'arraybuffer';
}
Utils.ajax(args);
}
/////////////////////////////////////////////////////////////////////////////
// API
/////////////////////////////////////////////////////////////////////////////
var Transport = {
scandir: function(item, callback, options) {
var root = OSjs.VFS.getRootFromPath(item.path);
httpCall('scandir', item, function(error, response) {
var list = null;
if ( !error ) {
var json = null;
try {
json = JSON.parse(response);
} catch ( e ) {}
if ( json === null ) {
error = 'Failed to parse directory JSON';
} else {
var list = json.map(function(iter) {
iter.path = root + iter.path.replace(/^\//, '');
return iter;
});
var rel = OSjs.VFS.getRelativeURL(item.path);
if ( rel != '/' ) {
list.unshift({
filename: '..',
path: Utils.dirname(item.path),
type: 'dir',
size: 0
});
}
}
}
callback(error, list);
});
},
write: function(item, data, callback, options) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
read: function(item, callback, options) {
options = options || {};
var mime = item.mime || 'application/octet-stream';
httpCall('read', item, function(error, response) {
if ( !error ) {
if ( options.type === 'text' ) {
OSjs.VFS.abToText(response, mime, function(error, text) {
callback(error, text);
});
return;
}
}
callback(error, response);
});
},
copy: function(src, dest, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
move: function(src, dest, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
unlink: function(item, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
mkdir: function(item, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
exists: function(item, callback) {
httpCall('exists', item, function(err) {
callback(err, err ? false : true);
});
},
find: function(item, args, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
fileinfo: function(item, callback, options) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
url: function(item, callback, options) {
callback(false, makePath(item));
},
trash: function(item, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
untrash: function(item, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
emptyTrash: function(item, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
},
freeSpace: function(root, callback) {
callback(API._('ERR_VFS_UNAVAILABLE'));
}
}
/////////////////////////////////////////////////////////////////////////////
// WRAPPERS
/////////////////////////////////////////////////////////////////////////////
/**
* Make a WebDAV HTTP request for VFS
*
* @param String name Method name
* @param Object args Method arguments
* @param Function callback Callback => fn(error, result)
* @param Object option (Optional) request options
*
* @return void
* @api OSjs.VFS.Transports.WebDAV.request()
*/
function makeRequest(name, args, callback, options) {
args = args || [];
callback = callback || {};
if ( !Transport[name] ) {
throw new Error(API._('ERR_VFSMODULE_INVALID_METHOD_FMT', name));
}
var fargs = args;
fargs.push(callback);
fargs.push(options);
Transport[name].apply(Transport, fargs);
}
function makePath(file) {
var root = OSjs.VFS.getRootFromPath(file.path);
var rel = OSjs.VFS.getRelativeURL(file.path);
return BASE_MAP[root] + rel.replace(/^\/+/, '/');
}
/////////////////////////////////////////////////////////////////////////////
// EXPORTS
/////////////////////////////////////////////////////////////////////////////
OSjs.VFS.Transports.Web = {
request: makeRequest,
path: makePath
};
})(OSjs.Utils, OSjs.API);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment