implemented user backups
This commit is contained in:
@@ -22,9 +22,11 @@ import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:uuid/uuid_value.dart';
|
||||
import 'package:weblibre/core/logger.dart';
|
||||
import 'package:weblibre/domain/entities/profile.dart';
|
||||
import 'package:weblibre/extensions/iterable.dart';
|
||||
|
||||
const profilesDirName = 'weblibre_profiles';
|
||||
const profileDirPrefix = 'profile-';
|
||||
@@ -42,10 +44,54 @@ final profileTransformer =
|
||||
},
|
||||
);
|
||||
|
||||
final profileMozillaDirectoryTransformer =
|
||||
StreamTransformer<Directory, Directory>.fromHandlers(
|
||||
handleData: (entity, sink) {
|
||||
final mozillaDir = Directory(p.join(entity.path, 'mozilla'));
|
||||
|
||||
for (final entity in mozillaDir.listSync()) {
|
||||
if (entity is Directory) {
|
||||
final profile = p.basename(entity.path);
|
||||
if (profile.endsWith('.default')) {
|
||||
sink.add(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
Future<List<Directory>> getAvailableProfileDirectories(Directory profilesDir) {
|
||||
return profilesDir.list().transform(profileTransformer).toList();
|
||||
}
|
||||
|
||||
Future<void> clearMozillaProfileCache(String profileId) async {
|
||||
final cacheDir = await getApplicationCacheDirectory();
|
||||
final mozillaCacheDir = Directory(p.join(cacheDir.path, profileId));
|
||||
|
||||
if (await mozillaCacheDir.exists()) {
|
||||
await mozillaCacheDir.delete(recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Directory>> getProfilesWithDuplicateMozillaProfiles(
|
||||
Directory profilesDir,
|
||||
) async {
|
||||
final mozillaProfileDirs = await profilesDir
|
||||
.list()
|
||||
.transform(profileTransformer)
|
||||
.transform(profileMozillaDirectoryTransformer)
|
||||
.toList();
|
||||
|
||||
final duplicates = mozillaProfileDirs
|
||||
.map((dir) => p.basename(dir.path))
|
||||
.findDuplicates()
|
||||
.toSet();
|
||||
|
||||
return mozillaProfileDirs
|
||||
.where((dir) => duplicates.contains(p.basename(dir.path)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<UuidValue?> readStartupProfile(Directory dir) async {
|
||||
final file = File(p.join(dir.path, _startupProfileFileName));
|
||||
|
||||
|
||||
@@ -17,7 +17,10 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:nullability/nullability.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:weblibre/utils/uri_parser.dart' as uri_parser;
|
||||
|
||||
String? validateUrl(
|
||||
@@ -55,3 +58,87 @@ String? validateRequired(String? value, {String message = 'Value required'}) {
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
String? validatePath(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Path cannot be empty';
|
||||
}
|
||||
|
||||
// Check for invalid characters
|
||||
// ignore: unnecessary_raw_strings
|
||||
final invalidChars = RegExp(r'[<>"|?*]');
|
||||
if (invalidChars.hasMatch(value)) {
|
||||
return 'Path contains invalid characters';
|
||||
}
|
||||
|
||||
// Validate path structure
|
||||
try {
|
||||
p.normalize(value);
|
||||
} catch (e) {
|
||||
return 'Invalid path format';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
String? validateDirectoryExisting(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Path cannot be empty';
|
||||
}
|
||||
|
||||
if (!Directory(value).existsSync()) {
|
||||
return 'Directory is not existing';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
String? validateDirectoryNotExisting(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Path cannot be empty';
|
||||
}
|
||||
|
||||
if (Directory(value).existsSync()) {
|
||||
return 'Directory already exisits';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
String? validateFileNotExisting(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Path cannot be empty';
|
||||
}
|
||||
|
||||
if (File(value).existsSync()) {
|
||||
return 'File already exisits';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
String? validateFileExisting(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Path cannot be empty';
|
||||
}
|
||||
|
||||
if (!File(value).existsSync()) {
|
||||
return 'File not existing';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
final _profileNamePattern = RegExp(r"""^[^~)('!*<>:;,?"*|/_]+$""");
|
||||
|
||||
String? validateProfileName(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Name required';
|
||||
}
|
||||
|
||||
if (!_profileNamePattern.hasMatch(value)) {
|
||||
return 'Name contains invalid caharcters';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user