Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLSnap! Fileshare #1613

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions extensions/Bitter_130/SQLSnapFileshare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Name: SQLSnap! Fileshare
// ID: bitter130SQLSnapFileshare
// Description: Port of the SQLSnap! Fileshare extension for cloud file share utilization.
// By: Bitter_130

(function (Scratch) {
"use strict";

class SQLSnapFileshare {
constructor() {
this.serverURL =
"https://snapextensions.uni-goettingen.de/handleTextfile.php";
}

getInfo() {
return {
id: "bitter130SQLSnapFileshare",
name: "SQLSnap! " + Scratch.translate("Fileshare"),
color1: "#31b3d4",
color2: "#179fc2",
blocks: [
{
opcode: "setServerURL",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("set server URL to [URL]"),
arguments: {
URL: {
type: Scratch.ArgumentType.STRING,
defaultValue: this.serverURL,
},
},
},
{
opcode: "saveToServer",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("save [FILE] with content [CONTENT]"),
arguments: {
FILE: {
type: Scratch.ArgumentType.STRING,
defaultValue: Scratch.translate("data") + ".txt",
},
CONTENT: {
type: Scratch.ArgumentType.STRING,
defaultValue: Scratch.translate("Hello World!"),
},
},
},
{
opcode: "loadFromServer",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("load [FILE]"),
arguments: {
FILE: {
type: Scratch.ArgumentType.STRING,
defaultValue: Scratch.translate("data") + ".txt",
},
},
},
{
opcode: "fits",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("[DATA] fits on server?"),
arguments: {
DATA: {
type: Scratch.ArgumentType.STRING,
defaultValue: Scratch.translate("data"),
},
},
},
{
opcode: "deleteFromServer",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("delete [FILE]"),
arguments: {
FILE: {
type: Scratch.ArgumentType.STRING,
defaultValue: Scratch.translate("data") + ".txt",
},
},
},
],
};
}

setServerURL(args) {
args.URL = Scratch.Cast.toString(args.URL);
this.serverURL = args.URL;
Comment on lines +86 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be 1 line, also make sure to validate the url

}

async saveToServer(args) {
args.FILE = Scratch.Cast.toString(args.FILE);
args.CONTENT = Scratch.Cast.toString(args.CONTENT);

const url =
this.serverURL +
"?type=write" +
"&content=" +
encodeURIComponent(args.CONTENT) +
"&filename=./textfiles/" +
encodeURIComponent(args.FILE);

return await Scratch.fetch(url)
.then((response) => response.text())
.then((result) => result === "ok")
.catch((error) => {
console.error("Failed to save data:", error);
return false;
});
}

async loadFromServer(args) {
args.FILE = Scratch.Cast.toString(args.FILE);

const url =
this.serverURL +
"?type=read" +
"&filename=./textfiles/" +
encodeURIComponent(args.FILE);

return await Scratch.fetch(url)
.then((response) => response.text())
.then((text) => {
if (text === "ERROR: file does not exist") {
return ""; // Return empty string or any indication that file does not exist
} else {
return text;
}
})
.catch((error) => {
console.error("Failed to load data:", error);
return "can't get data";
});
}

fits(args) {
args.DATA = Scratch.Cast.toString(args.DATA);
return args.DATA.length < 10001;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could also be 1 line

}

async deleteFromServer(args) {
args.FILE = Scratch.Cast.toString(args.FILE);

const url =
this.serverURL +
"?type=delete" +
"&filename=./textfiles/" +
encodeURIComponent(args.FILE);

return await Scratch.fetch(url)
.then((response) => response.text())
.then((result) => result === "ok")
.catch((error) => {
console.error("Failed to delete data:", error);
return false;
});
}
}

Scratch.extensions.register(new SQLSnapFileshare());
})(Scratch);