From bb994dbd9e0227238678168606593ca82f9c808a Mon Sep 17 00:00:00 2001 From: Grishka Date: Sun, 22 Sep 2024 03:12:42 +0300 Subject: [PATCH] Allow receiving text into .txt files closes #166, closes #147, closes #112, closes #178, closes #123, closes #110 --- NearDrop.xcodeproj/project.pbxproj | 8 ++-- NearbyShare/InboundNearbyConnection.swift | 57 +++++++++++++++++------ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/NearDrop.xcodeproj/project.pbxproj b/NearDrop.xcodeproj/project.pbxproj index ac70347..b83e800 100644 --- a/NearDrop.xcodeproj/project.pbxproj +++ b/NearDrop.xcodeproj/project.pbxproj @@ -839,7 +839,7 @@ CODE_SIGN_ENTITLEMENTS = NearDrop/NearDrop.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 8; + CURRENT_PROJECT_VERSION = 9; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities"; INFOPLIST_KEY_LSUIElement = YES; @@ -851,7 +851,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 2.0.4; + MARKETING_VERSION = 2.1.0; PRODUCT_BUNDLE_IDENTIFIER = me.grishka.NearDrop; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; @@ -871,7 +871,7 @@ CODE_SIGN_ENTITLEMENTS = NearDrop/NearDrop.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 8; + CURRENT_PROJECT_VERSION = 9; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities"; INFOPLIST_KEY_LSUIElement = YES; @@ -883,7 +883,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 2.0.4; + MARKETING_VERSION = 2.1.0; PRODUCT_BUNDLE_IDENTIFIER = me.grishka.NearDrop; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; diff --git a/NearbyShare/InboundNearbyConnection.swift b/NearbyShare/InboundNearbyConnection.swift index c49990a..e50ae19 100644 --- a/NearbyShare/InboundNearbyConnection.swift +++ b/NearbyShare/InboundNearbyConnection.swift @@ -124,6 +124,16 @@ class InboundNearbyConnection: NearbyConnection{ } try sendDisconnectionAndDisconnect() return true + }else if let fileInfo=transferredFiles[id]{ + fileInfo.fileHandle?.write(payload) + transferredFiles[id]!.bytesTransferred+=Int64(payload.count) + fileInfo.progress?.completedUnitCount=transferredFiles[id]!.bytesTransferred + try fileInfo.fileHandle?.close() + transferredFiles[id]!.fileHandle=nil + fileInfo.progress?.unpublish() + transferredFiles.removeValue(forKey: id) + try sendDisconnectionAndDisconnect() + return true } return false } @@ -270,27 +280,32 @@ class InboundNearbyConnection: NearbyConnection{ currentState = .receivedPairedKeyResult } + private func makeFileDestinationURL(_ initialDest:URL) -> URL{ + var dest=initialDest + if FileManager.default.fileExists(atPath: dest.path){ + var counter=1 + var path:String + let ext=dest.pathExtension + let baseUrl=dest.deletingPathExtension() + repeat{ + path="\(baseUrl.path) (\(counter))" + if !ext.isEmpty{ + path+=".\(ext)" + } + counter+=1 + }while FileManager.default.fileExists(atPath: path) + dest=URL(fileURLWithPath: path) + } + return dest + } + private func processIntroductionFrame(_ frame:Sharing_Nearby_Frame) throws{ guard frame.hasV1, frame.v1.hasIntroduction else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.introduction") } currentState = .waitingForUserConsent if frame.v1.introduction.fileMetadata.count>0 && frame.v1.introduction.textMetadata.isEmpty{ let downloadsDirectory=(try FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: true)).resolvingSymlinksInPath() for file in frame.v1.introduction.fileMetadata{ - var dest=downloadsDirectory.appendingPathComponent(file.name) - if FileManager.default.fileExists(atPath: dest.path){ - var counter=1 - var path:String - let ext=dest.pathExtension - let baseUrl=dest.deletingPathExtension() - repeat{ - path="\(baseUrl.path) (\(counter))" - if !ext.isEmpty{ - path+=".\(ext)" - } - counter+=1 - }while FileManager.default.fileExists(atPath: path) - dest=URL(fileURLWithPath: path) - } + let dest=makeFileDestinationURL(downloadsDirectory.appendingPathComponent(file.name)) let info=InternalFileInfo(meta: FileMetadata(name: file.name, size: file.size, mimeType: file.mimeType), payloadID: file.payloadID, destinationURL: dest) @@ -308,6 +323,18 @@ class InboundNearbyConnection: NearbyConnection{ DispatchQueue.main.async { self.delegate?.obtainUserConsent(for: metadata, from: self.remoteDeviceInfo!, connection: self) } + }else if case .text=meta.type{ + let downloadsDirectory=(try FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: true)).resolvingSymlinksInPath() + let dateFormatter=DateFormatter() + dateFormatter.dateFormat="yyyy-MM-dd HH.mm.ss" + let dest=makeFileDestinationURL(downloadsDirectory.appendingPathComponent("\(dateFormatter.string(from: Date())).txt")) + let info=InternalFileInfo(meta: FileMetadata(name: dest.lastPathComponent, size: meta.size, mimeType: "text/plain"), + payloadID: meta.payloadID, + destinationURL: dest) + transferredFiles[meta.payloadID]=info + DispatchQueue.main.async { + self.delegate?.obtainUserConsent(for: TransferMetadata(files: [info.meta], id: self.id, pinCode: self.pinCode), from: self.remoteDeviceInfo!, connection: self) + } }else{ rejectTransfer(with: .unsupportedAttachmentType) }