Skip to content
Merged
5 changes: 5 additions & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const CONST = {
// Minimum width and height size in px for a selected image
AVATAR_MIN_WIDTH_PX: 80,
AVATAR_MIN_HEIGHT_PX: 80,

// Maximum width and height size in px for a selected image
AVATAR_MAX_WIDTH_PX: 4096,
AVATAR_MAX_HEIGHT_PX: 4096,

NEW_EXPENSIFY_URL: ACTIVE_EXPENSIFY_URL,
APP_DOWNLOAD_LINKS: {
ANDROID: `https://play.google.com/store/apps/details?id=${ANDROID_PACKAGE_NAME}`,
Expand Down
2 changes: 2 additions & 0 deletions src/components/AttachmentPicker/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ function getDataForUpload(fileData) {
const fileResult = {
name: fileName,
type: fileData.type,
width: fileData.width,
height: fileData.height,
uri: fileData.fileCopyUri || fileData.uri,
size: fileData.fileSize || fileData.size,
};
Expand Down
26 changes: 13 additions & 13 deletions src/components/AvatarWithImagePicker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import _ from 'underscore';
import React from 'react';
import {
Pressable, View, Image,
} from 'react-native';
import {Pressable, View} from 'react-native';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import Avatar from './Avatar';
Expand All @@ -21,6 +19,7 @@ import SpinningIndicatorAnimation from '../styles/animation/SpinningIndicatorAni
import Tooltip from './Tooltip';
import stylePropTypes from '../styles/stylePropTypes';
import * as FileUtils from '../libs/fileDownload/FileUtils';
import getImageResolution from '../libs/fileDownload/getImageResolution';

const propTypes = {
/** Avatar URL to display */
Expand Down Expand Up @@ -148,17 +147,16 @@ class AvatarWithImagePicker extends React.Component {
}

/**
* Check if the attachment resolution is bigger than required.
* Check if the attachment resolution matches constraints.
*
* @param {String} imageUri
* @param {Object} image
* @returns {Promise}
*/
isValidResolution(imageUri) {
return new Promise((resolve) => {
Image.getSize(imageUri, (width, height) => {
resolve(height >= CONST.AVATAR_MIN_HEIGHT_PX && width >= CONST.AVATAR_MIN_WIDTH_PX);
});
});
isValidResolution(image) {
return getImageResolution(image).then(resolution => (
(resolution.height >= CONST.AVATAR_MIN_HEIGHT_PX && resolution.width >= CONST.AVATAR_MIN_WIDTH_PX)
&& (resolution.height <= CONST.AVATAR_MAX_HEIGHT_PX && resolution.width <= CONST.AVATAR_MAX_WIDTH_PX)
));
}

/**
Expand All @@ -182,14 +180,16 @@ class AvatarWithImagePicker extends React.Component {
return;
}

this.isValidResolution(image.uri)
this.isValidResolution(image)
.then((isValidResolution) => {
if (!isValidResolution) {
this.showErrorModal(
this.props.translate('avatarWithImagePicker.imageUploadFailed'),
this.props.translate('avatarWithImagePicker.tooSmallResolution', {
this.props.translate('avatarWithImagePicker.resolutionConstraints', {
minHeightInPx: CONST.AVATAR_MIN_HEIGHT_PX,
minWidthInPx: CONST.AVATAR_MIN_WIDTH_PX,
maxHeightInPx: CONST.AVATAR_MAX_HEIGHT_PX,
maxWidthInPx: CONST.AVATAR_MAX_WIDTH_PX,
}),
);
return;
Expand Down
4 changes: 3 additions & 1 deletion src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ export default {
imageUploadFailed: 'Image upload failed',
deleteWorkspaceError: 'Sorry, there was an unexpected problem deleting your workspace avatar.',
sizeExceeded: ({maxUploadSizeInMB}) => `The selected image exceeds the maximum upload size of ${maxUploadSizeInMB}MB.`,
tooSmallResolution: ({minHeightInPx, minWidthInPx}) => `Please upload an image larger than ${minHeightInPx}x${minWidthInPx} pixels`,
resolutionConstraints: ({
minHeightInPx, minWidthInPx, maxHeightInPx, maxWidthInPx,
}) => `Please upload an image larger than ${minHeightInPx}x${minWidthInPx} pixels and smaller than ${maxHeightInPx}x${maxWidthInPx} pixels`,
notAllowedExtension: ({allowedExtensions}) => `Profile picture must be one of the following types: ${allowedExtensions.join(', ')}.`,
},
profilePage: {
Expand Down
4 changes: 3 additions & 1 deletion src/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ export default {
imageUploadFailed: 'Error al cargar la imagen',
deleteWorkspaceError: 'Lo sentimos, hubo un problema eliminando el avatar de su espacio de trabajo.',
sizeExceeded: ({maxUploadSizeInMB}) => `La imagen supera el tamaño máximo de ${maxUploadSizeInMB}MB.`,
tooSmallResolution: ({minHeightInPx, minWidthInPx}) => `Por favor elige una imagen mas grande que ${minHeightInPx}x${minWidthInPx} píxeles`,
resolutionConstraints: ({
minHeightInPx, minWidthInPx, maxHeightInPx, maxWidthInPx,
}) => `Por favor, elige una imagen más grande que ${minHeightInPx}x${minWidthInPx} píxeles y más pequeña que ${maxHeightInPx}x${maxWidthInPx} píxeles`,
notAllowedExtension: ({allowedExtensions}) => `La foto de perfil debe ser de uno de los siguientes tipos: ${allowedExtensions.join(', ')}.`,
},
profilePage: {
Expand Down
33 changes: 33 additions & 0 deletions src/libs/fileDownload/getImageResolution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Get image resolution
* File object is returned as a result of a user selecting image using the <input type="file" />
* We need to create a new Image object and get dimensions from it
* Opposite to native where we already have width and height properties coming from library
*
* new Image() is used specifically for performance reasons, opposed to using FileReader (5ms vs +100ms)
* because FileReader is slow and causes a noticeable delay in the UI when selecting an image.
*
* @param {*} file Picked file blob
* @returns {Promise}
*/
function getImageResolution(file) {
if (!(file instanceof File)) {
return Promise.reject(new Error('Object is not an instance of File'));
}

return new Promise((resolve, reject) => {
const image = new Image();
const objectUrl = URL.createObjectURL(file);
image.onload = function () {
resolve({
width: this.naturalWidth,
height: this.naturalHeight,
});
URL.revokeObjectURL(objectUrl);
};
image.onerror = reject;
image.src = objectUrl;
});
}

export default getImageResolution;
14 changes: 14 additions & 0 deletions src/libs/fileDownload/getImageResolution.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Get image resolution
* Image object is returned as a result of a user selecting image using the react-native-image-picker
* Image already has width and height properties coming from library so we just need to return them on native
* Opposite to web where we need to create a new Image object and get dimensions from it
*
* @param {*} file Picked file blob
* @returns {Promise}
*/
function getImageResolution(file) {
return Promise.resolve({width: file.width, height: file.height});
}

export default getImageResolution;