def rd_pic(username, img_name, root, r): conn, cursor = pool.create_conn() sql = "select * from pic_route where pic_id = %s" img_name = img_name.split(".", 1)[0] print(type(img_name)) conn.commit() if not cursor.execute(sql, img_name): pool.close_conn(conn, cursor) return 10003 _, user_db, img_db = cursor.fetchone() if user_db != username: pool.close_conn(conn, cursor) return 10004 if r: img_stream = '' # 以图片流的形式返回给前端 with open(root + img_name + '.jpg', 'rb') as img_f: img_stream = img_f.read() # 到底用不用base64我还在考虑w # img_stream = base64.b64encode(img_stream) return img_stream else: t1 = Thread(target=delete_file, args=(str(img_name), root)) t2 = Thread(target=delete_path, args=(str(img_name), )) t1.start() t2.start() t1.join() t2.join() return 0
def delete_path(img_name): conn, cursor = pool.create_conn() sql = "delete from pic_route where pic_id = %s" if cursor.execute(sql, str(img_name)): print("yes") else: print("no") conn.commit() pool.close_conn(conn, cursor)
def save_pic(user_name, img, root): # 以时间戳命名,精确到0.001秒 img_name = str(round(time.time() * 1000)) img.save(os.path.join(root, img_name + ".jpg")) # f"{root}/{img_name}" conn, cursor = pool.create_conn() sql = "insert into pic_route values (NULL, %s, %s)" cursor.execute(sql, (user_name, img_name)) conn.commit() pool.close_conn(conn, cursor) return img_name
def recreate_user_account(): conn, cursor = pool.create_conn() # cursor.execute("DROP TABLE IF EXISTS USER_ACCOUNT") sql = """CREATE TABLE IF NOT EXISTS `USER_ACCOUNT`( `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT, `USERNAME` VARCHAR(200) NOT NULL, `PASSWORD` VARCHAR(200) NOT NULL, PRIMARY KEY ( `ID` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8;""" cursor.execute(sql) conn.commit() pool.close_conn(conn, cursor)
def recreate_client_token(): conn, cursor = pool.create_conn() # cursor.execute("DROP TABLE IF EXISTS PIC_ROUTE") sql = """CREATE TABLE IF NOT EXISTS `CLIENT_TOKEN`( `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT, `USERNAME` VARCHAR(200) NOT NULL, `TOKEN` VARCHAR(200) NOT NULL, PRIMARY KEY ( `ID` ) )ENGINE=InnoDB DEFAULT CHARSET=utf8;""" cursor.execute(sql) conn.commit() pool.close_conn(conn, cursor)
def register(username, password): conn, cursor = pool.create_conn() sql = "select * from user_account where username = %s" # 用户名已存在,已注册 if cursor.execute(sql, username): conn.commit() pool.close_conn(conn, cursor) return False sql = "insert into user_account values(null, %s, %s)" cursor.execute(sql, (username, password)) conn.commit() pool.close_conn(conn, cursor) return True
def login(username, password): conn, cursor = pool.create_conn() sql = "select * from user_account where username = %s" # 如果没有找到该用户名对应的用户信息 if not cursor.execute(sql, username): conn.commit() pool.close_conn(conn, cursor) return 10001 password_db = cursor.fetchone()[2] conn.commit() pool.close_conn(conn, cursor) # 输入密码错误 if password_db != password: return 10002 # 成功登陆 return 0