Skip to content

Commit

Permalink
[iOS] Don't percent encode file paths passed into path based file i/o
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed Aug 19, 2024
1 parent 7d20b69 commit 517c849
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ extension AsyncFileManager {
lock: Bool
) async throws {
let url = try url(for: .libraryDirectory, in: .userDomainMask).appending(path: path.value)
try await setAttributes([.posixPermissions: lock ? 0 : 0o755], ofItemAtPath: url.path())
try await setAttributes(
[.posixPermissions: lock ? 0 : 0o755],
ofItemAtPath: url.path(percentEncoded: false)
)
}

/// Returns whether or a given WebDataPath is currently locked
func isWebDataLocked(atPath path: WebDataPath) async throws -> Bool {
let url = try url(for: .libraryDirectory, in: .userDomainMask).appending(path: path.value)
let attributes = try await attributesOfItem(atPath: url.path())
let attributes = try await attributesOfItem(atPath: url.path(percentEncoded: false))
if let posixPermissions = attributes[.posixPermissions] as? NSNumber {
return posixPermissions == 0o755
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ class TemporaryDocument: NSObject {
try await AsyncFileManager.default.removeItem(at: url)
}

await AsyncFileManager.default.createFile(atPath: url.path(), contents: data)
await AsyncFileManager.default.createFile(
atPath: url.path(percentEncoded: false),
contents: data
)

localFileURL = url
pendingContinuation?.resume(returning: url)
Expand Down
6 changes: 3 additions & 3 deletions ios/brave-ios/Sources/BraveNews/Composer/FeedDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public class FeedDataSource: ObservableObject {
assert(!resource.isLocalized || localeIdentifier != nil)
let fileManager = FileManager.default
let filename = resourceFilename(for: resource, localeIdentifier: localeIdentifier)
let cachedPath = cacheDirectoryURL?.appending(path: filename).path()
let cachedPath = cacheDirectoryURL?.appending(path: filename).path(percentEncoded: false)
if let cachedPath = cachedPath,
let attributes = try? fileManager.attributesOfItem(atPath: cachedPath),
let date = attributes[.modificationDate] as? Date
Expand Down Expand Up @@ -337,7 +337,7 @@ public class FeedDataSource: ObservableObject {
for: .applicationSupportDirectory,
appending: Self.cacheFolderName,
create: true
).appending(path: name).path()
).appending(path: name).path(percentEncoded: false)
if loadExpiredData || !isResourceExpired(resource, localeIdentifier: localeIdentifier),
let cachedPath = cachedPath,
await fileManager.fileExists(atPath: cachedPath)
Expand Down Expand Up @@ -391,7 +391,7 @@ public class FeedDataSource: ObservableObject {
create: true
)
let createdFile = await AsyncFileManager.default.createFile(
atPath: cachePath.appending(path: filename).path(),
atPath: cachePath.appending(path: filename).path(percentEncoded: false),
contents: data
)
if !createdFile {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ struct OPMLImporterViewModifier: ViewModifier {
}

nonisolated private func importOPML(from url: URL) async {
guard url.isFileURL, let data = await AsyncFileManager.default.contents(atPath: url.path())
guard url.isFileURL,
let data = await AsyncFileManager.default.contents(atPath: url.path(percentEncoded: false))
else {
isPresented = false
importError = .noFeedsFound
Expand Down
4 changes: 2 additions & 2 deletions ios/brave-ios/Sources/BraveShared/AsyncFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ extension AsyncFileManager {
create: false
)
var directory = searchPathDirectory.appending(path: pathComponent)
let exists = await fileExists(atPath: directory.path())
let exists = await fileExists(atPath: directory.path(percentEncoded: false))
if !exists && create {
try await createDirectory(at: directory, withIntermediateDirectories: true)
if excludeFromBackups {
Expand Down Expand Up @@ -318,7 +318,7 @@ extension AsyncFileManager {
public func utf8Contents(
at url: URL
) async -> String? {
guard let data = fileManager.contents(atPath: url.path()) else {
guard let data = fileManager.contents(atPath: url.path(percentEncoded: false)) else {
return nil
}
return String(decoding: data, as: UTF8.self)
Expand Down

0 comments on commit 517c849

Please sign in to comment.