You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import List, Dict, Any, Type
|
|
|
|
from sqlalchemy import Column, Integer, String, DateTime, func, text
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from website.db_mysql import get_session, to_json_list, to_json, dict_to_obj
|
|
|
|
|
|
"""
|
|
CREATE TABLE `files` (
|
|
`id` int NOT NULL AUTO_INCREMENT,
|
|
`filename` varchar(255) NOT NULL DEFAULT '',
|
|
`filepath` varchar(255) NOT NULL DEFAULT '',
|
|
`md5_str` varchar(32) NOT NULL DEFAULT '',
|
|
`filesize` int DEFAULT NULL,
|
|
`filetype` varchar(50) DEFAULT '',
|
|
`user` int DEFAULT '0',
|
|
`create_time` datetime DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (`id`)
|
|
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='文件表';
|
|
"""
|
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
class File(Base):
|
|
__tablename__ = 'files'
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
filename = Column(String(255), nullable=False, default='')
|
|
filepath = Column(String(255), nullable=False, default='')
|
|
md5_str = Column(String(32), nullable=False, default='')
|
|
filesize = Column(Integer, nullable=True)
|
|
filetype = Column(String(50), nullable=True, default='')
|
|
user = Column(Integer, nullable=True, default=0)
|
|
create_time = Column(DateTime, nullable=False, default=func.now())
|
|
|
|
def __repr__(self):
|
|
return f"File(id={self.id}, filename='{self.filename}', filepath='{self.filepath}')"
|
|
|
|
|
|
class FileRepository(object):
|
|
|
|
def get_file_by_md5(self, md5_list: List[str]) -> List[File]:
|
|
resp = []
|
|
with get_session() as session:
|
|
# return (
|
|
# session.query(File).filter(File.md5_str == md5_str).first()
|
|
# )
|
|
files = session.query(File).filter(File.md5_str.in_(md5_list)).all()
|
|
for file in files:
|
|
obj_dict = file.__dict__
|
|
del obj_dict['_sa_instance_state']
|
|
# print(obj_dict)
|
|
resp.append(dict_to_obj(obj_dict))
|
|
|
|
# return session.query(File).filter(File.md5_str.in_(md5_list)).all()
|
|
return resp
|
|
|
|
|