diff --git a/AIStudyApp/AIStudyApp/Core/Models/APIModels.swift b/AIStudyApp/AIStudyApp/Core/Models/APIModels.swift index 79abf09..fd85dc3 100644 --- a/AIStudyApp/AIStudyApp/Core/Models/APIModels.swift +++ b/AIStudyApp/AIStudyApp/Core/Models/APIModels.swift @@ -636,6 +636,10 @@ struct AddSourceRequest: Codable { let fileId: String? let type: String? let title: String? + let originalFilename: String? + let mimeType: String? + let sizeBytes: Int? + let originalObjectKey: String? } // MARK: - Learning Activity diff --git a/AIStudyApp/AIStudyApp/Core/Services/APIService.swift b/AIStudyApp/AIStudyApp/Core/Services/APIService.swift index 599f19a..8fe12b7 100644 --- a/AIStudyApp/AIStudyApp/Core/Services/APIService.swift +++ b/AIStudyApp/AIStudyApp/Core/Services/APIService.swift @@ -538,8 +538,8 @@ class KnowledgeSourceService { return try await client.request("/knowledge-bases/\(kbId)/sources/\(id)") } - func add(kbId: String, fileId: String? = nil, type: String? = nil, title: String? = nil) async throws -> KnowledgeSource { - let body = AddSourceRequest(fileId: fileId, type: type, title: title) + func add(kbId: String, fileId: String? = nil, type: String? = nil, title: String? = nil, originalFilename: String? = nil, mimeType: String? = nil, sizeBytes: Int? = nil, originalObjectKey: String? = nil) async throws -> KnowledgeSource { + let body = AddSourceRequest(fileId: fileId, type: type, title: title, originalFilename: originalFilename, mimeType: mimeType, sizeBytes: sizeBytes, originalObjectKey: originalObjectKey) return try await client.request("/knowledge-bases/\(kbId)/sources", method: "POST", body: body) } diff --git a/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift b/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift index 76196e3..984f455 100644 --- a/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift +++ b/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift @@ -1300,13 +1300,22 @@ struct ImportPage: View { do { let mime = url.pathExtension.lowercased() == "pdf" ? "application/pdf" : "text/plain" print("[ImportPage] Uploading to COS: \(name), mime=\(mime)") - let uploadResult = try await FileUploadService.shared.uploadData(data, filename: name, mimeType: mime) - print("[ImportPage] COS upload OK: \(String(describing: uploadResult))") - statusMessage = "开始导入 \(name)..." - print("[ImportPage] Calling POST /imports: kbId=\(kb), fileName=\(name)") - let importResult = try await DocumentImportService.shared.create(knowledgeBaseId: kb, fileName: name, sourceType: "file") - print("[ImportPage] Import created: \(String(describing: importResult))") - statusMessage = "\(name) 导入任务已创建" + let (fileId, objectKey) = try await FileUploadService.shared.uploadDataWithKey(data, filename: name, mimeType: mime) + print("[ImportPage] COS upload OK: fileId=\(fileId), objectKey=\(objectKey)") + statusMessage = "创建资料..." + print("[ImportPage] Calling POST /knowledge-bases/\(kb)/sources: fileName=\(name)") + let source = try await KnowledgeSourceService.shared.add( + kbId: kb, + fileId: fileId, + type: "file", + title: name, + originalFilename: name, + mimeType: mime, + sizeBytes: data.count, + originalObjectKey: objectKey + ) + print("[ImportPage] Source created: id=\(source.id), title=\(source.title ?? name)") + statusMessage = "\(name) 导入完成" } catch { print("[ImportPage] ❌ Error for \(name): \(error.localizedDescription)") print("[ImportPage] Full error: \(error)")