originalSize property

Size? originalSize

Returns the size of the attachment if it is an image or giffy. Otherwise, returns null.

Implementation

Size? get originalSize {
  // Return null if the attachment is not an image or giffy.
  if (type != AttachmentType.image && type != AttachmentType.giphy) {
    return null;
  }

  // Calculate size locally if the attachment is not uploaded yet.
  final file = this.file;
  if (file != null) {
    ImageInput? input;
    if (file.bytes != null) {
      input = MemoryInput(file.bytes!);
    } else if (file.path != null) {
      input = FileInput(File(file.path!));
    }

    // Return null if the file does not contain enough information.
    if (input == null) return null;

    try {
      final size = ImageSizeGetter.getSize(input);
      if (size.needRotate) {
        return Size(size.height.toDouble(), size.width.toDouble());
      }
      return Size(size.width.toDouble(), size.height.toDouble());
    } catch (e, stk) {
      debugPrint('Error getting image size: $e\n$stk');
      return null;
    }
  }

  // Otherwise, use the size provided by the server.
  final width = originalWidth;
  final height = originalHeight;
  if (width == null || height == null) return null;
  return Size(width.toDouble(), height.toDouble());
}