Substitute user path with tilde in URL - swift

Assume a file URL containing /Users/me/a/b. Is there a better method than a naive string replace using NSHomeDirectory() to get the short form ~/a/b?
This is what I'm currently using
.replacingOccurrences(of: NSHomeDirectory(), with: "~", options: .anchored, range: nil)
PS: No NSString casting!

If you are in Sandboxed app then usage of getpwuid function is needed.
extension FileManager {
/// Returns path to real home directory in Sandboxed application
public var realHomeDirectory: String? {
if let home = getpwuid(getuid()).pointee.pw_dir {
return string(withFileSystemRepresentation: home, length: Int(strlen(home)))
}
return nil
}
}
Example usage:
func configure(url: URL) {
...
var directory = url.path.deletingLastPathComponent
toolTip = url.path
if let homeDir = FileManager.default.realHomeDirectory {
// FYI: `url.path.abbreviatingWithTildeInPath` not working for sandboxed apps.
let abbreviatedPath = url.path.replacingFirstOccurrence(of: homeDir, with: "~")
directory = abbreviatedPath.deletingLastPathComponent
toolTip = abbreviatedPath
}
directoryLabel.text = directory
}
More about getpwuid function: How do I get the users home directory in a sandboxed app?
Result of usage in NSView:

How about converting to NSString and then abbreviating it with:
var path = url.path as NSString!
var abbrevPath=path?.abbreviatingWithTildeInPath

