Skip to content

Commit e525090

Browse files
committed
fix unexpectable usp file name problem
1 parent 836763f commit e525090

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

SatsDecoder/systems/image_receiver.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def open(self):
4141
self.fn.unlink()
4242
except IsADirectoryError:
4343
self.fn.rmdir()
44-
except FileNotFoundError:
44+
except (FileNotFoundError, PermissionError):
4545
pass
4646
self.f = open(self.fn, mode)
4747

@@ -183,8 +183,18 @@ def push_data(self, data, t=None, **kw):
183183
def clear(self):
184184
for i in self.images.values():
185185
i.close()
186+
self.images = {}
186187
self.current_fid = ''
187188

189+
def close(self, fid=None):
190+
if not fid:
191+
fid = self.current_fid
192+
self.current_fid = ''
193+
if fid:
194+
f = self.images.pop(fid, None)
195+
if f:
196+
f.close()
197+
188198
def strftime(self, t=None):
189199
self.last_date = t or dt.datetime.now(dt.timezone.utc)
190200
return self.last_date.strftime(self.time_fmt)

SatsDecoder/systems/usp.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,8 @@
14771477

14781478

14791479
class UspImageReceiver(ImageReceiver):
1480+
default_pfx = 'USP_unknown'
1481+
14801482
def __init__(self, outdir):
14811483
super().__init__(outdir, '.jpg')
14821484
self.last_fname = ''
@@ -1487,14 +1489,16 @@ def re_fname(self, fname, t):
14871489
t = self.last_date
14881490
self.last_fname = fname
14891491
x = pathlib.Path(fname)
1490-
return str(x.with_name(x.stem + '_' + self.strftime(t) + x.suffix))
1491-
return fname
1492+
return x.stem + '_' + self.strftime(t) + x.suffix
1493+
elif fname:
1494+
return str(pathlib.Path(fname).name)
1495+
return fname or f'{self.default_pfx}_{self.strftime(t)}'
14921496

14931497
def generate_fid(self, fname='', force=0, t=None):
1494-
if self.current_fid.startswith('unknown_') and fname:
1498+
if self.current_fid.startswith(self.default_pfx) and fname:
14951499
self.rename_image(self.current_fid, self.re_fname(fname, t))
14961500
elif force or not (self.current_fid and self.merge_mode):
1497-
self.current_fid = self.re_fname(fname, t) or f'USP_unknown_{self.strftime(t)}'
1501+
self.current_fid = self.re_fname(fname, t)
14981502
return self.current_fid
14991503

15001504
def push_data(self, data, t=None, **kw):
@@ -1511,7 +1515,12 @@ def push_data(self, data, t=None, **kw):
15111515

15121516
elif data.message == FILETRANSFER_INIT:
15131517
self.generate_fid(packet.file_name.partition('\0')[0], 1, t)
1514-
img = self.get_image(t=t)
1518+
try:
1519+
img = self.get_image(t=t)
1520+
except (FileNotFoundError, PermissionError):
1521+
self.close()
1522+
img = self.get_image(t=t)
1523+
15151524
with img.lock:
15161525
img.has_starter = 1
15171526

0 commit comments

Comments
 (0)