uploadAvatar method

Future<void> uploadAvatar(
  1. Uint8List imageBytes
)

Uploads a new avatar image, replacing the current one.

Preprocesses the image before upload: converts to JPEG, normalizes EXIF orientation, and compresses. See _preprocessAvatar.

Updates the stored avatar filename in the database and clears the local avatar cache so the next getAvatar call fetches the new image.

Throws NotLoggedInException if not logged in. Throws AvatarTooLargeException if processed image exceeds maxAvatarSize. Throws FormatException if the image cannot be decoded.

Implementation

Future<void> uploadAvatar(Uint8List imageBytes) async {
  final user = await _database.select(_database.users).getSingleOrNull();
  if (user == null) {
    throw NotLoggedInException();
  }

  imageBytes = await _preprocessAvatar(imageBytes);

  if (imageBytes.length > maxAvatarSize) {
    throw AvatarTooLargeException(
      size: imageBytes.length,
      limit: maxAvatarSize,
    );
  }

  final newFilename = await withAuth(
    () => _portalService.uploadAvatar(imageBytes, user.avatarFilename),
  );

  await (_database.update(_database.users)
        ..where((u) => u.id.equals(user.id)))
      .write(UsersCompanion(avatarFilename: Value(newFilename)));

  await _clearAvatarCache();
}