mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
Implemented user profile upload and show on web/mobile (#191)
* Update mobile dependencies * Added image picker * Added mechanism to upload profile image * Added image type to send to web * Added styling for circle avatar * Fixxed issue with sharp cannot resize image properly * Finished displaying and uploading user profile * Added user profile to web
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class UploadProfileImageResponse {
|
||||
final String userId;
|
||||
final String profileImagePath;
|
||||
UploadProfileImageResponse({
|
||||
required this.userId,
|
||||
required this.profileImagePath,
|
||||
});
|
||||
|
||||
UploadProfileImageResponse copyWith({
|
||||
String? userId,
|
||||
String? profileImagePath,
|
||||
}) {
|
||||
return UploadProfileImageResponse(
|
||||
userId: userId ?? this.userId,
|
||||
profileImagePath: profileImagePath ?? this.profileImagePath,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
final result = <String, dynamic>{};
|
||||
|
||||
result.addAll({'userId': userId});
|
||||
result.addAll({'profileImagePath': profileImagePath});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
factory UploadProfileImageResponse.fromMap(Map<String, dynamic> map) {
|
||||
return UploadProfileImageResponse(
|
||||
userId: map['userId'] ?? '',
|
||||
profileImagePath: map['profileImagePath'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory UploadProfileImageResponse.fromJson(String source) => UploadProfileImageResponse.fromMap(json.decode(source));
|
||||
|
||||
@override
|
||||
String toString() => 'UploadProfileImageReponse(userId: $userId, profileImagePath: $profileImagePath)';
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is UploadProfileImageResponse && other.userId == userId && other.profileImagePath == profileImagePath;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => userId.hashCode ^ profileImagePath.hashCode;
|
||||
}
|
||||
@@ -2,8 +2,15 @@ import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:immich_mobile/shared/models/upload_profile_image_repsonse.model.dart';
|
||||
import 'package:immich_mobile/shared/models/user_info.model.dart';
|
||||
import 'package:immich_mobile/shared/services/network.service.dart';
|
||||
import 'package:immich_mobile/utils/dio_http_interceptor.dart';
|
||||
import 'package:immich_mobile/utils/files_helper.dart';
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
|
||||
class UserService {
|
||||
final NetworkService _networkService = NetworkService();
|
||||
@@ -21,4 +28,39 @@ class UserService {
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
Future<UploadProfileImageResponse?> uploadProfileImage(XFile image) async {
|
||||
var dio = Dio();
|
||||
dio.interceptors.add(AuthenticatedRequestInterceptor());
|
||||
String savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
||||
var mimeType = FileHelper.getMimeType(image.path);
|
||||
|
||||
final imageData = MultipartFile.fromBytes(
|
||||
await image.readAsBytes(),
|
||||
filename: image.name,
|
||||
contentType: MediaType(
|
||||
mimeType["type"],
|
||||
mimeType["subType"],
|
||||
),
|
||||
);
|
||||
|
||||
final formData = FormData.fromMap({'file': imageData});
|
||||
|
||||
try {
|
||||
Response res = await dio.post(
|
||||
'$savedEndpoint/user/profile-image',
|
||||
data: formData,
|
||||
);
|
||||
|
||||
var payload = UploadProfileImageResponse.fromJson(res.toString());
|
||||
|
||||
return payload;
|
||||
} on DioError catch (e) {
|
||||
debugPrint("Error uploading file: ${e.response}");
|
||||
return null;
|
||||
} catch (e) {
|
||||
debugPrint("Error uploading file: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user