-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (90 loc) · 3.18 KB
/
main.py
File metadata and controls
100 lines (90 loc) · 3.18 KB
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
from fastapi import FastAPI,Body, HTTPException, Header, File, UploadFile, Response
from fastapi.responses import FileResponse
from datetime import time,datetime,timedelta
from pydantic import BaseModel
from uuid import UUID,uuid4
import qrcode_terminal
import redis
from typing import List
from configparser import ConfigParser
from pathlib import Path
import hashlib
config = ConfigParser()
config.read('setting.cfg')
app = FastAPI()
redis = redis.Redis(host=config.get("redis","host"),port=config.getint("redis","port"))
upload_path = Path(config.get("base","upload_path"))
def out_md5(src):
m = hashlib.md5()
m.update(src)
return m.hexdigest()
@app.post("/")
async def post_file( user_agent: str = Header(None), file: UploadFile = File(None)):
if "curl" in user_agent:
if file != None:
fuid = str(uuid4()).split("-")[0]
print(type(file.file))
file_content = await file.read()
with open(upload_path.joinpath(fuid), "wb") as f:
f.write(file_content)
md5_str = out_md5(file_content)
redis.set(fuid,file.filename)
furl = config.get('base','url') + fuid + "/info"
qr = qrcode_terminal.qr_terminal_str(furl)
return Response(
qr + "\n- - - - - -\n"
+ "File Name: " + file.filename + "\n"
+ "File Info URL: " + furl+ "\n"
+ "md5: " + md5_str + "\n"
+ "- - - - - -\n")
return {"msg":"ohh my god"}
else:
return {"are you ok"}
@app.get("/")
async def get_root( user_agent: str = Header(None)):
return {
"Hello": "World!",
"msg": {
"/help": "display this help",
"/version": "output version information",
"/debug": "echo some info",
"/docs": "Swagger Docs",
"/": "curl -F 'file=@demo.py' " + config.get('base','url')
}
}
@app.get("/debug")
def echo_ua(user_agent: str = Header("unknown")):
return {"User-Agent":user_agent}
@app.get("/version")
def echo_version():
return {"version":"0.1"}
@app.get('/{fuid}/info')
def get_info_by_uuid(fuid: str = None):
p = upload_path.joinpath(fuid)
if (not p.exists()) or (not redis.exists(fuid)):
raise HTTPException(404)
with open(upload_path.joinpath(fuid),"rb") as f:
md5_str = out_md5(f.read())
return {
'UID':fuid,
'File Name': redis.get(fuid).decode('utf-8'),
'File Size': str(int(p.stat().st_size)/1024) + " kb",
'MD5': md5_str,
'Download URL': config.get('base','url') + fuid + "/down"
}
@app.get('/{fuid}/down')
def download_file_by_uuid(fuid: str = None):
p = upload_path.joinpath(fuid)
print(p)
if (not p.exists()) or (not redis.exists(fuid)):
raise HTTPException(404)
file_name = redis.get(fuid)
return FileResponse(p.resolve(),media_type='text/plain', filename=file_name)
@app.get('/{fuid}/del')
def del_file_by_uuid(fuid: str = None):
p = upload_path.joinpath(fuid)
if (not p.exists()) or (not redis.exists(fuid)):
raise HTTPException(404)
p.unlink(missing_ok=True)
redis.delete(fuid)
return {"msg":"delete success"}