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

File-type version update #540

Merged
merged 12 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/deploy_beta.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: filestack-js-beta
on:
push:
branches: [ develop ]
branches: [ development ]
jobs:
build:
runs-on: ubuntu-latest
Expand Down
28,270 changes: 24,360 additions & 3,910 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@
"abab": "^2.0.6",
"debug": "^4.3.4",
"eventemitter3": "^5.0.0",
"fast-xml-parser": "^4.2.3",
"file-type": "^12.4.2",
"fast-xml-parser": "^4.2.4",
"file-type": "^16.5.4",
"follow-redirects": "^1.15.2",
"isutf8": "^4.0.0",
"jsonschema": "^1.4.1",
"lodash.clonedeep": "^4.5.0",
"p-queue": "^6.6.2",
"spark-md5": "^3.0.2",
"ts-node": "^10.9.1"
"ts-node": "^8.10.2"
},
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"@babel/preset-env": "^7.23.7",
"@inrupt/jest-jsdom-polyfills": "^1.6.0",
"@purtuga/esm-webpack-plugin": "^1.2.1",
"@types/jest": "^29.4.0",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/api/upload/file_tools.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { File as FsFile } from './file';
import { SanitizeOptions, getMimetype } from './../../utils';
import { FilestackError } from './../../../filestack_error';
import fileType from 'file-type';

export type RawFile = Blob | Buffer | File | string;
export type NamedInputFile = {
Expand Down Expand Up @@ -178,8 +177,9 @@ export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions): Pr
return readFile(file).then(
async res => {
let mime = file.type;
let minimumBytes = 4100;
if (!file.type || file.type.length === 0 || file.type === 'text/plain') {
mime = getMimetype(await res.slice(0, fileType.minimumBytes), filename);
mime = await getMimetype(await res.slice(0, minimumBytes), filename);
}

return new FsFile(
Expand Down
11 changes: 6 additions & 5 deletions src/lib/api/upload/file_tools.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const isFileBase = (input: InputFile): input is string => {
* @param {*} inputFile
* @returns {Promise<File>}
*/
export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions): Promise<FsFile> => {
export const getFile = async(input: InputFile, sanitizeOptions?: SanitizeOptions): Promise<FsFile> => {
let filename;

if (isFileNamed(input)) {
Expand All @@ -74,21 +74,21 @@ export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions): Pr
if (isFilePath(input)) {
let path = input;
return new Promise((resolve, reject) => {
require('fs').readFile(path, (err, buffer) => {
require('fs').readFile(path, async(err, buffer) => {
if (err) {
return reject(err);
}

if (!filename) {
filename = require && require('path').basename(path);
}

let mime = await getMimetype(buffer, filename);
return resolve(
new FsFile(
{
name: filename,
size: buffer.byteLength,
type: getMimetype(buffer, filename),
type: mime,
slice: (start, end) => Promise.resolve(buffer.slice(start, end)),
},
sanitizeOptions
Expand All @@ -108,12 +108,13 @@ export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions): Pr
}

if (isFileBuffer(input)) {
let mime = await getMimetype(input, filename);
return Promise.resolve(
new FsFile(
{
name: filename,
size: input.byteLength,
type: getMimetype(input, filename),
type: mime,
// @ts-ignore
slice: (start, end) => Promise.resolve(input.slice(start, end)),
},
Expand Down
1 change: 1 addition & 0 deletions src/lib/utils/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const ExtensionsMap = {
'application/octet-stream': [
'bin',
'dms',
'tdms',
'lrf',
'mar',
'so',
Expand Down
9 changes: 9 additions & 0 deletions src/lib/utils/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ describe('utils:index', () => {
const result = resolveHost(hosts, cname);
checkHosts(result, cname);
});

it('should be idempotent', () => {
const cname = 'stage.filestackapi.com';
const firstResult = resolveHost(hosts, cname);
const copiedResult = JSON.parse(JSON.stringify(firstResult));
const secondResult = resolveHost(copiedResult, cname);

expect(JSON.stringify(firstResult)).toEqual(JSON.stringify(secondResult));
});
});

describe('removeEmpty', () => {
Expand Down
13 changes: 8 additions & 5 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { Session } from '../client';
import { Hosts } from './../../config';
import { ExtensionsMap } from './extensions';
import fileType from 'file-type';
import { fromBuffer } from 'file-type';
import isutf8 from 'isutf8';

/**
Expand Down Expand Up @@ -101,9 +101,13 @@ export const uniqueId = (len: number = 10): string => {
* @param {Uint8Array | Buffer} file
* @returns {string} - mimetype
*/
export const getMimetype = (file: Uint8Array | Buffer, name?: string): string => {
let type = fileType(file);

export const getMimetype = async(file: Uint8Array | Buffer, name?: string): Promise<string> => {
let type;
try {
type = await fromBuffer(file);
} catch(e) {
console.warn("An exception occurred while processing the buffer:", e.message);
}
const excludedMimetypes = ['text/plain', 'application/octet-stream', 'application/x-ms', 'application/x-msi', 'application/zip'];

if (type && excludedMimetypes.indexOf(type.mime) === -1) {
Expand Down Expand Up @@ -134,7 +138,6 @@ export const getMimetype = (file: Uint8Array | Buffer, name?: string): string =>
return type.mime;
}

return 'application/octet-stream';
};

/**
Expand Down
5 changes: 4 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const banner = fs.readFileSync('./LICENSE', 'utf8').replace('{year}', new Date()

const config = {
mode: 'production',
node: { Buffer: false },
node: {
Buffer: false,
fs: 'empty',
},
watchOptions: {
ignored: /node_modules/
},
Expand Down
Loading