Allow receiving text into .txt files

closes #166, closes #147, closes #112, closes #178, closes #123, closes #110
This commit is contained in:
Grishka
2024-09-22 03:12:42 +03:00
parent 8157c925d7
commit bb994dbd9e
2 changed files with 46 additions and 19 deletions

View File

@@ -839,7 +839,7 @@
CODE_SIGN_ENTITLEMENTS = NearDrop/NearDrop.entitlements; CODE_SIGN_ENTITLEMENTS = NearDrop/NearDrop.entitlements;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES; COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 8; CURRENT_PROJECT_VERSION = 9;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities"; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_LSUIElement = YES; INFOPLIST_KEY_LSUIElement = YES;
@@ -851,7 +851,7 @@
"@executable_path/../Frameworks", "@executable_path/../Frameworks",
); );
MACOSX_DEPLOYMENT_TARGET = 10.15; MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 2.0.4; MARKETING_VERSION = 2.1.0;
PRODUCT_BUNDLE_IDENTIFIER = me.grishka.NearDrop; PRODUCT_BUNDLE_IDENTIFIER = me.grishka.NearDrop;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_EMIT_LOC_STRINGS = YES;
@@ -871,7 +871,7 @@
CODE_SIGN_ENTITLEMENTS = NearDrop/NearDrop.entitlements; CODE_SIGN_ENTITLEMENTS = NearDrop/NearDrop.entitlements;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES; COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 8; CURRENT_PROJECT_VERSION = 9;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities"; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_LSUIElement = YES; INFOPLIST_KEY_LSUIElement = YES;
@@ -883,7 +883,7 @@
"@executable_path/../Frameworks", "@executable_path/../Frameworks",
); );
MACOSX_DEPLOYMENT_TARGET = 10.15; MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 2.0.4; MARKETING_VERSION = 2.1.0;
PRODUCT_BUNDLE_IDENTIFIER = me.grishka.NearDrop; PRODUCT_BUNDLE_IDENTIFIER = me.grishka.NearDrop;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_EMIT_LOC_STRINGS = YES;

View File

@@ -124,6 +124,16 @@ class InboundNearbyConnection: NearbyConnection{
} }
try sendDisconnectionAndDisconnect() try sendDisconnectionAndDisconnect()
return true 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 return false
} }
@@ -270,13 +280,8 @@ class InboundNearbyConnection: NearbyConnection{
currentState = .receivedPairedKeyResult currentState = .receivedPairedKeyResult
} }
private func processIntroductionFrame(_ frame:Sharing_Nearby_Frame) throws{ private func makeFileDestinationURL(_ initialDest:URL) -> URL{
guard frame.hasV1, frame.v1.hasIntroduction else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.introduction") } var dest=initialDest
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){ if FileManager.default.fileExists(atPath: dest.path){
var counter=1 var counter=1
var path:String var path:String
@@ -291,6 +296,16 @@ class InboundNearbyConnection: NearbyConnection{
}while FileManager.default.fileExists(atPath: path) }while FileManager.default.fileExists(atPath: path)
dest=URL(fileURLWithPath: 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{
let dest=makeFileDestinationURL(downloadsDirectory.appendingPathComponent(file.name))
let info=InternalFileInfo(meta: FileMetadata(name: file.name, size: file.size, mimeType: file.mimeType), let info=InternalFileInfo(meta: FileMetadata(name: file.name, size: file.size, mimeType: file.mimeType),
payloadID: file.payloadID, payloadID: file.payloadID,
destinationURL: dest) destinationURL: dest)
@@ -308,6 +323,18 @@ class InboundNearbyConnection: NearbyConnection{
DispatchQueue.main.async { DispatchQueue.main.async {
self.delegate?.obtainUserConsent(for: metadata, from: self.remoteDeviceInfo!, connection: self) 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{ }else{
rejectTransfer(with: .unsupportedAttachmentType) rejectTransfer(with: .unsupportedAttachmentType)
} }