117 lines
3.2 KiB
Python
117 lines
3.2 KiB
Python
import os
|
|
from traceback import format_exc
|
|
from ahserver.serverenv import get_serverenv
|
|
from ahserver.auth_api import get_session_userinfo
|
|
from sqlor.dbpools import DBPools
|
|
from appPublic.uniqueID import getID
|
|
from appPublic.log import debug, error, exception
|
|
from ahserver.filestorage import FileStorage
|
|
import hashlib
|
|
|
|
def get_file_hash(filepath, algo='sha256', chunk_size=8192):
|
|
h = hashlib.new(algo)
|
|
with open(filepath, 'rb') as f:
|
|
while chunk := f.read(chunk_size):
|
|
h.update(chunk)
|
|
return h.hexdigest()
|
|
|
|
def get_dbname():
|
|
f = get_serverenv('get_module_dbname')
|
|
dbname = f('filemgr')
|
|
return dbname
|
|
|
|
class FileMgr:
|
|
async def add_file(request, params_kw):
|
|
fs = FileStorage()
|
|
webpath = params_kw.upfile
|
|
realpath = fs.realpath(wwebpath)
|
|
filesize = os.path.getsize(realpath)
|
|
hashvalue = get_file_hash(realpath)
|
|
dbname = get_dbname()
|
|
u = await get_session_userinfo(request)
|
|
db = DBPools()
|
|
async with db.sqlorContext(dbname) as sor:
|
|
recs = await sor.R('file',{'hashvalue': hashvalue})
|
|
if len(recs) > 0:
|
|
os.unlink(realpath)
|
|
r = recs[0]
|
|
webpath = r.webpath
|
|
realpath = r.realpath
|
|
ns = {
|
|
"id": getID(),
|
|
"folderid": params_kw.fid,
|
|
"fiid":params_kw.fiid,
|
|
"name":os.path.basename(params_kw.upfile),
|
|
"webpath": webpath,
|
|
"realpath": realpath,
|
|
"filetpye":params_kw.upfile.split('.')[-1].lower(),
|
|
"filesize":filesize,
|
|
"owner": u.userorgid,
|
|
"hashvalue": hashvalue
|
|
}
|
|
await sor.C('file', ns)
|
|
|
|
async def del_file(request, fid):
|
|
db = DBPools()
|
|
dbname = get_dbname()
|
|
async with db.sqlorContext(dbname) as sor:
|
|
recs = await sor.R('file', {'id': fid})
|
|
if recs:
|
|
delrec = recs[0]
|
|
await sor.D('file', {'id':fid})
|
|
remain = sor.R('file', {'hashvalue':delrec.hashvalue})
|
|
if not remain:
|
|
os.unlink(delrec.realpath)
|
|
|
|
async def has_sub(request, sor, folderid):
|
|
sql = """select unique a.* from folder a
|
|
left join folder b on a.id = b.parentid
|
|
left join file c on a.id=c.folderid
|
|
where id=${folderid}$"""
|
|
recs = await sor.sqlExe(sql, {'folderid':folderid})
|
|
if recs:
|
|
return True
|
|
return False
|
|
|
|
async def get_subfolder(self, request, fid, fiid):
|
|
userinfo = await get_session_userinfo(request)
|
|
dbname = get_dbname()
|
|
db = DBPools()
|
|
async with db.sqlorContext(dbname) as sor:
|
|
return await self.sor_get_subfolder(sor, request, fid, fiid)
|
|
return await self.sor_get_subfile(sor, request, fid, fiid)
|
|
return []
|
|
|
|
async def sor_get_subfolder(self, sor, request, fid, fiid):
|
|
sql = """select x.*, 'folder' as filetype,
|
|
case when y.id is null then 1
|
|
else 0 end as is_left
|
|
from folder as x left join (
|
|
select unique a.* from folder a
|
|
left join folder b on a.id = b.parentid
|
|
left join file c on a.id=c.folderid
|
|
where b.id is not null or c.id is n
|
|
ot null;
|
|
) as y
|
|
on x.id = y.id
|
|
where x.parentid = ${fid}$ and fiid=${fiid}$
|
|
"""
|
|
ns = {
|
|
'fid':fid,
|
|
'fiid':fiid
|
|
}
|
|
recs = await sor.sqlExe(sql, ns)
|
|
return recs
|
|
|
|
async def sor_get_subfile(self, sor, request, fid, fiid):
|
|
userinfo = await get_session_userinfo(request)
|
|
sql = """select a.*, 1 as is_leaf from file where folderid=${fid}$ and fiid=${fiid}"""
|
|
ns = {
|
|
'fid':fid,
|
|
'fiid':fiid
|
|
}
|
|
recs = await sor.sqlExe(sql, ns)
|
|
return recs
|
|
|
|
|