NSPathUtilities
- (NSString *)stringByAbbreviatingWithTildeInPath;
- (NSString *)stringByExpandingTildeInPath;
[blueprintsDict writeToFile: [[NSString stringWithFormat:#"~/MbPatchBlueprints.plist"] stringByExpandingTildeInPath] atomically:YES];

I noticed this question doesn't have an accepted answer yet, and I ran into the same issue. I tried the NSString methods, but those didn't work in a sandboxed context anyway, and the getpwuid method felt… wrong. So here's a pure Swift solution that's only really hardcoded insofar as it assumes the sandbox root to share the first 3 path components (/, Users, your username) with your actual home directory:
extension URL {
var pathAbbreviatingWithTilde: String {
// find home directory path (more difficulty because we're sandboxed, so it's somewhere deep in our actual home dir)
let sandboxedHomeDir = FileManager.default.homeDirectoryForCurrentUser
let components = sandboxedHomeDir.pathComponents
guard components.first == "/" else { return path }
let homeDir = "/" + components.dropFirst().prefix(2).joined(separator: "/")
// replace home dir in path with tilde for brevity and aesthetics
guard path.hasPrefix(homeDir) else { return path }
return "~" + path.dropFirst(homeDir.count)
}
}

Related

Get path of a file in a data set located in Assets.xcassets

I have a data set of audio files in my Assets.xcassets:
I'm trying to get the path of one of those audio files like this:
let path: String = Bundle.main.path(forResource: "acoustic_grand_piano/A4", ofType: "f32")!
But I get a EXC_BAD_INSTRUCTION. I tried to look on the internet but I don't find anything on Data Sets.
How can I get the content of one of these files?
Thanks!
Try this:
Manually put your files into a folder, named anything you want.
Append ".bundle" to the folder to create a bundle. You'll get a warning, accept it. Congrats, you've just created your first bundle! :-)
Manually drag that folder into your app.
Get at your files by using the following code....
public func returnFile(_ named:String) -> String {
let path: String = Bundle.main.path(forResource: "myAudioFiles", ofType: "bundle")! + "/" + name + ".f32"
do {
return try String(contentsOfFile: path)
}
catch let error as NSError {
return error.description
}
}
Now, my files are text files of CIKernel code. Since your's are audio files you may need to change the String return to something else.
EDIT:
In my case I'm using a framework, as I wish to share these files/images with extensions and other apps. If you are working in such a set up, here's the unaltered code:
public func returnFile(_ resource:String, _ fileName:String, _ fileType:String) -> String {
let identifier = "com.companyname.appname" // replace with framework bundle identifier
let fileBundle = Bundle.init(identifier: identifier)
let filePath = (fileBundle?.path(forResource: resource, ofType: "bundle"))! + "/" + fileName + "." + fileType
do {
return try String(contentsOfFile: filePath)
}
catch let error as NSError {
return error.description
}
}

fileExistsAtPath check for filename?

How to check whether there is a file in a directory with only the name without extension? Now the files are written in my directory, their name will be generated from the id file. Accordingly, when I'm looking for a file, let file = "\ (fileId) .pdf", in the directory it is, but if no extension, it will not be found. Either return as easier extension from the server?
public var isDownloaded: Bool {
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.URLByAppendingPathComponent("\(fileMessageModel.attachment.id)")!.path!
let fileManager = NSFileManager.defaultManager()
return fileManager.fileExistsAtPath(filePath)
}
enumeratorAtPath creates a deep enumerator -- i.e. it will scan contents of subfolders and their subfolders too. For a shallow search, user contentOfDirectortAtPath:
func file(fileName: String, existsAt path: String) -> Bool {
var isFound = false
if let pathContents = try? NSFileManager.defaultManager().contentsOfDirectoryAtPath(path) {
pathContents.forEach { file in
if (file as NSString).stringByDeletingPathExtension == fileName {
isFound = true
return
}
}
}
return isFound
}
if let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
if file("something", existsAt: path) {
// The file exists, do something about it
}
}
What about iterating over the files in the directory and testing the name with extension excluded?
let filemanager:FileManager = FileManager()
let files = filemanager.enumeratorAtPath(/* your directory path */)
while let file = files?.nextObject() {
// Remove file name extension
// Do file name comparison here
}
In terms of time complexity is will be O(n), however, as long as there are not too many files, you are good to go. On the other hand, if there are many files, you will need to consider a more efficient way to traverse, may be a trie data structure consisted of all file names in that directory.

Swift: How to expand a tilde in a path String

How can I expand a path String with a tilde in Swift? I have a string like "~/Desktop" and I'd like to use this path with the NSFileManager methods, which requires the tilde to be expanded to "/Users/<myuser>/Desktop".
(This question with a clear problem statement doesn't exist yet, this should be easily findable. Some similar but not satisfying questions are Can not make path to the file in Swift, Simple way to read local file using Swift?, Tilde-based Paths in Objective-C)
Tilde expansion
Swift 1
"~/Desktop".stringByExpandingTildeInPath
Swift 2
NSString(string: "~/Desktop").stringByExpandingTildeInPath
Swift 3
NSString(string: "~/Desktop").expandingTildeInPath
Home Directory
Additionally you can get the home directory like this (returns a String/String?):
NSHomeDirectory()
NSHomeDirectoryForUser("<User>")
In Swift 3 and OS X 10.12 it's also possible to use this (returns a URL/URL?):
FileManager.default().homeDirectoryForCurrentUser
FileManager.default().homeDirectory(forUser: "<User>")
Edit: In Swift 3.1 this got changed to FileManager.default.homeDirectoryForCurrentUser
Return string:
func expandingTildeInPath(_ path: String) -> String {
return path.replacingOccurrences(of: "~", with: FileManager.default.homeDirectoryForCurrentUser.path)
}
Return URL:
func expandingTildeInPath(_ path: String) -> URL {
return URL(fileURLWithPath: path.replacingOccurrences(of: "~", with: FileManager.default.homeDirectoryForCurrentUser.path))
}
If OS less than 10.12, replace
FileManager.default.homeDirectoryForCurrentUser
with
URL(fileURLWithPath: NSHomeDirectory()
Here is a solution that does not depend on the NSString class and works with Swift 4:
func absURL ( _ path: String ) -> URL {
guard path != "~" else {
return FileManager.default.homeDirectoryForCurrentUser
}
guard path.hasPrefix("~/") else { return URL(fileURLWithPath: path) }
var relativePath = path
relativePath.removeFirst(2)
return URL(fileURLWithPath: relativePath,
relativeTo: FileManager.default.homeDirectoryForCurrentUser
)
}
func absPath ( _ path: String ) -> String {
return absURL(path).path
}
Test code:
print("Path: \(absPath("~"))")
print("Path: \(absPath("/tmp/text.txt"))")
print("Path: \(absPath("~/Documents/text.txt"))")
The reason for splitting the code into two methods is that nowadays you rather want URLs when working with files and folders and not string paths (all new APIs use URLs for paths).
By the way, if you just want to know the absolute path of ~/Desktop or ~/Documents and similar folders, there's an even easier way for that:
let desktop = FileManager.default.urls(
for: .desktopDirectory, in: .userDomainMask
)[0]
print("Desktop: \(desktop.path)")
let documents = FileManager.default.urls(
for: .documentDirectory, in: .userDomainMask
)[0]
print("Documents: \(documents.path)")
Swift 4 Extension
public extension String {
public var expandingTildeInPath: String {
return NSString(string: self).expandingTildeInPath
}
}

How to handle symlinks when reading data from a file path in swift

I have a file path as a string. I want to:
Test if there's a file there
Read the contents of the file as a string
the problem I'm having is that sometimes that file path involves a symbolic link (symlink). Maybe to the file itself. Maybe to one of the directories above the file.
[EDIT] closing this because the following code (that I started with), actually works just fine, there were just multiple levels of user error involved. Thanks for the input folks.
func getUserResource(relativeFilePath: String) -> String? {
let fileManager = NSFileManager.defaultManager()
let userFilePath = NSHomeDirectory() + relativeFilePath
if(fileManager.fileExistsAtPath(userFilePath))
{
do {
return try String(contentsOfFile: userFilePath, encoding: NSUTF8StringEncoding);
} catch {
return nil;
}
}
return nil;
}
If you're not sure if the symlink leads to a file or directory, you should be using fileExistsAtPath(path:, isDirectory:). fileExistsAtPath will always return true for a symlink, because technically there is a file at that path. By passing a boolean pointer to isDirectory, you can follow the symlink to a file or to a directory:
Assume symlinkToSomeFile is a symbolic link to a file and symlinkToSomeDir is a symbolic link to a directory.
let symlinkFilePath = NSHomeDirectory() + "/temp/symlinkToSomeFile"
let symlinkDirPath = NSHomeDirectory() + "/temp/symlinkToSomeDir"
var fileCheck: ObjCBool = false
var dirCheck: ObjCBool = false
print(fileManager.fileExistsAtPath(symlinkFilePath, isDirectory: &fileCheck)) // true
print(fileCheck) // false
print(fileManager.fileExistsAtPath(symlinkDirPath, isDirectory: &dirCheck)) // true
print(dirCheck) // true

I cant read my text files from my application's Bundle

I used to read the text files from my application's bundle by using the following code. However, no matter what my application can't find them anymore. I am 100% sure that all my files are in the Assets.xcassets, I can see them, edit them, transform them from a directory to another. But my application doesn't want to read them, please tell me what I missed!!
this is the procedure I am using...
func readBundle(file:String) -> String
{
var res: String = ""
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "txt")
{
let fm = NSFileManager()
let exists = fm.fileExistsAtPath(path)
if(exists)
{
let c = fm.contentsAtPath(path)
res = NSString(data: c!, encoding: NSUTF8StringEncoding) as! String
}
}
return res
}
I am using it like this:
let res = readBundle("test")
print(res)
when storing non image files in XCAssets, you should use NSDataAsset to acccess their content
https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSDataAsset_Class/
func readBundle(file:String) -> String
{
var res = ""
if let asset = NSDataAsset(name: file) ,
string = String(data:asset.data, encoding: NSUTF8StringEncoding){
res = string
}
return res
}
In the another option then 'XCAssets' you can create a separate folder/group of your resources other than images in the project structure, check if they exist in the Copy Bundle Resource in the Build phases section of your project's main target
If you add resource like this your current code should work as it is
func readBundle(file:String) -> String
{
var res: String = ""
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "txt")
{
//you should be able to get the path
//other code as you has written in the question
}
return res
}