Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
import Foundation
|
|
|
|
|
import SMBClient
|
2026-05-18 16:07:41 +03:00
|
|
|
import os.log
|
|
|
|
|
|
|
|
|
|
private let log = Logger(subsystem: "com.albert.nasbackup", category: "SMBService")
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
|
|
|
|
|
final class SMBService: NASTransferProtocol {
|
|
|
|
|
private var client: SMBClient?
|
|
|
|
|
private(set) var isConnected: Bool = false
|
|
|
|
|
|
|
|
|
|
// remotePath format: "/ShareName/optional/subpath"
|
|
|
|
|
// splitPath returns ("ShareName", "optional/subpath")
|
|
|
|
|
private func splitPath(_ path: String) -> (share: String, relativePath: String) {
|
|
|
|
|
let trimmed = path.trimmingCharacters(in: .init(charactersIn: "/"))
|
|
|
|
|
let parts = trimmed.split(separator: "/", maxSplits: 1, omittingEmptySubsequences: true)
|
|
|
|
|
let share = parts.isEmpty ? "" : String(parts[0])
|
|
|
|
|
let rel = parts.count > 1 ? String(parts[1]) : ""
|
|
|
|
|
return (share, rel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func connect(to host: String, port: Int, username: String, password: String) async throws {
|
2026-05-18 16:07:41 +03:00
|
|
|
log.info("SMB connecting — host=\(host, privacy: .public):\(port) user=\(username, privacy: .private)")
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
let c = SMBClient(host: host, port: port)
|
|
|
|
|
do {
|
|
|
|
|
try await c.login(username: username, password: password)
|
|
|
|
|
} catch {
|
2026-05-18 16:07:41 +03:00
|
|
|
log.error("SMB connect failed — host=\(host, privacy: .public):\(port) error=\(error.localizedDescription, privacy: .public)")
|
|
|
|
|
// Propagate a connection error rather than always claiming auth failed —
|
|
|
|
|
// the real cause could be host unreachable, wrong port, or network timeout.
|
|
|
|
|
let nsErr = error as NSError
|
|
|
|
|
if nsErr.domain == NSURLErrorDomain ||
|
|
|
|
|
nsErr.code == NSURLErrorTimedOut ||
|
|
|
|
|
nsErr.code == NSURLErrorCannotConnectToHost ||
|
|
|
|
|
nsErr.code == NSURLErrorNetworkConnectionLost {
|
|
|
|
|
throw BackupError.connectionFailed("\(host):\(port) — \(error.localizedDescription)")
|
|
|
|
|
}
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
throw BackupError.authenticationFailed
|
|
|
|
|
}
|
2026-05-18 16:07:41 +03:00
|
|
|
log.info("SMB auth OK — \(host, privacy: .public):\(port)")
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
self.client = c
|
|
|
|
|
self.isConnected = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func disconnect() {
|
|
|
|
|
Task { try? await client?.logoff() }
|
|
|
|
|
client = nil
|
|
|
|
|
isConnected = false
|
|
|
|
|
}
|
|
|
|
|
|
fix: SMB share enumeration, BrowseView polish, sign out, Login branding
- Fix "Bad Network Name": BrowseView at root "/" for SMB now calls
listShares() instead of listDirectory with empty share name.
SMBClient.listShares() filters IPC/admin/hidden ($) shares.
- Add listShares() to NASTransferProtocol; implement in SMB (filters
ipc/special/$-suffix) and SFTP (returns []).
- BrowseView: premium error state (wifi.exclamationmark icon + Retry
button with ScaleButtonStyle), empty state, safeAreaInset bottom bar
replacing toolbar bottomBar (shows current path chip + compact Select
button), proper disabled state for root selection on SMB.
- FolderRow: replace hardcoded .white with AppTheme.inkInverse for dark
mode correctness; use folder.fill icon; ScaleButtonStyle for tap feedback.
- LoginView: larger logo (80pt), bolder "Kisani" wordmark (32pt bold),
tagline, staggered spring entrance animations (0.08-0.36s delay).
- SettingsView: Sign Out button in ACCOUNT section with
confirmationDialog (destructive, clears connection + resets session).
- ConnectionStore: signOut() resets isSessionActive, onboardingPhotoShown,
and clears savedConnection (triggers Keychain delete).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-16 16:41:16 +03:00
|
|
|
func listShares() async throws -> [String] {
|
|
|
|
|
guard let client else { throw BackupError.connectionFailed("Not connected") }
|
|
|
|
|
let shares = try await client.listShares()
|
|
|
|
|
return shares
|
|
|
|
|
.filter { share in
|
|
|
|
|
!share.name.hasSuffix("$") &&
|
|
|
|
|
!share.type.contains(.ipc) &&
|
|
|
|
|
!share.type.contains(.special)
|
|
|
|
|
}
|
|
|
|
|
.map { $0.name }
|
|
|
|
|
}
|
|
|
|
|
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
func listDirectory(at path: String) async throws -> [NASItem] {
|
|
|
|
|
guard let client else { throw BackupError.connectionFailed("Not connected") }
|
|
|
|
|
let (share, rel) = splitPath(path)
|
2026-05-18 16:07:41 +03:00
|
|
|
log.info("SMB listDirectory — share=\(share, privacy: .public) path=\(rel.isEmpty ? "/" : rel, privacy: .public)")
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
try await client.connectShare(share)
|
|
|
|
|
let files = try await client.listDirectory(path: rel.isEmpty ? "" : rel)
|
2026-05-18 16:07:41 +03:00
|
|
|
let items = files.compactMap { file -> NASItem? in
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
guard file.name != "." && file.name != ".." else { return nil }
|
|
|
|
|
let fullPath = rel.isEmpty ? "/\(share)/\(file.name)" : "/\(share)/\(rel)/\(file.name)"
|
|
|
|
|
return NASItem(
|
|
|
|
|
name: file.name,
|
|
|
|
|
path: fullPath,
|
|
|
|
|
isDirectory: file.isDirectory,
|
|
|
|
|
size: Int64(file.size),
|
|
|
|
|
modifiedDate: file.lastWriteTime
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-05-18 16:07:41 +03:00
|
|
|
log.info("SMB listDirectory result — \(items.count) items at \(path, privacy: .public)")
|
|
|
|
|
return items
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createDirectory(at path: String) async throws {
|
|
|
|
|
guard let client else { throw BackupError.connectionFailed("Not connected") }
|
|
|
|
|
let (share, rel) = splitPath(path)
|
|
|
|
|
try await client.connectShare(share)
|
|
|
|
|
try await client.createDirectory(path: rel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fileExists(at remotePath: String) async throws -> Bool {
|
|
|
|
|
guard let client else { throw BackupError.connectionFailed("Not connected") }
|
|
|
|
|
let (share, rel) = splitPath(remotePath)
|
|
|
|
|
try await client.connectShare(share)
|
|
|
|
|
let dir = (rel as NSString).deletingLastPathComponent
|
|
|
|
|
let filename = (rel as NSString).lastPathComponent
|
|
|
|
|
let files = try await client.listDirectory(path: dir)
|
|
|
|
|
return files.contains { $0.name == filename }
|
|
|
|
|
}
|
|
|
|
|
|
feat: Kisani UI overhaul — brand identity, gallery, visual polish
- Rename app to Kisani (CFBundleDisplayName, xcodeproj → Kisani.xcodeproj)
- BackupView: kisani. wordmark + SwiftUI-drawn server logo mark (no asset,
adaptive ink/bg), 206pt progress ring with active-state glow, 4-page
swipeable ring (progress/pending/failed/uploaded), 4-metric stats strip,
friendly NAS card (Home Server label), hamburger → AppMenuView sheet
- GalleryView: new NAS + local photo grid with source picker chip, 3-col
lazy grid, ThumbnailCache, sync-status dots (green = backed up)
- MainTabView: Browse tab → Gallery tab; tab bar uses ultraThinMaterial
blur background (systemGray3 icons, 9.5pt labels)
- All app icon sizes regenerated from 1024pt source via sips
- LANMonitor: nasReachable Bool?, checkNASReachability(host:port:) TCP ping
- project.yml: photo library usage description updated to Kisani branding
Co-Authored-By: Kutesir <kutesir@provoc.ug>
Co-Authored-By: Sentry <sentry@provoc.ug>
2026-05-17 11:17:13 +03:00
|
|
|
func downloadData(at remotePath: String) async throws -> Data {
|
|
|
|
|
guard let client else { throw BackupError.connectionFailed("Not connected") }
|
|
|
|
|
let (share, rel) = splitPath(remotePath)
|
|
|
|
|
try await client.connectShare(share)
|
|
|
|
|
return try await client.download(path: rel)
|
|
|
|
|
}
|
|
|
|
|
|
Add live backup status reconciliation with NAS manifest
BackupStatusService (new singleton, ObservableObject, PHPhotoLibraryChangeObserver):
- Loads cached BackupStatusSnapshot instantly from UserDefaults on init
- Full reconcile: fetch phone assets → connect NAS → load/build manifest → compare
→ enforce invariant (alreadySafe ≤ phoneTotal), persist result
- Debounced (30s) for photo library changes; force=true bypasses debounce
- If NAS unreachable: keeps last cached numbers, marks connectionState = .offline
- PHPhotoLibraryChangeObserver triggers refresh on any library change
BackupManifest (new):
- Stored at {remotePath}/.kisani.json on NAS
- Indexed by localIdentifier (PHAsset stable ID); filename fallback for legacy entries
- Built from directory listing if manifest missing (bootstrap for existing backups)
- merged/updated after each backup run via NASTransferProtocol.writeData
BackupStatusSnapshot (new):
- Single source of truth: phoneTotal, alreadySafe, needBackup (derived), nasArchiveTotal,
lastCheckedAt, connectionState
- Invariant enforced in service: alreadySafe = min(safe, phoneTotal)
Protocol / services:
- NASTransferProtocol: adds writeData(_ data: Data, to remotePath: String)
- SMBService: implements writeData via temp file + SMBClient.upload
- SFTPService: implements writeData via SFTP ByteBuffer write
BackupEngine:
- Tracks ManifestEntry per successful upload during backup loop
- After backup: calls BackupStatusService.refreshAfterBackup(entries:connection:)
which applies optimistic UI update then writes manifest + triggers reconcile
BackupView:
- Reads all stats from BackupStatusService.snapshot (not vm/nasFileCount)
- Stats labels: "NAS Archive" / "On iPhone" / "Need Backup" / "Already Safe"
- Live refresh triggers: .task (force), scenePhase.active (force),
nasReachable change (force), remotePath change (force), backup completed (+2s)
- Subtle status row below stats: "Checking…" spinner or "Updated X ago" with
wifi-slash icon when NAS offline; tap refresh button for forced reconcile
- AppMenuView sheet now correctly passes engine EnvironmentObject
BackupViewModel: stripped to auth-only (photosAuthStatus + requestPhotosAccess)
Co-Authored-By: Kutesir <kutesir@provoc.ug>
Co-Authored-By: Sentry <sentry@provoc.ug>
2026-05-17 12:51:03 +03:00
|
|
|
func writeData(_ data: Data, to remotePath: String) async throws {
|
|
|
|
|
guard let client else { throw BackupError.connectionFailed("Not connected") }
|
|
|
|
|
let (share, rel) = splitPath(remotePath)
|
|
|
|
|
try await client.connectShare(share)
|
|
|
|
|
let tmpURL = FileManager.default.temporaryDirectory
|
|
|
|
|
.appendingPathComponent(UUID().uuidString)
|
|
|
|
|
try data.write(to: tmpURL)
|
|
|
|
|
defer { try? FileManager.default.removeItem(at: tmpURL) }
|
|
|
|
|
do {
|
|
|
|
|
try await client.upload(localPath: tmpURL, remotePath: rel) { _, _, _ in }
|
|
|
|
|
} catch {
|
|
|
|
|
throw BackupError.uploadFailed(BackupManifest.remoteFilename, underlying: error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Initial scaffold — NASBackup iOS app
Full project scaffold: XcodeGen project.yml, Core models/protocols/errors,
SMBService (kishikawakatsumi/SMBClient), SFTPService (Citadel 0.9.2),
PhotoLibraryService, LANMonitor, BackupEngine, BackgroundTaskManager,
all feature UIs (Login, Connect, Browse, Backup, History, Settings),
Shared components and theme. Builds clean with zero errors.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 17:05:04 +03:00
|
|
|
func upload(localURL: URL, remotePath: String, progress: @escaping (Int64, Int64) -> Void) async throws {
|
|
|
|
|
guard let client else { throw BackupError.connectionFailed("Not connected") }
|
|
|
|
|
let (share, rel) = splitPath(remotePath)
|
|
|
|
|
try await client.connectShare(share)
|
|
|
|
|
let filename = localURL.lastPathComponent
|
|
|
|
|
do {
|
|
|
|
|
try await client.upload(localPath: localURL, remotePath: rel) { _, _, bytesSent in
|
|
|
|
|
let total = (try? localURL.resourceValues(forKeys: [.fileSizeKey]).fileSize)
|
|
|
|
|
.flatMap { Int64($0) } ?? 0
|
|
|
|
|
progress(bytesSent, total)
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
throw BackupError.uploadFailed(filename, underlying: error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|