44 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
import 'dart:typed_data';
 | 
						|
 | 
						|
import 'package:dio/dio.dart';
 | 
						|
import 'package:freezed_annotation/freezed_annotation.dart';
 | 
						|
import 'package:http_parser/http_parser.dart';
 | 
						|
import 'package:logging/logging.dart';
 | 
						|
import 'package:moneymgr_mobile/services/api/api_client.dart';
 | 
						|
 | 
						|
part 'files_api.freezed.dart';
 | 
						|
part 'files_api.g.dart';
 | 
						|
 | 
						|
@freezed
 | 
						|
abstract class UploadResult with _$UploadResult {
 | 
						|
  const factory UploadResult({required int id}) = _UploadResult;
 | 
						|
 | 
						|
  factory UploadResult.fromJson(Map<String, dynamic> json) =>
 | 
						|
      _$UploadResultFromJson(json);
 | 
						|
}
 | 
						|
 | 
						|
extension FilesApi on ApiClient {
 | 
						|
  /// Upload a file
 | 
						|
  Future<UploadResult> uploadFile({
 | 
						|
    required String filename,
 | 
						|
    required String mimeType,
 | 
						|
    required Uint8List bytes,
 | 
						|
  }) async {
 | 
						|
    final res = await execute(
 | 
						|
      "/file",
 | 
						|
      method: "POST",
 | 
						|
      data: FormData.fromMap({
 | 
						|
        "file": MultipartFile.fromBytes(
 | 
						|
          bytes,
 | 
						|
          filename: filename,
 | 
						|
          contentType: MediaType.parse(mimeType),
 | 
						|
        ),
 | 
						|
      }),
 | 
						|
    );
 | 
						|
 | 
						|
    Logger.root.fine("Successfully uploaded file with response=${res.data}");
 | 
						|
 | 
						|
    return UploadResult.fromJson(res.data);
 | 
						|
  }
 | 
						|
}
 |