remove redundant code from ssh image provider

This commit is contained in:
lukas-heiligenbrunner 2022-09-27 11:53:58 +02:00
parent 84d34336ca
commit fc0ab443e9
2 changed files with 11 additions and 42 deletions

View File

@ -73,37 +73,22 @@ class SSHDataProvider extends DataProvider {
} }
class _SSHImageProvider extends ImageProvider<_SSHImageProvider> { class _SSHImageProvider extends ImageProvider<_SSHImageProvider> {
const _SSHImageProvider(this.uri, this.sftpClient, {this.scale = 1.0}); const _SSHImageProvider(this.uri, this.sftpClient);
final Uri uri; final Uri uri;
final SftpClient sftpClient; final SftpClient sftpClient;
/// The scale to place in the [ImageInfo] object of the image.
final double scale;
@override @override
Future<_SSHImageProvider> obtainKey(ImageConfiguration configuration) { Future<_SSHImageProvider> obtainKey(ImageConfiguration configuration) {
return SynchronousFuture<_SSHImageProvider>(this); return SynchronousFuture<_SSHImageProvider>(this);
} }
@override
ImageStreamCompleter load(_SSHImageProvider key, DecoderCallback decode) {
return MultiFrameImageStreamCompleter(
codec: _loadAsync(key, null, decode),
scale: key.scale,
debugLabel: key.uri.path,
informationCollector: () => <DiagnosticsNode>[
ErrorDescription('Path: ${uri.path}'),
],
);
}
@override @override
ImageStreamCompleter loadBuffer( ImageStreamCompleter loadBuffer(
_SSHImageProvider key, DecoderBufferCallback decode) { _SSHImageProvider key, DecoderBufferCallback decode) {
return MultiFrameImageStreamCompleter( return MultiFrameImageStreamCompleter(
codec: _loadAsync(key, decode, null), codec: _loadAsync(key, decode),
scale: key.scale, scale: 1.0,
debugLabel: key.uri.path, debugLabel: key.uri.path,
informationCollector: () => <DiagnosticsNode>[ informationCollector: () => <DiagnosticsNode>[
ErrorDescription('Path: ${uri.path}'), ErrorDescription('Path: ${uri.path}'),
@ -111,8 +96,8 @@ class _SSHImageProvider extends ImageProvider<_SSHImageProvider> {
); );
} }
Future<ui.Codec> _loadAsync(_SSHImageProvider key, Future<ui.Codec> _loadAsync(
DecoderBufferCallback? decode, DecoderCallback? decodeDeprecated) async { _SSHImageProvider key, DecoderBufferCallback decode) async {
assert(key == this); assert(key == this);
Directory tempDir = await getTemporaryDirectory(); Directory tempDir = await getTemporaryDirectory();
@ -125,7 +110,6 @@ class _SSHImageProvider extends ImageProvider<_SSHImageProvider> {
bytes = await tmpPic.readAsBytes(); bytes = await tmpPic.readAsBytes();
} else { } else {
final file = await sftpClient.open(uri.toFilePath()); final file = await sftpClient.open(uri.toFilePath());
// todo do not load whole image in ram, create tempfile instead.
bytes = await file.readBytes(); bytes = await file.readBytes();
await tmpPic.create(recursive: true); await tmpPic.create(recursive: true);
await tmpPic.writeAsBytes(bytes); await tmpPic.writeAsBytes(bytes);
@ -138,26 +122,20 @@ class _SSHImageProvider extends ImageProvider<_SSHImageProvider> {
'bytes are empty is empty and cannot be loaded as an image.'); 'bytes are empty is empty and cannot be loaded as an image.');
} }
if (decode != null) {
return decode(await ui.ImmutableBuffer.fromUint8List(bytes)); return decode(await ui.ImmutableBuffer.fromUint8List(bytes));
} }
return decodeDeprecated!(bytes);
}
@override @override
bool operator ==(Object other) { bool operator ==(Object other) {
if (other.runtimeType != runtimeType) { if (other.runtimeType != runtimeType) {
return false; return false;
} }
return other is _SSHImageProvider && return other is _SSHImageProvider && other.uri.path == uri.path;
other.uri.path == uri.path &&
other.scale == scale;
} }
@override @override
int get hashCode => Object.hash(uri.path, scale); int get hashCode => uri.path.hashCode;
@override @override
String toString() => String toString() => '${objectRuntimeType(this, 'FileImage')}("${uri.path}")';
'${objectRuntimeType(this, 'FileImage')}("${uri.path}", scale: $scale)';
} }

View File

@ -1,9 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:gallery/data_provider/data_provider.dart'; import 'package:gallery/data_provider/data_provider.dart';
import 'package:gallery/data_provider/local_data_provider.dart';
import 'package:gallery/data_provider/ssh_data_provider.dart';
import 'package:gallery/image_tile.dart'; import 'package:gallery/image_tile.dart';
import 'data_provider/local_data_provider.dart';
import 'full_screen_image_view.dart'; import 'full_screen_image_view.dart';
class MyHomePage extends StatefulWidget { class MyHomePage extends StatefulWidget {
@ -18,15 +17,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
Future<Folder>? folder; Future<Folder>? folder;
// DataProvider dprovider = SSHDataProvider( final DataProvider dprovider = LocalDataProvider("/home");
// initialPath: "/media/",
// host: "",
// password: "",
// port: 22,
// username: "");
// DataProvider dprovider = LocalDataProvider("${(await getApplicationDocumentsDirectory()).path}/../Downloads/");
DataProvider dprovider = LocalDataProvider("/home");
final ScrollController _controller = ScrollController(); final ScrollController _controller = ScrollController();