Image Uploader Scripting API
Loading...
Searching...
No Matches
Image Uploader Scripting API
Version
1.4.2

Contents

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

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 (PDF)

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


Example

const UPLOAD_URL = "http://example.com/upload.php";
test <- "example"; // global variable
// This is the function that performs the upload of the file
// @param string pathToFile
// @param UploadParams options
// @return int - success(1), failure(0)
function UploadFile(pathToFile, options) {
local task = options.getTask().getFileTask();
nm.setUrl(UPLOAD_URL);
nm.addQueryParamFile("file", pathToFile, task.getDisplayName(), GetFileMimeType(pathToFile));
nm.addQueryParam("submit", "Upload file!");
nm.doUploadMultipartData();
if (nm.responseCode() != 200) {
return ResultCode.Failure;
}
local directUrl = _RegexSimple(nm.responseBody(), "\\[IMG\\](.+)\\[/IMG\\]",0);
if (directUrl == "") {
return ResultCode.Failure;
}
options.setDirectUrl(directUrl);
return ResultCode.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 _RegexSimple(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;
}
// Authenticating on remote server (optional function)
// @var CFolderList list
// @return int - success(1), failure(0)
function Authenticate() {
// TODO: Your code
return ResultCode.Success;
}
// Optional function:
// Retrieving folder (album) list from server
// @var CFolderList list
// @return int - success(1), failure(0)
//
function GetFolderList(list) {
// TODO: Your code
return ResultCode.Success;
}
// Create an folder or an album
// @var CFolderItem parentAlbum
// @var CFolderItem album
// @return int - success(1), failure(0)
//
function CreateFolder(parentAlbum,album) {
// TODO: Your code
return ResultCode.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 ResultCode.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"];
}
int UploadFile(string pathToFile, UploadParams params)
int Authenticate()
array GetFolderAccessTypeList()

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

Gumbo-query is a library that provides CSS selector-like queries for Gumbo-Parser. Use ScriptAPI::Document class.

local txt = "<h1><a id=\"logo\" class=\"link\">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("https://svistunov.dev");
doc = Document(nm.responseBody());
doc.find("a").each( function(index,elem) {
print(elem.text()+"-\r\n");
});