Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
d5a10300
Commit
d5a10300
authored
Jun 18, 2024
by
Timothy J. Baek
Browse files
refac: uploads delete
parent
9fa8633d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
21 deletions
+73
-21
backend/apps/webui/routers/files.py
backend/apps/webui/routers/files.py
+39
-19
src/lib/apis/files/index.ts
src/lib/apis/files/index.ts
+31
-0
src/lib/components/admin/Settings/Documents.svelte
src/lib/components/admin/Settings/Documents.svelte
+3
-2
No files found.
backend/apps/webui/routers/files.py
View file @
d5a10300
...
@@ -32,11 +32,11 @@ from constants import ERROR_MESSAGES
...
@@ -32,11 +32,11 @@ from constants import ERROR_MESSAGES
from
importlib
import
util
from
importlib
import
util
import
os
import
os
import
uuid
import
uuid
import
os
,
shutil
,
logging
,
re
from
config
import
SRC_LOG_LEVELS
,
UPLOAD_DIR
from
config
import
SRC_LOG_LEVELS
,
UPLOAD_DIR
import
logging
log
=
logging
.
getLogger
(
__name__
)
log
=
logging
.
getLogger
(
__name__
)
log
.
setLevel
(
SRC_LOG_LEVELS
[
"MODELS"
])
log
.
setLevel
(
SRC_LOG_LEVELS
[
"MODELS"
])
...
@@ -111,6 +111,43 @@ async def list_files(user=Depends(get_verified_user)):
...
@@ -111,6 +111,43 @@ async def list_files(user=Depends(get_verified_user)):
return
files
return
files
############################
# Delete All Files
############################
@
router
.
delete
(
"/all"
)
async
def
delete_all_files
(
user
=
Depends
(
get_admin_user
)):
result
=
Files
.
delete_all_files
()
if
result
:
folder
=
f
"
{
UPLOAD_DIR
}
"
try
:
# Check if the directory exists
if
os
.
path
.
exists
(
folder
):
# Iterate over all the files and directories in the specified directory
for
filename
in
os
.
listdir
(
folder
):
file_path
=
os
.
path
.
join
(
folder
,
filename
)
try
:
if
os
.
path
.
isfile
(
file_path
)
or
os
.
path
.
islink
(
file_path
):
os
.
unlink
(
file_path
)
# Remove the file or link
elif
os
.
path
.
isdir
(
file_path
):
shutil
.
rmtree
(
file_path
)
# Remove the directory
except
Exception
as
e
:
print
(
f
"Failed to delete
{
file_path
}
. Reason:
{
e
}
"
)
else
:
print
(
f
"The directory
{
folder
}
does not exist"
)
except
Exception
as
e
:
print
(
f
"Failed to process the directory
{
folder
}
. Reason:
{
e
}
"
)
return
{
"message"
:
"All files deleted successfully"
}
else
:
raise
HTTPException
(
status_code
=
status
.
HTTP_400_BAD_REQUEST
,
detail
=
ERROR_MESSAGES
.
DEFAULT
(
"Error deleting files"
),
)
############################
############################
# Get File By Id
# Get File By Id
############################
############################
...
@@ -181,20 +218,3 @@ async def delete_file_by_id(id: str, user=Depends(get_verified_user)):
...
@@ -181,20 +218,3 @@ async def delete_file_by_id(id: str, user=Depends(get_verified_user)):
status_code
=
status
.
HTTP_401_UNAUTHORIZED
,
status_code
=
status
.
HTTP_401_UNAUTHORIZED
,
detail
=
ERROR_MESSAGES
.
NOT_FOUND
,
detail
=
ERROR_MESSAGES
.
NOT_FOUND
,
)
)
############################
# Delete All Files
############################
@
router
.
delete
(
"/all"
)
async
def
delete_all_files
(
user
=
Depends
(
get_admin_user
)):
result
=
Files
.
delete_all_files
()
if
result
:
return
{
"message"
:
"All files deleted successfully"
}
else
:
raise
HTTPException
(
status_code
=
status
.
HTTP_400_BAD_REQUEST
,
detail
=
ERROR_MESSAGES
.
DEFAULT
(
"Error deleting files"
),
)
src/lib/apis/files/index.ts
View file @
d5a10300
...
@@ -150,3 +150,34 @@ export const deleteFileById = async (token: string, id: string) => {
...
@@ -150,3 +150,34 @@ export const deleteFileById = async (token: string, id: string) => {
return
res
;
return
res
;
};
};
export
const
deleteAllFiles
=
async
(
token
:
string
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/files/all`
,
{
method
:
'
DELETE
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
authorization
:
`Bearer
${
token
}
`
}
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
then
((
json
)
=>
{
return
json
;
})
.
catch
((
err
)
=>
{
error
=
err
.
detail
;
console
.
log
(
err
);
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
;
};
src/lib/components/admin/Settings/Documents.svelte
View file @
d5a10300
<script lang="ts">
<script lang="ts">
import { getDocs } from '$lib/apis/documents';
import { getDocs } from '$lib/apis/documents';
import { deleteAllFiles, deleteFileById } from '$lib/apis/files';
import {
import {
getQuerySettings,
getQuerySettings,
scanDocs,
scanDocs,
...
@@ -217,8 +218,8 @@
...
@@ -217,8 +218,8 @@
<ResetUploadDirConfirmDialog
<ResetUploadDirConfirmDialog
bind:show={showResetUploadDirConfirm}
bind:show={showResetUploadDirConfirm}
on:confirm={() => {
on:confirm={
async
() => {
const res =
resetUploadDir
(localStorage.token).catch((error) => {
const res =
await deleteAllFiles
(localStorage.token).catch((error) => {
toast.error(error);
toast.error(error);
return null;
return null;
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment