Skip to content

Commit

Permalink
fix: Fixed sendFile from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Apr 12, 2021
1 parent 2961832 commit ff63fed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 77 deletions.
87 changes: 13 additions & 74 deletions src/api/helpers/download-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import axios from 'axios';

export async function downloadFileToBase64(
_path: string,
_mines: string[]
_mines: (string | RegExp)[] = []
): Promise<string | false> {
if (!Array.isArray(_mines)) {
console.error(`set mines string array, not "${typeof _mines}" `);
Expand All @@ -38,9 +38,18 @@ export async function downloadFileToBase64(
});

const mimeType = response.headers['content-type'];
if (!_mines.includes(mimeType)) {
console.error(`Content-Type "${mimeType}" of ${_path} is not allowed`);
return false;

if (_mines.length) {
const isValidMime = _mines.some((m) => {
if (typeof m === 'string') {
return m === mimeType;
}
return m.exec(mimeType);
});
if (!isValidMime) {
console.error(`Content-Type "${mimeType}" of ${_path} is not allowed`);
return false;
}
}

const content = Buffer.from(response.data, 'binary').toString('base64');
Expand All @@ -50,73 +59,3 @@ export async function downloadFileToBase64(

return false;
}

export function MINES() {
const obj = [
'audio/aac',
'application/x-abiword',
'application/octet-stream',
'video/x-msvideo',
'application/vnd.amazon.ebook',
'application/octet-stream',
'application/x-bzip',
'application/x-bzip2',
'application/x-csh',
'text/css',
'text/csv',
'application/msword',
'application/vnd.ms-fontobject',
'application/epub+zip',
'image/gif',
'text/html',
'image/x-icon',
'text/calendar',
'application/java-archive',
'image/jpeg',
'application/javascript',
'application/json',
'audio/midi',
'video/mpeg',
'application/vnd.apple.installer+xml',
'application/vnd.oasis.opendocument.presentation',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.text',
'audio/ogg',
'video/ogg',
'application/ogg',
'font/otf',
'image/png',
'application/pdf',
'application/vnd.ms-powerpoint',
'application/x-rar-compressed',
'application/rtf',
'application/x-sh',
'image/svg+xml',
'application/x-shockwave-flash',
'application/x-tar',
'image/tiff',
'application/typescript',
'font/ttf',
'application/vnd.visio',
'audio/x-wav',
'audio/webm',
'video/webm',
'image/webp',
'font/woff',
'font/woff2',
'application/xhtml+xml',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-',
'officedocument.spreadsheetml.sheet',
'application/xml',
'application/vnd.mozilla.xul+xml',
'application/zip',
'video/3gpp',
'audio/3gpp',
'video/3gpp2',
'audio/3gpp2',
'application/x-7z-compressed',
];

return obj;
}
2 changes: 1 addition & 1 deletion src/api/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

export { fileToBase64 } from './file-to-base64';
export { base64MimeType } from './base64-mimetype';
export { downloadFileToBase64, MINES } from './download-file';
export { downloadFileToBase64 } from './download-file';
export { stickerSelect, resizeImg } from './select-sticker';
export { scrapeImg } from './scrape-img-qr';
export { scrapeLogin } from './scrape-login';
Expand Down
3 changes: 1 addition & 2 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
base64MimeType,
downloadFileToBase64,
fileToBase64,
MINES,
stickerSelect,
} from '../helpers';
import { filenameFromMimeType } from '../helpers/filename-from-mimetype';
Expand Down Expand Up @@ -397,7 +396,7 @@ export class SenderLayer extends ListenerLayer {
caption?: string
) {
return new Promise(async (resolve, reject) => {
let base64 = await downloadFileToBase64(filePath, MINES()),
let base64 = await downloadFileToBase64(filePath),
obj: { erro: boolean; to: string; text: string };

if (!base64) {
Expand Down

0 comments on commit ff63fed

Please sign in to comment.