Spaces:
Running
Running
2410191218
Browse files- app.py +8 -1
- public/script.js +19 -13
app.py
CHANGED
@@ -19,6 +19,7 @@ app.add_middleware(
|
|
19 |
)
|
20 |
|
21 |
router = APIRouter()
|
|
|
22 |
|
23 |
class TextRequest(BaseModel): text: str
|
24 |
|
@@ -56,10 +57,15 @@ async def load(zip_data: TextRequest):
|
|
56 |
|
57 |
return data
|
58 |
|
|
|
|
|
|
|
59 |
@router.post('/sb3')
|
60 |
async def sb3(req_data: TextRequest):
|
|
|
|
|
61 |
data = json.loads(req_data.text)
|
62 |
-
with zipfile.ZipFile('mmp4.zip', 'r') as template_zip:
|
63 |
with template_zip.open('project.json') as f:
|
64 |
project = json.loads(f.read().decode('utf-8'))
|
65 |
|
@@ -109,6 +115,7 @@ async def sb3(req_data: TextRequest):
|
|
109 |
zip_buffer.seek(0)
|
110 |
sb3_base64 = base64.b64encode(zip_buffer.getvalue()).decode('utf-8')
|
111 |
|
|
|
112 |
return sb3_base64
|
113 |
|
114 |
app.include_router(router, prefix='/api')
|
|
|
19 |
)
|
20 |
|
21 |
router = APIRouter()
|
22 |
+
processing = False
|
23 |
|
24 |
class TextRequest(BaseModel): text: str
|
25 |
|
|
|
57 |
|
58 |
return data
|
59 |
|
60 |
+
@router.get('/check')
|
61 |
+
async def check(): return 'ng' if processing else 'ok'
|
62 |
+
|
63 |
@router.post('/sb3')
|
64 |
async def sb3(req_data: TextRequest):
|
65 |
+
if processing: return ''
|
66 |
+
processing = True
|
67 |
data = json.loads(req_data.text)
|
68 |
+
with zipfile.ZipFile(Path('mmp4.zip'), 'r') as template_zip:
|
69 |
with template_zip.open('project.json') as f:
|
70 |
project = json.loads(f.read().decode('utf-8'))
|
71 |
|
|
|
115 |
zip_buffer.seek(0)
|
116 |
sb3_base64 = base64.b64encode(zip_buffer.getvalue()).decode('utf-8')
|
117 |
|
118 |
+
processing = False
|
119 |
return sb3_base64
|
120 |
|
121 |
app.include_router(router, prefix='/api')
|
public/script.js
CHANGED
@@ -118,20 +118,26 @@ const addCos = () => {
|
|
118 |
}
|
119 |
|
120 |
const sb3 = async () => {
|
121 |
-
fetch(`/api/
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
135 |
};
|
136 |
|
137 |
const save = async () => {
|
|
|
118 |
}
|
119 |
|
120 |
const sb3 = async () => {
|
121 |
+
fetch(`/api/check`)
|
122 |
+
.then(res => res.text())
|
123 |
+
.then(resData => {
|
124 |
+
if (resData == `ok`) {
|
125 |
+
fetch(`/api/sb3`, {
|
126 |
+
method: `POST`,
|
127 |
+
headers: { 'Content-Type': 'application/json' },
|
128 |
+
body: JSON.stringify({ text: JSON.stringify([Blockly.JavaScript.workspaceToCode(workspace).split('$')[1], ...costumes]) })
|
129 |
+
})
|
130 |
+
.then(data => data.json())
|
131 |
+
.then(sb3Base64 => {
|
132 |
+
const sb3Data = Uint8Array.from(atob(sb3Base64), c => c.charCodeAt(0));
|
133 |
|
134 |
+
const a = document.createElement('a');
|
135 |
+
a.href = URL.createObjectURL(new Blob([sb3Data], { type: 'application/x-scratch-project' }));
|
136 |
+
a.download = 'project.sb3';
|
137 |
+
a.click();
|
138 |
+
});
|
139 |
+
} else alert(`.sb3 出力中のユーザーがいるため、時間を空けてから押してください`);
|
140 |
+
})
|
141 |
};
|
142 |
|
143 |
const save = async () => {
|