feat: Add receipt import functionality with file upload and parsing
- Implemented receipt file upload in ImportRepository with multipart request handling. - Created ParsedReceiptItem model for parsed receipt data. - Added ReceiptImportTab for user interface to upload and review receipts. - Updated ImportScreen to include the new ReceiptImportTab alongside RecipeImportTab. - Introduced flutter_bootstrap.js and index.html for web app initialization. - Added wimp.wasm and flutter.js for enhanced web performance and capabilities.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import '../domain/parsed_receipt_item.dart';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
@@ -12,6 +13,34 @@ import '../domain/quick_import_result.dart';
|
||||
/// • [importFile] — multipart upload (PDF / image bytes, max 10 MB).
|
||||
/// • [importUrl] — JSON body with `{ input: url }`.
|
||||
class ImportRepository {
|
||||
/// Upload a receipt file for parsing (Fas 6b).
|
||||
/// Returns a list of parsed receipt items.
|
||||
Future<List<ParsedReceiptItem>> importReceiptFile({
|
||||
required Uint8List bytes,
|
||||
required String filename,
|
||||
String? token,
|
||||
}) async {
|
||||
final uri = Uri.parse('$_baseUrl/receipt-import');
|
||||
final request = http.MultipartRequest('POST', uri);
|
||||
if (token != null) {
|
||||
request.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
request.files.add(
|
||||
http.MultipartFile.fromBytes('file', bytes, filename: filename),
|
||||
);
|
||||
final streamed = await _client.send(request).timeout(
|
||||
const Duration(seconds: 120),
|
||||
onTimeout: () => throw ApiException(
|
||||
type: ApiErrorType.network,
|
||||
message: 'Importen tog för lång tid. Försök igen.',
|
||||
),
|
||||
);
|
||||
final response = await http.Response.fromStream(streamed);
|
||||
final parsed = _parseResponse(response);
|
||||
final items = (parsed['items'] as List?) ?? parsed as List?;
|
||||
if (items == null) throw ApiException(type: ApiErrorType.unknown, message: 'Felaktigt svar från servern.');
|
||||
return items.map((e) => ParsedReceiptItem.fromJson(e as Map<String, dynamic>)).toList();
|
||||
}
|
||||
final http.Client _client;
|
||||
final String _baseUrl;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user