Compare commits

...

2 commits

Author SHA1 Message Date
Izalia Mae 828dc59387 a 2022-04-23 12:08:52 -04:00
Izalia Mae 84f7007541 allow localhost and fix save id on load 2022-04-23 09:42:07 -04:00
3 changed files with 41 additions and 9 deletions

View file

@ -1,7 +1,7 @@
import sys
from bl2_save_edit import Bl2Save
from izzylib import Path, Url
from izzylib import Path, Url, logging
from izzylib_http_async.server.application import Application
from izzylib_http_async.server.error import Forbidden
@ -46,12 +46,24 @@ class Server(Application):
self.read_saves()
def get_save_by_id(self, saveid):
def get_save(self, key, value):
for save in self.saves:
if save.id == saveid:
if save[key] == value:
return save
raise KeyError(f'Cannot find save with id: {saveid}')
raise KeyError(f'Cannot find save: {key} = {value}')
def get_save_by_id(self, saveid):
return self.get_save('id', saveid)
def get_save_by_filename(self, filename):
for save in self.saves:
if save.path.name == filename:
return save
raise KeyError(f'Cannot find save with filename: {filename}')
def read_saves(self):
@ -61,6 +73,16 @@ class Server(Application):
save = Bl2Save(path)
try:
save_id = int(save.path.stem[4:], 16)
if save_id != save.id:
logging.warning('Save ID does not match savefile:', repr(save))
save.id = save_id
except ValueError:
pass
if int(save.path.permissions()) not in [644, 444]:
save.path.chmod(644)
@ -68,7 +90,7 @@ class Server(Application):
async def check_address_type(request):
if not Url.new(request.remote).is_address_type('private'):
if not any([request.remote.private, request.remote.loopback]):
raise Forbidden('Only private access allowed')
if request.path.startswith('/api') or request.path.endswith(('js', 'css')):

View file

@ -57,8 +57,8 @@ class ApiList(BaseView):
__path__ = ['/api/list']
async def get(self, request, response):
save_list = [save.id for save in self.app.saves]
response.set_json(save_list)
save_list = {save.id for save in self.app.saves}
response.set_json(list(save_list))
class ApiSave(BaseView):

View file

@ -57,6 +57,7 @@ class SaveHandler(FileSystemEventHandler):
super().__init__()
self.server = server
self.modified_times = dict()
def on_any_event(self, event):
@ -65,12 +66,21 @@ class SaveHandler(FileSystemEventHandler):
return
path = Path(event.src_path)
save = self.server.get_save_by_filename(name)
if not path.suffix == 'sav':
return
self.handle_save_change(path.name, event.event_type)
self.handle_save_change(save, event.event_type)
def handle_save_change(self, name, event):
def handle_save_change(self, save, event):
if event == 'modified':
try:
if self.modified_times[save.path.name] == save.last_save:
return
except KeyError:
self.modified_times[save.path.name] = save.last_save
print(name, event)