-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): support local filesystem read/save actions
- Loading branch information
1 parent
3d79409
commit 7d437d5
Showing
6 changed files
with
75 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
class LunaFile { | ||
String path; | ||
String name; | ||
List<int> data; | ||
String name; | ||
String? path; | ||
|
||
LunaFile({ | ||
required this.path, | ||
required this.name, | ||
required this.data, | ||
required this.name, | ||
this.path, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,61 @@ | ||
// ignore: always_use_package_imports | ||
import '../filesystem.dart'; | ||
// ignore: avoid_web_libraries_in_flutter | ||
import 'dart:html'; | ||
import 'package:file_picker/file_picker.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:lunasea/system/filesystem/file.dart'; | ||
import 'package:lunasea/system/filesystem/filesystem.dart'; | ||
import 'package:lunasea/system/logger.dart'; | ||
import 'package:lunasea/vendor.dart'; | ||
import 'package:lunasea/widgets/ui.dart'; | ||
|
||
bool isPlatformSupported() => false; | ||
LunaFileSystem getFileSystem() => | ||
throw UnsupportedError('LunaFileSystem unsupported'); | ||
bool isPlatformSupported() => true; | ||
LunaFileSystem getFileSystem() { | ||
if (isPlatformSupported()) return _Web(); | ||
throw UnsupportedError('LunaFileSystem unsupported'); | ||
} | ||
|
||
class _Web implements LunaFileSystem { | ||
@override | ||
Future<void> nuke() async {} | ||
|
||
@override | ||
Future<bool> save(BuildContext context, String name, List<int> data) async { | ||
try { | ||
final blob = Blob([utf8.decode(data)]); | ||
final anchor = AnchorElement(href: Url.createObjectUrlFromBlob(blob)); | ||
anchor.setAttribute('download', name); | ||
anchor.click(); | ||
return true; | ||
} catch (error, stack) { | ||
LunaLogger().error('Failed to save to filesystem', error, stack); | ||
rethrow; | ||
} | ||
} | ||
|
||
@override | ||
Future<LunaFile?> read(BuildContext context, List<String> extensions) async { | ||
try { | ||
final result = await FilePicker.platform.pickFiles(withData: true); | ||
|
||
if (result?.files.isNotEmpty ?? false) { | ||
String? _ext = result!.files[0].extension; | ||
if (LunaFileSystem.isValidExtension(extensions, _ext)) { | ||
return LunaFile( | ||
name: result.files[0].name, | ||
data: result.files[0].bytes!, | ||
); | ||
} else { | ||
showLunaErrorSnackBar( | ||
title: 'lunasea.InvalidFileTypeSelected'.tr(), | ||
message: extensions.map((s) => '.$s').join(', '), | ||
); | ||
} | ||
} | ||
|
||
return null; | ||
} catch (error, stack) { | ||
LunaLogger().error('Failed to read from filesystem', error, stack); | ||
rethrow; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters