Skip to content

Commit

Permalink
feat: 添加cdn的测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
easonchiu committed Jun 5, 2020
1 parent f529566 commit bb20412
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
43 changes: 43 additions & 0 deletions lib/__tests__/cdn.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import CDN from '../cdn';

describe('cdn file', () => {
it('with empty', () => {
expect(CDN.image()).toBe('https://cdn.atzuche.com/static/images/space.png');
});

it('no oss process', () => {
expect(CDN.image('hehehe')).toBe('https://carphoto.atzuche.com/hehehe');
});

it('with oss process', () => {
expect(CDN.image('hehehe', 100)).toBe(
'https://carphoto.atzuche.com/hehehe?x-oss-process=image/resize,m_fill,w_100',
);
});

it('with oss process', () => {
expect(CDN.image('hehehe', 100, 100)).toBe(
'https://carphoto.atzuche.com/hehehe?x-oss-process=image/resize,m_fill,w_100,h_100',
);
});

it('with empty', () => {
expect(CDN.asset()).toBe('https://cdn.atzuche.com/static/images/space.png');
});

it('no oss process', () => {
expect(CDN.asset('hehehe')).toBe('https://cdn.atzuche.com/hehehe');
});

it('with oss process', () => {
expect(CDN.asset('hehehe', 100)).toBe(
'https://cdn.atzuche.com/hehehe?x-oss-process=image/resize,m_fill,w_100',
);
});

it('with oss process', () => {
expect(CDN.asset('hehehe', 100, 100)).toBe(
'https://cdn.atzuche.com/hehehe?x-oss-process=image/resize,m_fill,w_100,h_100',
);
});
});
57 changes: 49 additions & 8 deletions lib/cdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,76 @@ export const cdn = !isDev

export const cdn_host = !isDev ? 'https://cdn.atzuche.com/' : 'https://cdn-test.atzuche.com/';

const withOssProcess = (url: string, width?: number, height?: number) => {
if (typeof width !== 'number' && typeof height !== 'number') {
return url;
}

let process = '';
if (typeof width === 'number') {
process += `x-oss-process=image/resize,m_fill,w_${width}`;
}
if (typeof height === 'number') {
process =
process === '' ? `x-oss-process=image/resize,m_fill,h_${height}` : `${process},h_${height}`;
}
if (url.indexOf('?') > 0) {
return `${url}&${process}`;
}
return `${url}?${process}`;
};

const CDN = {
/**
* cdn前缀的图片
* carphoto.atzuche.com
*/
image(path: string) {
image(path: string, width?: number, height?: number) {
if (!path) {
return 'https://cdn.atzuche.com/static/images/space.png';
return withOssProcess('https://cdn.atzuche.com/static/images/space.png', width, height);
}

if (path.indexOf('http') === 0) {
return withOssProcess(path, width, height);
}

if (!isDev) {
return 'https://carphoto.atzuche.com/' + path.replace(/^\/+/, '');
return withOssProcess(
'https://carphoto.atzuche.com/' + path.replace(/^\/+/, ''),
width,
height,
);
}

return 'https://at-images-test.oss-cn-hangzhou.aliyuncs.com/' + path.replace(/^\/+/, '');
return withOssProcess(
'https://at-images-test.oss-cn-hangzhou.aliyuncs.com/' + path.replace(/^\/+/, ''),
width,
height,
);
},

/**
* cdn前缀的静态资源
* cdn.atzuche.com
*/
asset(path: string) {
asset(path: string, width?: number, height?: number) {
if (!path) {
return 'https://cdn.atzuche.com/static/images/space.png';
return withOssProcess('https://cdn.atzuche.com/static/images/space.png', width, height);
}

if (path.indexOf('http') === 0) {
return withOssProcess(path, width, height);
}

if (!isDev) {
return 'https://cdn.atzuche.com/' + path.replace(/^\/+/, '');
return withOssProcess('https://cdn.atzuche.com/' + path.replace(/^\/+/, ''), width, height);
}

return 'https://cdn-test.atzuche.com/' + path.replace(/^\/+/, '');
return withOssProcess(
'https://cdn-test.atzuche.com/' + path.replace(/^\/+/, ''),
width,
height,
);
},
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auto-libs",
"version": "0.2.17",
"version": "0.2.20",
"description": "凹凸 公用组件",
"main": "build/index",
"types": "build/index",
Expand Down

0 comments on commit bb20412

Please sign in to comment.