Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4 #18

Merged
merged 5 commits into from
Nov 24, 2022
Merged

v4 #18

Show file tree
Hide file tree
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
49 changes: 3 additions & 46 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
.buildlog/
.history
.svn/
pubspec.lock
migrate_working_dir/

# IntelliJ related
*.iml
Expand All @@ -22,54 +22,11 @@ pubspec.lock
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
2 changes: 1 addition & 1 deletion .metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file should be version controlled and should not be manually edited.

version:
revision: 4d7946a68d26794349189cf21b3f68cc6fe61dcb
revision: e99c9c7cd9f6c0b2f8ae6e3ebfd585239f5568f4
channel: stable

project_type: package
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encrypt_file_loader

encrypt_file_loader is a library that can store files encrypted with [webcrypto](https://pub.dev/packages/webcrypto).
encrypt_file_loader downloads and saves the encrypted file. It can then set the key and decrypt the file.

The DB is created by [Drift](https://pub.dev/packages/drift).
Decrypt using [webcrypto](https://pub.dev/packages/webcrypto). This library runs fast on Android and iOS.
The DB is created by [Isar](https://pub.dev/packages/isar).
3 changes: 3 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
52 changes: 31 additions & 21 deletions lib/src/encrypt_file_loader.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:io';

import 'package:encrypt_file_loader/src/isar/cache.dart';
import 'package:encrypt_file_loader/src/isar/db/connection.dart';
import 'package:encrypt_file_loader/src/result/decrypt_result.dart';
import 'package:encrypt_file_loader/src/webcrypto/crypto_type.dart';
import 'package:http/http.dart' as http;

import 'moor/filename.dart';
import 'result/decrypt_result.dart';
import 'webcrypto/crypto_type.dart';
import 'package:isar/isar.dart';

/// Result pattern on [EncryptFileLoader.load]
enum LoadResult {
Expand All @@ -29,36 +30,41 @@ enum LoadResult {

/// [EncryptFileLoader] is a class that loads, caches, and decrypts.
class EncryptFileLoader {
final _db = Database();

/// Load file from server or internal db.
Future<LoadResult> load({
required String url,
String group = 'no_group',
}) async {
final cache = await _db.getFile(url);
final isar = await Connection.instance;
final cache = await isar.caches.filter().urlEqualTo(url).findFirst();
if (cache != null) {
return LoadResult.cached;
}

try {
final response = await http.get(Uri.parse(url));
final uri = Uri.parse(url);
final response = await http.get(uri);
if (response.statusCode == 200) {
String? filename;
final disposition = response.headers['Content-Disposition']?.toString();
if (disposition != null) {
final reg = RegExp('''/filename[^;=\n]*=((['"]).*?2|[^;\n]*)/''');
final match = reg.firstMatch(disposition);
filename = match?[0];
} else {
filename = uri.pathSegments.last;
}

final entry = createEntity(
final entry = Caches(
url: url,
group: group,
bytes: response.bodyBytes,
filename: filename,
updated: DateTime.now().toUtc(),
);
_db.insertCache(entry);
await isar.writeTxn(() async {
await isar.caches.put(entry);
});

return LoadResult.load;
}
Expand All @@ -78,7 +84,8 @@ class EncryptFileLoader {
required String url,
required CryptoType type,
}) async {
final cache = await _db.getFile(url);
final isar = await Connection.instance;
final cache = await isar.caches.filter().urlEqualTo(url).findFirst();
if (cache == null) {
return null;
}
Expand All @@ -91,25 +98,28 @@ class EncryptFileLoader {

/// Delete all files.
/// Returns the amount of rows that were deleted.
Future<int> deleteAll() async {
return await _db.deleteAll();
Future<void> deleteAll() async {
final isar = await Connection.instance;
return await isar.writeTxn(
() async => await isar.clear(),
);
}

/// Delete files that belong to the [group].
/// Returns the amount of rows that were deleted.
Future<int> deleteGroup(String group) async {
return await _db.deleteGroup(group);
final isar = await Connection.instance;
return await isar.writeTxn(
() async => await isar.caches.filter().groupEqualTo(group).deleteAll(),
);
}

/// Delete files older than the [base].
/// Returns the amount of rows that were deleted.
Future<int> deleteOldFiles(DateTime base) async {
return await _db.deleteOldFiles(base);
}

/// Rebuilds the database file,
/// repacking it into a minimal amount of disk space.
Future<void> vacuum() async {
await _db.customStatement('vacuum;');
final isar = await Connection.instance;
return await isar.writeTxn(
() async => await isar.caches.filter().updatedLessThan(base).deleteAll(),
);
}
}
32 changes: 32 additions & 0 deletions lib/src/isar/cache.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:isar/isar.dart';

part 'cache.g.dart';

/// Cache
@collection
class Caches {
Caches({
required this.url,
required this.group,
required this.bytes,
required this.filename,
required this.updated,
});

Id id = Isar.autoIncrement;

/// server url
final String url;

/// grouping key
final String group;

/// downloaded data
final List<int> bytes;

/// filename (nullable)
final String? filename;

/// last edited date and time
final DateTime updated;
}
Loading