-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
120 lines (110 loc) · 4.29 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(function(Scratch) {
'use strict';
const getGofileServer = async () => {
try {
const response = await Scratch.fetch('https://api.gofile.io/getServer');
if (!response.ok) {
throw new Error('HTTP error ' + response.status);
}
const serverData = await response.json();
return `https://${serverData.data.server}.gofile.io/uploadFile`;
} catch (error) {
console.error('Failed to fetch Gofile server: ', error);
}
};
class Upload {
getInfo() {
return {
id: "clayuploadfile",
name: "Upload",
blocks: [
{
opcode: "upload",
blockType: Scratch.BlockType.REPORTER,
text: "upload file to [url]",
arguments: {
url: {
type: Scratch.ArgumentType.STRING,
defaultValue: "https://store1.gofile.io/uploadFile",
},
},
},
{
opcode: "uploadToWebsite",
blockType: Scratch.BlockType.REPORTER,
text: "upload file to [url]",
arguments: {
url: {
type: Scratch.ArgumentType.STRING,
defaultValue: "gofile.io",
menu: 'websites'
},
}
}
],
menus: {
websites: {
acceptReporters: false,
items: ["gofile.io", "file.io"]
}
}
};
}
upload(args) {
return this.performUpload(args.url, true);
}
async uploadToWebsite(args) {
let url;
switch (args.url.toLowerCase()){
case 'gofile.io':
url = await getGofileServer();
break;
case 'file.io':
url = 'https://file.io/';
break;
}
return this.performUpload(url, false);
}
performUpload(url, rawJson = false) {
return new Promise((resolve, reject) => {
const inputElement = document.createElement("input");
inputElement.type = "file";
inputElement.style.display = "none";
document.body.appendChild(inputElement);
inputElement.click();
inputElement.addEventListener("change", function() {
if (this.files && this.files[0]) {
const formData = new FormData();
formData.append("file", this.files[0], this.files[0].name);
const options = {
body: formData,
method: 'POST',
mode: 'cors',
};
Scratch.fetch(url, options)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Upload failed");
}
})
.then(data => {
const result = rawJson ? JSON.stringify(data) : data.link || data.data.downloadPage;
resolve(result);
inputElement.remove();
})
.catch(error => {
reject(error.message);
inputElement.remove();
});
} else {
reject("No file chosen");
inputElement.remove();
}
});
});
}
}
Scratch.extensions.register(new Upload());
})(Scratch);