add ci build and caching of sftp files

This commit is contained in:
2022-09-27 00:30:13 +02:00
parent 7a95780b5b
commit 84d34336ca
6 changed files with 91 additions and 41 deletions

View File

@ -16,7 +16,6 @@ class Folder {
Folder(this.items, this.self, this.parent);
}
abstract class DataProvider {
final List<String> validSuffix = [".jpg", ".jpeg", ".png"];

View File

@ -1,5 +1,4 @@
import 'dart:io';
import 'dart:typed_data';
import 'package:dartssh2/dartssh2.dart';
import 'package:flutter/cupertino.dart';
@ -8,6 +7,8 @@ import 'package:gallery/data_provider/data_provider.dart';
import 'dart:ui' as ui show Codec, ImmutableBuffer;
import 'package:path_provider/path_provider.dart';
class SSHDataProvider extends DataProvider {
final String host;
final int port;
@ -48,6 +49,8 @@ class SSHDataProvider extends DataProvider {
final items = await sftpClient!.listdir(dir.path);
List<Item> res = [];
for (final val in items) {
if (val.filename == "." || val.filename == "..") continue;
if (validSuffix.any((suff) => val.filename.endsWith(suff))) {
res.add(Item(false, Uri.file(dir.path + val.filename), val.filename));
} else if (val.attr.isDirectory) {
@ -56,6 +59,10 @@ class SSHDataProvider extends DataProvider {
}
}
res.sort(
(a, b) => b.isFolder ? 1 : -1,
);
return Folder(res, dir.uri, dir.parent.uri);
}
@ -66,13 +73,8 @@ class SSHDataProvider extends DataProvider {
}
class _SSHImageProvider extends ImageProvider<_SSHImageProvider> {
/// Creates an object that decodes a [File] as an image.
///
/// The arguments must not be null.
const _SSHImageProvider(this.uri, this.sftpClient, {this.scale = 1.0})
: assert(scale != null);
const _SSHImageProvider(this.uri, this.sftpClient, {this.scale = 1.0});
/// The file to decode into an image.
final Uri uri;
final SftpClient sftpClient;
@ -113,14 +115,27 @@ class _SSHImageProvider extends ImageProvider<_SSHImageProvider> {
DecoderBufferCallback? decode, DecoderCallback? decodeDeprecated) async {
assert(key == this);
final file = await sftpClient.open(uri.toFilePath());
// todo do not load whole image in ram, create tempfile instead.
final Uint8List bytes = await file.readBytes();
Directory tempDir = await getTemporaryDirectory();
String tempPath = "${tempDir.path}/gallery";
// check if temp file exists
Uint8List bytes;
final File tmpPic = File(tempPath + uri.toFilePath());
if (await tmpPic.exists()) {
// use temp file
bytes = await tmpPic.readAsBytes();
} else {
final file = await sftpClient.open(uri.toFilePath());
// todo do not load whole image in ram, create tempfile instead.
bytes = await file.readBytes();
await tmpPic.create(recursive: true);
await tmpPic.writeAsBytes(bytes);
}
if (bytes.lengthInBytes == 0) {
// The file may become available later.
PaintingBinding.instance.imageCache.evict(key);
throw StateError('$file is empty and cannot be loaded as an image.');
throw StateError(
'bytes are empty is empty and cannot be loaded as an image.');
}
if (decode != null) {