horiyouta commited on
Commit
529bb0c
·
1 Parent(s): 1d5dbe3

2410191208

Browse files
Files changed (3) hide show
  1. __pycache__/app.cpython-310.pyc +0 -0
  2. app.py +15 -16
  3. public/script.js +13 -7
__pycache__/app.cpython-310.pyc ADDED
Binary file (3.79 kB). View file
 
app.py CHANGED
@@ -1,8 +1,9 @@
1
  from fastapi.middleware.cors import CORSMiddleware
2
  from fastapi.staticfiles import StaticFiles
3
- from fastapi import APIRouter, FastAPI
4
  import base64, json, zipfile, uvicorn
5
  from strgen import StringGenerator
 
6
  from pathlib import Path
7
  from io import BytesIO
8
  from PIL import Image
@@ -11,12 +12,7 @@ app = FastAPI()
11
 
12
  app.add_middleware(
13
  CORSMiddleware,
14
- allow_origins=[
15
- "http://localhost:3000",
16
- "http://localhost:8000",
17
- "http://127.0.0.1:3000",
18
- "http://127.0.0.1:8000",
19
- ],
20
  allow_credentials=True,
21
  allow_methods=["*"],
22
  allow_headers=["*"],
@@ -24,8 +20,11 @@ app.add_middleware(
24
 
25
  router = APIRouter()
26
 
 
 
27
  @router.post('/save')
28
- def save(data):
 
29
  zip_buffer = BytesIO()
30
 
31
  with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
@@ -41,8 +40,8 @@ def save(data):
41
  return base64.b64encode(zip_buffer.getvalue()).decode('utf-8')
42
 
43
  @router.post('/load')
44
- def load(zip_data):
45
- zip_buffer = BytesIO(base64.b64decode(zip_data))
46
  data = []
47
  with zipfile.ZipFile(zip_buffer, 'r') as zip_file:
48
  data.append(zip_file.open('data.xml').read().decode('utf-8'))
@@ -58,8 +57,8 @@ def load(zip_data):
58
  return data
59
 
60
  @router.post('/sb3')
61
- def sb3(data):
62
- # まず、mmp4.zipからproject.jsonを読み込む
63
  with zipfile.ZipFile('mmp4.zip', 'r') as template_zip:
64
  with template_zip.open('project.json') as f:
65
  project = json.loads(f.read().decode('utf-8'))
@@ -112,8 +111,8 @@ def sb3(data):
112
 
113
  return sb3_base64
114
 
115
- app.include_router(router, prefix="/api")
116
 
117
- if __name__ == "__main__":
118
- app.mount("/", StaticFiles(directory=Path('public'), html=True), name='public')
119
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  from fastapi.middleware.cors import CORSMiddleware
2
  from fastapi.staticfiles import StaticFiles
3
+ from fastapi import FastAPI, APIRouter
4
  import base64, json, zipfile, uvicorn
5
  from strgen import StringGenerator
6
+ from pydantic import BaseModel
7
  from pathlib import Path
8
  from io import BytesIO
9
  from PIL import Image
 
12
 
13
  app.add_middleware(
14
  CORSMiddleware,
15
+ allow_origins=["*"],
 
 
 
 
 
16
  allow_credentials=True,
17
  allow_methods=["*"],
18
  allow_headers=["*"],
 
20
 
21
  router = APIRouter()
22
 
23
+ class TextRequest(BaseModel): text: str
24
+
25
  @router.post('/save')
26
+ async def save(req_data: TextRequest):
27
+ data = json.loads(req_data.text)
28
  zip_buffer = BytesIO()
29
 
30
  with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
 
40
  return base64.b64encode(zip_buffer.getvalue()).decode('utf-8')
41
 
42
  @router.post('/load')
43
+ async def load(zip_data: TextRequest):
44
+ zip_buffer = BytesIO(base64.b64decode(zip_data.text))
45
  data = []
46
  with zipfile.ZipFile(zip_buffer, 'r') as zip_file:
47
  data.append(zip_file.open('data.xml').read().decode('utf-8'))
 
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'))
 
111
 
112
  return sb3_base64
113
 
114
+ app.include_router(router, prefix='/api')
115
 
116
+ if __name__ == '__main__':
117
+ app.mount('/', StaticFiles(directory=Path('public'), html=True), name='public')
118
+ uvicorn.run(app, host='0.0.0.0', port=7860)
public/script.js CHANGED
@@ -118,10 +118,12 @@ const addCos = () => {
118
  }
119
 
120
  const sb3 = async () => {
121
- fetch(`/sb3`, {
122
  method: `POST`,
123
- body: JSON.stringify([Blockly.JavaScript.workspaceToCode(workspace).split('$')[1], ...costumes])
 
124
  })
 
125
  .then(sb3Base64 => {
126
  const sb3Data = Uint8Array.from(atob(sb3Base64), c => c.charCodeAt(0));
127
 
@@ -133,10 +135,12 @@ const sb3 = async () => {
133
  };
134
 
135
  const save = async () => {
136
- fetch(`/save`, {
137
  method: `POST`,
138
- body: JSON.stringify([Blockly.JavaScript.workspaceToCode(workspace).split('$')[1], ...costumes])
 
139
  })
 
140
  .then(base64Zip => {
141
  const zipData = Uint8Array.from(atob(base64Zip), c => c.charCodeAt(0));
142
 
@@ -156,10 +160,12 @@ const load = async () => {
156
  const fileReader = new FileReader();
157
  fileReader.addEventListener(`load`, async () => {
158
  const base64Data = btoa(String.fromCharCode.apply(null, new Uint8Array(fileReader.result)));
159
- fetch(`/load`, {
160
- method: `POST`,
161
- body: base64Data
 
162
  })
 
163
  .then(data => {
164
  const dom = (new DOMParser()).parseFromString(data[0], `application/xml`);
165
  workspace.clear();
 
118
  }
119
 
120
  const sb3 = async () => {
121
+ fetch(`/api/sb3`, {
122
  method: `POST`,
123
+ headers: { 'Content-Type': 'application/json' },
124
+ body: JSON.stringify({ text: JSON.stringify([Blockly.JavaScript.workspaceToCode(workspace).split('$')[1], ...costumes]) })
125
  })
126
+ .then(data => data.json())
127
  .then(sb3Base64 => {
128
  const sb3Data = Uint8Array.from(atob(sb3Base64), c => c.charCodeAt(0));
129
 
 
135
  };
136
 
137
  const save = async () => {
138
+ fetch(`/api/save`, {
139
  method: `POST`,
140
+ headers: { 'Content-Type': 'application/json' },
141
+ body: JSON.stringify({ text: JSON.stringify([Blockly.Xml.domToText(Blockly.Xml.workspaceToDom(workspace)), Blockly.JavaScript.workspaceToCode(workspace), ...costumes]) })
142
  })
143
+ .then(data => data.json())
144
  .then(base64Zip => {
145
  const zipData = Uint8Array.from(atob(base64Zip), c => c.charCodeAt(0));
146
 
 
160
  const fileReader = new FileReader();
161
  fileReader.addEventListener(`load`, async () => {
162
  const base64Data = btoa(String.fromCharCode.apply(null, new Uint8Array(fileReader.result)));
163
+ fetch(`/api/load`, {
164
+ method: `post`,
165
+ headers: { 'Content-Type': 'application/json' },
166
+ body: JSON.stringify({ text: base64Data })
167
  })
168
+ .then(res => res.json())
169
  .then(data => {
170
  const dom = (new DOMParser()).parseFromString(data[0], `application/xml`);
171
  workspace.clear();