Skip to content

Commit

Permalink
Upload data and string
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Jul 15, 2019
1 parent 40aafa4 commit 04fc627
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.build
/Packages
/*.xcodeproj
.swiftpm
28 changes: 26 additions & 2 deletions Sources/Shout/SFTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ public class SFTP {
/// - permissions: the file permissions to create the new file with; defaults to FilePermissions.default
/// - Throws: SSHError if local file can't be read or upload fails
public func upload(localURL: URL, remotePath: String, permissions: FilePermissions = .default) throws {
let data = try Data(contentsOf: localURL, options: .alwaysMapped)
try upload(data: data, remotePath: remotePath, permissions: permissions)
}

/// Upload a file from the local device to the remote server
///
/// - Parameters:
/// - string: String to be uploaded as a file
/// - remotePath: the location on the remote server whether the file should be uploaded to
/// - permissions: the file permissions to create the new file with; defaults to FilePermissions.default
/// - Throws: SSHError if string is not valid or upload fails
public func upload(string: String, remotePath: String, permissions: FilePermissions = .default) throws {
guard let data = string.data(using: .utf8) else {
throw SSHError.genericError("Unable to convert string to utf8 data")
}
try upload(data: data, remotePath: remotePath, permissions: permissions)
}

/// Upload a file from the local device to the remote server
///
/// - Parameters:
/// - data: Data to be uploaded as a file
/// - remotePath: the location on the remote server whether the file should be uploaded to
/// - permissions: the file permissions to create the new file with; defaults to FilePermissions.default
/// - Throws: SSHError if upload fails
public func upload(data: Data, remotePath: String, permissions: FilePermissions = .default) throws {
let sftpHandle = try SFTPHandle(
cSession: cSession,
sftpSession: sftpSession,
Expand All @@ -118,8 +144,6 @@ public class SFTP {
mode: LIBSSH2_SFTP_S_IFREG | permissions.rawValue
)

let data = try Data(contentsOf: localURL, options: .alwaysMapped)

var offset = 0
while offset < data.count {
let upTo = Swift.min(offset + SFTPHandle.bufferSize, data.count)
Expand Down

0 comments on commit 04fc627

Please sign in to comment.