@@ -6,12 +6,12 @@ import (
6
6
"path/filepath"
7
7
"strings"
8
8
"time"
9
-
9
+
10
+ "github.com/OpenListTeam/OpenList/v4/internal/conf"
10
11
"github.com/OpenListTeam/OpenList/v4/internal/model/tables"
11
- "github.com/OpenListTeam/OpenList/v4/pkg/tempdir"
12
12
"github.com/pkg/errors"
13
- "gorm.io/gorm"
14
13
log "github.com/sirupsen/logrus"
14
+ "gorm.io/gorm"
15
15
)
16
16
17
17
func CreateSliceUpload (su * tables.SliceUpload ) error {
@@ -52,11 +52,11 @@ func GetIncompleteSliceUploads() ([]*tables.SliceUpload, error) {
52
52
tables .SliceUploadStatusProxyComplete ,
53
53
tables .SliceUploadStatusPendingComplete ,
54
54
}).Find (& uploads ).Error
55
-
55
+
56
56
if err != nil {
57
57
return nil , errors .WithStack (err )
58
58
}
59
-
59
+
60
60
return uploads , nil
61
61
}
62
62
@@ -75,10 +75,10 @@ func UpdateSliceStatusAtomic(taskID string, sliceNum int, status []byte) error {
75
75
if err := tx .Where ("task_id = ?" , taskID ).First (& su ).Error ; err != nil {
76
76
return err
77
77
}
78
-
78
+
79
79
// 更新分片状态
80
80
tables .SetSliceUploaded (su .SliceUploadStatus , sliceNum )
81
-
81
+
82
82
// 保存更新
83
83
return tx .Save (& su ).Error
84
84
}))
@@ -88,15 +88,15 @@ func UpdateSliceStatusAtomic(taskID string, sliceNum int, status []byte) error {
88
88
func CleanupOrphanedSliceUploads () error {
89
89
// 清理超过24小时的未完成任务
90
90
cutoff := time .Now ().Add (- 24 * time .Hour )
91
-
91
+
92
92
var orphanedTasks []tables.SliceUpload
93
- if err := db .Where ("status IN (?, ?) AND updated_at < ?" ,
94
- tables .SliceUploadStatusWaiting ,
95
- tables .SliceUploadStatusUploading ,
93
+ if err := db .Where ("status IN (?, ?) AND updated_at < ?" ,
94
+ tables .SliceUploadStatusWaiting ,
95
+ tables .SliceUploadStatusUploading ,
96
96
cutoff ).Find (& orphanedTasks ).Error ; err != nil {
97
97
return errors .WithStack (err )
98
98
}
99
-
99
+
100
100
cleanedCount := 0
101
101
for _ , task := range orphanedTasks {
102
102
// 清理临时文件
@@ -107,106 +107,106 @@ func CleanupOrphanedSliceUploads() error {
107
107
log .Debugf ("Removed orphaned tmp file: %s" , task .TmpFile )
108
108
}
109
109
}
110
-
110
+
111
111
// 删除数据库记录
112
112
if err := db .Delete (& task ).Error ; err != nil {
113
113
log .Errorf ("Failed to delete orphaned slice upload task %s: %v" , task .TaskID , err )
114
114
} else {
115
115
cleanedCount ++
116
116
}
117
117
}
118
-
118
+
119
119
if cleanedCount > 0 {
120
120
log .Infof ("Cleaned up %d orphaned slice upload tasks" , cleanedCount )
121
121
}
122
-
122
+
123
123
// 额外清理:扫描临时目录中的孤儿文件
124
124
return cleanupOrphanedTempFiles ()
125
125
}
126
126
127
127
// cleanupOrphanedTempFiles 清理临时目录中的孤儿文件
128
128
func cleanupOrphanedTempFiles () error {
129
129
// 获取临时目录路径,使用共享的tempdir包
130
- tempDir := tempdir .GetPersistentTempDir ()
131
-
130
+ tempDir := conf .GetPersistentTempDir ()
131
+
132
132
// 检查临时目录是否存在
133
133
if _ , err := os .Stat (tempDir ); os .IsNotExist (err ) {
134
134
log .Debugf ("Temp directory does not exist: %s" , tempDir )
135
135
return nil
136
136
}
137
-
137
+
138
138
// 获取所有活跃的分片上传任务的临时文件列表
139
139
var activeTasks []tables.SliceUpload
140
- if err := db .Where ("tmp_file IS NOT NULL AND tmp_file != '' AND status IN (?, ?)" ,
141
- tables .SliceUploadStatusWaiting ,
140
+ if err := db .Where ("tmp_file IS NOT NULL AND tmp_file != '' AND status IN (?, ?)" ,
141
+ tables .SliceUploadStatusWaiting ,
142
142
tables .SliceUploadStatusUploading ).Find (& activeTasks ).Error ; err != nil {
143
143
return errors .WithStack (err )
144
144
}
145
-
145
+
146
146
// 构建活跃文件的映射表
147
147
activeFiles := make (map [string ]bool )
148
148
for _ , task := range activeTasks {
149
149
if task .TmpFile != "" {
150
150
activeFiles [task .TmpFile ] = true
151
151
}
152
152
}
153
-
153
+
154
154
cleanedCount := 0
155
155
cutoff := time .Now ().Add (- 24 * time .Hour ) // 只清理超过24小时的文件
156
-
156
+
157
157
// 遍历临时目录
158
158
err := filepath .WalkDir (tempDir , func (path string , d fs.DirEntry , err error ) error {
159
159
if err != nil {
160
160
log .Warnf ("Failed to access path %s: %v" , path , err )
161
161
return nil // 继续处理其他文件
162
162
}
163
-
163
+
164
164
// 跳过目录
165
165
if d .IsDir () {
166
166
return nil
167
167
}
168
-
168
+
169
169
// 只处理分片上传临时文件(以slice_upload_开头)
170
170
if ! strings .HasPrefix (d .Name (), "slice_upload_" ) {
171
171
return nil
172
172
}
173
-
173
+
174
174
// 检查文件是否在活跃任务列表中
175
175
if activeFiles [path ] {
176
176
return nil // 文件仍在使用中,跳过
177
177
}
178
-
178
+
179
179
// 检查文件修改时间
180
180
info , err := d .Info ()
181
181
if err != nil {
182
182
log .Warnf ("Failed to get file info for %s: %v" , path , err )
183
183
return nil
184
184
}
185
-
185
+
186
186
// 只清理超过24小时的文件
187
187
if info .ModTime ().After (cutoff ) {
188
188
return nil
189
189
}
190
-
190
+
191
191
// 删除孤儿文件
192
192
if err := os .Remove (path ); err != nil {
193
193
log .Warnf ("Failed to remove orphaned temp file %s: %v" , path , err )
194
194
} else {
195
195
log .Debugf ("Removed orphaned temp file: %s" , path )
196
196
cleanedCount ++
197
197
}
198
-
198
+
199
199
return nil
200
200
})
201
-
201
+
202
202
if err != nil {
203
203
log .Errorf ("Failed to walk temp directory %s: %v" , tempDir , err )
204
204
return errors .WithStack (err )
205
205
}
206
-
206
+
207
207
if cleanedCount > 0 {
208
208
log .Infof ("Cleaned up %d orphaned temp files from %s" , cleanedCount , tempDir )
209
209
}
210
-
210
+
211
211
return nil
212
212
}
0 commit comments