Skip to content

Commit

Permalink
feat: Download files (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmorley authored Jun 24, 2024
1 parent 95f3cc8 commit d2195f2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
23 changes: 23 additions & 0 deletions Reconnect/Model/BrowserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,27 @@ class BrowserModel {
}
}

func download(path: String) {
Task {
let fileManager = FileManager.default
let downloadsUrl = fileManager.urls(for: .downloadsDirectory, in: .userDomainMask)[0]

let filename = path.windowsLastPathComponent
let destinationURL = downloadsUrl.appendingPathComponent(filename)

do {
try await fileServer.copyFile(fromRemotePath: filename, toLocalPath: destinationURL.path)
} catch {
print("Failed to download file at path '\(path)' to destination path '\(destinationURL.path)' with error \(error).")
lastError = error
}

do {
if let directoryUrls = try? FileManager.default.contentsOfDirectory(at: downloadsUrl, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions.skipsSubdirectoryDescendants) {
print(directoryUrls)
}
}
}
}

}
24 changes: 24 additions & 0 deletions Reconnect/PLP/FileServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ class FileServer {
}
}

func syncQueue_copyFile(fromRemotePath remoteSourcePath: String, toLocalPath localDestinationPath: String) throws {
dispatchPrecondition(condition: .onQueue(workQueue))
let result = client.copyFromPsion(remoteSourcePath, localDestinationPath, nil) { context, status in
print("progress = \(status)")
return 1 // 0 is cancel
}
guard result.rawValue == 0 else {
throw ReconnectError.rfsvError(result)
}
}

func copyFile(fromRemotePath remoteSourcePath: String, toLocalPath localDestinationPath: String) async throws {
return try await withCheckedThrowingContinuation { continuation in
workQueue.async {
do {
try self.syncQueue_copyFile(fromRemotePath: remoteSourcePath, toLocalPath: localDestinationPath)
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
}

func syncQueue_mkdir(path: String) throws {
dispatchPrecondition(condition: .onQueue(workQueue))
let result = client.mkdir(path)
Expand Down
6 changes: 4 additions & 2 deletions Reconnect/Reconnect.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.device.serial</key>
<true/>
<key>com.apple.security.files.downloads.read-write</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.device.serial</key>
<true/>
</dict>
</plist>
11 changes: 11 additions & 0 deletions Reconnect/Views/BrowserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ struct BrowserView: View {
model.navigate(to: items.first!)
}
.disabled(items.count != 1 || !(items.first?.isDirectory ?? false))

Divider()

Button("Download") {
for item in items {
// TODO: Directories?
model.download(path: item)
}
}

Divider()

Button("Delete") {
for item in items {
model.delete(path: item)
Expand Down
2 changes: 1 addition & 1 deletion dependencies/plptools

0 comments on commit d2195f2

Please sign in to comment.