Skip to content

Commit 669e178

Browse files
authored
feat(utils): add converting extension to mimetype (#400)
1 parent 9cda41e commit 669e178

File tree

5 files changed

+79
-16
lines changed

5 files changed

+79
-16
lines changed

src/lib/client.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { FilestackError } from './../filestack_error';
2222
import { metadata, MetadataOptions, remove, retrieve, RetrieveOptions } from './api/file';
2323
import { transform, TransformOptions } from './api/transform';
2424
import { storeURL } from './api/store';
25-
import { resolveHost, getVersion } from './utils';
25+
import * as Utils from './utils';
2626
import { Upload, InputFile, UploadOptions, StoreUploadOptions, UploadTags } from './api/upload';
2727
import { preview, PreviewOptions } from './api/preview';
2828
import { CloudClient } from './api/cloud';
@@ -100,6 +100,16 @@ export class Client extends EventEmitter {
100100

101101
private forwardErrors: boolean = true;
102102

103+
/**
104+
* Returns filestack utils
105+
*
106+
* @readonly
107+
* @memberof Client
108+
*/
109+
get utils() {
110+
return Utils;
111+
}
112+
103113
constructor(apikey: string, options?: ClientOptions) {
104114
super();
105115

@@ -111,7 +121,7 @@ export class Client extends EventEmitter {
111121
/* istanbul ignore next */
112122
Sentry.configureScope(scope => {
113123
scope.setTag('apikey', apikey);
114-
scope.setTag('sdk-version', getVersion());
124+
scope.setTag('sdk-version', Utils.getVersion());
115125
scope.setExtra('clientOptions', options);
116126
});
117127

@@ -171,7 +181,7 @@ export class Client extends EventEmitter {
171181
}
172182

173183
this.session.cname = cname;
174-
this.session.urls = resolveHost(this.session.urls, cname);
184+
this.session.urls = Utils.resolveHost(this.session.urls, cname);
175185
}
176186

177187
/**

src/lib/request/adapters/http.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,7 @@ export class HttpAdapter implements AdapterInterface {
291291
});
292292

293293
if (config.timeout) {
294-
console.log(config.timeout);
295294
req.setTimeout(config.timeout, () => {
296-
console.log('socket timeouted===========');
297295
req.abort();
298296

299297
if (cancelListener) {

src/lib/utils/extensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
export const Map = {
18+
export const ExtensionsMap = {
1919
'application/andrew-inset': ['ez'],
2020
'application/applixware': ['aw'],
2121
'application/atom+xml': ['atom'],

src/lib/utils/index.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import { resolveCdnUrl, resolveHost, removeEmpty, uniqueTime, uniqueId, md5, sanitizeName, filterObject, b64, getVersion, cleanUpCallbacks } from './index';
17+
import { resolveCdnUrl, resolveHost, removeEmpty, uniqueTime, uniqueId, md5, extensionToMime, sanitizeName, filterObject, b64, getVersion, cleanUpCallbacks } from './index';
1818
import { config } from '../../config';
1919
const v = require('../../../../package.json').version;
2020

@@ -182,6 +182,34 @@ describe('utils:index', () => {
182182
});
183183
});
184184

185+
describe('extensionToMime', () => {
186+
it('should return mimetype if mime is passed', () => {
187+
expect(extensionToMime('')).toEqual(undefined);
188+
});
189+
190+
it('should return same mime is passed', () => {
191+
expect(extensionToMime('image/png')).toEqual('image/png');
192+
expect(extensionToMime('image/jpg')).toEqual('image/jpg');
193+
expect(extensionToMime('application/pdf')).toEqual('application/pdf');
194+
});
195+
196+
it('it should return correct mimes for ext', () => {
197+
expect(extensionToMime('.png')).toEqual('image/png');
198+
expect(extensionToMime('.jpg')).toEqual('image/jpeg');
199+
expect(extensionToMime('.pdf')).toEqual('application/pdf');
200+
201+
expect(extensionToMime('.key')).toEqual('application/vnd.apple.keynote');
202+
expect(extensionToMime('.zip')).toEqual('application/zip');
203+
expect(extensionToMime('.numbers')).toEqual('application/vnd.apple.numbers');
204+
});
205+
206+
it('it should extract extensions form filename or ext with dot', () => {
207+
expect(extensionToMime('test.png')).toEqual('image/png');
208+
expect(extensionToMime('test.jpg')).toEqual('image/jpeg');
209+
expect(extensionToMime('test.pdf')).toEqual('application/pdf');
210+
});
211+
});
212+
185213
describe('CleanupCallbacks', () => {
186214
it('should set callbacks as undefined and return untouched object', () => {
187215
const testObj = {

src/lib/utils/index.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { Session } from '../client';
1919
import { Hosts } from './../../config';
20-
import { Map } from './extensions';
20+
import { ExtensionsMap } from './extensions';
2121
import fileType from 'file-type';
2222
import * as isutf8 from 'isutf8';
2323

@@ -111,14 +111,10 @@ export const getMimetype = (file: Uint8Array | Buffer, name?: string): string =>
111111
}
112112

113113
if (name && name.indexOf('.') > -1) {
114-
const ext = name.split('.').pop();
115-
const keys = Object.keys(Map);
116-
const mapLen = keys.length;
117-
118-
for (let i = 0; i < mapLen; i++) {
119-
if (Map[keys[i]].indexOf(ext) > -1) {
120-
return keys[i];
121-
}
114+
const mime = extensionToMime(name);
115+
116+
if (mime) {
117+
return mime;
122118
}
123119
}
124120

@@ -141,6 +137,37 @@ export const getMimetype = (file: Uint8Array | Buffer, name?: string): string =>
141137
return 'application/octet-stream';
142138
};
143139

140+
/**
141+
* Change extension to according mimetype using ext=>mimetype map
142+
*
143+
* @param ext - string
144+
* @return string|boolean
145+
*/
146+
export const extensionToMime = (ext: string) => {
147+
if (!ext || ext.length === 0) {
148+
return;
149+
}
150+
151+
if (ext.split('/').length === 2) {
152+
return ext;
153+
}
154+
155+
if (ext.indexOf('.') > -1) {
156+
ext = ext.split('.').pop();
157+
}
158+
159+
const keys = Object.keys(ExtensionsMap);
160+
const mapLen = keys.length;
161+
162+
for (let i = 0; i < mapLen; i++) {
163+
if (ExtensionsMap[keys[i]].indexOf(ext) > -1) {
164+
return keys[i];
165+
}
166+
}
167+
168+
return;
169+
};
170+
144171
/**
145172
* Sanitizer Options
146173
*/

0 commit comments

Comments
 (0)