Skip to content

Commit

Permalink
Merging from dart3a
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jun 16, 2024
2 parents ffd8d2a + 41060f3 commit 43aa150
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 55 deletions.
58 changes: 55 additions & 3 deletions storage/lib/src/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,50 @@ class GetFilesOptions {
'autoPaginate': autoPaginate,
if (pageToken != null) 'pageToken': pageToken
}.toString();

// Copy options
GetFilesOptions copyWith({
int? maxResults,
String? prefix,
bool? autoPaginate,
String? pageToken,
}) {
return GetFilesOptions(
maxResults: maxResults ?? this.maxResults,
prefix: prefix ?? this.prefix,
autoPaginate: autoPaginate ?? this.autoPaginate,
pageToken: pageToken ?? this.pageToken,
);
}
}

/// GetFiles response
abstract class GetFilesResponse {
List<File> get files;

GetFilesOptions? get nextQuery;

/// Default implementation
factory GetFilesResponse(
{required List<File> files, GetFilesOptions? nextQuery}) {
return _GetFilesResponse(files: files, nextQuery: nextQuery);
}
}

class _GetFilesResponse implements GetFilesResponse {
@override
final List<File> files;

@override
final GetFilesOptions? nextQuery;

_GetFilesResponse({required this.files, required this.nextQuery});

@override
String toString() => {
'files': files.length,
if (nextQuery != null) 'nextQuery': nextQuery
}.toString();
}

