The ArOZ Online JavaScript Gateway Interface (AGI) allows developers to create server-side scripts using JavaScript that can interact with the ArOZ Online system. AGI scripts run in a sandboxed Otto JavaScript VM environment, providing access to system functions while maintaining security.
// Send a simple response
sendResp("Hello, World!");
// Send JSON response
sendJSONResp({message: "Hello", status: "success"});
// Send OK response
sendOK();
// Load required libraries
if (requirelib("filelib") == true) {
// File operations are now available
var content = filelib.readFile("user:/example.txt");
}
BUILD_VERSION: Current system build versionINTERNAL_VERSION: Internal version numberLOADED_MODULES: Array of loaded system modulesLOADED_STORAGES: Array of available storage pools__FILE__: Current script file pathHTTP_RESP: Response content (automatically set)HTTP_HEADER: Response content type (automatically set)USERNAME: Current user's usernameUSERICON: Current user's icon pathUSERQUOTA_TOTAL: User's total storage quotaUSERQUOTA_USED: User's used storage quotaUSER_VROOTS: User's accessible virtual root pathsUSER_MODULES: User's accessible modulessendResp(content)Sends a response to the client.
sendResp("Operation completed successfully");
echo(content)Appends content to the response.
echo("Hello ");
echo("World!"); // Response: "Hello World!"
sendOK()Sends a simple "ok" response.
sendOK(); // Response: "ok"
sendJSONResp(object)Sends a JSON response.
sendJSONResp({
status: "success",
data: [1, 2, 3]
});
newDBTableIfNotExists(tableName)Creates a new database table if it doesn't exist.
if (newDBTableIfNotExists("myapp_settings")) {
echo("Table created successfully");
}
DBTableExists(tableName)Checks if a database table exists.
if (DBTableExists("user_preferences")) {
echo("Table exists");
}
writeDBItem(tableName, key, value)Writes a key-value pair to a database table.
writeDBItem("settings", "theme", "dark");
readDBItem(tableName, key)Reads a value from a database table.
var theme = readDBItem("settings", "theme");
if (theme) {
echo("Current theme: " + theme);
}
listDBTable(tableName)Lists all key-value pairs in a table.
var settings = listDBTable("settings");
echo(JSON.stringify(settings));
deleteDBItem(tableName, key)Deletes a key-value pair from a table.
deleteDBItem("settings", "old_key");
dropDBTable(tableName)Drops an entire database table.
dropDBTable("temporary_data");
registerModule(jsonConfig)Registers a module with the system.
var moduleConfig = {
Name: "MyApp",
Desc: "My custom application",
Group: "Utilities",
IconPath: "MyApp/icon.png",
Version: "1.0",
StartDir: "MyApp/index.html",
SupportFW: true,
LaunchFWDir: "MyApp/index.html",
SupportEmb: true,
LaunchEmb: "MyApp/embedded.html",
InitFWSize: [800, 600],
InitEmbSize: [400, 300],
SupportedExt: [".txt", ".md"]
};
registerModule(JSON.stringify(moduleConfig));
addNightlyTask(scriptPath)Adds a script to run nightly.
addNightlyTask("MyApp/tasks/backup.js");
pathCanWrite(virtualPath)Checks if the current user can write to a path.
if (pathCanWrite("user:/documents/")) {
echo("Can write to documents folder");
}
getUserPermissionGroup()Gets the current user's permission group information.
var groupInfo = JSON.parse(getUserPermissionGroup());
echo("User group: " + groupInfo.name);
userIsAdmin()Checks if the current user is an administrator.
if (userIsAdmin()) {
echo("User is admin");
}
userExists(username)Checks if a user exists (admin only).
if (userExists("john_doe")) {
echo("User exists");
}
createUser(username, password, defaultGroup)Creates a new user account (admin only).
if (createUser("newuser", "password123", "users")) {
echo("User created successfully");
}
removeUser(username)Removes a user account (admin only).
if (removeUser("olduser")) {
echo("User removed successfully");
}
execd(scriptPath, payload)Executes another script asynchronously with optional payload.
execd("MyApp/worker.js", "process_data");
filelib)Load with: requirelib("filelib")
filelib.writeFile(virtualPath, content)Writes content to a file.
filelib.writeFile("user:/notes.txt", "Hello, World!");
filelib.readFile(virtualPath)Reads content from a file.
var content = filelib.readFile("user:/notes.txt");
echo(content);
filelib.deleteFile(virtualPath)Deletes a file.
filelib.deleteFile("user:/temp.txt");
filelib.mkdir(virtualPath)Creates a directory.
filelib.mkdir("user:/newfolder");
filelib.readdir(virtualPath, sortMode)Lists directory contents.
var files = filelib.readdir("user:/documents", "name");
// Returns array of file objects
filelib.fileExists(virtualPath)Checks if a file exists.
if (filelib.fileExists("user:/config.json")) {
echo("Config file exists");
}
filelib.isDir(virtualPath)Checks if path is a directory.
if (filelib.isDir("user:/documents")) {
echo("Is a directory");
}
filelib.filesize(virtualPath)Gets file size in bytes.
var size = filelib.filesize("user:/largefile.zip");
echo("File size: " + size + " bytes");
filelib.mtime(virtualPath)Gets file modification time.
var mtime = filelib.mtime("user:/document.txt");
echo("Modified: " + mtime);
filelib.md5(virtualPath)Calculates MD5 hash of a file.
var hash = filelib.md5("user:/important.doc");
echo("MD5: " + hash);
filelib.walk(virtualPath, mode)Recursively walks a directory.
// List all files recursively
var allFiles = filelib.walk("user:/documents", "file");
// List all directories recursively
var allDirs = filelib.walk("user:/documents", "folder");
// List everything
var everything = filelib.walk("user:/documents", "all");
filelib.glob(pattern)Finds files matching a pattern.
var txtFiles = filelib.glob("user:/documents/*.txt");
filelib.aglob(pattern)Finds files with advanced pattern matching.
var jsFiles = filelib.aglob("user:/**/*.js");
imagelib)Load with: requirelib("imagelib")
imagelib.getImageDimension(imagePath)Gets image dimensions.
var dimensions = imagelib.getImageDimension("user:/photo.jpg");
// Returns: {width: 1920, height: 1080}
imagelib.hasExif(imagePath)Checks if image has EXIF data.
if (imagelib.hasExif("user:/photo.jpg")) {
echo("Image has EXIF data");
}
imagelib.getExif(imagePath)Extracts EXIF data from image.
var exif = JSON.parse(imagelib.getExif("user:/photo.jpg"));
echo("Camera: " + exif.Make);
imagelib.resizeImage(inputPath, outputPath, width, height)Resizes an image.
imagelib.resizeImage("user:/photo.jpg", "user:/photo_small.jpg", 800, 600);
imagelib.cropImage(inputPath, outputPath, x, y, width, height)Crops an image.
imagelib.cropImage("user:/photo.jpg", "user:/photo_crop.jpg", 100, 100, 500, 500);
imagelib.loadThumbString(imagePath, size)Generates a base64 thumbnail.
var thumbnail = imagelib.loadThumbString("user:/photo.jpg", 200);
echo("<img src='data:image/jpeg;base64," + thumbnail + "' />");
http)Load with: requirelib("http")
http.get(url)Makes a GET request.
var response = http.get("https://api.example.com/data");
echo(response);
http.post(url, jsonData)Makes a POST request with JSON data.
var data = JSON.stringify({name: "John", age: 30});
var response = http.post("https://api.example.com/users", data);
http.head(url)Makes a HEAD request.
var headers = http.head("https://example.com");
http.download(url, savePath)Downloads a file from URL.
http.download("https://example.com/file.zip", "user:/downloads/file.zip");
http.getb64(url)Gets response as base64.
var b64data = http.getb64("https://example.com/image.png");
http.getCode(url)Gets HTTP status code.
var statusCode = http.getCode("https://example.com");
if (statusCode == 200) {
echo("Site is up");
}
http.redirect(url, statusCode)Redirects the client to another URL.
http.redirect("https://example.com/newpage", 302);
share)Load with: requirelib("share")
share.shareFile(virtualPath, timeout)Shares a file and returns share UUID.
var shareId = share.shareFile("user:/document.pdf", 3600); // 1 hour timeout
echo("Share URL: /share/" + shareId);
share.removeShare(shareUUID)Removes a file share.
share.removeShare("share-uuid-here");
share.checkShareExists(shareUUID)Checks if a share exists.
if (share.checkShareExists("share-uuid")) {
echo("Share exists");
}
share.fileIsShared(virtualPath)Checks if a file is shared.
if (share.fileIsShared("user:/document.pdf")) {
echo("File is shared");
}
share.getFileShareUUID(virtualPath)Gets the share UUID for a file.
var shareId = share.getFileShareUUID("user:/document.pdf");
if (shareId) {
echo("Share ID: " + shareId);
}
share.checkSharePermission(shareUUID)Gets share permission level.
var permission = share.checkSharePermission("share-uuid");
// Returns permission level (read/write/etc)
iot)Load with: requirelib("iot")
iot.scan()Scans for available IoT devices.
var devices = iot.scan();
// Returns array of device objects
iot.list()Lists cached IoT devices.
var devices = iot.list();
iot.connect(deviceId, username, password, token)Connects to an IoT device.
if (iot.connect("device123", "admin", "password", "")) {
echo("Connected to device");
}
iot.disconnect(deviceId)Disconnects from an IoT device.
iot.disconnect("device123");
iot.exec(deviceId, endpoint, payload)Executes a command on an IoT device.
var result = iot.exec("device123", "set_temperature", {value: 25});
iot.status(deviceId)Gets device status.
var status = iot.status("device123");
iot.ready()Checks if IoT system is ready.
if (iot.ready()) {
echo("IoT system ready");
}
iot.iconTag(deviceId)Gets device icon tag.
var iconTag = iot.iconTag("device123");
appdata)Load with: requirelib("appdata")
appdata.readFile(relativePath)Reads a file from the web root.
var config = appdata.readFile("MyApp/config.json");
var configObj = JSON.parse(config);
appdata.listDir(relativePath)Lists contents of a web directory.
var files = JSON.parse(appdata.listDir("MyApp/templates"));
for (var i = 0; i < files.length; i++) {
echo("File: " + files[i]);
}
ffmpeg)Load with: requirelib("ffmpeg")
ffmpeg.convert(inputPath, outputPath, compression)Converts media files using FFmpeg.
// Convert video to different format
ffmpeg.convert("user:/video.mp4", "user:/video.webm", 0);
// Convert with compression
ffmpeg.convert("user:/audio.wav", "user:/audio.mp3", 5);
websocket)Load with: requirelib("websocket")
websocket.upgrade(timeout)Upgrades HTTP connection to WebSocket.
if (websocket.upgrade(300)) { // 5 minute timeout
echo("WebSocket upgraded");
}
websocket.send(message)Sends a message through WebSocket.
websocket.send("Hello from server!");
websocket.read()Reads a message from WebSocket.
var message = websocket.read();
if (message) {
echo("Received: " + message);
}
websocket.close()Closes the WebSocket connection.
websocket.close();
These functions are available when AGI scripts are called via HTTP requests.
REQ_METHODHTTP request method.
echo("Request method: " + REQ_METHOD);
getPara(key)Gets a GET parameter.
var username = getPara("username");
if (username) {
echo("Hello, " + username);
}
postPara(key)Gets a POST parameter.
var password = postPara("password");
readBody()Reads the raw request body.
var rawData = readBody();
var data = JSON.parse(rawData);
AGI scripts should handle errors appropriately:
try {
var content = filelib.readFile("user:/file.txt");
if (content === false) {
sendJSONResp({error: "File not found"});
} else {
sendJSONResp({content: content});
}
} catch (e) {
sendJSONResp({error: "Script error: " + e.message});
}
// Load required libraries
requirelib("filelib");
// Get uploaded file info from POST parameters
var filename = postPara("filename");
var filedata = postPara("filedata");
if (filename && filedata) {
// Decode base64 data
var decodedData = atob(filedata);
// Save to user directory
var savePath = "user:/uploads/" + filename;
if (filelib.writeFile(savePath, decodedData)) {
sendJSONResp({
status: "success",
message: "File uploaded successfully",
path: savePath
});
} else {
sendJSONResp({
status: "error",
message: "Failed to save file"
});
}
} else {
sendJSONResp({
status: "error",
message: "Missing filename or filedata"
});
}
// Load file library
requirelib("filelib");
// Create settings table if not exists
newDBTableIfNotExists("user_settings");
// Handle different actions
var action = getPara("action");
if (action === "get") {
var key = getPara("key");
var value = readDBItem("user_settings", key);
sendJSONResp({value: value});
} else if (action === "set") {
var key = postPara("key");
var value = postPara("value");
writeDBItem("user_settings", key, value);
sendJSONResp({status: "saved"});
} else if (action === "list") {
var settings = listDBTable("user_settings");
sendJSONResp(settings);
} else {
sendJSONResp({error: "Invalid action"});
}
This documentation covers all available AGI APIs with practical examples. For more advanced usage, refer to the existing module implementations in the system.