2
This commit is contained in:
1 parent
cce34e7960
commit
b49e096355
56 files changed
+8343
-296
No files matched your search
@@ -0,0 +1,179 @@
|
||||
import 'dart:io';
|
||||
import 'package:archive/archive_io.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
/// 软件内部私有存储项目管理服务 (files/projects)
|
||||
class InternalProjectService {
|
||||
static final InternalProjectService instance = InternalProjectService._();
|
||||
factory InternalProjectService() => instance;
|
||||
InternalProjectService._();
|
||||
|
||||
/// 自定义项目存储根目录(主要用于单元测试与隔离环境)
|
||||
@visibleForTesting
|
||||
Directory? customProjectsDir;
|
||||
|
||||
Directory? _cachedProjectsDir;
|
||||
|
||||
/// 判断指定路径是否为内部存储项目路径 (`files/projects/<projectName>`)
|
||||
bool isInternalProject(String? projectPath) {
|
||||
if (projectPath == null || projectPath.trim().isEmpty) return false;
|
||||
final cleanPath = p.normalize(projectPath.trim());
|
||||
|
||||
if (customProjectsDir != null) {
|
||||
final customParent = p.normalize(customProjectsDir!.path);
|
||||
return p.equals(p.dirname(cleanPath), customParent);
|
||||
}
|
||||
|
||||
if (_cachedProjectsDir != null) {
|
||||
final cachedParent = p.normalize(_cachedProjectsDir!.path);
|
||||
return p.equals(p.dirname(cleanPath), cachedParent);
|
||||
}
|
||||
|
||||
final parentDir = p.normalize(p.dirname(cleanPath));
|
||||
final parts = p.split(parentDir);
|
||||
if (parts.length >= 2 &&
|
||||
parts[parts.length - 2] == 'files' &&
|
||||
parts[parts.length - 1] == 'projects') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// 获取软件内部存储的项目基础目录: `files/projects`
|
||||
Future<Directory> getProjectsDirectory() async {
|
||||
if (customProjectsDir != null) {
|
||||
if (!customProjectsDir!.existsSync()) {
|
||||
customProjectsDir!.createSync(recursive: true);
|
||||
}
|
||||
return customProjectsDir!;
|
||||
}
|
||||
|
||||
if (_cachedProjectsDir != null) {
|
||||
if (!_cachedProjectsDir!.existsSync()) {
|
||||
_cachedProjectsDir!.createSync(recursive: true);
|
||||
}
|
||||
return _cachedProjectsDir!;
|
||||
}
|
||||
|
||||
final appDir = await getApplicationSupportDirectory();
|
||||
// Android 下 getApplicationSupportDirectory 对应 .../files
|
||||
// 若路径已以 files 结尾,则直接拼接 projects;否则拼接 files/projects 保持规范一致
|
||||
final baseDir = p.basename(appDir.path) == 'files'
|
||||
? appDir.path
|
||||
: p.join(appDir.path, 'files');
|
||||
|
||||
final projectsDir = Directory(p.join(baseDir, 'projects'));
|
||||
if (!projectsDir.existsSync()) {
|
||||
projectsDir.createSync(recursive: true);
|
||||
}
|
||||
_cachedProjectsDir = projectsDir;
|
||||
return projectsDir;
|
||||
}
|
||||
|
||||
/// 获取所有已创建的内部项目列表(按文件夹名称升序排列)
|
||||
Future<List<Directory>> listProjects() async {
|
||||
final dir = await getProjectsDirectory();
|
||||
if (!dir.existsSync()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
final entities = dir.listSync(followLinks: false);
|
||||
final projects = entities.whereType<Directory>().toList();
|
||||
projects.sort((a, b) => p
|
||||
.basename(a.path)
|
||||
.toLowerCase()
|
||||
.compareTo(p.basename(b.path).toLowerCase()));
|
||||
return projects;
|
||||
} catch (e) {
|
||||
debugPrint('获取内部项目列表失败: $e');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// 检查指定项目名称是否已存在
|
||||
Future<bool> projectExists(String name) async {
|
||||
final dir = await getProjectsDirectory();
|
||||
final targetDir = Directory(p.join(dir.path, name.trim()));
|
||||
return targetDir.existsSync();
|
||||
}
|
||||
|
||||
/// 在 `files/projects` 下新建一个项目文件夹
|
||||
Future<Directory> createProject(String name) async {
|
||||
final trimmedName = name.trim();
|
||||
if (trimmedName.isEmpty) {
|
||||
throw const FileSystemException('项目名称不能为空');
|
||||
}
|
||||
|
||||
final dir = await getProjectsDirectory();
|
||||
final projectDir = Directory(p.join(dir.path, trimmedName));
|
||||
if (projectDir.existsSync()) {
|
||||
throw FileSystemException('同名项目已存在', projectDir.path);
|
||||
}
|
||||
|
||||
projectDir.createSync(recursive: true);
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
/// 重命名指定的项目文件夹
|
||||
Future<Directory> renameProject(Directory projectDir, String newName) async {
|
||||
final trimmedName = newName.trim();
|
||||
if (trimmedName.isEmpty) {
|
||||
throw const FileSystemException('项目名称不能为空');
|
||||
}
|
||||
|
||||
final parent = projectDir.parent;
|
||||
final newDir = Directory(p.join(parent.path, trimmedName));
|
||||
if (p.normalize(projectDir.path) == p.normalize(newDir.path)) {
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
if (newDir.existsSync()) {
|
||||
throw FileSystemException('同名项目已存在', newDir.path);
|
||||
}
|
||||
|
||||
return projectDir.renameSync(newDir.path);
|
||||
}
|
||||
|
||||
/// 递归删除项目文件夹
|
||||
Future<void> deleteProject(Directory projectDir) async {
|
||||
if (projectDir.existsSync()) {
|
||||
projectDir.deleteSync(recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
/// 从外部压缩文件解压导入为内部项目 (`files/projects/<projectName>`)
|
||||
Future<Directory> importProjectFromArchive(String archivePath, String projectName) async {
|
||||
final trimmedName = projectName.trim();
|
||||
if (trimmedName.isEmpty) {
|
||||
throw const FileSystemException('项目名称不能为空');
|
||||
}
|
||||
|
||||
final dir = await getProjectsDirectory();
|
||||
final projectDir = Directory(p.join(dir.path, trimmedName));
|
||||
if (projectDir.existsSync()) {
|
||||
throw FileSystemException('同名项目已存在', projectDir.path);
|
||||
}
|
||||
|
||||
final archiveFile = File(archivePath);
|
||||
if (!archiveFile.existsSync()) {
|
||||
throw FileSystemException('压缩包文件不存在', archivePath);
|
||||
}
|
||||
|
||||
projectDir.createSync(recursive: true);
|
||||
try {
|
||||
await extractFileToDisk(archivePath, projectDir.path);
|
||||
return projectDir;
|
||||
} catch (e) {
|
||||
if (projectDir.existsSync()) {
|
||||
try {
|
||||
projectDir.deleteSync(recursive: true);
|
||||
} catch (_) {}
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user