/
URL+Ext.swift
66 lines (59 loc) · 2.09 KB
/
URL+Ext.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// URL+Ext.swift
// ImageX
//
// Created by Condy on 2023/7/7.
//
import Foundation
import Harbeth
extension URL: ImageXCompatible { }
extension ImageXEngine where URL == Base {
/// Determine if this represents an image file stored in a baked.
public var isBaked: Bool {
let bakedImageFileExtensions = Set([
"jpg", "jpeg", "png", "tiff", "tif", "gif", "heic", "heif"
])
return bakedImageFileExtensions.contains(base.pathExtension.lowercased())
}
/// Set extended attribute.
func setExtendedAttribute(data: Data, forName name: String) throws {
try base.withUnsafeFileSystemRepresentation { fileSystemPath in
let result = data.withUnsafeBytes {
setxattr(fileSystemPath, name, $0.baseAddress, data.count, 0, 0)
}
guard result >= 0 else {
throw URL.posixError(errno)
}
}
}
/// Get extended attribute.
func extendedAttribute(forName name: String) throws -> Data {
let data = try base.withUnsafeFileSystemRepresentation { fileSystemPath -> Data in
// Determine attribute size:
let length = getxattr(fileSystemPath, name, nil, 0, 0, 0)
guard length >= 0 else {
throw URL.posixError(errno)
}
// Create buffer with required size:
var data = Data(count: length)
// Retrieve attribute:
let result = data.withUnsafeMutableBytes { [count = data.count] in
getxattr(fileSystemPath, name, $0.baseAddress, count, 0, 0)
}
guard result >= 0 else {
throw URL.posixError(errno)
}
return data
}
return data
}
}
extension URL {
/// Helper function to create an NSError from a Unix errno.
fileprivate static func posixError(_ err: Int32) -> NSError {
let userInfo = [
NSLocalizedDescriptionKey: String(cString: strerror(err))
]
return NSError(domain: NSPOSIXErrorDomain, code: Int(err), userInfo: userInfo)
}
}