Skip to content

Commit

Permalink
fix(engages): read attachment bug (#3915)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mambatukaa committed Jan 2, 2023
1 parent 921cdac commit 5166379
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
31 changes: 29 additions & 2 deletions packages/api-utils/src/commonUtils.ts
@@ -1,4 +1,6 @@
import * as crypto from 'crypto';
import { getEnv } from './core';
import { URL } from 'url';

import { IEncryptionData } from './types';

Expand Down Expand Up @@ -94,11 +96,36 @@ export const pluralFormation = (type: string) => {
};

export const removeLastTrailingSlash = url => {
if (typeof url !== 'string') return url;
if (typeof url !== 'string') {
return url;
}
return url.replace(/\/$/, '');
};

export const removeExtraSpaces = text => {
if (typeof text !== 'string') return;
if (typeof text !== 'string') {
return;
}
return text.replace(/\s+/g, ' ').trim();
};

// check if valid url
export const isValidURL = (url: string): boolean => {
try {
return Boolean(new URL(url));
} catch (e) {
return false;
}
};

export const readFileUrl = (value: string) => {
if (!value || isValidURL(value) || value.includes('/')) {
return value;
}

const DOMAIN = getEnv({
name: 'DOMAIN'
});

return `${DOMAIN}/gateway/read-file?key=${value}`;
};
10 changes: 1 addition & 9 deletions packages/api-utils/src/editorAttributeUtils.ts
@@ -1,5 +1,5 @@
import * as _ from 'lodash';
import { URL } from 'url';
import { isValidURL } from './commonUtils';

export interface IReplacer {
key: string;
Expand All @@ -26,14 +26,6 @@ export interface ICustomerField {
selectOptions?: Array<{ label: string; value: string }>;
}

const isValidURL = (url: string): boolean => {
try {
return Boolean(new URL(url));
} catch (e) {
return false;
}
};

export const getCustomerName = customer => {
if (customer.firstName || customer.lastName) {
return (customer.firstName || '') + ' ' + (customer.lastName || '');
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-engages-api/src/emailUtils.ts
Expand Up @@ -4,13 +4,14 @@ import * as Random from 'meteor-random';
import { IAttachment } from '@erxes/api-utils/src/types';
import { ICustomer } from './types';
import { getEnv } from './utils';
import { readFileUrl } from '@erxes/api-utils/src/commonUtils';

dotenv.config();

const prepareAttachments = (attachments: IAttachment[] = []) => {
return attachments.map(file => ({
filename: file.name || '',
path: file.url || ''
path: readFileUrl(file.url || '')
}));
};

Expand Down

0 comments on commit 5166379

Please sign in to comment.