|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:path/path.dart' as p; |
| 5 | +import 'package:path_provider/path_provider.dart'; |
| 6 | + |
| 7 | +/// Resolves where Querya stores local profile data (DB, themes, extensions, logs). |
| 8 | +/// |
| 9 | +/// Default: OS application-support paths. |
| 10 | +/// Portable: when [envPortable] is truthy and/or a [sidecarDirName] folder exists |
| 11 | +/// next to the binary (or `$APPIMAGE`), data goes under that folder. |
| 12 | +/// |
| 13 | +/// Secrets still use the OS keyring via `flutter_secure_storage` (not redirected). |
| 14 | +abstract final class AppDataRoot { |
| 15 | + static const envPortable = 'QUERYA_PORTABLE'; |
| 16 | + static const sidecarDirName = 'QueryaData'; |
| 17 | + |
| 18 | + @visibleForTesting |
| 19 | + static String? mockPortableRootPath; |
| 20 | + |
| 21 | + @visibleForTesting |
| 22 | + static String? mockInstallDirectory; |
| 23 | + |
| 24 | + @visibleForTesting |
| 25 | + static Map<String, String>? mockEnvironment; |
| 26 | + |
| 27 | + @visibleForTesting |
| 28 | + static void resetMocks() { |
| 29 | + mockPortableRootPath = null; |
| 30 | + mockInstallDirectory = null; |
| 31 | + mockEnvironment = null; |
| 32 | + } |
| 33 | + |
| 34 | + static Map<String, String> get _env => |
| 35 | + mockEnvironment ?? Platform.environment; |
| 36 | + |
| 37 | + /// Directory containing the running binary, or the AppImage file's parent. |
| 38 | + static String? installDirectoryPath() { |
| 39 | + if (mockInstallDirectory != null) return mockInstallDirectory; |
| 40 | + final appImage = _env['APPIMAGE']; |
| 41 | + if (appImage != null && appImage.trim().isNotEmpty) { |
| 42 | + return p.dirname(appImage); |
| 43 | + } |
| 44 | + final exe = Platform.resolvedExecutable; |
| 45 | + if (exe.isEmpty) return null; |
| 46 | + return p.dirname(exe); |
| 47 | + } |
| 48 | + |
| 49 | + static bool envRequestsPortable() { |
| 50 | + final raw = _env[envPortable]; |
| 51 | + if (raw == null) return false; |
| 52 | + final v = raw.trim().toLowerCase(); |
| 53 | + return v == '1' || v == 'true' || v == 'yes' || v == 'on'; |
| 54 | + } |
| 55 | + |
| 56 | + /// Portable data root, or `null` when using normal OS support paths. |
| 57 | + /// |
| 58 | + /// When [envPortable] is set, creates [sidecarDirName] next to the install |
| 59 | + /// directory if it does not exist yet. |
| 60 | + static Future<Directory?> resolvePortableRoot() async { |
| 61 | + if (mockPortableRootPath != null) { |
| 62 | + return Directory(mockPortableRootPath!); |
| 63 | + } |
| 64 | + |
| 65 | + final installDir = installDirectoryPath(); |
| 66 | + if (installDir == null || installDir.isEmpty) return null; |
| 67 | + |
| 68 | + final sidecar = Directory(p.join(installDir, sidecarDirName)); |
| 69 | + final forced = envRequestsPortable(); |
| 70 | + |
| 71 | + if (forced) { |
| 72 | + if (!await sidecar.exists()) { |
| 73 | + await sidecar.create(recursive: true); |
| 74 | + } |
| 75 | + return sidecar; |
| 76 | + } |
| 77 | + |
| 78 | + if (await sidecar.exists()) { |
| 79 | + return sidecar; |
| 80 | + } |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + static Future<bool> isPortableMode() async => |
| 85 | + (await resolvePortableRoot()) != null; |
| 86 | + |
| 87 | + /// Application-support equivalent: portable root or [getApplicationSupportDirectory]. |
| 88 | + static Future<Directory> applicationSupportDirectory() async { |
| 89 | + final portable = await resolvePortableRoot(); |
| 90 | + if (portable != null) return portable; |
| 91 | + return getApplicationSupportDirectory(); |
| 92 | + } |
| 93 | +} |
0 commit comments