Skip to content

Commit

Permalink
Allow sending links
Browse files Browse the repository at this point in the history
  • Loading branch information
grishka committed Sep 26, 2023
1 parent b8eea26 commit 8d19703
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
8 changes: 6 additions & 2 deletions NearbyShare/NearbyConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,16 @@ class NearbyConnection{
}

internal func sendTransferSetupFrame(_ frame:Sharing_Nearby_Frame) throws{
try sendBytesPayload(data: try frame.serializedData(), id: Int64.random(in: Int64.min...Int64.max))
}

internal func sendBytesPayload(data:Data, id:Int64) throws{
var transfer=Location_Nearby_Connections_PayloadTransferFrame()
transfer.packetType = .data
transfer.payloadChunk.offset=0
transfer.payloadChunk.flags=0
transfer.payloadChunk.body=try frame.serializedData()
transfer.payloadHeader.id=Int64.random(in: Int64.min...Int64.max)
transfer.payloadChunk.body=data
transfer.payloadHeader.id=id
transfer.payloadHeader.type = .bytes
transfer.payloadHeader.totalSize=Int64(transfer.payloadChunk.body.count)
transfer.payloadHeader.isSensitive=false
Expand Down
23 changes: 18 additions & 5 deletions NearbyShare/OutboundNearbyConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class OutboundNearbyConnection:NearbyConnection{
private var totalBytesToSend:Int64=0
private var totalBytesSent:Int64=0
private var cancelled:Bool=false
private var textPayloadID:Int64=0

enum State{
case initial, sentUkeyClientInit, sentUkeyClientFinish, sentPairedKeyEncryption, sentPairedKeyResult, sentIntroduction, sendingFiles
Expand All @@ -33,6 +34,9 @@ class OutboundNearbyConnection:NearbyConnection{
init(connection: NWConnection, id: String, urlsToSend:[URL]){
self.urlsToSend=urlsToSend
super.init(connection: connection, id: id)
if urlsToSend.count==1 && !urlsToSend[0].isFileURL{
textPayloadID=Int64.random(in: Int64.min...Int64.max)
}
}

deinit {
Expand Down Expand Up @@ -256,16 +260,16 @@ class OutboundNearbyConnection:NearbyConnection{
var introduction=Sharing_Nearby_Frame()
introduction.version = .v1
introduction.v1.type = .introduction
if urlsToSend.count==1 && urlsToSend[0].scheme != "file"{
if urlsToSend.count==1 && !urlsToSend[0].isFileURL{
var meta=Sharing_Nearby_TextMetadata()
meta.type = .url
meta.textTitle=urlsToSend[0].host ?? "URL"
meta.size=Int64(urlsToSend[0].absoluteString.utf8.count)
meta.payloadID=Int64.random(in: Int64.min...Int64.max)
meta.payloadID=textPayloadID
introduction.v1.introduction.textMetadata.append(meta)
}else{
for url in urlsToSend{
guard url.scheme=="file" else {continue}
guard url.isFileURL else {continue}
var meta=Sharing_Nearby_FileMetadata()
meta.name=OutboundNearbyConnection.sanitizeFileName(name: url.lastPathComponent)
let attrs=try FileManager.default.attributesOfItem(atPath: url.path)
Expand Down Expand Up @@ -309,7 +313,11 @@ class OutboundNearbyConnection:NearbyConnection{
case .accept:
currentState = .sendingFiles
delegate?.outboundConnectionTransferAccepted(connection: self)
try sendNextFileChunk()
if urlsToSend.count==1 && !urlsToSend[0].isFileURL{
try sendURL()
}else{
try sendNextFileChunk()
}
case .reject, .unknown:
delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .userRejected))
try sendDisconnectionAndDisconnect()
Expand All @@ -325,8 +333,13 @@ class OutboundNearbyConnection:NearbyConnection{
}
}

private func sendURL() throws{
try sendBytesPayload(data: Data(urlsToSend[0].absoluteString.utf8), id: textPayloadID)
delegate?.outboundConnectionTransferFinished(connection: self)
try sendDisconnectionAndDisconnect()
}

private func sendNextFileChunk() throws{
print("SEND NEXT: \(Thread.current)")
if cancelled{
return
}
Expand Down
1 change: 1 addition & 0 deletions ShareExtension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
AND NOT (ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.folder")
).@count == $extensionItem.attachments.@count
).@count &gt; 0</string>
Expand Down

0 comments on commit 8d19703

Please sign in to comment.