diff --git a/app/svganim/strokes.py b/app/svganim/strokes.py index e624853..0d8dcba 100644 --- a/app/svganim/strokes.py +++ b/app/svganim/strokes.py @@ -5,6 +5,8 @@ from ctypes.wintypes import tagMSG import json from os import X_OK, PathLike import os +import random +import string import subprocess from typing import Optional, Union import shelve @@ -202,6 +204,14 @@ class AnimationSlice: return self.id[0] return self.id[0] + f"{extension}?t_in={self.t_in}&t_out={self.t_out}" + + def getHash(self): + ''' + A repeatable way to create a relatively unique hash for the slice + Used e.g. to export the slice. + ''' + random.seed('-'.join([str(b) if b is not None else "" for b in self.id])) + return ''.join(random.choices(string.ascii_uppercase + string.digits, k=8)) def get_bounding_box(self, stroke_thickness: float = 3.5) -> Viewbox: """Stroke_thickness 3.5 == 1mm. If it should not be considered, just set it to 0. diff --git a/app/webserver.py b/app/webserver.py index 01c3043..ad9807b 100644 --- a/app/webserver.py +++ b/app/webserver.py @@ -263,24 +263,26 @@ class ExportHandler(tornado.web.RequestHandler): if t_in is not None and t_out is not None: animation = animation.getSlice(float(t_in), float(t_out)) + identifier = animation.getHash() + with tempfile.TemporaryDirectory() as tdir: - with ZipFile(tdir + '/annotation.zip', 'w') as archive: + with ZipFile(tdir + f'/annotation-{identifier}.zip', 'w') as archive: logger.info('write svg') svgstring = animation.get_as_svg() - archive.writestr('drawing.svg', svgstring) + archive.writestr(f'annotation-{identifier}.svg', svgstring) logger.info('write png') - archive.writestr('drawing.png', cairosvg.svg2png(bytestring=svgstring)) + archive.writestr(f'annotation-{identifier}.png', cairosvg.svg2png(bytestring=svgstring)) logger.info('write mp3') audio = await animation.audio.export(format="mp3") - archive.writestr('drawing.mp3', audio.read()) + archive.writestr(f'annotation-{identifier}.mp3', audio.read()) logger.info('write json') data = animation.asDict() - data['audio']['file'] = 'drawing.mp3'; - archive.writestr('annotation.json', json.dumps(data)) + data['audio']['file'] = f'annotation-{identifier}.mp3'; + archive.writestr(f'annotation-{identifier}.json', json.dumps(data)) logger.info('write js') @@ -290,21 +292,22 @@ class ExportHandler(tornado.web.RequestHandler): archive.writestr('wNumb-1.2.0.min.js', fp.read()) logger.info('write html') - html = """ + html = f""" - + """ - archive.writestr('drawing.html', html) + archive.writestr(f'annotation-{identifier}.html', html) - with open(tdir + '/annotation.zip', 'rb') as fp: + with open(tdir + f'/annotation-{identifier}.zip', 'rb') as fp: self.set_header("Content-Type", "application/zip") + self.set_header("Content-Disposition", f"attachment;filename=annotation-{identifier}.zip") self.write(fp.read()) logger.info('done')