Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 45 additions & 59 deletions Sources/PowerSync/attachments/FileManagerLocalStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,85 +13,71 @@ public actor FileManagerStorageAdapter: LocalStorageAdapter {
}

public func saveFile(filePath: String, data: Data) async throws -> Int64 {
return try await Task {
let url = URL(fileURLWithPath: filePath)
let url = URL(fileURLWithPath: filePath)

// Make sure the parent directory exists
try fileManager.createDirectory(at: url.deletingLastPathComponent(),
withIntermediateDirectories: true)
// Make sure the parent directory exists
try fileManager.createDirectory(at: url.deletingLastPathComponent(),
withIntermediateDirectories: true)

// Write data to file
try data.write(to: url)
// Write data to file
try data.write(to: url)

// Return the size of the data
return Int64(data.count)
}.value
// Return the size of the data
return Int64(data.count)
}

public func readFile(filePath: String, mediaType _: String?) async throws -> Data {
return try await Task {
let url = URL(fileURLWithPath: filePath)

if !fileManager.fileExists(atPath: filePath) {
throw PowerSyncAttachmentError.fileNotFound(filePath)
}

// Read data from file
do {
return try Data(contentsOf: url)
} catch {
throw PowerSyncAttachmentError.ioError(error)
}
}.value
let url = URL(fileURLWithPath: filePath)

if !fileManager.fileExists(atPath: filePath) {
throw PowerSyncAttachmentError.fileNotFound(filePath)
}

// Read data from file
do {
return try Data(contentsOf: url)
} catch {
throw PowerSyncAttachmentError.ioError(error)
}
}

public func deleteFile(filePath: String) async throws {
try await Task {
if fileManager.fileExists(atPath: filePath) {
try fileManager.removeItem(atPath: filePath)
}
}.value
if fileManager.fileExists(atPath: filePath) {
try fileManager.removeItem(atPath: filePath)
}
}

public func fileExists(filePath: String) async throws -> Bool {
return await Task {
fileManager.fileExists(atPath: filePath)
}.value
return fileManager.fileExists(atPath: filePath)
}

public func makeDir(path: String) async throws {
try await Task {
try fileManager.createDirectory(atPath: path,
withIntermediateDirectories: true,
attributes: nil)
}.value
try fileManager.createDirectory(atPath: path,
withIntermediateDirectories: true,
attributes: nil)
}

public func rmDir(path: String) async throws {
try await Task {
if fileManager.fileExists(atPath: path) {
try fileManager.removeItem(atPath: path)
}
}.value
if fileManager.fileExists(atPath: path) {
try fileManager.removeItem(atPath: path)
}
}

public func copyFile(sourcePath: String, targetPath: String) async throws {
try await Task {
if !fileManager.fileExists(atPath: sourcePath) {
throw PowerSyncAttachmentError.fileNotFound(sourcePath)
}

// Ensure target directory exists
let targetUrl = URL(fileURLWithPath: targetPath)
try fileManager.createDirectory(at: targetUrl.deletingLastPathComponent(),
withIntermediateDirectories: true)

// If target already exists, remove it first
if fileManager.fileExists(atPath: targetPath) {
try fileManager.removeItem(atPath: targetPath)
}

try fileManager.copyItem(atPath: sourcePath, toPath: targetPath)
}.value
if !fileManager.fileExists(atPath: sourcePath) {
throw PowerSyncAttachmentError.fileNotFound(sourcePath)
}

// Ensure target directory exists
let targetUrl = URL(fileURLWithPath: targetPath)
try fileManager.createDirectory(at: targetUrl.deletingLastPathComponent(),
withIntermediateDirectories: true)

// If target already exists, remove it first
if fileManager.fileExists(atPath: targetPath) {
try fileManager.removeItem(atPath: targetPath)
}

try fileManager.copyItem(atPath: sourcePath, toPath: targetPath)
}
}