mixin StorageMixin implements Storage {
Expand Down Expand Up @@ -62,6 +99,8 @@ abstract class Bucket {

Future<bool> exists();

Future<void> create();

Future<GetFilesResponse> getFiles([GetFilesOptions? options]);
}

Expand All @@ -81,6 +120,11 @@ mixin BucketMixin implements Bucket {
throw UnimplementedError('file($path)');
}

@override
Future<void> create() {
throw UnimplementedError();
}

@override
String get name => throw UnimplementedError('name');
}
Expand All @@ -90,10 +134,12 @@ abstract class File {

Future<void> writeAsString(String text);

@Deprecated('Use writeAsBytes or writeAsString')
Future<void> save(/* String | List<int> */ dynamic content);

Future<bool> exists();

@Deprecated('Use readAsBytes or readAsString')
Future<Uint8List> download();

Future<Uint8List> readAsBytes();
Expand All @@ -108,7 +154,7 @@ abstract class File {
/// The bucket instance the is attached to.
Bucket get bucket;

/// Available when listed through getFiles
/// Available when listed through getFiles (not on flutter though...)
FileMetadata? get metadata;

/// Read meatada
Expand Down Expand Up @@ -182,8 +228,14 @@ mixin FileMixin implements File {

// To deprecate
@override
Future<void> save(content) {
throw UnimplementedError('save');
Future<void> save(dynamic content) {
if (content is String) {
return writeAsString(content);
} else if (content is List<int>) {
return writeAsBytes(_asUint8List(content));
} else {
throw ArgumentError('content must be a String or a List<int>');
}
}
}

Expand Down
15 changes: 11 additions & 4 deletions storage_fs/lib/src/storage_fs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ class BucketFs with BucketMixin implements Bucket {
@override
final String name;

String get dataPath => fs.path.join(localPath!, 'data');
String get dataPath => fs.path.join(localPath, 'data');

String get metaPath => fs.path.join(localPath!, 'meta');
String? localPath;
String get metaPath => fs.path.join(localPath, 'meta');
late String localPath;

BucketFs(this.storage, String? name) : name = name ?? '_default' {
if (storage.service.basePath != null) {
Expand All @@ -173,7 +173,14 @@ class BucketFs with BucketMixin implements Bucket {

@override
Future<bool> exists() async {
return await storage.service.fileSystem.directory(localPath!).exists();
return await storage.service.fileSystem.directory(localPath).exists();
}

@override
Future<void> create() async {
await storage.service.fileSystem
.directory(localPath)
.create(recursive: true);
}

fs_shim.FileSystem get fs => storage.service.fileSystem;
Expand Down
11 changes: 7 additions & 4 deletions storage_fs/test/storage_fs_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ import 'package:tekartik_firebase_storage_fs/storage_fs_io.dart';
import 'package:tekartik_firebase_storage_test/storage_test.dart';
import 'package:test/test.dart';

var _bucketName = 'my_bucket';
void main() {
var firebase = FirebaseLocal();

group('storage_fs_io', () {
run(
runStorageTests(
firebase: firebase,
storageService: storageServiceIo,
storageOptions: TestStorageOptions(bucket: 'my_bucket'));
storageOptions: TestStorageOptions(bucket: _bucketName));

var fileSystem = (storageServiceIo as StorageServiceFs).fileSystem;
var app = firebase.initializeApp();
var storage = storageServiceIo.storage(app);
setUpAll(() {});
setUpAll(() async {
await storage.bucket(_bucketName).create();
});
tearDownAll(() {
return app.delete();
});
Expand Down Expand Up @@ -53,7 +56,7 @@ void main() {
// delete a top folder to force creating the tree again
try {
await fileSystem
.directory(fileFs.bucket.localPath!)
.directory(fileFs.bucket.localPath)
.delete(recursive: true);
} catch (_) {}
expect(await bucket.exists(), isFalse);
Expand Down
13 changes: 10 additions & 3 deletions storage_fs/test/storage_fs_memory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import 'package:tekartik_firebase_storage_fs/storage_fs.dart';
import 'package:tekartik_firebase_storage_test/storage_test.dart';
import 'package:test/test.dart';

var _bucketName = 'my_bucket';
void main() {
var firebase = FirebaseLocal();

group('storage_fs_memory', () {
run(
firebase: firebase,
var app = firebase.initializeApp();

/// Need to create the bucket.
setUpAll(() async {
var storage = storageServiceMemory.storage(app);
await storage.bucket(_bucketName).create();
});
runStorageAppTests(app,
storageService: storageServiceMemory,
storageOptions: TestStorageOptions(bucket: 'my_bucket'));
storageOptions: TestStorageOptions(bucket: _bucketName));

test('new', () async {
var app = firebase.initializeApp();
Expand Down
4 changes: 2 additions & 2 deletions storage_fs/test/storage_fs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ void main() {
expect(bucket.localPath,
'.dart_tool/tekartik_firebase_local/_default/storage/_default');
var file = bucket.file('test');
expect(file.fsFile.path, url.join(bucket.localPath!, 'data/test'));
expect(file.fsFile.path, url.join(bucket.localPath, 'data/test'));
file = bucket.file('/test');
expect(file.fsFile.path, url.join(bucket.localPath!, 'data/test'));
expect(file.fsFile.path, url.join(bucket.localPath, 'data/test'));

app.delete();
});
Expand Down
2 changes: 2 additions & 0 deletions storage_rest/lib/src/bucket_rest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class BucketRest with BucketMixin implements Bucket {

StorageRestImpl get impl => storageRest as StorageRestImpl;

@override
Future<bool> exists() => impl.bucketExists(this);
@override
File file(String path) => FileRest(this, path);

Expand Down
15 changes: 15 additions & 0 deletions storage_rest/lib/src/storage_rest_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'package:tekartik_http/http.dart';

import 'import.dart';

final storageGoogleApisReadWriteScope = api.StorageApi.devstorageReadWriteScope;

abstract class StorageServiceRest extends StorageService {}

/// Storage rest helper.
Expand Down Expand Up @@ -141,6 +143,19 @@ class StorageRestImpl with StorageMixin implements StorageRest {
Future<void> deleteFile(BucketRest bucket, String path) async {
await storageApi.objects.delete(bucket.name, path);
}

Future<bool> bucketExists(BucketRest bucketRest) async {
try {
await storageApi.buckets.get(bucketRest.name);
return true;
} on api.DetailedApiRequestError catch (e) {
// DetailedApiRequestError(status: 404, message: The specified bucket does not exist.)
if (e.status == httpStatusCodeNotFound) {
return false;
}
rethrow;
}
}
}

class GetFilesResponseRest implements GetFilesResponse {
Expand Down
6 changes: 5 additions & 1 deletion storage_rest/lib/storage_rest.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export 'src/storage_rest_impl.dart'
show StorageServiceRest, storageServiceRest, StorageRest;
show
StorageServiceRest,
storageServiceRest,
StorageRest,
storageGoogleApisReadWriteScope;
2 changes: 1 addition & 1 deletion storage_rest/test/storage_rest_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Future main() async {
if (context != null) {
var firebase = firebaseRest;
group('all', () {
run(
runStorageTests(
firebase: firebase,
storageService: storageServiceRest,
options: context.options,
Expand Down
13 changes: 13 additions & 0 deletions storage_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Storage tes Setup

In `pubspec.yaml`:

```yaml
dependencies:
tekartik_firebase_storage_test:
git:
url: https://github.com/tekartik/firebase_storage.dart
path: storage_test
ref: dart3a
version: '>=0.4.1'
```
Loading

0 comments on commit 43aa150

Please sign in to comment.