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

Contents

  1. Introduction
  2. Functions to implement
  3. Example
  4. Global API functions
  5. Parsing different formats
  6. Auth 2.0 authorization example
  7. Upload filter script
  8. 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 BASE_URL = "http://example.com";
function _PrintError(t, txt) {
local errorMessage = "[example.com] " + txt + " Response code: " + nm.responseCode();
if (t != null && "error" in t) {
errorMessage += "\n" + t.error;
}
WriteLog("error", errorMessage);
}
// 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();
local apiToken = ServerParams.getParam("apiToken");
if (apiToken == "") {
WriteLog("error", "[example.com] API token not set!");
return ResultCode.Failure;
}
nm.doGet(BASE_URL + "/");
if (nm.responseCode() != 200) {
return ResultCode.Failure;
}
// Parsing HTML with Gumbo-query
local doc = Document(nm.responseBody());
local csrf = doc.find("input[name=csrf]").at(1).attr("value");
if (csrf == "") {
csrf = doc.find("meta[name=csrf]").attr("value");
}
if (csrf == "") {
WriteLog("error", "[example.com] Cannot obtain CSRF token.");
return ResultCode.Failure;
}
nm.setUrl(BASE_URL + "/upload.php?fname=" + nm.urlEncode(task.getDisplayName()));
nm.addQueryHeader("X-CSRF-token", csrf);
nm.addPostField("token", apiToken); // Add a POST parameter to the request
nm.addPostFieldFile("file", pathToFile, task.getDisplayName(), GetFileMimeType(pathToFile));
nm.addPostField("submit", "Upload file!");
nm.doUploadMultipartData(); // Make multipart/form-data request
if (nm.responseCode() != 200) {
WriteLog("error", "[example.com] Upload failed. Response code:" + nm.responseCode());
return ResultCode.Failure;
}
// Extracting links from the server response
// Method 1 (with regular expression)
local reg = CRegExp("\\[IMG\\](.+)\\[/IMG\\]", "mi");
local directUrl = "";
local viewUrl = "";
local thumbnailUrl = "";
if (reg.match(nm.responseBody())) {
directUrl = reg.getMatch(1);
}
// Method 2 (with parsing JSON)
local t = ParseJSON(nm.responseBody());
if (nm.responseCode() != 200) {
_PrintError(t, "Failed to upload");
return ResultCode.Failure;
}
if (t != null && "url" in t) {
viewUrl = t.view_url;
thumbnailUrl = t.thumb_url;
} else {
_PrintError(t, "Failed to upload");
return ResultCode.Failure;
}
if (directUrl == "") {
WriteLog("error", "[example.com] Upload failed. Cannot obtain the direct URL!");
return ResultCode.Failure;
}
options.setDirectUrl(directUrl);
options.setViewUrl(viewUrl);
options.setThumbUrl(thumbnailUrl);
return ResultCode.Success;
}
// Authenticating on remote server (optional function)
// @var CFolderList list
// @return int - success(1), failure(0)
function Authenticate() {
if (ServerParams.getParam("token") != "") {
return ResultCode.Success;
}
local username = ServerParams.getParam("Login"); // Predefined parameter, no need to define it in GetServerParamList
local password = ServerParams.getParam("Password"); // Predefined parameter, no need to define it in GetServerParamList
// Performing request
// ...
local t = ParseJSON(nm.responseBody());
if ("access_token" in t) {
ServerParams.setParam("token", t.access_token);
return ResultCode.Success;
}
return ResultCode.Failure;
}
// Optional function:
// Retrieving folder (album) list from server
// @var CFolderList list
// @return int - success(1), failure(0)
function GetFolderList(list) {
nm.doGet(BASE_URL + "/albums");
local obj = ParseJSON(nm.responseBody());
if (obj != null) {
if ("success" in obj && obj.success) {
local rootFolder = CFolderItem();
rootFolder.setId("/");
rootFolder.setTitle("My Images");
list.AddFolderItem(rootFolder);
local albums = obj.result.albums;
local count = albums.len();
for (local i = 0; i < count; i++) {
local item = albums[i];
local folder = CFolderItem();
folder.setId(item.id);
folder.setTitle(item.title);
folder.setParentId("/");
folder.setAccessType(item.public ? 1 : 0);
folder.setViewUrl("https://example.com/a/" + item.id);
list.AddFolderItem(folder);
}
return ResultCode.Success;
} else if ("error" in obj){
WriteLog("error", "[example.com] error: " + obj.error.error_message);
}
} else if (nm.responseCode() != 200){
WriteLog("error", "[example.com] response code: " + m.responseCode());
}
return ResultCode.Failure;
}
// Create an folder or an album (optional function)
// @var CFolderItem parentAlbum
// @var CFolderItem album
// @return int - success(1), failure(0)
//
function CreateFolder(parentAlbum, album) {
local req = {
title = album.getTitle(),
description = album.getSummary(),
public = album.getAccessType() == 0 ? "FALSE" : "TRUE"
};
nm.setUrl(BASE_URL + "/v2/albums");
nm.addQueryHeader("Content-Type", "application/json");
nm.doPost(ToJSON(req));
local obj = ParseJSON(nm.responseBody());
if (obj != null) {
if ("success" in obj && obj.success) {
local remoteAlbum = obj.result;
album.setId(remoteAlbum.id);
album.setTitle(remoteAlbum.title);
album.setSummary(remoteAlbum.description);
album.setAccessType(remoteAlbum.public ? 1:0);
album.setViewUrl("https://example.com/a/" + remoteAlbum.id);
return ResultCode.Success;
} else if ("error" in obj){
WriteLog("error", "[example.com] error: " + obj.error.error_message);
}
} else if (nm.responseCode() != 200){
WriteLog("error", "[example.com] response code: " + m.responseCode());
}
return ResultCode.Failure;
}
// Modify a folder or an album (update name, description) (optional function)
// @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: (optional function)
// private, public, only for friends, etc.
// @return array
return ["Private", "Public"];
}
// optional function
function GetServerParamList() {
return {
apiToken = "API Token",
albumid = "Album ID",
age = "Age",
striptags = "Strip Tags",
notify = {
title = "Email notification on download",
type = "boolean" // checkbox
},
}
}
int UploadFile(string pathToFile, UploadParams params)
int Authenticate()
table GetServerParamList()
array GetFolderAccessTypeList()
void WriteLog(string type, string message)
string GetFileMimeType(string filename)
object ParseJSON(string json)

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.
Sync(global object) is an instance of ServerSync.