Skip to content

Commit

Permalink
Update for iOS 18.2 RC
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon <[email protected]>
  • Loading branch information
Brandon-T committed Dec 11, 2024
1 parent b51b8fe commit c21ba1c
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class BookmarksImportExportUtility {
}
}

guard let nativePath = BookmarksImportExportUtility.nativeURLPathFromURL(path) else {
guard let nativePath = path.fileSystemRepresentation else {
Logger.module.error("Bookmarks Import - Invalid FileSystem Path")
return false
}
Expand All @@ -97,55 +97,32 @@ class BookmarksImportExportUtility {
defer { state = .none }

if path.pathExtension.lowercased() == "zip" {
guard
let importsPath = try? await uniqueFileName(
"SafariImports",
folder: AsyncFileManager.default.temporaryDirectory
)
else {
return false
}

guard let nativeImportsPath = BookmarksImportExportUtility.nativeURLPathFromURL(importsPath)
else {
return false
}
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }

do {
try await AsyncFileManager.default.createDirectory(
at: importsPath,
withIntermediateDirectories: true
)
} catch {
guard let zipFileExtractedURL = try? await ZipImporter.unzip(path: path) else {
return false
}

defer {
Task {
try await AsyncFileManager.default.removeItem(at: importsPath)
try await AsyncFileManager.default.removeItem(at: zipFileExtractedURL)
}
}

if await Unzip.unzip(nativePath, toDirectory: nativeImportsPath) {
let bookmarksFileURL = importsPath.appending(path: "Bookmarks").appendingPathExtension(
let bookmarksFileURL = zipFileExtractedURL.appending(path: "Bookmarks")
.appendingPathExtension(
"html"
)
guard
let nativeBookmarksPath = BookmarksImportExportUtility.nativeURLPathFromURL(
bookmarksFileURL
)
else {
Logger.module.error("Bookmarks Import - Invalid FileSystem Path")
return false
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }
return await doImport(bookmarksFileURL, nativeBookmarksPath)
guard
let nativeBookmarksPath = bookmarksFileURL.fileSystemRepresentation
else {
Logger.module.error("Bookmarks Import - Invalid FileSystem Path")
return false
}

return false
return await doImport(bookmarksFileURL, nativeBookmarksPath)
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
Expand All @@ -162,7 +139,7 @@ class BookmarksImportExportUtility {
"Bookmarks Import - Error Exporting while an Import/Export operation is in progress"
)

guard let nativePath = BookmarksImportExportUtility.nativeURLPathFromURL(path) else {
guard let nativePath = path.fileSystemRepresentation else {
Logger.module.error("Bookmarks Export - Invalid FileSystem Path")
return false
}
Expand Down Expand Up @@ -206,33 +183,3 @@ class BookmarksImportExportUtility {
case none
}
}

// MARK: - Parsing
extension BookmarksImportExportUtility {
static func nativeURLPathFromURL(_ url: URL) -> String? {
return url.withUnsafeFileSystemRepresentation { bytes -> String? in
guard let bytes = bytes else { return nil }
return String(cString: bytes)
}
}

func uniqueFileName(_ filename: String, folder: URL) async throws -> URL {
let basePath = folder.appending(path: filename)
let fileExtension = basePath.pathExtension
let filenameWithoutExtension =
!fileExtension.isEmpty ? String(filename.dropLast(fileExtension.count + 1)) : filename

var proposedPath = basePath
var count = 0

while await AsyncFileManager.default.fileExists(atPath: proposedPath.path) {
count += 1

let proposedFilenameWithoutExtension = "\(filenameWithoutExtension) (\(count))"
proposedPath = folder.appending(path: proposedFilenameWithoutExtension)
.appending(path: fileExtension)
}

return proposedPath
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class HistoryImportExportUtility {
}
}

guard let nativePath = HistoryImportExportUtility.nativeURLPathFromURL(path) else {
guard let nativePath = path.fileSystemRepresentation else {
Logger.module.error("History Import - Invalid FileSystem Path")
return false
}
Expand All @@ -61,51 +61,31 @@ class HistoryImportExportUtility {
defer { state = .none }

if path.pathExtension.lowercased() == "zip" {
guard
let importsPath = try? await uniqueFileName(
"SafariImports",
folder: AsyncFileManager.default.temporaryDirectory
)
else {
return false
}
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }

guard let nativeImportsPath = HistoryImportExportUtility.nativeURLPathFromURL(importsPath)
else {
return false
}

do {
try await AsyncFileManager.default.createDirectory(
at: importsPath,
withIntermediateDirectories: true
)
} catch {
guard let zipFileExtractedURL = try? await ZipImporter.unzip(path: path) else {
return false
}

defer {
Task {
try await AsyncFileManager.default.removeItem(at: importsPath)
try await AsyncFileManager.default.removeItem(at: zipFileExtractedURL)
}
}

if await Unzip.unzip(nativePath, toDirectory: nativeImportsPath) {
let historyFileURL = importsPath.appending(path: "History").appendingPathExtension("json")
guard
let nativeHistoryPath = HistoryImportExportUtility.nativeURLPathFromURL(historyFileURL)
else {
Logger.module.error("History Import - Invalid FileSystem Path")
return false
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }
return await doImport(historyFileURL, nativeHistoryPath)
let historyFileURL = zipFileExtractedURL.appending(path: "History").appendingPathExtension(
"json"
)
guard
let nativeHistoryPath = historyFileURL.fileSystemRepresentation
else {
Logger.module.error("History Import - Invalid FileSystem Path")
return false
}

return false
return await doImport(historyFileURL, nativeHistoryPath)
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
Expand All @@ -124,33 +104,3 @@ class HistoryImportExportUtility {
case none
}
}

// MARK: - Parsing
extension HistoryImportExportUtility {
static func nativeURLPathFromURL(_ url: URL) -> String? {
return url.withUnsafeFileSystemRepresentation { bytes -> String? in
guard let bytes = bytes else { return nil }
return String(cString: bytes)
}
}

func uniqueFileName(_ filename: String, folder: URL) async throws -> URL {
let basePath = folder.appending(path: filename)
let fileExtension = basePath.pathExtension
let filenameWithoutExtension =
!fileExtension.isEmpty ? String(filename.dropLast(fileExtension.count + 1)) : filename

var proposedPath = basePath
var count = 0

while await AsyncFileManager.default.fileExists(atPath: proposedPath.path) {
count += 1

let proposedFilenameWithoutExtension = "\(filenameWithoutExtension) (\(count))"
proposedPath = folder.appending(path: proposedFilenameWithoutExtension)
.appending(path: fileExtension)
}

return proposedPath
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PasswordsImportExportUtility {
}
}

guard let nativePath = PasswordsImportExportUtility.nativeURLPathFromURL(path) else {
guard let nativePath = path.fileSystemRepresentation else {
Logger.module.error("Passwords Import - Invalid FileSystem Path")
return false
}
Expand All @@ -51,55 +51,33 @@ class PasswordsImportExportUtility {
defer { state = .none }

if path.pathExtension.lowercased() == "zip" {
guard
let importsPath = try? await uniqueFileName(
"SafariImports",
folder: AsyncFileManager.default.temporaryDirectory
)
else {
return false
}
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }

guard let nativeImportsPath = PasswordsImportExportUtility.nativeURLPathFromURL(importsPath)
else {
return false
}

do {
try await AsyncFileManager.default.createDirectory(
at: importsPath,
withIntermediateDirectories: true
)
} catch {
guard let zipFileExtractedURL = try? await ZipImporter.unzip(path: path) else {
return false
}

defer {
Task {
try await AsyncFileManager.default.removeItem(at: importsPath)
try await AsyncFileManager.default.removeItem(at: zipFileExtractedURL)
}
}

if await Unzip.unzip(nativePath, toDirectory: nativeImportsPath) {
let passwordsFileURL = importsPath.appending(path: "Passwords").appendingPathExtension(
let passwordsFileURL = zipFileExtractedURL.appending(path: "Passwords")
.appendingPathExtension(
"csv"
)
guard
let nativePasswordsPath = PasswordsImportExportUtility.nativeURLPathFromURL(
passwordsFileURL
)
else {
Logger.module.error("Passwords Import - Invalid FileSystem Path")
return false
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }
return await doImport(passwordsFileURL, nativePasswordsPath)
guard
let nativePasswordsPath = passwordsFileURL.fileSystemRepresentation
else {
Logger.module.error("Passwords Import - Invalid FileSystem Path")
return false
}

return false
return await doImport(passwordsFileURL, nativePasswordsPath)
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
Expand All @@ -118,33 +96,3 @@ class PasswordsImportExportUtility {
case none
}
}

// MARK: - Parsing
extension PasswordsImportExportUtility {
static func nativeURLPathFromURL(_ url: URL) -> String? {
return url.withUnsafeFileSystemRepresentation { bytes -> String? in
guard let bytes = bytes else { return nil }
return String(cString: bytes)
}
}

func uniqueFileName(_ filename: String, folder: URL) async throws -> URL {
let basePath = folder.appending(path: filename)
let fileExtension = basePath.pathExtension
let filenameWithoutExtension =
!fileExtension.isEmpty ? String(filename.dropLast(fileExtension.count + 1)) : filename

var proposedPath = basePath
var count = 0

while await AsyncFileManager.default.fileExists(atPath: proposedPath.path) {
count += 1

let proposedFilenameWithoutExtension = "\(filenameWithoutExtension) (\(count))"
proposedPath = folder.appending(path: proposedFilenameWithoutExtension)
.appending(path: fileExtension)
}

return proposedPath
}
}
Loading

0 comments on commit c21ba1c

Please sign in to comment.