Secure pulling & ulouding files in minio

This commit is contained in:
2024-08-01 13:38:18 +02:00
parent be983ea382
commit d3ab42af60
3 changed files with 72 additions and 18 deletions

View File

@@ -2,8 +2,13 @@ import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
class BuildFileView extends StatefulWidget {
final String pdfLink;
const BuildFileView({super.key, required this.pdfLink});
final String link;
final String path;
const BuildFileView({
super.key,
required this.link,
required this.path,
});
@override
State<BuildFileView> createState() => _BuildFileViewState();
@@ -12,14 +17,16 @@ class BuildFileView extends StatefulWidget {
class _BuildFileViewState extends State<BuildFileView> {
late PdfViewerController pdfViewerController = PdfViewerController();
String getExtType(String pdfLink) {
String getExtType(String path) {
//print(pdfLink.split(".")[1]);
return pdfLink.split(".").last;
return path.split(".").last;
}
@override
Widget build(BuildContext context) {
if (getExtType(widget.pdfLink).toLowerCase() == "pdf") {
print(
"${widget.link} ================================================================");
if (getExtType(widget.path).toLowerCase() == "pdf") {
//print(widget.pdfLink);
return SizedBox(
width: 700,
@@ -27,7 +34,7 @@ class _BuildFileViewState extends State<BuildFileView> {
children: [
Expanded(
child: SfPdfViewer.network(
widget.pdfLink,
widget.link,
controller: pdfViewerController,
),
),
@@ -39,7 +46,7 @@ class _BuildFileViewState extends State<BuildFileView> {
return InteractiveViewer(
maxScale: 5.0,
//minScale: 0.,
child: Image.network(widget.pdfLink),
child: Image.network(widget.link),
);
}
}

View File

@@ -29,6 +29,36 @@ class BuildFilesList extends StatefulWidget {
class _BuildFilesListState extends State<BuildFilesList> {
int indexOn = 0;
final baseAPI = AppEnviroment.baseApiUrl;
String fileUrl = "";
Future<String> getFileUrlApiCall(String filePath) async {
var url;
if (AppEnviroment.getEnv() == "Dev") {
url = "$baseAPI/minio/pull/file/$filePath/dev";
} else {
url = "$baseAPI/minio/pull/file/$filePath/prod";
}
//print(url);
var response = await http.get(Uri.parse(url));
// print("here1");
//print(response.statusCode);
if (response.statusCode == 200) {
//print("here2");
String body = response.body;
//print(body);
//print("here3");
var decodedData = jsonDecode(body);
//print("Dedoced: ${decodedData['minioURL']}");
return decodedData['minioURL'];
//AppUser u = AppUser.fromJson(decodedData);
// print(u.email);
//return "AlometThere";
} else {
throw Exception("Error: GetUserData status code ${response.statusCode}");
}
}
Future<void> deleteFileApiCall(String filePath, int fileID) async {
// delete file from minio
@@ -101,7 +131,7 @@ class _BuildFilesListState extends State<BuildFilesList> {
);
}
void viewFilePopUp(String fileName, String filePath, int fileID) {
void viewFilePopUp(String fileName, String filePath, int fileID, String url) {
showDialog(
context: context,
barrierDismissible: false,
@@ -154,15 +184,18 @@ class _BuildFilesListState extends State<BuildFilesList> {
const SizedBox(height: 25.0),
Expanded(
child: BuildFileView(
pdfLink:
"${AppEnviroment.baseFileUrl}/mih/$filePath")),
link: url,
path: filePath,
//pdfLink: '${AppEnviroment.baseFileUrl}/mih/$filePath',
)),
SizedBox(
width: 300,
height: 100,
child: MyButton(
onTap: () {
html.window.open(
'${AppEnviroment.baseFileUrl}/mih/$filePath',
url,
// '${AppEnviroment.baseFileUrl}/mih/$filePath',
'download');
},
buttonText: "Dowload",
@@ -231,12 +264,20 @@ class _BuildFilesListState extends State<BuildFilesList> {
Icons.arrow_forward,
color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
),
onTap: () {
onTap: () async {
await getFileUrlApiCall(widget.files[index].file_path)
.then((urlHere) {
//print(url);
setState(() {
fileUrl = urlHere;
});
});
viewFilePopUp(
widget.files[index].file_name,
widget.files[index].file_path,
widget.files[index].idpatient_files,
);
widget.files[index].file_name,
widget.files[index].file_path,
widget.files[index].idpatient_files,
fileUrl);
},
);
},

View File

@@ -16,6 +16,7 @@ import 'package:patient_manager/objects/appUser.dart';
import 'package:patient_manager/objects/files.dart';
import 'package:supertokens_flutter/http.dart' as http;
import 'package:http/http.dart' as http2;
import 'package:supertokens_flutter/supertokens.dart';
import '../objects/patients.dart';
@@ -132,18 +133,23 @@ class _PatientFilesState extends State<PatientFiles> {
);
},
);
var token = await SuperTokens.getAccessToken();
//print(t);
//print("here1");
var request = http2.MultipartRequest(
'POST', Uri.parse("${AppEnviroment.baseApiUrl}/minio/upload/file/"));
request.headers['accept'] = 'application/json';
request.fields['app_id'] = widget.selectedPatient.app_id;
request.headers['Authorization'] = 'Bearer $token';
request.headers['Content-Type'] = 'multipart/form-data';
request.fields['app_id'] = widget.selectedPatient.app_id;
request.files.add(await http2.MultipartFile.fromBytes('file', file.bytes!,
filename: file.name.replaceAll(RegExp(r' '), '-')));
//print("here2");
var response1 = await request.send();
//print("here3");
//print(response1.statusCode);
print(response1.statusCode);
print(response1.toString());
if (response1.statusCode == 200) {
//print("here3");
var fname = file.name.replaceAll(RegExp(r' '), '-');