server-rx : PDF receiver, printing via Linux lp-tool
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
import json
|
||||
|
||||
from flask import Flask, request
|
||||
|
||||
app = Flask(__name__)
|
||||
SECRET = None
|
||||
|
||||
with open("server-rx.json", 'r', encoding = "utf-8") as file:
|
||||
server_data = json.load(file)
|
||||
SECRET = server_data['secret']
|
||||
|
||||
def go_to_print(file_path, samples):
|
||||
os.system(f"lp -n {samples} {file_path}")
|
||||
|
||||
@app.route('/printfile', methods=['POST'])
|
||||
def print_file():
|
||||
if 'file' not in request.files:
|
||||
return 'Файл не был предоставлен.', 400
|
||||
if 'secret-key' not in request.form:
|
||||
return 'Секретный ключ не был предоставлен.', 400
|
||||
if request.form['secret-key'] != SECRET:
|
||||
return 'Авторизация не удалась.', 400
|
||||
file = request.files['file']
|
||||
filename = "files/" + file.filename
|
||||
with open(filename, 'wb') as f:
|
||||
f.write(file.read())
|
||||
samples = 1
|
||||
if b'samples' in request.data:
|
||||
samples = request.data[b'samples']
|
||||
try:
|
||||
go_to_print(filename, samples)
|
||||
return 'Файл отправлен на печать.', 200
|
||||
except Exception as e:
|
||||
return f'Ошибка при печати файла: {str(e)}.', 500
|
||||
finally:
|
||||
return 'OK', 200
|
||||
Reference in New Issue
Block a user