download/save image on linux

This commit is contained in:
julian 2023-07-26 15:52:52 -06:00
parent b2aa4272b4
commit 0175939d63

View file

@ -65,60 +65,73 @@ class _MonkeyViewState extends ConsumerState<MonkeyView> {
} }
} }
void getMonkeySVG(String address) async { // void getMonkeySVG(String address) async {
if (address.isEmpty) { // if (address.isEmpty) {
//address shouldn't be empty // //address shouldn't be empty
return; // return;
} // }
//
// final http.Response response = await http
// .get(Uri.parse('https://monkey.banano.cc/api/v1/monkey/$address'));
//
// if (response.statusCode == 200) {
// final decodedResponse = response.bodyBytes;
// Directory directory = await getApplicationDocumentsDirectory();
// late Directory sampleFolder;
//
// if (Platform.isAndroid) {
// directory = Directory("/storage/emulated/0/");
// sampleFolder = Directory('${directory!.path}Documents');
// } else if (Platform.isIOS) {
// sampleFolder = Directory(directory!.path);
// } else if (Platform.isLinux) {
// sampleFolder = Directory('${directory!.path}Documents');
// } else if (Platform.isWindows) {
// sampleFolder = Directory('${directory!.path}Documents');
// } else if (Platform.isMacOS) {
// sampleFolder = Directory('${directory!.path}Documents');
// }
//
// try {
// if (!sampleFolder.existsSync()) {
// sampleFolder.createSync(recursive: true);
// }
// } catch (e, s) {
// // todo: come back to this
// debugPrint("$e $s");
// }
//
// final docPath = sampleFolder.path;
// final filePath = "$docPath/monkey.svg";
//
// File imgFile = File(filePath);
// await imgFile.writeAsBytes(decodedResponse);
// } else {
// throw Exception("Failed to get MonKey");
// }
// }
final http.Response response = await http Future<Directory?> getDocsDir() async {
.get(Uri.parse('https://monkey.banano.cc/api/v1/monkey/$address')); try {
return await getApplicationDocumentsDirectory();
if (response.statusCode == 200) { } catch (_) {
final decodedResponse = response.bodyBytes; return null;
Directory directory = await getApplicationDocumentsDirectory();
late Directory sampleFolder;
if (Platform.isAndroid) {
directory = Directory("/storage/emulated/0/");
sampleFolder = Directory('${directory!.path}Documents');
} else if (Platform.isIOS) {
sampleFolder = Directory(directory!.path);
} else if (Platform.isLinux) {
sampleFolder = Directory('${directory!.path}Documents');
} else if (Platform.isWindows) {
sampleFolder = Directory('${directory!.path}Documents');
} else if (Platform.isMacOS) {
sampleFolder = Directory('${directory!.path}Documents');
}
try {
if (!sampleFolder.existsSync()) {
sampleFolder.createSync(recursive: true);
}
} catch (e, s) {
// todo: come back to this
debugPrint("$e $s");
}
final docPath = sampleFolder.path;
final filePath = "$docPath/monkey.svg";
File imgFile = File(filePath);
await imgFile.writeAsBytes(decodedResponse);
} else {
throw Exception("Failed to get MonKey");
} }
} }
void getMonkeyPNG(String address) async { Future<void> downloadMonkey(String address, bool isPNG) async {
if (address.isEmpty) { if (address.isEmpty) {
//address shouldn't be empty //address shouldn't be empty
return; return;
} }
final http.Response response = await http.get(Uri.parse( String url = "https://monkey.banano.cc/api/v1/monkey/$address";
'https://monkey.banano.cc/api/v1/monkey/${address}?format=png&size=512&background=false'));
if (isPNG) {
url += '?format=png&size=512&background=false';
}
final http.Response response = await http.get(Uri.parse(url));
if (response.statusCode == 200) { if (response.statusCode == 200) {
if (Platform.isAndroid) { if (Platform.isAndroid) {
@ -126,33 +139,30 @@ class _MonkeyViewState extends ConsumerState<MonkeyView> {
} }
final decodedResponse = response.bodyBytes; final decodedResponse = response.bodyBytes;
Directory directory = await getApplicationDocumentsDirectory(); final Directory? sampleFolder = await getDocsDir();
late Directory sampleFolder;
if (Platform.isAndroid) { print("PATH: ${sampleFolder?.path}");
directory = Directory("/storage/emulated/0/");
sampleFolder = Directory('${directory!.path}Documents'); if (sampleFolder == null) {
} else if (Platform.isIOS) { print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
sampleFolder = Directory(directory!.path); return;
} else if (Platform.isLinux) {
sampleFolder = Directory('${directory!.path}Documents');
} else if (Platform.isWindows) {
sampleFolder = Directory('${directory!.path}Documents');
} else if (Platform.isMacOS) {
sampleFolder = Directory('${directory!.path}Documents');
} }
try { // try {
if (!sampleFolder.existsSync()) { // if (!sampleFolder.existsSync()) {
sampleFolder.createSync(recursive: true); // sampleFolder.createSync(recursive: true);
} // }
} catch (e, s) { // } catch (e, s) {
// todo: come back to this // // todo: come back to this
debugPrint("$e $s"); // debugPrint("$e $s");
} // }
final docPath = sampleFolder.path; final docPath = sampleFolder.path;
final filePath = "$docPath/monkey.png"; String filePath = "$docPath/monkey_$address";
filePath += isPNG ? ".png" : ".svg";
// todo check if monkey.png exists
File imgFile = File(filePath); File imgFile = File(filePath);
await imgFile.writeAsBytes(decodedResponse); await imgFile.writeAsBytes(decodedResponse);
@ -318,11 +328,11 @@ class _MonkeyViewState extends ConsumerState<MonkeyView> {
flex: 1, flex: 1,
), ),
if (imageBytes != null) if (imageBytes != null)
Container( SizedBox(
child: SvgPicture.memory(
Uint8List.fromList(imageBytes!)),
width: 300, width: 300,
height: 300, height: 300,
child:
SvgPicture.memory(Uint8List.fromList(imageBytes)),
), ),
const Spacer( const Spacer(
flex: 1, flex: 1,
@ -332,16 +342,28 @@ class _MonkeyViewState extends ConsumerState<MonkeyView> {
child: Column( child: Column(
children: [ children: [
SecondaryButton( SecondaryButton(
label: "Download as SVG", label: "Save as SVG",
onPressed: () async { onPressed: () async {
getMonkeySVG(receivingAddress); await showLoading(
whileFuture:
downloadMonkey(receivingAddress, false),
context: context,
isDesktop: Util.isDesktop,
message: "Saving MonKey svg",
);
}, },
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
SecondaryButton( SecondaryButton(
label: "Download as PNG", label: "Download as PNG",
onPressed: () { onPressed: () async {
getMonkeyPNG(receivingAddress); await showLoading(
whileFuture:
downloadMonkey(receivingAddress, true),
context: context,
isDesktop: Util.isDesktop,
message: "Downloading MonKey png",
);
}, },
), ),
], ],