Skip to content

Commit

Permalink
feat(web): support local filesystem read/save actions
Browse files Browse the repository at this point in the history
  • Loading branch information
JagandeepBrar committed Oct 25, 2022
1 parent 3d79409 commit 7d437d5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 22 deletions.
9 changes: 4 additions & 5 deletions docs/getting-started/platform-restrictions.mdx
Expand Up @@ -20,11 +20,10 @@ The tables below showcases which modules or features may not be supported on eac

## Feature Support

| Feature | Android | iOS | Linux | macOS | Windows | Web (Hosted) | Web (Self-Hosted) |
| :--------------------: | :-----: | :-: | :---: | :---: | :-----: | :----------: | :---------------: |
| Local Backup & Restore ||||||||
| LunaSea Accounts ||||||||
| Notifications ||||||||
| Feature | Android | iOS | Linux | macOS | Windows | Web (Hosted) | Web (Self-Hosted) |
| :--------------: | :-----: | :-: | :---: | :---: | :-----: | :----------: | :---------------: |
| LunaSea Accounts ||||||||
| Notifications ||||||||

## Web-Specific Restrictions

Expand Down
5 changes: 2 additions & 3 deletions lib/modules/nzbget/routes/nzbget.dart
Expand Up @@ -166,13 +166,12 @@ class _State extends State<NZBGetRoute> {
'nzb',
]);
if (_file != null) {
String _name = _file.path.substring(_file.path.lastIndexOf('/') + 1);
if (_file.data.isNotEmpty) {
await _api.uploadFile(_file.data, _name).then((value) {
await _api.uploadFile(_file.data, _file.name).then((value) {
_refreshKeys[0]?.currentState?.show();
showLunaSuccessSnackBar(
title: 'Uploaded NZB (File)',
message: _name,
message: _file.name,
);
});
} else {
Expand Down
5 changes: 2 additions & 3 deletions lib/modules/sabnzbd/routes/sabnzbd.dart
Expand Up @@ -219,13 +219,12 @@ class _State extends State<SABnzbdRoute> {
'gz',
]);
if (_file != null) {
String _name = _file.path.substring(_file.path.lastIndexOf('/') + 1);
if (_file.data.isNotEmpty) {
await _api.uploadFile(_file.data, _name).then((value) {
await _api.uploadFile(_file.data, _file.name).then((value) {
_refreshKeys[0]?.currentState?.show();
showLunaSuccessSnackBar(
title: 'Uploaded NZB (File)',
message: _name,
message: _file.name,
);
});
} else {
Expand Down
8 changes: 4 additions & 4 deletions lib/system/filesystem/file.dart
@@ -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,
});
}
65 changes: 60 additions & 5 deletions lib/system/filesystem/platform/filesystem_html.dart
@@ -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;
}
}
}
5 changes: 3 additions & 2 deletions lib/system/filesystem/platform/filesystem_io.dart
Expand Up @@ -5,6 +5,7 @@ import 'package:lunasea/database/database.dart';
import 'package:lunasea/vendor.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:cross_file/cross_file.dart';

import 'package:lunasea/widgets/ui.dart';
import 'package:lunasea/system/logger.dart';
Expand Down Expand Up @@ -98,8 +99,8 @@ class _Mobile extends _Shared {
Rect? rect;
if (box != null) rect = box.localToGlobal(Offset.zero) & box.size;

ShareResult result = await Share.shareFilesWithResult(
[path],
ShareResult result = await Share.shareXFiles(
[XFile(path)],
sharePositionOrigin: rect,
);
switch (result.status) {
Expand Down

0 comments on commit 7d437d5

Please sign in to comment.