Skip to content

Commit

Permalink
No throw in Squirrel scripts (#375)
Browse files Browse the repository at this point in the history
* No 'throw' in .nut scripts

* Small refactoring of scripts

Fixed Login/Password issue for vgy.me
  • Loading branch information
zenden2k authored Mar 31, 2024
1 parent 074a865 commit 203f2d9
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 89 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ Dist/serverchecker
/Build*/
.vs/
.vscode/
.idea/
/Dist/Packages/
/Dist/Build_Release_Temp/
/Dist/Build_Release_Temp/
/Dist/Libs/
40 changes: 17 additions & 23 deletions Data/Scripts/dropbox.nut
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ try {
} catch ( ex ) {
}

function signRequest(url, token) {
function _SignRequest(url, token) {
nm.addQueryHeader("Authorization", "Bearer " + token);

return url;
Expand All @@ -42,12 +42,6 @@ function OnUrlChangedCallback(data) {
br.close();
}
}
function sendOauthRequest(url, token) {
nm.setUrl(url);
signRequest(url, token);
nm.doPost("" );
return 0;
}

function RefreshToken() {
local expiresIn = 0;
Expand Down Expand Up @@ -78,7 +72,7 @@ function RefreshToken() {
return 1;
}

function ObtainAccessToken() {
function _ObtainAccessToken() {
if (authCode != ""){
local url = "https://api.dropboxapi.com/oauth2/token";
nm.setUrl(url);
Expand Down Expand Up @@ -126,7 +120,7 @@ function Authenticate() {
browser.navigateToUrl(url);
browser.showModal();

return ObtainAccessToken();
return _ObtainAccessToken();
}

function IsAuthenticated() {
Expand All @@ -143,7 +137,7 @@ function DoLogout() {
}
local url = "https://api.dropboxapi.com/2/auth/token/revoke";
nm.setUrl(url);
signRequest(url, token);
_SignRequest(url, token);
nm.addQueryHeader("Content-Type", "application/json");
nm.doPost("null");
ServerParams.setParam("token", "");
Expand Down Expand Up @@ -202,23 +196,23 @@ function UploadFile(FileName, options) {
}
if( session==""){
url = "https://content.dropboxapi.com/2/files/upload_session/start" ;
signRequest(url, token);
_SignRequest(url, token);
local arg ={
close=false
};
local json = reg_replace(ToJSON(arg),"\n","");
local json = _RegReplace(ToJSON(arg),"\n","");
nm.addQueryHeader("Dropbox-API-Arg", json);
} else{
url = "https://content.dropboxapi.com/2/files/upload_session/append_v2" ;
signRequest(url, token);
_SignRequest(url, token);
local arg ={
cursor={
session_id=session,
offset=offset
},
close=false
};
local json = reg_replace(ToJSON(arg),"\n","");
local json = _RegReplace(ToJSON(arg),"\n","");
nm.addQueryHeader("Dropbox-API-Arg", json);
}
local chunkSize = min(chunkSize,fileSize.tofloat()-offset).tointeger();
Expand Down Expand Up @@ -266,10 +260,10 @@ function UploadFile(FileName, options) {
mute=false
}
};
local json = reg_replace(ToJSON(arg),"\n","");
local json = _RegReplace(ToJSON(arg),"\n","");
nm.addQueryHeader("Dropbox-API-Arg", json);
nm.addQueryHeader("Content-Type", "application/octet-stream");
signRequest(url, token);
_SignRequest(url, token);
nm.setMethod("POST");
nm.doPost("");

Expand All @@ -280,15 +274,15 @@ function UploadFile(FileName, options) {

} else {
url = "https://content.dropboxapi.com/2/files/upload" ;
signRequest(url, token);
_SignRequest(url, token);
local arg ={
path=remotePath,
mode="add",
autorename=true,
mute=false
};
nm.addQueryHeader("Content-Type", "application/octet-stream");
local json = reg_replace(ToJSON(arg),"\n","");
local json = _RegReplace(ToJSON(arg),"\n","");
nm.addQueryHeader("Dropbox-API-Arg", json);
nm.setUrl(url);
nm.doUpload(FileName, "");
Expand All @@ -303,14 +297,14 @@ function UploadFile(FileName, options) {
fileId = data.id;

url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings" ;
signRequest(url, token);
_SignRequest(url, token);
local arg ={
path=remotePath,
settings={
requested_visibility="public"
}
};
local json = reg_replace(ToJSON(arg),"\n","");
local json = _RegReplace(ToJSON(arg),"\n","");
nm.addQueryHeader("Content-Type","application/json")
nm.setUrl(url);
nm.enableResponseCodeChecking(false);
Expand All @@ -322,12 +316,12 @@ function UploadFile(FileName, options) {
if (nm.responseCode() == 409) { // Shared link already exists
data = ParseJSON(nm.responseBody());
url = "https://api.dropboxapi.com/2/sharing/list_shared_links" ;
signRequest(url, token);
_SignRequest(url, token);
local arg ={
path=remotePath,
direct_only=true
};
local json = reg_replace(ToJSON(arg),"\n","");
local json = _RegReplace(ToJSON(arg),"\n","");
nm.addQueryHeader("Content-Type","application/json")
nm.setUrl(url);
nm.doUpload("",json);
Expand Down Expand Up @@ -359,7 +353,7 @@ function UploadFile(FileName, options) {
return 0;
}

function reg_replace(str, pattern, replace_with) {
function _RegReplace(str, pattern, replace_with) {
local resultStr = str;
local res;
local start = 0;
Expand Down
56 changes: 40 additions & 16 deletions Data/Scripts/googledrive.nut
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ function _ClearAuthData() {
ServerParams.setParam("tokenTime", "");
}

function _CheckResponse(except = true) {
function _CheckResponse(except = false) {
if (nm.responseCode() == 403) {
if (nm.responseBody().find("Invalid token", 0)!= null) {
WriteLog("warning", nm.responseBody());
_ClearAuthData();
if (except) {
throw "unauthorized_exception";
}
return -2;
} else {
WriteLog("error", "403 Access denied" );
}
Expand All @@ -30,7 +31,7 @@ function _CheckResponse(except = true) {
if (except) {
throw "unauthorized_exception";
}
return 0;
return -2;
} else if (/*nm.responseCode() == 0 ||*/ (nm.responseCode() >= 400 && nm.responseCode() <= 499)) {
WriteLog("error", "Response code " + nm.responseCode() + "\r\n" + nm.errorString() );
return 0;
Expand Down Expand Up @@ -65,8 +66,12 @@ function RefreshToken() {
nm.addQueryParam("client_secret", clientSecret);
nm.addQueryParam("grant_type", "refresh_token");
nm.doPost("");
if (_CheckResponse()) {
local data = nm.responseBody();
local code = _CheckResponse();
if (code < 1) {
WriteLog("error", "[googledrive] refreshing token failed, response code: " + nm.responseCode());
return code;
} else {
local data = nm.responseBody();
local t = ParseJSON(data);
if ("access_token" in t) {
token = t.access_token;
Expand Down Expand Up @@ -140,7 +145,9 @@ function Authenticate() {
nm.addQueryParam("redirect_uri", redirectUrl);
nm.addQueryParam("grant_type", "authorization_code");
nm.doPost("");
if (!_CheckResponse(false)) {
local code = _CheckResponse();
if (code < 1) {
WriteLog("error", "[googledrive] Auth failed, response code: " + nm.responseCode());
return 0;
}
local data = nm.responseBody();
Expand Down Expand Up @@ -194,8 +201,11 @@ function DoLogout() {
function GetFolderList(list) {
nm.addQueryHeader("Authorization", _GetAuthorizationString());
nm.doGet("https://www.googleapis.com/drive/v2/files");

if (nm.responseCode() == 200) {
local code = _CheckResponse();
if (code < 1) {
WriteLog("error", "[googledrive] Getting folder list failed, response code: " + nm.responseCode());
return code;
} else {
local t = ParseJSON(nm.responseBody());
if ( t != null ) {
local count = t.items.len();
Expand Down Expand Up @@ -228,7 +238,11 @@ function CreateFolder(parentFolder, folder) {
};
nm.addQueryHeader("Content-Type","application/json");
nm.doPost(ToJSON(data));
if (_CheckResponse()) {
local code = _CheckResponse();
if (code < 1) {
WriteLog("error", "[googledrive] Creating folder failed, response code: " + nm.responseCode());
return code;
} else {
local responseData = nm.responseBody();
local item = ParseJSON(responseData);
if ( item != null ) {
Expand All @@ -251,11 +265,13 @@ function ModifyFolder(folder) {
title = folder.getTitle(),
};
nm.doUpload("", ToJSON(postData));
if (_CheckResponse()) {
return 1;
local code = _CheckResponse();
if (code < 1) {
WriteLog("error", "[googledrive] Modifying folder failed, response code: " + nm.responseCode());
return code;
}

return 0;
return 1;
}

function UploadFile(FileName, options) {
Expand All @@ -278,17 +294,25 @@ function UploadFile(FileName, options) {
nm.setUrl("https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable");
nm.addQueryHeader("Authorization", _GetAuthorizationString());
nm.setMethod("POST");
nm.doUpload("", str);
if (_CheckResponse()) {
nm.doPost(str);
local code = _CheckResponse();
if (code < 1) {
WriteLog("error", "[googledrive] Starting upload failed, response code: " + nm.responseCode());
return code;
} else {
local sessionUri = nm.responseHeaderByName("Location");
if (sessionUri != "") {
nm.setMethod("PUT");
nm.addQueryHeader("Authorization", _GetAuthorizationString());
nm.addQueryHeader("Content-Type", mimeType);
nm.addQueryHeader("Content-Length", fileSizeStr);
//nm.addQueryHeader("Content-Length", fileSizeStr);
nm.setUrl(sessionUri);
nm.doUpload(FileName, "");
if (_CheckResponse()) {
code = _CheckResponse();
if (code < 1) {
WriteLog("error", "[googledrive] Starting upload failed, response code: " + nm.responseCode());
return code;
} else {
local responseData = nm.responseBody();
local item = ParseJSON(responseData);
nm.addQueryHeader("Authorization", _GetAuthorizationString());
Expand All @@ -299,7 +323,7 @@ function UploadFile(FileName, options) {
};
nm.addQueryHeader("Content-Type", "application/json");
nm.setMethod("POST");
nm.doUpload("", ToJSON(postData));
nm.doPost(ToJSON(postData));
options.setViewUrl(item.alternateLink);

if (item.mimeType.find("image/", 0) == 0) {
Expand Down
17 changes: 10 additions & 7 deletions Data/Scripts/imgur.nut
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
Sergey Svistunov @zenden2k
*/

clientId <- "b38ba6b0919a898";
const CLIENT_ID = "b38ba6b0919a898";

function GetAuthorizationString() {
local token = ServerParams.getParam("token");
local tokenType = ServerParams.getParam("tokenType");
if (token == "") {
return "Client-ID " + clientId;
return "Client-ID " + CLIENT_ID;
}
return "Bearer" + " " + token;
}
Expand All @@ -19,7 +19,7 @@ function IsAuthenticated() {
return ServerParams.getParam("token") != "" ? 1 : 0;
}

function checkResponse(except = true) {
function checkResponse(except = false) {
if ( nm.responseCode() == 403 ) {
if ( nm.responseBody().find("Invalid token",0)!= null) {
WriteLog("warning", nm.responseBody());
Expand All @@ -32,7 +32,7 @@ function checkResponse(except = true) {
if (except) {
throw "unauthorized_exception";
}
return 0;
return -2;
} else {
WriteLog("error", "403 Access denied" );
return 0;
Expand Down Expand Up @@ -70,7 +70,7 @@ function RefreshToken() {
// Refresh access token
nm.setUrl("https://api.imgur.com/oauth2/token");
nm.addQueryParam("refresh_token", refreshToken);
nm.addQueryParam("client_id", clientId);
nm.addQueryParam("client_id", CLIENT_ID);
nm.addQueryParam("client_secret", "d91cd90f01cadaa1796d8d9b9231c218c11ed628");
nm.addQueryParam("grant_type", "refresh_token");
nm.doPost("");
Expand Down Expand Up @@ -112,7 +112,7 @@ function Authenticate() {
return 1;
}
local login = ServerParams.getParam("Login");
local url = "https://api.imgur.com/oauth2/authorize?client_id=" + nm.urlEncode(clientId) +"&response_type=token&state=token";
local url = "https://api.imgur.com/oauth2/authorize?client_id=" + nm.urlEncode(CLIENT_ID) +"&response_type=token&state=token";
ShellOpenUrl(url);

local confirmCode = InputDialog(tr("imgur.confirmation.text", "You need to need to sign in to your Imgur account\r\n in web browser which just have opened and then\r\n copy confirmation code into the text field below.\r\nPlease enter confirmation code:"),"");
Expand Down Expand Up @@ -145,7 +145,10 @@ function UploadFile(FileName, options) {
nm.addQueryHeader("Authorization", GetAuthorizationString());
nm.addQueryParamFile("image", FileName, ExtractFileName(FileName),"");
nm.doUploadMultipartData();
if (nm.responseCode() == 200) {
local code = checkResponse();
if (code < 1) {
return code;
} else if (nm.responseCode() == 200) {
local retdoc = nm.responseBody();
local json = ParseJSON(retdoc);
if (json != null) {
Expand Down
Loading

0 comments on commit 203f2d9

Please sign in to comment.