Skip to content

Commit 88aa707

Browse files
committed
major improvements
1 parent 7df5efd commit 88aa707

File tree

5 files changed

+396
-407
lines changed

5 files changed

+396
-407
lines changed

Django Files/API/DFAPI.swift

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ struct DFAPI {
168168
public func renameFile(fileID: Int, name: String) async -> Bool {
169169
do {
170170
let jsonData = try JSONSerialization.data(withJSONObject: ["name": name])
171-
let jsonString = String(data: jsonData, encoding: .utf8) ?? "{}"
172171

173172
let _ = try await makeAPIRequest(
174173
body: jsonData,
@@ -183,7 +182,9 @@ struct DFAPI {
183182
}
184183
}
185184

186-
public func uploadFile(url: URL, fileName: String? = nil, taskDelegate: URLSessionTaskDelegate? = nil) async -> DFUploadResponse?{
185+
186+
187+
public func uploadFile(url: URL, fileName: String? = nil, albums: String = "", privateUpload: Bool = false, taskDelegate: URLSessionTaskDelegate? = nil) async -> DFUploadResponse?{
187188
let boundary = UUID().uuidString
188189
let filename = fileName ?? (url.absoluteString as NSString).lastPathComponent
189190

@@ -201,9 +202,16 @@ struct DFAPI {
201202
data.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)
202203

203204
do{
204-
let responseBody = try await makeAPIRequest(body: data, path: getAPIPath(.upload), parameters: [:], method: .post, expectedResponse: .ok, headerFields: [.contentType: "multipart/form-data; boundary=\(boundary)"], taskDelegate: taskDelegate)
205+
var headers: [HTTPField.Name: String] = [.contentType: "multipart/form-data; boundary=\(boundary)"]
206+
if !albums.isEmpty {
207+
headers[HTTPField.Name("Albums")!] = albums
208+
}
209+
if privateUpload {
210+
headers[HTTPField.Name("Private")!] = "true"
211+
}
212+
let responseBody = try await makeAPIRequest(body: data, path: getAPIPath(.upload), parameters: [:], method: .post, expectedResponse: .ok, headerFields: headers, taskDelegate: taskDelegate)
205213
return try decoder.decode(DFUploadResponse.self, from: responseBody)
206-
}catch {
214+
} catch {
207215
print("Request failed \(error)")
208216
return nil;
209217
}
@@ -281,6 +289,16 @@ struct DFAPI {
281289
let token: String
282290
}
283291

292+
@MainActor
293+
private func updateSessionCookies(_ selectedServer: DjangoFilesSession, _ cookies: [HTTPCookie]) {
294+
selectedServer.cookies = cookies
295+
}
296+
297+
@MainActor
298+
private func updateSessionToken(_ selectedServer: DjangoFilesSession, _ token: String) {
299+
selectedServer.token = token
300+
}
301+
284302
public func localLogin(username: String, password: String, selectedServer: DjangoFilesSession) async -> Bool {
285303
let request = DFLocalLoginRequest(username: username, password: password)
286304
do {
@@ -309,17 +327,13 @@ struct DFAPI {
309327
cookies.forEach { cookie in
310328
HTTPCookieStorage.shared.setCookie(cookie)
311329
}
312-
await MainActor.run {
313-
selectedServer.cookies = cookies
314-
}
330+
await updateSessionCookies(selectedServer, cookies)
315331
}
316332

317333
let userToken = try JSONDecoder().decode(UserToken.self, from: data)
318334

319335
// Update the token in the server object
320-
await MainActor.run {
321-
selectedServer.token = userToken.token
322-
}
336+
await updateSessionToken(selectedServer, userToken.token)
323337
return true
324338
} catch {
325339
print("Local login request failed \(error)")
@@ -406,15 +420,11 @@ struct DFAPI {
406420
HTTPCookieStorage.shared.setCookie(cookie)
407421
print("Received cookie from response: \(cookie)")
408422
}
409-
await MainActor.run {
410-
selectedServer.cookies = cookies
411-
}
423+
await updateSessionCookies(selectedServer, cookies)
412424
}
413425

414-
// Update the token in the server object
415-
await MainActor.run {
416-
selectedServer.token = token
417-
}
426+
// Update the token in the server object using the MainActor method
427+
await updateSessionToken(selectedServer, token)
418428

419429
return true
420430
} catch {
@@ -423,17 +433,18 @@ struct DFAPI {
423433
}
424434
}
425435

426-
public func getFiles(page: Int = 1) async -> DFFilesResponse? {
436+
public func getFiles(page: Int = 1, album: Int? = nil) async -> DFFilesResponse? {
427437
do {
438+
var parameters: [String: String] = [:]
439+
if let album = album {
440+
parameters["album"] = String(album)
441+
}
442+
428443
let responseBody = try await makeAPIRequest(
429444
path: getAPIPath(.files) + "\(page)/",
430-
parameters: [:],
445+
parameters: parameters,
431446
method: .get
432447
)
433-
434-
print(String(data: responseBody, encoding: String.Encoding.utf8))
435-
436-
// Use the default decoder since dates are now handled as strings
437448
let specialDecoder = JSONDecoder()
438449
specialDecoder.keyDecodingStrategy = .convertFromSnakeCase
439450
return try specialDecoder.decode(DFFilesResponse.self, from: responseBody)

Django Files/Views/AlbumList.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct AlbumListView: View {
9494
}
9595
}
9696
.navigationDestination(for: DFAlbum.self) { album in
97-
Text("Album: \(album.name)")
97+
FileListView(server: server, albumID: album.id)
9898
}
9999
.listStyle(.plain)
100100
.refreshable {

0 commit comments

Comments
 (0)