-
Notifications
You must be signed in to change notification settings - Fork 5
/
upload.py
279 lines (230 loc) · 8.45 KB
/
upload.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding: utf-8 -*-
import datetime
import hashlib
import os
import re
import sqlite3
from pathlib import Path
import requests
from flask import jsonify
from config import Config
from upload_oss import OSSClient
class Uploader:
def __init__(self):
config = Config()
self.ossClient = OSSClient()
self.folder_name = "upload"
self.folder = os.path.join("assets", self.folder_name)
self.domain = config.get("static_domain")
self.static_type = config.get("static_type")
self.max_file_size = config.get("static_max_file_size")
self.dbpath = os.path.join("files", "db", "upload.db")
self.init_db()
def init_db(self):
os.makedirs(os.path.dirname(self.dbpath), exist_ok=True)
conn = sqlite3.connect(self.dbpath)
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS uploads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ip TEXT NOT NULL,
upload_time TEXT NOT NULL,
user_agent TEXT NOT NULL,
file_size INTEGER NOT NULL,
file_extension TEXT NOT NULL,
original_filename TEXT NOT NULL,
filepath TEXT NOT NULL,
storage_directory TEXT NOT NULL,
file_hash TEXT NOT NULL
)
"""
)
# print("Database check done.")
conn.close()
def query_by_filename(self, original_filename):
conn = sqlite3.connect(self.dbpath)
cursor = conn.cursor()
cursor.execute(
"SELECT filepath FROM uploads WHERE original_filename=?",
(original_filename,),
)
result = cursor.fetchone()
filepath = result[0] if result else None
conn.close()
return filepath
def query_by_hash(self, file_hash):
conn = sqlite3.connect(self.dbpath)
cursor = conn.cursor()
cursor.execute("SELECT filepath FROM uploads WHERE file_hash=?", (file_hash,))
result = cursor.fetchone()
filepath = result[0] if result else None
conn.close()
return filepath
def fetch(self, request):
data = request.get_json()
if not data:
return jsonify({"error": "No input data provided"})
originalURL = data.get("url")
newURL = self.query_by_filename(originalURL)
if newURL:
return jsonify(
{
"code": 0,
"msg": "done",
"data": {"originalURL": originalURL, "url": newURL},
}
)
newURL = self.fetch_file(originalURL, request)
return jsonify(
{
"code": 0,
"msg": "done",
"data": {"originalURL": originalURL, "url": newURL},
}
)
def fetch_file(self, url, request):
ip = request.remote_addr
user_agent = request.user_agent.string
final_path = ""
try:
# 发送GET请求获取图片数据
response = requests.get(url)
response.raise_for_status()
content = response.content
# 从URL中提取图片文件名
filename = self.get_filename(Path(url).name)
year = datetime.datetime.now().strftime("%Y")
month = datetime.datetime.now().strftime("%m")
filepath = os.path.join(self.folder, year, month)
# 构造图片的本地保存路径
saveto = os.path.join(filepath, filename)
# 将图片数据写入本地文件
with open(saveto, "wb") as f:
f.write(content)
size = len(content)
ext = os.path.splitext(filename)[1]
orig_name = url
hash_value = self.get_hash(saveto)
final_path = f"{self.domain}/{self.folder_name}/{year}/{month}/{filename}"
if self.static_type == "oss":
final_path = self.ossClient.upload_file(saveto)
conn = sqlite3.connect(self.dbpath)
cursor = conn.cursor()
cursor.execute(
"INSERT INTO uploads (ip, upload_time, user_agent, file_size, "
"file_extension, original_filename, filepath, storage_directory, file_hash) "
"VALUES (?,?,?,?,?,?,?,?,?)",
(
ip,
datetime.datetime.now(),
user_agent,
size,
ext,
orig_name,
final_path,
filepath,
hash_value,
),
)
conn.commit()
conn.close()
except Exception as ex:
print(ex)
return final_path
def upload(self, request):
files = request.files
ip = request.remote_addr
user_agent = request.user_agent.string
succMap = {}
errFiles = []
conn = sqlite3.connect(self.dbpath)
cursor = conn.cursor()
for name, f in files.items():
try:
# 1. 检查文件名是否为空
if not name:
continue
# 2. 检查上传内容是否为空
# if not f.stream.read(1):
# errFiles.append(name)
# continue
# 新增:校验文件大小
if f.content_length > int(self.max_file_size):
errFiles.append(name)
continue
filename = self.get_filename(f.filename)
filepath = self.save_file(f, filename)
with open(filepath, "wb") as file:
file.write(f.read())
succMap[f.filename] = self.save_one_file(
f, ip, user_agent, cursor, filename, filepath
)
except Exception as e:
errFiles.append(f.filename)
conn.commit()
conn.close()
return {
"msg": "done",
"code": 0,
"data": {"errFiles": errFiles, "succMap": succMap},
}
def delete_file(self, path):
if os.path.exists(path):
if os.path.isfile(path):
os.remove(path)
else:
print(f"Path {path} does not exist")
def save_one_file(self, f, ip, user_agent, cursor, filename, filepath):
size = f.content_length
ext = os.path.splitext(f.filename)[1]
orig_name = f.filename
hash_value = self.get_hash(filepath)
final_path = self.query_by_hash(hash_value)
if final_path:
self.delete_file(filepath)
return final_path
year = datetime.datetime.now().strftime("%Y")
month = datetime.datetime.now().strftime("%m")
final_path = f"{self.domain}/{self.folder_name}/{year}/{month}/{filename}"
if self.static_type == "oss":
final_path = self.ossClient.upload_file(filepath)
cursor.execute(
"INSERT INTO uploads (ip, upload_time, user_agent, file_size, "
"file_extension, original_filename, filepath, storage_directory, file_hash) "
"VALUES (?,?,?,?,?,?,?,?,?)",
(
ip,
datetime.datetime.now(),
user_agent,
size,
ext,
orig_name,
final_path,
filepath,
hash_value,
),
)
return final_path
def get_filename(self, filename):
dt = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
name, ext = os.path.splitext(filename)
return f"{dt}{ext}"
def save_file(self, fileobj, filename):
year = datetime.datetime.now().strftime("%Y")
month = datetime.datetime.now().strftime("%m")
path = f"{self.folder}/{year}/{month}"
if not os.path.exists(path):
os.makedirs(path)
filepath = os.path.join(path, filename)
filepath = re.sub(r"\\", "/", filepath)
# with codecs.open(filepath, "wb", encoding="utf-8") as file:
# file.write(fileobj.read())
# fileobj.save(filepath)
return filepath
def get_hash(self, filepath):
hash_md5 = hashlib.md5()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()