1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# -*- coding: utf-8 -*-
from flask import Flask, redirect,request,render_template,session
import os
import hashlib
import sqlite3
import shlex
import sys
# 设置默认编码为UTF-8
import io
import codecs
# Monkey patch Flask's safe_join to handle unicode paths
import flask.helpers
original_safe_join = flask.helpers.safe_join
def safe_join_utf8(directory, filename):
if isinstance(directory, str):
directory = directory.decode('utf-8', errors='replace')
if isinstance(filename, str):
filename = filename.decode('utf-8', errors='replace')
return original_safe_join(directory, filename)
flask.helpers.safe_join = safe_join_utf8
# Ensure all paths are properly encoded
template_folder = 'templates'
static_folder = 'static'
static_url_path = '/static'
app = Flask(__name__, template_folder=template_folder, static_folder=static_folder, static_url_path=static_url_path)
app.secret_key='**************************'
# Configure Flask to handle non-ASCII characters
app.config['JSON_AS_ASCII'] = False
@app.route('/',methods=['GET'])
def index():
conn = sqlite3.connect('imgDB.db')
c = conn.cursor()
c.execute('select img_url,img_name from img')
img_list = c.fetchall()
return render_template('index.html',img_list=img_list)
@app.route('/upload',methods=['GET','POST'])
def upload():
if request.method == 'GET':
# 初次访问不传入提示信息,避免不必要的Unicode比较
return render_template('upload.html')
else:
try:
conn = sqlite3.connect('imgDB.db')
cur = conn.cursor()
file = request.files['file']
img_name = file.filename
suffix = img_name.split('.')[-1]
save_file_name = hashlib.md5(img_name.encode('utf-8')).hexdigest() + '.' + suffix
img_url = 'static/img/{}'.format(save_file_name)
file.save(img_url)
# Use subprocess instead of os.popen for better encoding handling
import subprocess
try:
img_info = subprocess.check_output(['file', '-b', img_url], stderr=subprocess.STDOUT)
img_info = img_info.decode('utf-8', errors='replace')
except Exception as e:
img_info = "Error getting file info: {}".format(str(e))
# Use parameterized query for all values including img_info
cur.execute('insert into img(img_name,img_url,img_info) values(?,?,?)',(img_name,img_url,img_info))
conn.commit()
cur.close()
except Exception as e:
print(str(e))
return render_template('upload.html', msg=u'上传失败', success=False)
return render_template('upload.html', msg=u'上传成功', success=True)
@app.route('/login',methods=['GET','POST'])
def login():
if request.method == 'GET':
# 初次访问不传入提示信息,避免不必要的Unicode比较
return render_template('login.html')
else:
username = request.form['username']
password = request.form['password']
conn = sqlite3.connect('imgDB.db')
cur = conn.cursor()
cur.execute('select * from user where username=? and password=?',(username,password))
result = cur.fetchone()
if result:
session['username'] = username
return redirect("/admin")
else:
# 错误提示统一为Unicode并传递success布尔值
return render_template('login.html', msg=u'用户名或密码错误', success=False)
@app.route('/admin',methods=['GET'])
def admin():
if 'username' in session and session['username'] == 'admin':
conn = sqlite3.connect('imgDB.db')
c = conn.cursor()
c.execute('select img_name,img_url,img_info from img')
items = c.fetchall()
# 传递user对象到模板,避免Jinja2中'user'未定义错误
user = {'username': session['username']}
return render_template('admin.html', items=items, user=user)
else:
return redirect('/login')
@app.route('/showExif',methods=['GET'])
def showExif():
if 'username' in session and session['username'] == 'admin':
img_url = request.args.get('img_url')
# Use subprocess instead of os.popen for better encoding handling
import subprocess
try:
img_info = subprocess.check_output(['tools/exiftool/exiftool', img_url], stderr=subprocess.STDOUT)
return img_info.decode('utf-8', errors='replace')
except Exception as e:
return "Error processing image: {}".format(str(e))
else:
return redirect('/login')
@app.route('/showInfo',methods=['GET'])
def showInfo():
img_name = request.args.get('img_name')
conn = sqlite3.connect('imgDB.db')
c = conn.cursor()
c.execute('select img_info from img where img_name=\''+img_name+"\'")
img_info = c.fetchone()
if img_info and img_info[0]:
# Handle potential encoding issues
try:
return img_info[0]
except UnicodeDecodeError:
# Try to decode with utf-8
return img_info[0].decode('utf-8', errors='replace')
return ''
if __name__ == '__main__':
app.run(host='0.0.0.0',port=80,debug=True)
|