getDocumentFile method

Future<String> getDocumentFile(
  1. String host,
  2. String docId,
  3. String directory
)

Download a document (PDF) and save it to the local directory.

host - The host server URL. docId - The ID of the document. directory - The directory where the document will be saved.

Returns the path of the downloaded document file.

Implementation

Future<String> getDocumentFile(
    String host, String docId, String directory) async {
  final url = '$host/api/storage/documents/$docId/content';

  try {
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      final timestamp = DateTime.now().millisecondsSinceEpoch;
      final docPath = join(directory, 'document_$timestamp.pdf');
      final file = File(docPath);
      await file.writeAsBytes(response.bodyBytes);
      return docPath;
    }
  } catch (error) {}

  return '';
}