Skip to content

Commit 28a719f

Browse files
author
孙圣翔
committed
add missing dep, fix copy
1 parent 74c50f5 commit 28a719f

File tree

8 files changed

+73
-39
lines changed

8 files changed

+73
-39
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
dev:
22
npx nodemon -e "js html py" --exec "uvicorn servefs.main:app --port 7001"
33

4-
watch:
5-
npx nodemon -e "js html py" --exec "poetry run servefs -d ./files --port 7001"
4+
auth:
5+
npx nodemon -e "js html py" --exec "poetry run servefs -b foo:123 -d ./files --port 7001"
66

77
reload:
88
uvicorn servefs.main:app --port 7001 --reload

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ uvicorn = "^0.24.0"
3232
python-multipart = "^0.0.6"
3333
typer = "^0.9.0"
3434
rich = "^13.7.0"
35+
aiofiles = "^24.1.0"
3536

3637
[tool.poetry.group.dev.dependencies]
3738
pytest = "^7.4.3"

servefs/__main__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .cli import app
2+
3+
if __name__ == "__main__":
4+
app()

servefs/main.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import os
2-
import base64
32
from pathlib import Path
43

5-
from fastapi import FastAPI, Request, HTTPException
4+
from fastapi import FastAPI, HTTPException
65
from fastapi.responses import JSONResponse
7-
from starlette.middleware.base import BaseHTTPMiddleware
86

7+
from .middleware.auth import AuthManager, AuthMiddleware
98
from .routes.api import router as api_router
109
from .routes.page import init_static_files
1110
from .routes.page import router as page_router
12-
from .middleware.auth import AuthManager, AuthMiddleware
1311

1412
# Get debug mode from environment variable
1513
DEBUG = os.getenv("SERVEFS_DEBUG", "false").lower() == "true"

servefs/middleware/auth.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import base64
2-
from typing import Optional
2+
from typing import Optional, Tuple
33

44
from fastapi import Request
55
from starlette.middleware.base import (BaseHTTPMiddleware,
66
RequestResponseEndpoint)
7-
from starlette.responses import Response, JSONResponse
7+
from starlette.responses import JSONResponse, Response
88

99

1010
class Permission:
@@ -29,7 +29,7 @@ def configure(self, basic_auth: Optional[str] = None):
2929
else:
3030
self.auth_enabled = False
3131

32-
def check_auth(self, auth_header: Optional[str]) -> tuple[bool, str]:
32+
def check_auth(self, auth_header: Optional[str]) -> Tuple[bool, str]:
3333
"""检查认证信息
3434
返回: (是否认证成功, 权限级别)
3535
"""

servefs/routes/api.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import mimetypes
22
import shutil
3+
from typing import List
34

45
from fastapi import APIRouter, File, Form, HTTPException, Request, UploadFile
56
from fastapi.responses import JSONResponse
7+
68
from ..middleware.auth import Permission
79

810
router = APIRouter(prefix="/api", tags=["api"])
@@ -171,7 +173,7 @@ async def rename_file(file_path: str, request: Request):
171173
return {"error": str(e)}
172174

173175
@router.post("/upload/{path:path}")
174-
async def upload_files(path: str, files: list[UploadFile] = File(...), paths: list[str] = Form(...), request: Request = None):
176+
async def upload_files(path: str, files: List[UploadFile] = File(...), paths: List[str] = Form(...), request: Request = None):
175177
"""Upload files to the specified path"""
176178
try:
177179
target_path = request.app.state.ROOT_DIR / path

servefs/static/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<script src="https://unpkg.com/element-plus"></script>
1313
<!-- Import Element Plus Icons -->
1414
<script src="https://unpkg.com/@element-plus/icons-vue"></script>
15+
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
1516
<link rel="stylesheet" href="/static/css/style.css">
1617
</head>
1718
<body>

servefs/static/js/main.js

+57-29
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,34 @@ const app = createApp({
215215

216216
// 复制文件链接的方法
217217
const copyFileLink = async (item) => {
218-
try {
219-
const fullUrl = new URL(item.download_url, window.location.origin).href;
220-
await navigator.clipboard.writeText(fullUrl);
221-
// 更新 tooltip 文本
218+
const fullUrl = new URL(item.download_url, window.location.origin).href;
219+
// 创建一个临时按钮用于复制
220+
const button = document.createElement('button');
221+
button.setAttribute('data-clipboard-text', fullUrl);
222+
document.body.appendChild(button);
223+
224+
// 初始化 clipboard.js
225+
const clipboard = new ClipboardJS(button);
226+
227+
clipboard.on('success', () => {
222228
item.tooltipText = 'Copied';
223-
} catch (error) {
229+
ElMessage.success('Link copied to clipboard');
230+
cleanup();
231+
});
232+
233+
clipboard.on('error', () => {
224234
item.tooltipText = 'Failed to copy';
235+
ElMessage.error('Failed to copy to clipboard');
236+
cleanup();
237+
});
238+
239+
// 触发复制
240+
button.click();
241+
242+
// 清理函数
243+
function cleanup() {
244+
clipboard.destroy();
245+
document.body.removeChild(button);
225246
}
226247
};
227248

@@ -404,6 +425,12 @@ const app = createApp({
404425
}
405426
});
406427

428+
// 处理对话框关闭
429+
const handleClose = (done) => {
430+
previewDialog.value.visible = false;
431+
done();
432+
};
433+
407434
// 图片导航相关
408435
const imageList = ref([]);
409436
const hasPrevImage = computed(() => previewDialog.value.isImage && previewDialog.value.currentImageIndex > 0);
@@ -518,40 +545,41 @@ const app = createApp({
518545
};
519546

520547
return {
521-
fileList,
548+
confirmDelete,
549+
copyFileLink,
522550
currentPath,
523-
pathSegments,
524-
previewDialog,
525551
deleteDialog,
526-
isDragOver,
527-
uploadProgress,
552+
downloadFile,
528553
fileInput,
554+
fileList,
529555
folderInput,
530-
handleItemClick,
531-
navigateTo,
532-
getPathUpTo,
533-
saveFile,
534-
saveAndClose,
535-
showDeleteDialog,
536-
confirmDelete,
537-
isImageFile,
538-
isVideoFile,
539556
formatFileSize,
540-
handleFileSelect,
557+
getPathUpTo,
558+
handleClose,
541559
handleFileDrop,
542-
downloadFile,
543-
previewText,
560+
handleImageKeydown,
561+
handleItemClick,
562+
handleFileSelect,
544563
hasPrevImage,
545564
hasNextImage,
546-
showPrevImage,
547-
showNextImage,
548-
handleImageKeydown,
565+
hasWritePermission,
549566
imagePreview,
550-
renameFile,
551-
copyFileLink,
567+
isDragOver,
568+
isImageFile,
569+
isVideoFile,
570+
login,
571+
navigateTo,
552572
onTooltipHide,
553-
hasWritePermission,
554-
login, // 暴露登录方法
573+
pathSegments,
574+
previewDialog,
575+
previewText,
576+
renameFile,
577+
saveAndClose,
578+
saveFile,
579+
showDeleteDialog,
580+
showNextImage,
581+
showPrevImage,
582+
uploadProgress,
555583
};
556584
}
557585
});

0 commit comments

Comments
 (0)