119 lines
3.6 KiB
Python
119 lines
3.6 KiB
Python
# This is a sample Python script.
|
|
|
|
# Press Shift+F10 to execute it or replace it with your code.
|
|
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
|
|
import json
|
|
|
|
from flask import Flask, request, send_from_directory
|
|
from flask_uploads import UploadSet, configure_uploads, IMAGES
|
|
from werkzeug.utils import secure_filename
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
import os
|
|
import datetime
|
|
import socket
|
|
import shutil
|
|
|
|
app = Flask(__name__)
|
|
app.config['UPLOADED_PHOTOS_DEST'] = 'static/uploads'
|
|
photos = UploadSet('photos', IMAGES)
|
|
configure_uploads(app, photos)
|
|
|
|
image_index = 0
|
|
|
|
paper_ip1 = '127.0.0.1'
|
|
paper_port1 = 9532
|
|
paper_ip2 = '127.0.0.1'
|
|
paper_port2 = 9533
|
|
|
|
|
|
def delete_files_in_directory(directory):
|
|
# 遍历目录下的所有文件和文件夹
|
|
for root, dirs, files in os.walk(directory):
|
|
# 删除文件
|
|
for file in files:
|
|
file_path = os.path.join(root, file)
|
|
os.remove(file_path)
|
|
print(f"已删除文件: {file_path}")
|
|
|
|
# 删除文件夹
|
|
for dir in dirs:
|
|
dir_path = os.path.join(root, dir)
|
|
shutil.rmtree(dir_path)
|
|
print(f"已删除文件夹: {dir_path}")
|
|
|
|
|
|
delete_files_in_directory(app.config['UPLOADED_PHOTOS_DEST'])
|
|
|
|
|
|
def broadcast_photo_ready(file, url):
|
|
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
data = {
|
|
'cmd': 'photo_ready',
|
|
'file': file,
|
|
'download': url
|
|
}
|
|
|
|
print('send ulr', url, ' new_filename:', file)
|
|
message = json.dumps(data)
|
|
udp_socket.sendto(message.encode('utf-8'), (paper_ip1, paper_port1))
|
|
udp_socket.sendto(message.encode('utf-8'), (paper_ip1, paper_port1))
|
|
udp_socket.close()
|
|
|
|
|
|
def clear_old_files():
|
|
today = datetime.datetime.now().date()
|
|
root_dir = app.config['UPLOADED_PHOTOS_DEST']
|
|
for subdir, dirs, files in os.walk(root_dir):
|
|
for file in files:
|
|
file_path = os.path.join(subdir, file)
|
|
file_date = datetime.datetime.fromtimestamp(os.path.getmtime(file_path)).date()
|
|
if (today - file_date).days > 1:
|
|
os.remove(file_path)
|
|
|
|
|
|
# 启动定时任务
|
|
scheduler = BackgroundScheduler()
|
|
scheduler.add_job(clear_old_files, 'interval', hours=8)
|
|
scheduler.start()
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return 'welcome to my webpage!'
|
|
|
|
|
|
@app.route('/upload', methods=['POST'])
|
|
def upload():
|
|
global image_index
|
|
if 'photo' in request.files:
|
|
file = request.files['photo']
|
|
if file.filename == '':
|
|
return 'No file selected'
|
|
|
|
if file:
|
|
filename = secure_filename(file.filename)
|
|
# 获取当前已上传的图片数量,用作新文件名
|
|
new_filename = filename + str(datetime.datetime.utcnow().timestamp()) + '.jpg'
|
|
file.save(os.path.join(app.config['UPLOADED_PHOTOS_DEST'], new_filename))
|
|
url = request.form.get('url')
|
|
# broadcast_photo_ready(new_filename, url)
|
|
print(f'File {new_filename} uploaded successfully')
|
|
return new_filename
|
|
|
|
return 'No file uploaded'
|
|
|
|
|
|
@app.route('/download/<filename>', methods=['GET'])
|
|
def download(filename):
|
|
print(filename)
|
|
return send_from_directory(app.config['UPLOADED_PHOTOS_DEST'], filename, as_attachment=True)
|
|
|
|
|
|
# Press the green button in the gutter to run the script.
|
|
if __name__ == '__main__':
|
|
delete_files_in_directory(app.config['UPLOADED_PHOTOS_DEST'])
|
|
app.run(host='0.0.0.0', debug=True)
|
|
|
|
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
|