feat: 将 scanDir 持久化到 app_config 表,移除 localStorage 存储

This commit is contained in:
2025-08-07 21:17:44 +08:00
parent b707fcb890
commit 44bfc02a7d
2 changed files with 21 additions and 13 deletions
+6 -10
View File
@@ -210,8 +210,8 @@ export default {
starting: false, // 点完按钮后、等后端回话前的"转圈"
moving: false, // 点完「移动文件」后等后端回话前的"转圈"
cancelling: false, // 点完「中止扫描」后等后端确认的"转圈"
scanDir: localStorage.getItem('scan-dir') || '', // 要扫描的目录(记住上次填的)
destDir: localStorage.getItem('scan-dest-dir') || '', // 整理后文件放哪(记住上次填的)
scanDir: '', // 要扫描的目录(从后端状态回填)
destDir: '', // 整理后文件放哪(从后端状态回填)
// ---------- 扫描进度与状态 ----------
status: {
@@ -299,9 +299,10 @@ export default {
const wasRunning = this.status.state === 'running' // 之前是不是在跑
const isRunning = data.state === 'running' // 现在是不是在跑
const prevLineCount = (this.status.lines || []).length
// 第一次拿到状态时,把后端记的"上次存放目录"回填进来
if (!this.status.initialized && data.destDir) {
this.destDir = data.destDir
// 第一次拿到状态时,把后端记的"上次目录"回填进来
if (!this.status.initialized) {
if (data.rootDir) this.scanDir = data.rootDir
if (data.destDir) this.destDir = data.destDir
}
this.status = data
this.status.initialized = true
@@ -411,9 +412,6 @@ export default {
async handleStart() {
this.starting = true
try {
// 记住这次填的目录,下次打开页面自动填上
localStorage.setItem('scan-dir', this.scanDir)
localStorage.setItem('scan-dest-dir', this.destDir)
const res = await request.post('/api/scan/start', {
dir: this.scanDir.trim(),
destDir: this.destDir.trim()
@@ -476,10 +474,8 @@ export default {
if (!this.fsData.current) return
if (this.pickerTarget === 'dest') {
this.destDir = this.fsData.current
localStorage.setItem('scan-dest-dir', this.destDir)
} else {
this.scanDir = this.fsData.current
localStorage.setItem('scan-dir', this.scanDir)
}
this.dirPickerVisible = false
},
+15 -3
View File
@@ -71,7 +71,10 @@ type ScanStatus struct {
// ============================ 应用级常量 ============================
// 应用级配置键。目前只存了两个:上次使用的存放目录、色情检测服务是否真的起来了。
const ConfigKeyDestDir = "scan.destDir"
const (
ConfigKeyScanDir = "scan.scanDir"
ConfigKeyDestDir = "scan.destDir"
)
// nsfwDetectorReadyKey 记录最近一次扫描时色情检测服务是否真的起来了(python+nudenet 可用)。
// 配置开了但检测器没起来(没装 python/模型下载失败等)也能区分出来,前端据此提示。
@@ -147,7 +150,13 @@ type ScanService struct {
// NewScanService 创建扫描服务。
// 注意 store 可以传 nil——数据库打不开时服务照常能用,只是不记录媒体信息。
func NewScanService(st *store.Store) *ScanService {
return &ScanService{store: st, hub: NewStatusHub(), state: ScanIdle}
s := &ScanService{store: st, hub: NewStatusHub(), state: ScanIdle}
// 从数据库恢复上次用过的目录,状态接口可以直接返回给前端回填
if st != nil {
s.rootDir = st.GetConfig(ConfigKeyScanDir)
s.destDir = st.GetConfig(ConfigKeyDestDir)
}
return s
}
// Subscribe 让调用方(通常是 WebSocket 处理器)订阅进度更新。
@@ -209,8 +218,11 @@ func (s *ScanService) Start(rootDir, destDir string) error {
s.ctx, s.cancel = context.WithCancel(context.Background())
s.mu.Unlock()
// 把存放目录记到数据库里,下次打开页面自动填上,省得用户重敲
// 把扫描目录和存放目录记到数据库里,下次打开页面自动填上,省得用户重敲
if s.store != nil {
if err := s.store.SaveConfig(ConfigKeyScanDir, rootDir); err != nil {
fmt.Printf("=== 保存扫描目录配置失败: %v\n", err)
}
if err := s.store.SaveConfig(ConfigKeyDestDir, destDir); err != nil {
fmt.Printf("=== 保存存放目录配置失败: %v\n", err)
}