Skip to content

Commit 3e0de65

Browse files
committed
feat: 加载路径时检测是否为目录
1 parent edac65a commit 3e0de65

File tree

6 files changed

+51
-16
lines changed

6 files changed

+51
-16
lines changed

backend/src/router/dir.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ module.exports = async function ({ query }) {
3737
result.dirs.sort((i, j) => ((i.name || '').toLocaleLowerCase() > (j.name || '').toLocaleLowerCase() ? 1 : -1))
3838

3939
return { code: 200, msg: '操作成功', data: result }
40-
} catch (error) {
41-
if (error.code === 'EACCES' || error.code === 'EPERM') {
40+
} catch (err) {
41+
if (err.code === 'EACCES' || err.code === 'EPERM') {
4242
return { code: 401, msg: '权限不足,无法读取目录', query }
4343
} else if (err.code === 'ENOENT') {
4444
return { code: 400, msg: '目录不存在', query }

backend/src/router/read.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ module.exports = async function ({ query }) {
2828
stream: fs.createReadStream(filePath),
2929
},
3030
}
31-
} catch (error) {
32-
if (error.code === 'EACCES' || error.code === 'EPERM') {
31+
} catch (err) {
32+
if (err.code === 'EACCES' || err.code === 'EPERM') {
3333
return { code: 401, msg: '权限不足,无法读取文件', query }
3434
} else if (err.code === 'ENOENT') {
3535
return { code: 400, msg: '文件不存在', query }

backend/src/router/type.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require('fs')
2+
3+
module.exports = async function ({ query }) {
4+
if (!query.path) {
5+
return { code: 400, msg: '缺少文件路径参数', query }
6+
}
7+
8+
const qPath = query.path[0] === '/' ? query.path : `/${query.path}`
9+
10+
try {
11+
if (!fs.existsSync(qPath)) {
12+
return { code: 404, msg: '路径不存在', query }
13+
}
14+
15+
const stat = fs.statSync(qPath)
16+
17+
return { code: 200, msg: '操作成功', data: { isDirectory: stat.isDirectory() } }
18+
} catch (err) {
19+
if (err.code === 'EACCES' || err.code === 'EPERM') {
20+
return { code: 401, msg: '权限不足,无法读取', query }
21+
} else if (err.code === 'ENOENT') {
22+
return { code: 400, msg: '内容不存在', query }
23+
} else {
24+
return { code: 400, msg: `读取内容失败: ${err.message}`, query }
25+
}
26+
}
27+
}

backend/src/utils/exec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const router = {
55
'/save': { run: require('../router/save') },
66
'/del': { run: require('../router/del') },
77
'/dir': { run: require('../router/dir') },
8+
'/type': { run: require('../router/type') },
89
}
910

1011
module.exports = async function exec(data) {

frontend/src/layout/DialogOpen.vue

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ import { useEditorStore } from '@/store/editor'
8282
import { useLikeStore } from '@/store/like'
8383
import { useMenuStore } from '@/store/menu'
8484
85+
import api from '@/utils/api'
86+
8587
const user = useUserStore()
8688
const open = useOpenStore()
8789
const menu = useMenuStore()
@@ -94,11 +96,27 @@ const { cfg } = storeToRefs(user)
9496
onMounted(async () => {
9597
const query = new URLSearchParams(window.location.search).get('path') || ''
9698
if (query) {
97-
editor.add(query)
99+
const {
100+
data: { data },
101+
} = await api.get<{ data: { isDirectory: boolean } }>('/type', { params: { path: query } })
102+
103+
if (data.isDirectory) {
104+
changeDir(query)
105+
} else {
106+
editor.add(query)
107+
108+
if (!user.cfg.folderNotOpenInQuery) {
109+
menu.toggle('folder')
110+
}
111+
}
98112
} else {
99113
if (cfg.value.startOpen) {
100114
show.value = 'file'
101115
}
116+
117+
if (user.cfg.folderDefOpen) {
118+
menu.toggle('folder')
119+
}
102120
}
103121
})
104122

frontend/src/layout/ViewMenu.vue

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,13 @@
2525
</template>
2626

2727
<script lang="ts" setup>
28-
import { onMounted } from 'vue'
2928
import { Folder, Timer, Notification, Setting } from '@element-plus/icons-vue'
3029
3130
import { useMenuStore } from '@/store/menu'
3231
import { useLikeStore } from '@/store/like'
33-
import { useUserStore } from '@/store/user'
3432
3533
const menu = useMenuStore()
3634
const like = useLikeStore()
37-
const user = useUserStore()
38-
39-
onMounted(() => {
40-
const query = new URLSearchParams(window.location.search).get('path') || ''
41-
42-
if ((!query || !user.cfg.folderNotOpenInQuery) && user.cfg.folderDefOpen) {
43-
menu.toggle('folder')
44-
}
45-
})
4635
4736
const windowOpen = () => window.open('./')
4837
</script>

0 commit comments

Comments
 (0)