Skip to content

Commit 39bbfd6

Browse files
authored
Lectures: Improve attachment loading (#359)
* Improve lecture attachment loading * include file extension in name
1 parent e5a5fc5 commit 39bbfd6

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

ArtemisKit/Sources/CourseView/LectureTab/LectureAttachmentSheet.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ struct LectureAttachmentSheet: View {
2828

2929
private func loadAttachment() async {
3030
var link: String?
31+
var fileName: String?
3132
switch attachment {
3233
case .file(let attachment):
3334
link = attachment.link
35+
fileName = attachment.name
3436
case .url(let attachment):
3537
// TODO
3638
link = nil
@@ -44,6 +46,6 @@ struct LectureAttachmentSheet: View {
4446
}
4547

4648
let normalizedLink = link.hasPrefix("/api/core/files/") ? link : "/api/core/files/\(link)"
47-
previewURL = await LectureServiceFactory.shared.getAttachmentFile(link: normalizedLink)
49+
previewURL = await LectureServiceFactory.shared.getAttachmentFile(link: normalizedLink, name: fileName)
4850
}
4951
}

ArtemisKit/Sources/CourseView/Services/LectureService/LectureService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SharedModels
1111

1212
protocol LectureService {
1313
func getLectureDetails(lectureId: Int) async -> DataState<Lecture>
14-
func getAttachmentFile(link: String) async -> DataState<URL>
14+
func getAttachmentFile(link: String, name: String?) async -> DataState<URL>
1515
func updateLectureUnitCompletion(lectureId: Int, lectureUnitId: Int64, completed: Bool) async -> NetworkResponse
1616
func getAssociatedChannel(for lectureId: Int, in courseId: Int) async -> DataState<Channel>
1717
}

ArtemisKit/Sources/CourseView/Services/LectureService/LectureServiceImpl.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,26 @@ class LectureServiceImpl: LectureService {
6969
}
7070
}
7171

72-
func getAttachmentFile(link: String) async -> DataState<URL> {
72+
func getAttachmentFile(link: String, name: String? = nil) async -> DataState<URL> {
7373
guard let url = URL(string: link, relativeTo: UserSessionFactory.shared.institution?.baseURL) else {
7474
return .failure(error: UserFacingError(title: "Wrong URL"))
7575
}
7676
do {
77-
let (data, _) = try await URLSession.shared.data(from: url)
77+
var (data, response) = try await URLSession.shared.data(from: url)
7878

79-
guard let suggestedFilename = URL(string: link)?.lastPathComponent else { return .failure(error: UserFacingError(title: "")) }
79+
// Accessing lecture attachment as student needs a different URL sometimes
80+
if (response as? HTTPURLResponse)?.statusCode == 403 {
81+
let lastPathComponent = url.lastPathComponent
82+
let newUrl = url
83+
.deletingLastPathComponent()
84+
.appending(path: "student")
85+
.appending(path: lastPathComponent)
86+
87+
(data, _) = try await URLSession.shared.data(from: newUrl)
88+
}
89+
90+
let fileExtension = url.pathExtension
91+
let suggestedFilename = name?.appending("." + fileExtension) ?? url.lastPathComponent
8092
let previewURL = FileManager.default.temporaryDirectory.appendingPathComponent(suggestedFilename)
8193
try data.write(to: previewURL, options: .atomic) // atomic option overwrites it if needed
8294
return .done(response: previewURL)

0 commit comments

Comments
 (0)