-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,4 @@ fastlane/screenshots | |
fastlane/test_output | ||
*.xctestplan | ||
Package.resolved | ||
.swiftpm |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,11 @@ This is small Swift library for iOS, macOS and tvOS which wraps [libsmb2](https: | |
[![Swift Version][swift-image]][swift-url] | ||
[![Platform][platform-image]](#) | ||
[![License][license-image]][license-url] | ||
|
||
[![Build Status][travis-image]][travis-url] | ||
[![Release version][release-image]][release-url] | ||
|
||
[![Swift Version Compatibility][swift-version-image]][swift-version-url] | ||
[![Platform Compatibility ][platform-image]][platform-url] | ||
|
||
## Getting Started | ||
|
||
To use AMSMB2, add the following dependency to your Package.swift: | ||
|
@@ -30,87 +31,66 @@ dependencies: [ | |
|
||
## Usage | ||
|
||
Just read inline help to find what each function does. It's straightforward. It's thread safe and any queue. | ||
Just read inline help to find what each function does. It's straightforward. It's thread safe. | ||
|
||
To do listing files in directory and file operations you must use this template: | ||
|
||
```swift | ||
import AMSMB2 | ||
|
||
class SMBClient { | ||
class SMBClient: @unchecked Sendable { | ||
/// connect to: `smb://[email protected]/share` | ||
|
||
let serverURL = URL(string: "smb://XXX.XXX.XX.XX")! | ||
let credential = URLCredential(user: "guest", password: "", persistence: URLCredential.Persistence.forSession) | ||
let share = "share" | ||
|
||
lazy private var client = AMSMB2(url: self.serverURL, credential: self.credential)! | ||
lazy private var client = SMB2Manager(url: self.serverURL, credential: self.credential)! | ||
|
||
private func connect(handler: @escaping (Result<AMSMB2, Error>) -> Void) { | ||
private func connect() async throws -> SMB2Manager { | ||
// AMSMB2 can handle queueing connection requests | ||
client.connectShare(name: self.share) { error in | ||
if let error = error { | ||
handler(.failure(error)) | ||
} else { | ||
handler(.success(self.client)) | ||
} | ||
} | ||
try await client.connectShare(name: self.share) | ||
return self.client | ||
} | ||
|
||
func listDirectory(path: String) { | ||
connect { result in | ||
switch result { | ||
case .success(let client): | ||
client.contentsOfDirectory(atPath: path) { result in | ||
switch result { | ||
case .success(let files): | ||
for entry in files { | ||
print("name:", entry[.nameKey] as! String, | ||
", path:", entry[.pathKey] as! String, | ||
", type:", entry[.fileResourceTypeKey] as! URLFileResourceType, | ||
", size:", entry[.fileSizeKey] as! Int64, | ||
", modified:", entry[.contentModificationDateKey] as! Date, | ||
", created:", entry[.creationDateKey] as! Date) | ||
} | ||
|
||
case .failure(let error): | ||
print(error) | ||
} | ||
Task { | ||
do { | ||
let client = try await connect() | ||
let files = try await client.contentsOfDirectory(atPath: path) | ||
for entry in files { | ||
print( | ||
"name:", entry[.nameKey] as! String, | ||
", path:", entry[.pathKey] as! String, | ||
", type:", entry[.fileResourceTypeKey] as! URLFileResourceType, | ||
", size:", entry[.fileSizeKey] as! Int64, | ||
", modified:", entry[.contentModificationDateKey] as! Date, | ||
", created:", entry[.creationDateKey] as! Date) | ||
} | ||
|
||
case .failure(let error): | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
} | ||
|
||
func moveItem(path: String, to toPath: String) { | ||
self.connect { result in | ||
switch result { | ||
case .success(let client): | ||
client.moveItem(atPath: path, toPath: toPath) { error in | ||
if let error = error { | ||
print(error) | ||
} else { | ||
print("\(path) moved successfully.") | ||
} | ||
|
||
// Disconnecting is optional, it will be called eventually | ||
// when `AMSMB2` object is freed. | ||
// You may call it explicitly to detect errors. | ||
client.disconnectShare(completionHandler: { (error) in | ||
if let error = error { | ||
print(error) | ||
} | ||
}) | ||
} | ||
Task { | ||
do { | ||
let client = try await self.connect() | ||
try await client.moveItem(atPath: path, toPath: toPath) | ||
print("\(path) moved successfully.") | ||
|
||
case .failure(let error): | ||
// Disconnecting is optional, it will be called eventually | ||
// when `AMSMB2` object is freed. | ||
// You may call it explicitly to detect errors. | ||
try await client.disconnectShare() | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
``` | ||
|
||
## License | ||
|
@@ -124,7 +104,10 @@ You **must** link this library dynamically to your app if you intend to distribu | |
[platform-image]: https://img.shields.io/cocoapods/p/AMSMB2.svg | ||
[license-image]: https://img.shields.io/github/license/amosavian/AMSMB2.svg | ||
[license-url]: LICENSE | ||
[travis-image]: https://travis-ci.com/amosavian/AMSMB2.svg | ||
[travis-url]: https://travis-ci.com/amosavian/AMSMB2 | ||
[release-url]: https://github.com/amosavian/AMSMB2/releases | ||
|
||
[swift-version-image]: https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Famosavian%2FAMSMB2%2Fbadge%3Ftype%3Dswift-versions | ||
[swift-version-url]: https://swiftpackageindex.com/amosavian/AMSMB2 | ||
[platform-image]: https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Famosavian%2FAMSMB2%2Fbadge%3Ftype%3Dplatforms | ||
[platform-url]: https://swiftpackageindex.com/amosavian/AMSMB2 | ||
[release-image]: https://img.shields.io/github/release/amosavian/AMSMB2.svg | ||
[release-url]: https://github.com/amosavian/AMSMB2/releases |