mirror of
https://github.com/langgenius/dify.git
synced 2026-01-13 21:57:48 +08:00
Fixes session scope bug in FileService.delete_file (#27911)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
parent
26a1c84881
commit
665d49d375
@ -112,7 +112,7 @@ class Storage:
|
|||||||
def exists(self, filename):
|
def exists(self, filename):
|
||||||
return self.storage_runner.exists(filename)
|
return self.storage_runner.exists(filename)
|
||||||
|
|
||||||
def delete(self, filename):
|
def delete(self, filename: str):
|
||||||
return self.storage_runner.delete(filename)
|
return self.storage_runner.delete(filename)
|
||||||
|
|
||||||
def scan(self, path: str, files: bool = True, directories: bool = False) -> list[str]:
|
def scan(self, path: str, files: bool = True, directories: bool = False) -> list[str]:
|
||||||
|
|||||||
@ -3,8 +3,8 @@ import os
|
|||||||
import uuid
|
import uuid
|
||||||
from typing import Literal, Union
|
from typing import Literal, Union
|
||||||
|
|
||||||
from sqlalchemy import Engine
|
from sqlalchemy import Engine, select
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import Session, sessionmaker
|
||||||
from werkzeug.exceptions import NotFound
|
from werkzeug.exceptions import NotFound
|
||||||
|
|
||||||
from configs import dify_config
|
from configs import dify_config
|
||||||
@ -29,7 +29,7 @@ PREVIEW_WORDS_LIMIT = 3000
|
|||||||
|
|
||||||
|
|
||||||
class FileService:
|
class FileService:
|
||||||
_session_maker: sessionmaker
|
_session_maker: sessionmaker[Session]
|
||||||
|
|
||||||
def __init__(self, session_factory: sessionmaker | Engine | None = None):
|
def __init__(self, session_factory: sessionmaker | Engine | None = None):
|
||||||
if isinstance(session_factory, Engine):
|
if isinstance(session_factory, Engine):
|
||||||
@ -236,11 +236,10 @@ class FileService:
|
|||||||
return content.decode("utf-8")
|
return content.decode("utf-8")
|
||||||
|
|
||||||
def delete_file(self, file_id: str):
|
def delete_file(self, file_id: str):
|
||||||
with self._session_maker(expire_on_commit=False) as session:
|
with self._session_maker() as session, session.begin():
|
||||||
upload_file: UploadFile | None = session.query(UploadFile).where(UploadFile.id == file_id).first()
|
upload_file = session.scalar(select(UploadFile).where(UploadFile.id == file_id))
|
||||||
|
|
||||||
if not upload_file:
|
if not upload_file:
|
||||||
return
|
return
|
||||||
storage.delete(upload_file.key)
|
storage.delete(upload_file.key)
|
||||||
session.delete(upload_file)
|
session.delete(upload_file)
|
||||||
session.commit()
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user