Skip to content

Commit

Permalink
Allow receiving text into .txt files
Browse files Browse the repository at this point in the history
closes #166, closes #147, closes #112, closes #178, closes #123, closes #110
  • Loading branch information
grishka committed Sep 22, 2024
1 parent 8157c92 commit bb994db
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
8 changes: 4 additions & 4 deletions NearDrop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
57 changes: 42 additions & 15 deletions NearbyShare/InboundNearbyConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit bb994db

Please sign in to comment.