processWithTransactionProtection 使用事务保护的文件处理
(task *UploadTask, saveOriginal bool)
| 188 | |
| 189 | // processWithTransactionProtection 使用事务保护的文件处理 |
| 190 | func (fp *FileProcessor) processWithTransactionProtection(task *UploadTask, saveOriginal bool) error { |
| 191 | var uploadFile *model.UploadFile |
| 192 | var err error |
| 193 | |
| 194 | if GetBatchUploadManagerInstance().IsBatchCancelled(task.BatchID) { |
| 195 | return errBatchUploadCancelled |
| 196 | } |
| 197 | |
| 198 | // 更新批量上传进度 |
| 199 | fp.updateBatchProgress(task, model.UploadStatusUploading, 26) |
| 200 | |
| 201 | if GetBatchUploadManagerInstance().IsBatchCancelled(task.BatchID) { |
| 202 | return errBatchUploadCancelled |
| 203 | } |
| 204 | |
| 205 | // 创建上传记录(事务化处理) |
| 206 | if saveOriginal { |
| 207 | uploadFile, err = fp.createUploadFileRecordFromTask(task) |
| 208 | } else { |
| 209 | uploadFile, err = fp.createLightweightUploadRecord(task) |
| 210 | } |
| 211 | if err != nil { |
| 212 | return fmt.Errorf("创建上传记录失败: %v", err) |
| 213 | } |
| 214 | task.DatabaseID = uploadFile.ID |
| 215 | |
| 216 | if GetBatchUploadManagerInstance().IsBatchCancelled(task.BatchID) { |
| 217 | fp.cleanupCancelledUpload(task, uploadFile, 0) |
| 218 | return errBatchUploadCancelled |
| 219 | } |
| 220 | |
| 221 | // 标记为已上传并更新进度 |
| 222 | if err := uploadFile.MarkAsUploaded(); err != nil { |
| 223 | return fmt.Errorf("标记为已上传失败: %v", err) |
| 224 | } |
| 225 | |
| 226 | // 更新批量上传进度 |
| 227 | fp.updateBatchProgress(task, model.UploadStatusUploaded, 100) |
| 228 | |
| 229 | if GetBatchUploadManagerInstance().IsBatchCancelled(task.BatchID) { |
| 230 | fp.cleanupCancelledUpload(task, uploadFile, 0) |
| 231 | return errBatchUploadCancelled |
| 232 | } |
| 233 | |
| 234 | // 记录处理开始 |
| 235 | fmt.Printf("上传记录已创建 - ID: %d, 文件: %s\n", uploadFile.ID, task.FileHeader.Filename) |
| 236 | |
| 237 | library, err := model.GetLibraryByID(task.EID, task.LibraryID) |
| 238 | if err != nil { |
| 239 | uploadFile.MarkAsFailed(fmt.Sprintf("获取知识库信息失败: %v", err)) |
| 240 | fp.updateBatchProgress(task, "failed", 0) |
| 241 | return fmt.Errorf("获取知识库信息失败: %v", err) |
| 242 | } |
| 243 | |
| 244 | // 创建文件记录,使用 base_path |
| 245 | dirManager := NewDirectoryManager() |
| 246 | fileID, err := dirManager.CreateFileRecord(task.EID, task.LibraryID, task.RelativePath, uploadFile.ID, task.BasePath, task.UserID, task.DuplicateMode, library.IsPersonalLibrary(), task.OriginType, task.OriginSource, task.OriginRefID) |
| 247 |
no test coverage detected