horiyouta commited on
Commit
2bbbf45
·
1 Parent(s): 957a58f

2410191307

Browse files
Files changed (3) hide show
  1. app.py +10 -3
  2. public/mmp4.zip +0 -3
  3. public/script.js +22 -13
app.py CHANGED
@@ -20,6 +20,7 @@ app.add_middleware(
20
 
21
  router = APIRouter()
22
  processing = False
 
23
 
24
  class TextRequest(BaseModel): text: str
25
 
@@ -60,15 +61,21 @@ async def load(zip_data: TextRequest):
60
  @router.get('/check')
61
  async def check():
62
  global processing
63
- return 'ng' if processing else 'ok'
 
 
 
 
 
 
64
 
65
  @router.post('/sb3')
66
  async def sb3(req_data: TextRequest):
67
- global processing
68
  if processing: return ''
69
  processing = True
70
  data = json.loads(req_data.text)
71
- with zipfile.ZipFile(Path('public').joinpath('mmp4.zip').resolve(), 'r') as template_zip:
72
  with template_zip.open('project.json') as f:
73
  project = json.loads(f.read().decode('utf-8'))
74
 
 
20
 
21
  router = APIRouter()
22
  processing = False
23
+ defaultZip = None
24
 
25
  class TextRequest(BaseModel): text: str
26
 
 
61
  @router.get('/check')
62
  async def check():
63
  global processing
64
+ return 'ng' if processing or defaultZip is None else 'ok'
65
+
66
+ @router.post('/zip')
67
+ async def zip(zip_data: TextRequest):
68
+ global defaultZip
69
+ if not defaultZip: defaultZip = BytesIO(base64.b64decode(zip_data.text))
70
+ return ''
71
 
72
  @router.post('/sb3')
73
  async def sb3(req_data: TextRequest):
74
+ global processing, defaultZip
75
  if processing: return ''
76
  processing = True
77
  data = json.loads(req_data.text)
78
+ with zipfile.ZipFile(defaultZip, 'r') as template_zip:
79
  with template_zip.open('project.json') as f:
80
  project = json.loads(f.read().decode('utf-8'))
81
 
public/mmp4.zip DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:5e79821c3e65e85abf1c6eb8a45cf9143d6844f15e2402f080cd06de018af428
3
- size 11143447
 
 
 
 
public/script.js CHANGED
@@ -160,25 +160,34 @@ const save = async () => {
160
  const load = async () => {
161
  const file = document.createElement(`input`);
162
  file.type = `file`;
163
- file.accept = `.mf4`;
164
  file.click();
165
  file.addEventListener(`change`, async () => {
166
  const fileReader = new FileReader();
167
  fileReader.addEventListener(`load`, async () => {
168
  const base64Data = btoa(String.fromCharCode.apply(null, new Uint8Array(fileReader.result)));
169
- fetch(`/api/load`, {
170
- method: `post`,
171
- headers: { 'Content-Type': 'application/json' },
172
- body: JSON.stringify({ text: base64Data })
173
- })
174
- .then(res => res.json())
175
- .then(data => {
176
- const dom = (new DOMParser()).parseFromString(data[0], `application/xml`);
177
- workspace.clear();
178
- Blockly.Xml.domToWorkspace(dom.documentElement, workspace);
179
- costumes = data.slice(2, data.length);
180
- reloadCos();
 
 
 
 
 
 
 
181
  });
 
 
182
  });
183
  fileReader.readAsArrayBuffer(file.files[0]);
184
  });
 
160
  const load = async () => {
161
  const file = document.createElement(`input`);
162
  file.type = `file`;
163
+ file.accept = `.mf4,.zip`;
164
  file.click();
165
  file.addEventListener(`change`, async () => {
166
  const fileReader = new FileReader();
167
  fileReader.addEventListener(`load`, async () => {
168
  const base64Data = btoa(String.fromCharCode.apply(null, new Uint8Array(fileReader.result)));
169
+ if (file.files[0].name.endsWith(`.mf4`)) {
170
+ fetch(`/api/load`, {
171
+ method: `post`,
172
+ headers: { 'Content-Type': 'application/json' },
173
+ body: JSON.stringify({ text: base64Data })
174
+ })
175
+ .then(res => res.json())
176
+ .then(data => {
177
+ const dom = (new DOMParser()).parseFromString(data[0], `application/xml`);
178
+ workspace.clear();
179
+ Blockly.Xml.domToWorkspace(dom.documentElement, workspace);
180
+ costumes = data.slice(2, data.length);
181
+ reloadCos();
182
+ });
183
+ } else if (file.files[0].name.endsWith(`.zip`)) {
184
+ await fetch(`/api/zip`, {
185
+ method: `post`,
186
+ headers: { 'Content-Type': 'application/json' },
187
+ body: JSON.stringify({ text: base64Data })
188
  });
189
+ alert(`zip ファイルをセットしました`);
190
+ }
191
  });
192
  fileReader.readAsArrayBuffer(file.files[0]);
193
  });