Update main.py
Browse files
main.py
CHANGED
@@ -8,8 +8,9 @@ from generate_image import generate_image
|
|
8 |
from fastapi.staticfiles import StaticFiles
|
9 |
|
10 |
app = FastAPI()
|
|
|
11 |
# 挂载 tmp 文件夹下的静态文件
|
12 |
-
app.mount("/static", StaticFiles(directory="
|
13 |
# 添加跨域支持
|
14 |
origins = [
|
15 |
"*"
|
@@ -31,7 +32,7 @@ class Markdown(BaseModel):
|
|
31 |
@app.post("/generate_image")
|
32 |
async def generate_image_endpoint(markdown: Markdown):
|
33 |
# 生成随机文件名
|
34 |
-
image_filename = '
|
35 |
# 调用 generate_image 函数来生成图片
|
36 |
generate_image(markdown.markdown, image_filename)
|
37 |
# 返回图片的路径
|
@@ -41,8 +42,8 @@ async def generate_image_endpoint(markdown: Markdown):
|
|
41 |
def clean_expired_images():
|
42 |
expiration_time = int(os.environ.get("EXPIRATION_TIME", "3600")) # 默认过期时间为1小时
|
43 |
current_time = time.time()
|
44 |
-
for filename in os.listdir("
|
45 |
-
file_path = os.path.join("
|
46 |
if current_time - os.path.getmtime(file_path) > expiration_time:
|
47 |
os.remove(file_path)
|
48 |
|
|
|
8 |
from fastapi.staticfiles import StaticFiles
|
9 |
|
10 |
app = FastAPI()
|
11 |
+
os.mkdir("tmp")
|
12 |
# 挂载 tmp 文件夹下的静态文件
|
13 |
+
app.mount("/static", StaticFiles(directory="tmp"), name="static")
|
14 |
# 添加跨域支持
|
15 |
origins = [
|
16 |
"*"
|
|
|
32 |
@app.post("/generate_image")
|
33 |
async def generate_image_endpoint(markdown: Markdown):
|
34 |
# 生成随机文件名
|
35 |
+
image_filename = 'tmp/'+str(uuid.uuid4()) + ".png"
|
36 |
# 调用 generate_image 函数来生成图片
|
37 |
generate_image(markdown.markdown, image_filename)
|
38 |
# 返回图片的路径
|
|
|
42 |
def clean_expired_images():
|
43 |
expiration_time = int(os.environ.get("EXPIRATION_TIME", "3600")) # 默认过期时间为1小时
|
44 |
current_time = time.time()
|
45 |
+
for filename in os.listdir("tmp"):
|
46 |
+
file_path = os.path.join("tmp", filename)
|
47 |
if current_time - os.path.getmtime(file_path) > expiration_time:
|
48 |
os.remove(file_path)
|
49 |
|