1+ /* *
2+ * GoFile.io Image Uploader Script
3+ * Official API Documentation: https://gofile.io/api
4+ * Updated: May 16, 2025
5+ */
6+
7+ const UPLOAD_URL = " https://upload.gofile.io/uploadfile" ;
8+ const API_BASE_URL = " https://api.gofile.io" ;
9+
10+ function _PrintError (t, txt) {
11+ local errorMessage = " [gofile.io] " + txt + " Response code: " + nm. responseCode ();
12+ if (t != null && " message" in t) {
13+ errorMessage += " \n " + t. message ;
14+ } else if (t != null && " error" in t) {
15+ errorMessage += " \n " + t. error ;
16+ }
17+ WriteLog (" error" , errorMessage);
18+ }
19+
20+ /* *
21+ * This is the function that performs the upload of the file
22+ * @param string pathToFile
23+ * @param UploadParams options
24+ * @return int - success(1), failure(0)
25+ */
26+ function UploadFile (pathToFile, options) {
27+ /* *
28+ * @var task
29+ * @type FileUploadTaskWrapper
30+ */
31+ local task = options. getTask (). getFileTask ();
32+
33+ // Get API token if provided (for authenticated uploads)
34+ local apiToken = ServerParams. getParam (" Password" );
35+ local folderId = " " ; // options.getFolderID();
36+ local uploadRegion = ServerParams. getParam (" uploadRegion" );
37+
38+ // Determine upload URL based on region preference
39+ local uploadUrl = UPLOAD_URL;
40+ if (uploadRegion != " " && uploadRegion != " auto" ) {
41+ uploadUrl = " https://upload-" + uploadRegion + " .gofile.io/uploadfile" ;
42+ }
43+
44+ nm. setUrl (uploadUrl);
45+
46+ // Add authentication header if token is provided
47+ if (apiToken != " " ) {
48+ nm. addQueryHeader (" Authorization" , " Bearer " + apiToken);
49+ }
50+
51+ // Add the file (required parameter)
52+ nm. addPostFieldFile (" file" , pathToFile, task. getDisplayName (), GetFileMimeType (pathToFile));
53+
54+ // Add optional folderId if specified
55+ if (folderId != " " ) {
56+ nm. addPostField (" folderId" , folderId);
57+ }
58+
59+ // Perform multipart upload
60+ if (! nm. doUploadMultipartData ()) {
61+ return ResultCode. Failure ;
62+ }
63+
64+ // Parse response regardless of HTTP status code as required
65+ local t = ParseJSON (nm. responseBody ());
66+
67+ if (t == null ) {
68+ WriteLog (" error" , " [gofile.io] Invalid JSON response" );
69+ return ResultCode. Failure ;
70+ }
71+
72+ // Check if upload was successful
73+ if (! (" status" in t) || t. status != " ok" ) {
74+ _PrintError (t, " Upload failed" );
75+ return ResultCode. Failure ;
76+ }
77+
78+ if (! (" data" in t)) {
79+ _PrintError (t, " Upload failed - no data in response" );
80+ return ResultCode. Failure ;
81+ }
82+
83+ local data = t. data ;
84+ local fileId = " " ;
85+ local downloadPage = " " ;
86+ local directUrl = " " ;
87+ local guestToken = " " ;
88+
89+ // Extract file information from response
90+ if (" fileId" in data) {
91+ fileId = data. fileId ;
92+ }
93+ if (" downloadPage" in data) {
94+ downloadPage = data. downloadPage ;
95+ }
96+ if (" directLink" in data) {
97+ directUrl = data. directLink ;
98+ }
99+ if (" guestToken" in data) {
100+ guestToken = data. guestToken ;
101+ // Store guest token for future uploads to same folder
102+ ServerParams. setParam (" guestToken" , guestToken);
103+ }
104+ if (" parentFolder" in data) {
105+ // Store parent folder ID for future uploads
106+ ServerParams. setParam (" lastFolderId" , data. parentFolder );
107+ }
108+
109+ // Set URLs for the application
110+ options. setViewUrl (downloadPage);
111+ options. setDirectUrl (directUrl);
112+
113+ return ResultCode. Success ;
114+ }
115+
116+ /* *
117+ * Authenticating on remote server (optional function)
118+ * GoFile.io supports both authenticated and anonymous uploads
119+ * @return int - success(1), failure(0)
120+ */
121+ function Authenticate () {
122+ local userName = ServerParams. getParam (" Login" );
123+ local apiToken = ServerParams. getParam (" Password" );
124+
125+ if (apiToken == " " ) {
126+ if (userName != " " ) {
127+ WriteLog (" error" , " [gofile.io] You must set API token." );
128+ return ResultCode. Failure ;
129+ }
130+ // No token provided - will use anonymous upload
131+ return ResultCode. Success ;
132+ }
133+
134+ // Test the API token by getting account ID
135+ nm. addQueryHeader (" Authorization" , " Bearer " + apiToken);
136+ nm. doGet (API_BASE_URL + " /accounts/getid" );
137+
138+ local t = ParseJSON (nm. responseBody ());
139+
140+ if (t != null && " status" in t && t. status == " ok" ) {
141+ return ResultCode. Success ;
142+ } else {
143+ _PrintError (t, " Authentication failed - invalid API token" );
144+ return ResultCode. Failure ;
145+ }
146+ }
147+
148+ function _ObtainRootFolder (apiToken) {
149+ // Get account ID first
150+ nm. addQueryHeader (" Authorization" , " Bearer " + apiToken);
151+ nm. doGet (API_BASE_URL + " /accounts/getid" );
152+
153+ local accountResponse = ParseJSON (nm. responseBody ());
154+ if (accountResponse == null || ! (" status" in accountResponse) || accountResponse. status != " ok" ) {
155+ _PrintError (accountResponse, " Failed to get account ID" );
156+ return " " ;
157+ }
158+
159+ local accountId = accountResponse. data . id ;
160+
161+ // Get account details to find root folder
162+ nm. addQueryHeader (" Authorization" , " Bearer " + apiToken);
163+ nm. doGet (API_BASE_URL + " /accounts/" + accountId);
164+
165+ local detailsResponse = ParseJSON (nm. responseBody ());
166+ if (detailsResponse == null || ! (" status" in detailsResponse) || detailsResponse. status != " ok" ) {
167+ _PrintError (detailsResponse, " Failed to get account details" );
168+ return " " ;
169+ }
170+ return detailsResponse. data . rootFolder ;
171+ }
172+
173+ /* *
174+ * Optional function:
175+ * Retrieving folder (album) list from server
176+ * This requires premium account and authenticated access
177+ * @var CFolderList list
178+ * @return int - success(1), failure(0)
179+ */
180+ function GetFolderList (list) {
181+ local parentId = list. parentFolder (). getId ();
182+ local apiToken = ServerParams. getParam (" Password" );
183+
184+ if (parentId == " " ) {
185+ local rootFolderId = _ObtainRootFolder (apiToken);
186+ // Add root folder
187+ local rootFolder = CFolderItem ();
188+ rootFolder. setId (rootFolderId);
189+ rootFolder. setTitle (" Root (/)" );
190+ list. AddFolderItem (rootFolder);
191+ } else {
192+ nm. addQueryHeader (" Authorization" , " Bearer " + apiToken);
193+ nm. doGet (API_BASE_URL + " /contents/" + parentId);
194+ }
195+
196+ return ResultCode. Success ;
197+ }
198+
199+ /* *
200+ * Create a folder (optional function)
201+ * Requires API token and premium account
202+ * @var CFolderItem parentAlbum
203+ * @var CFolderItem album
204+ * @return int - success(1), failure(0)
205+ */
206+ function CreateFolder (parentAlbum, album) {
207+ local apiToken = ServerParams. getParam (" Password" );
208+
209+ if (apiToken == " " ) {
210+ WriteLog (" error" , " [gofile.io] API token required for folder creation" );
211+ return ResultCode. Failure ;
212+ }
213+
214+ local parentFolderId = parentAlbum. getId ();
215+
216+ if (parentFolderId == " " ) {
217+ parentFolderId = _ObtainRootFolder (apiToken);
218+ }
219+
220+ local folderName = album. getTitle ();
221+
222+ nm. setUrl (API_BASE_URL + " /contents/createFolder" );
223+ nm. addQueryHeader (" Authorization" , " Bearer " + apiToken);
224+ nm. addQueryHeader (" Content-Type" , " application/json" );
225+
226+ local requestData = {
227+ parentFolderId = parentFolderId
228+ };
229+
230+ if (folderName != " " ) {
231+ requestData. folderName <- folderName;
232+ }
233+
234+ nm. doPost (ToJSON (requestData));
235+
236+ local t = ParseJSON (nm. responseBody ());
237+ if (t != null && " status" in t && t. status == " ok" && " data" in t) {
238+ album. setId (t. data . id );
239+ album. setTitle (t. data . name );
240+ album. setViewUrl (" https://gofile.io/d/" + t. data . code );
241+ return ResultCode. Success ;
242+ } else {
243+ _PrintError (t, " Failed to create folder" );
244+ return ResultCode. Failure ;
245+ }
246+ }
247+
248+ /* *
249+ * Modify a folder (update name, description) (optional function)
250+ * @var CFolderItem album
251+ * @return int - success(1), failure(0)
252+ */
253+ function ModifyFolder (album) {
254+ local apiToken = ServerParams. getParam (" Password" );
255+
256+ if (apiToken == " " ) {
257+ WriteLog (" error" , " [gofile.io] API token required for folder modification" );
258+ return ResultCode. Failure ;
259+ }
260+
261+ local folderId = album. getId ();
262+ local folderName = album. getTitle ();
263+
264+ nm. setUrl (API_BASE_URL + " /contents/" + folderId + " /update" );
265+ nm. addQueryHeader (" Authorization" , " Bearer " + apiToken);
266+ nm. addQueryHeader (" Content-Type" , " application/json" );
267+
268+ local requestData = {
269+ attribute = " name" ,
270+ attributeValue = folderName
271+ };
272+
273+ nm. doPut (ToJSON (requestData));
274+
275+ local t = ParseJSON (nm. responseBody ());
276+ if (t != null && " status" in t && t. status == " ok" ) {
277+ return ResultCode. Success ;
278+ } else {
279+ _PrintError (t, " Failed to modify folder" );
280+ return ResultCode. Failure ;
281+ }
282+ }
283+
284+ /* *
285+ * A function that returns a list of types of access restrictions to an album
286+ * @return array
287+ */
288+ function GetFolderAccessTypeList () {
289+ return [" Public" , " Private" ];
290+ }
291+
292+ /* *
293+ * Define server parameters that user can configure
294+ * @return table
295+ */
296+ function GetServerParamList () {
297+ return {
298+ uploadRegion = {
299+ title = " Upload Region" ,
300+ type = " choice" ,
301+ items = [
302+ {
303+ id = " auto" ,
304+ label = " Automatic (Closest Region)"
305+ },
306+ {
307+ id = " eu-par" ,
308+ label = " Europe (Paris)"
309+ },
310+ {
311+ id = " na-phx" ,
312+ label = " North America (Phoenix)"
313+ },
314+ {
315+ id = " ap-sgp" ,
316+ label = " Asia Pacific (Singapore)"
317+ },
318+ {
319+ id = " ap-hkg" ,
320+ label = " Asia Pacific (Hong Kong)"
321+ },
322+ {
323+ id = " ap-tyo" ,
324+ label = " Asia Pacific (Tokyo)"
325+ },
326+ {
327+ id = " sa-sao" ,
328+ label = " South America (São Paulo)"
329+ }
330+ ]
331+ }
332+ }
333+ }
0 commit comments