Files
immich/mobile/lib/infrastructure/repositories/user_api.repository.dart
timonrieger 29adbcaa85 temporary workaround for 500 error
**Root cause:**
The autogenerated Dart OpenAPI client (`UsersApi.createProfileImage()`) had two issues:
1. It set `Content-Type: multipart/form-data` without a boundary, which overrode the correct header that Dart's `MultipartRequest` would set (`multipart/form-data; boundary=...`).
2. It added the file to both `mp.fields` and `mp.files`, creating a duplicate text field.

**Result:**
Multer on the server failed to parse the multipart body, so `@UploadedFile()` was `undefined` → accessing `file.path` in `UserService.createProfileImage()` threw → **500 Internal Server Error**.

**Workaround:**
Bypass the autogenerated method in `UserApiRepository.createProfileImage()` and send the multipart request directly using the same `ApiClient` (basePath + auth), ensuring:
- No manual `Content-Type` header (let `MultipartRequest` set it with boundary)
- File only in `mp.files`, not `mp.fields`
- Proper filename fallback
2026-01-26 00:19:59 +01:00

57 lines
2.2 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:http/http.dart';
import 'package:immich_mobile/domain/models/user.model.dart';
import 'package:immich_mobile/infrastructure/repositories/api.repository.dart';
import 'package:immich_mobile/infrastructure/utils/user.converter.dart';
import 'package:openapi/api.dart';
class UserApiRepository extends ApiRepository {
final UsersApi _api;
const UserApiRepository(this._api);
Future<UserDto?> getMyUser() async {
final (adminDto, preferenceDto) = await (_api.getMyUser(), _api.getMyPreferences()).wait;
if (adminDto == null) return null;
return UserConverter.fromAdminDto(adminDto, preferenceDto);
}
Future<String> createProfileImage({required String name, required Uint8List data}) async {
// Workaround: the autogenerated OpenAPI client forces `Content-Type: multipart/form-data`
// without a boundary, which can break multer parsing server-side.
// We bypass `UsersApi.createProfileImage()` and send the multipart request directly,
// while still reusing the configured basePath + auth headers + http client.
final apiClient = _api.apiClient;
final headers = <String, String>{};
await apiClient.authentication?.applyToParams(<QueryParam>[], headers);
headers.addAll(apiClient.defaultHeaderMap);
headers.remove('Content-Type');
headers.remove('content-type');
final filename = name.isNotEmpty ? name : 'profile-picture.png';
final uri = Uri.parse('${apiClient.basePath}/users/profile-image');
final request = MultipartRequest('POST', uri)
..headers.addAll(headers)
..files.add(MultipartFile.fromBytes('file', data, filename: filename));
final streamed = await apiClient.client.send(request);
final response = await Response.fromStream(streamed);
if (response.statusCode >= 400) {
throw ApiException(response.statusCode, response.body);
}
final body = jsonDecode(response.body) as Map<String, dynamic>;
return body['profileImagePath'] as String;
}
Future<List<UserDto>> getAll() async {
final dto = await checkNull(_api.searchUsers());
return dto.map(UserConverter.fromSimpleUserDto).toList();
}
}