export hpgl load from folder
This commit is contained in:
59
webui/app.py
59
webui/app.py
@@ -29,6 +29,7 @@ app.secret_key = webui_secret_key()
|
||||
app.config['MAX_CONTENT_LENGTH'] = webui_max_upload_mb() * 1024 * 1024
|
||||
UPLOAD_DIR = os.path.join(ROOT, 'webui', 'uploads')
|
||||
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
||||
OUTPUT_HPGL_DIR = os.path.join(ROOT, 'output', 'hpgl')
|
||||
FONT_DIRS = [
|
||||
os.path.join(ROOT, 'font'),
|
||||
os.path.join(ROOT, '.git', 'font'),
|
||||
@@ -119,6 +120,45 @@ def api_text_to_hpgl():
|
||||
return jsonify({'ok': True, 'filename': session['upload_filename'], 'preview_url': '/api/svg'})
|
||||
|
||||
|
||||
@app.route('/api/hpgl_files')
|
||||
def list_hpgl_files():
|
||||
"""List HPGL filenames in output/hpgl/."""
|
||||
if not os.path.isdir(OUTPUT_HPGL_DIR):
|
||||
return jsonify({'files': []})
|
||||
files = []
|
||||
for name in sorted(os.listdir(OUTPUT_HPGL_DIR)):
|
||||
if name.startswith('.'):
|
||||
continue
|
||||
path = os.path.join(OUTPUT_HPGL_DIR, name)
|
||||
if not os.path.isfile(path):
|
||||
continue
|
||||
low = name.lower()
|
||||
if low.endswith('.hpgl') or low.endswith('.plt') or '.' not in name:
|
||||
files.append(name)
|
||||
return jsonify({'files': files})
|
||||
|
||||
|
||||
@app.route('/api/load_hpgl', methods=['POST'])
|
||||
def load_hpgl_from_folder():
|
||||
"""Set current program from a file in output/hpgl/."""
|
||||
data = request.get_json() or {}
|
||||
filename = (data.get('filename') or '').strip()
|
||||
if not filename:
|
||||
return jsonify({'error': 'No filename'}), 400
|
||||
if os.path.basename(filename) != filename or '..' in filename:
|
||||
return jsonify({'error': 'Invalid filename'}), 400
|
||||
path = os.path.join(OUTPUT_HPGL_DIR, filename)
|
||||
if not os.path.isfile(path):
|
||||
return jsonify({'error': 'File not found'}), 404
|
||||
try:
|
||||
Program.parsefile(path)
|
||||
except Exception as e:
|
||||
return jsonify({'error': 'Invalid HPGL: {}'.format(str(e))}), 400
|
||||
session['upload_path'] = path
|
||||
session['upload_filename'] = filename
|
||||
return jsonify({'ok': True, 'filename': filename, 'preview_url': '/api/svg'})
|
||||
|
||||
|
||||
@app.route('/api/upload', methods=['POST'])
|
||||
def upload():
|
||||
if 'file' not in request.files:
|
||||
@@ -144,6 +184,25 @@ def upload():
|
||||
})
|
||||
|
||||
|
||||
@app.route('/api/download_hpgl')
|
||||
def download_hpgl():
|
||||
"""Return current program as downloadable HPGL file."""
|
||||
p = _get_program()
|
||||
if p is None:
|
||||
return jsonify({'error': 'No file loaded'}), 404
|
||||
filename = (session.get('upload_filename') or 'export').strip()
|
||||
if not filename.lower().endswith('.hpgl') and not filename.lower().endswith('.plt'):
|
||||
filename = filename + '.hpgl'
|
||||
return (
|
||||
str(p),
|
||||
200,
|
||||
{
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'Content-Disposition': 'attachment; filename="{}"'.format(filename),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.route('/api/dimensions')
|
||||
def dimensions():
|
||||
"""Return current program size in plotter units (e.g. 0.025 mm per unit)."""
|
||||
|
||||
Reference in New Issue
Block a user