Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'ReceiptUtils.js' lib to TypeScript #27794

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const CONST = {
ARROW_HIDE_DELAY: 3000,

API_ATTACHMENT_VALIDATIONS: {
// This is a list of file extensions that are not allowed to be uploaded to the server.
// TODO: This list should be updated to include all file types that are not allowed to be uploaded to the server.
UNALLOWED_EXTENSIONS: [''],
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved

// 24 megabytes in bytes, this is limit set on servers, do not update without wider internal discussion
MAX_SIZE: 25165824,

Expand Down
30 changes: 18 additions & 12 deletions src/libs/ReceiptUtils.js → src/libs/ReceiptUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import lodashGet from 'lodash/get';
import _ from 'underscore';
import Str from 'expensify-common/lib/str';
import {ImageSourcePropType} from 'react-native';
import * as FileUtils from './fileDownload/FileUtils';
import CONST from '../CONST';
import Receipt from './actions/Receipt';
Expand All @@ -9,34 +8,40 @@ import ReceiptDoc from '../../assets/images/receipt-doc.png';
import ReceiptGeneric from '../../assets/images/receipt-generic.png';
import ReceiptSVG from '../../assets/images/receipt-svg.png';

function validateReceipt(file) {
const {fileExtension} = FileUtils.splitExtensionFromFileName(lodashGet(file, 'name', ''));
if (_.contains(CONST.API_ATTACHMENT_VALIDATIONS.UNALLOWED_EXTENSIONS, fileExtension.toLowerCase())) {
function validateReceipt(file: File): boolean {
const {fileExtension} = FileUtils.splitExtensionFromFileName(file?.name ?? '') as {fileExtension: string};

if ((CONST.API_ATTACHMENT_VALIDATIONS.UNALLOWED_EXTENSIONS as readonly string[]).includes(fileExtension.toLowerCase())) {
Receipt.setUploadReceiptError(true, 'attachmentPicker.wrongFileType', 'attachmentPicker.notAllowedExtension');
return false;
}

if (lodashGet(file, 'size', 0) > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) {
const fileSize = file?.size ?? 0;
if (fileSize > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) {
Receipt.setUploadReceiptError(true, 'attachmentPicker.attachmentTooLarge', 'attachmentPicker.sizeExceeded');
return false;
}

if (lodashGet(file, 'size', 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) {
if (fileSize < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) {
Receipt.setUploadReceiptError(true, 'attachmentPicker.attachmentTooSmall', 'attachmentPicker.sizeNotMet');
return false;
}

return true;
}

type ThumbnailAndImageURI = {
thumbnail: string | null;
image: ImageSourcePropType | string;
};

/**
* Grab the appropriate receipt image and thumbnail URIs based on file type
*
* @param {String} path URI to image, i.e. blob:new.expensify.com/9ef3a018-4067-47c6-b29f-5f1bd35f213d or expensify.com/receipts/w_e616108497ef940b7210ec6beb5a462d01a878f4.jpg
* @param {String} filename of uploaded image or last part of remote URI
* @returns {Object}
* @param path URI to image, i.e. blob:new.expensify.com/9ef3a018-4067-47c6-b29f-5f1bd35f213d or expensify.com/receipts/w_e616108497ef940b7210ec6beb5a462d01a878f4.jpg
* @param filename of uploaded image or last part of remote URI
*/
function getThumbnailAndImageURIs(path, filename) {
function getThumbnailAndImageURIs(path: string, filename: string): ThumbnailAndImageURI {
const isReceiptImage = Str.isImage(filename);

// For local files, we won't have a thumbnail yet
Expand All @@ -48,7 +53,7 @@ function getThumbnailAndImageURIs(path, filename) {
return {thumbnail: `${path}.1024.jpg`, image: path};
}

const {fileExtension} = FileUtils.splitExtensionFromFileName(filename);
const {fileExtension} = FileUtils.splitExtensionFromFileName(filename) as {fileExtension?: string};
let image = ReceiptGeneric;
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
if (fileExtension === CONST.IOU.FILE_TYPES.HTML) {
image = ReceiptHTML;
Expand All @@ -61,6 +66,7 @@ function getThumbnailAndImageURIs(path, filename) {
if (fileExtension === CONST.IOU.FILE_TYPES.SVG) {
image = ReceiptSVG;
}

return {thumbnail: null, image};
}

Expand Down
Loading