Thank you for your reply. In your inspiration, I did a test in my code. Here are my results.
I am using let options:[NSURLBookmarkCreationOptions] = [ NSURLBookmarkCreationOptions(rawValue: 0), .MinimalBookmark, .SuitableForBookmarkFile ]
1 with option 0, the bookmark even a little smaller than the .MinimalBookmark option.
2 I can not get a file alias from option other than .SuitableForBookmark
3 the bookmark data on option .SuitableForBookmarkFile is almost as big as a file alias.
4 although you can not get a file alias directly from other option, you can still get then from bookmark to NSURL, then re-get a bookmark with .SuitableForBookmark option, then get the alias.
do {
try bookmark.writeToURL(newURL, options:.DataWritingAtomic)
try NSURL.writeBookmarkData(bookmark, toURL: aliasURL, options: NSURLBookmarkFileCreationOptions(0))
}
catch let error as NSError {
print(error.localizedDescription)
var isStale:ObjCBool = false
let newURL = try! NSURL(byResolvingBookmarkData: bookmark, options: .WithoutUI, relativeToURL: nil, bookmarkDataIsStale: &isStale)
if !isStale.boolValue {
let newBookmark = try! newURL.bookmarkDataWithOptions(.SuitableForBookmarkFile, includingResourceValuesForKeys: nil, relativeToURL: nil)
try! NSURL.writeBookmarkData(newBookmark, toURL: aliasURL, options: NSURLBookmarkFileCreationOptions(0))
}
}
My conclusion: You should always use option 0 or 512(.MinimalBookmark). They are small and fast. When you need to do an alias, you can restore the url from bookmark in your database, then get a full bookmark that can write to an alias file.
Here are the files I am testing.
-rw-r--r-- 1 zhaoxin staff 4433412850 7 23 22:00 1.mkv
-rw-r--r-- 1 zhaoxin staff 807974265 8 1 09:09 2.mkv
-rw-r--r--@ 1 zhaoxin staff 7330292 8 2 10:31 3.mov
Here is my testing code.
func bookMarkTest(url:NSURL) {
let basePath = "/Users/zhaoxin/Desktop/test"
let baseURL = NSURL(fileURLWithPath: basePath)
let options:[NSURLBookmarkCreationOptions] = [ NSURLBookmarkCreationOptions(rawValue: 0), .MinimalBookmark, .SuitableForBookmarkFile ]
for anOption in options {
let bookmark = try! url.bookmarkDataWithOptions(anOption, includingResourceValuesForKeys: nil, relativeToURL: nil)
let fileName = url.lastPathComponent!
let newName = fileName.stringByDeletingPathExtension + "." + anOption.rawValue.description + ".bookmark"
let newURL = NSURL(fileURLWithPath: newName, relativeToURL: baseURL)
let aliasName = fileName.stringByDeletingPathExtension + "." + anOption.rawValue.description + "." + url.pathExtension!
let aliasURL = NSURL(fileURLWithPath: aliasName, relativeToURL: baseURL)
do {
try bookmark.writeToURL(newURL, options:.DataWritingAtomic)
try NSURL.writeBookmarkData(bookmark, toURL: aliasURL, options: NSURLBookmarkFileCreationOptions(0))
}
catch let error as NSError {
print(error.localizedDescription)
}
}
}
func testSuit(path:String) {
let url = NSURL(fileURLWithPath: path)
bookMarkTest(url)
}
let paths = [
"/Users/zhaoxin/Desktop/test/1.mkv",
"/Users/zhaoxin/Desktop/test/2.mkv",
"/Users/zhaoxin/Desktop/test/3.mov"
]
for aPath in paths {
testSuit(aPath)
}
Results(without re-get the bookmark)
-rw-r--r-- 1 zhaoxin staff 904 8 2 10:50 1.0.bookmark
-rw-r--r-- 1 zhaoxin staff 3341788 8 2 10:50 1.1024.bookmark
-rw-r--r--@ 1 zhaoxin staff 3341796 8 2 10:50 1.1024.mkv
-rw-r--r-- 1 zhaoxin staff 928 8 2 10:50 1.512.bookmark
-rw-r--r-- 1 zhaoxin staff 4433412850 7 23 22:00 1.mkv
-rw-r--r-- 1 zhaoxin staff 904 8 2 10:50 2.0.bookmark
-rw-r--r-- 1 zhaoxin staff 3341788 8 2 10:50 2.1024.bookmark
-rw-r--r--@ 1 zhaoxin staff 3341796 8 2 10:50 2.1024.mkv
-rw-r--r-- 1 zhaoxin staff 928 8 2 10:50 2.512.bookmark
-rw-r--r-- 1 zhaoxin staff 807974265 8 1 09:09 2.mkv
-rw-r--r-- 1 zhaoxin staff 904 8 2 10:50 3.0.bookmark
-rw-r--r-- 1 zhaoxin staff 673188 8 2 10:50 3.1024.bookmark
-rw-r--r--@ 1 zhaoxin staff 673196 8 2 10:50 3.1024.mov
-rw-r--r-- 1 zhaoxin staff 928 8 2 10:50 3.512.bookmark
-rw-r--r--@ 1 zhaoxin staff 7330292 8 2 10:31 3.mov