Image Uploader Scripting API
Image Uploader Scripting API
Version
1.3.2

Contents

  1. Introduction
  2. Example
  3. Functions to implement
  4. Global functions
  5. Upload filter script
  6. Parsing HTML with gumbo-query
  7. Translating scripts into different languages

Introduction

Image Uploader is using scripts written in Squirrel 3 language. Squirrel is a high level imperative, object-oriented programming language, designed to be a light-weight scripting language that fits in the size, memory bandwidth, and real-time requirements of applications like video games.

Squirrel 3.0 reference manual
Squirrel 3.0 Standard Libraries manual

Scripts should be saved in utf-8 encoding in files with .nut extension and placed in the Data/Scripts directory.


Example

test <- "example"; // global variable
// Function which is actually doing upload of a file
// @param string pathToFile
// @param UploadParams options
// @return int - success(1), failure(0)
function UploadFile(pathToFile, options)
{
nm.setUrl("http://example.com/upload.php");
nm.addQueryParamFile("file", pathToFile, ExtractFileName(pathToFile),"");
nm.addQueryParam("submit", "Upload file!");
nm.doUploadMultipartData();
local response = nm.responseBody(); // 'local' it's like javascript's 'var' but only for local variables
local directUrl = regex_simple(response, "\\[IMG\\](.+)\\[/IMG\\]",0);
options.setDirectUrl(directUrl);
return 1; //SUCCESS
}
// Helper function that simplifies working with regular expressions
// @param string data - the string we are looking in
// @param string regStr - regular expression, in the format supported by the standard squirrel language library.
// http://www.squirrel-lang.org/doc/sqstdlib2.html#d0e2580
// This format does not support some of the features of the PCRE format used in servers.xml.
// @param int start - starting position
// @return string - returns text captured by the first subpattern.
//
function regex_simple(data,regStr,start)
{
local ex = regexp(regStr);
local res = ex.capture(data, start);
local resultStr = "";
if(res != null){
resultStr = data.slice(res[1].begin, res[1].end);
}
return resultStr;
}
function GetFolderList(list)
{
// TODO: Your code
return 1; //SUCCESS
}
function CreateFolder(parentAlbum,album)
{
// TODO: Your code
return 1; //SUCCESS
}
// Modify a folder or an album (update name, description)
// @var CFolderItem album
// @return int - success(1), failure(0)
//
function ModifyFolder(album)
{
// TODO: Your code
return 1; //SUCCESS
}
// A function that returns a list of types of access restrictions to an album:
// private, public, only for friends, etc.
// @return array
{
return ["Private", "Public"];
}

You have to implement at least one function — UploadFile.
If you want to support album listing/creating/modifying, you have to implement also GetFolderList, CreateFolder, ModifyFolder, GetFolderAccessTypeList.

nm - global object - is an instance of NetworkClient
ServerParams - global object - is an instance of ServerSettingsStruct

Parsing HTML with gumbo-query

local txt = "<h1><a id=\"logo\">some link</a></h1>";
local doc = Document(txt);
print(doc.find("h1 a").text()+"\r\n");
print(doc.find("h1 a").length()+"\r\n");
print(doc.find("#logo").attr("class")+"\r\n");
nm.doGet("http://zenden.ws");
doc = Document(nm.responseBody());
doc.find("a").each( function(index,elem) {
print(elem.text()+"-\r\n");
});