Skip to content

Commit 66b28bb

Browse files
authored
增加piclist图床支持 (#1297)
* add piclist image hosting * add piclist svg icon * some small fix * fix piclist icon * change icons path * recover icon.js --------- Co-authored-by: yqs112358 <yqs112358.test>
1 parent 76c5d69 commit 66b28bb

11 files changed

Lines changed: 116 additions & 11 deletions

File tree

chrome/icons/piclist.png

2.67 KB
Loading
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React, { Fragment } from 'react';
2+
import { FormComponentProps } from '@ant-design/compatible/lib/form';
3+
import { Form } from '@ant-design/compatible';
4+
import '@ant-design/compatible/assets/index.less';
5+
import { Input } from 'antd';
6+
7+
interface Props extends FormComponentProps {
8+
info: {
9+
uploadUrl: string;
10+
key: string;
11+
};
12+
}
13+
14+
export default ({ form: { getFieldDecorator }, info }: Props) => {
15+
const initInfo: Partial<Props['info']> = info || {};
16+
return (
17+
<Fragment>
18+
<Form.Item label="UploadUrl">
19+
{getFieldDecorator('uploadUrl', {
20+
initialValue: initInfo.uploadUrl,
21+
rules: [
22+
{
23+
required: true,
24+
},
25+
],
26+
})(<Input placeholder="please input piclist upload URL"></Input>)}
27+
</Form.Item>
28+
<Form.Item label="Key">
29+
{getFieldDecorator('key', {
30+
initialValue: initInfo.key,
31+
})(<Input placeholder="please input upload key (if needed)"></Input>)}
32+
</Form.Item>
33+
</Fragment>
34+
);
35+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Form from './form';
2+
import { ImageHostingServiceMeta } from '../interface';
3+
import Service from './service';
4+
5+
export default (): ImageHostingServiceMeta => {
6+
return {
7+
name: 'piclist',
8+
icon: 'icons/piclist.png',
9+
type: 'piclist',
10+
service: Service,
11+
form: Form,
12+
permission: {
13+
origins: ['<all_urls>'], // often to be self-hosted
14+
},
15+
};
16+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { IBasicRequestService } from '@/service/common/request';
2+
import { RequestHelper } from '@/service/request/common/request';
3+
import { UploadImageRequest, ImageHostingService } from '../interface';
4+
import { Base64ImageToBlob } from '@/common/blob';
5+
import Container from 'typedi';
6+
import md5 from '@web-clipper/shared/lib/md5';
7+
8+
export interface PiclistImageHostingOption {
9+
uploadUrl: string;
10+
key: string;
11+
}
12+
13+
export default class PiclistImageHostingService implements ImageHostingService {
14+
private config: PiclistImageHostingOption;
15+
16+
constructor(config: PiclistImageHostingOption) {
17+
this.config = config;
18+
}
19+
20+
getId = () => {
21+
let uploadUrl = this.config.uploadUrl
22+
if(this.config.key)
23+
uploadUrl += `?key=${this.config.key}`
24+
return md5(uploadUrl) // as id
25+
};
26+
27+
uploadImage = async ({ data }: UploadImageRequest) => {
28+
const blob = Base64ImageToBlob(data);
29+
return this.uploadBlob(blob);
30+
};
31+
32+
uploadImageUrl = async (url: string) => {
33+
const imageBlob = await Container.get(IBasicRequestService).download(url);
34+
return this.uploadBlob(imageBlob);
35+
};
36+
37+
private uploadBlob = async (blob: Blob): Promise<string> => {
38+
const request = new RequestHelper({ request: Container.get(IBasicRequestService) });
39+
let uploadUrl = this.config.uploadUrl
40+
if(this.config.key)
41+
uploadUrl += `?key=${this.config.key}`
42+
let formData = new FormData();
43+
formData.append('image', blob);
44+
let result = await request.postForm<{ success: boolean, result: string[] }>(
45+
uploadUrl,
46+
{
47+
data: formData,
48+
}
49+
);
50+
if(!result.success)
51+
throw new Error("Upload failed");
52+
return result.result[0];
53+
};
54+
}

src/components/avatar/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const IconAvatar: React.FC<IconAvatarProps> = ({ avatar, size, icon: _icon }) =>
1919
} else {
2020
fontSize = size;
2121
}
22-
if (icon.startsWith('http')) {
22+
if (icon.startsWith('http') || icon.indexOf('/') != -1) {
2323
return <Avatar size={fontSize} src={icon} />;
2424
}
2525
return <IconFont style={{ fontSize }} type={icon} />;

src/components/imageHostingSelectOption/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class Page extends React.Component<PageProps> {
2525
} = this.props;
2626
let avatar;
2727

28-
if (icon.startsWith('http')) {
28+
if (icon.startsWith('http') || icon.indexOf('/') != -1) {
2929
avatar = <Avatar src={icon} className={styles.avatar} />;
3030
} else {
3131
avatar = <IconFont type={icon} style={{ fontSize: 32 }} />;

src/components/imagehostingListItem/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class Page extends React.Component<PageProps> {
3838
} = this.props;
3939
let avatar;
4040

41-
if (icon.startsWith('http')) {
41+
if (icon.startsWith('http') || icon.indexOf('/') != -1) {
4242
avatar = <Avatar src={icon} className={styles.avatar} />;
4343
} else {
4444
avatar = <IconFont type={icon} style={{ fontSize: 32 }} />;

src/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export interface RemoteConfig {
1414
}
1515

1616
let config: WebClipperConfig = {
17-
icon: 'icon.png',
18-
iconDark: 'icon-dark.png',
17+
icon: 'icons/icon.png',
18+
iconDark: 'icons/icon-dark.png',
1919
yuqueClientId: 'D1AwzCeDPLFWGfcGv7ze',
2020
yuqueCallback: 'http://webclipper-oauth.yfd.im/yuque_oauth',
2121
yuqueScope: 'doc,group,repo,attach_upload',
@@ -25,7 +25,7 @@ let config: WebClipperConfig = {
2525

2626
if (process.env.NODE_ENV === 'development') {
2727
config = Object.assign({}, config, {
28-
icon: 'icon-dev.png',
28+
icon: 'icons/icon-dev.png',
2929
});
3030
}
3131

src/service/worker/worker/workerService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class WorkerService implements IWorkerService {
77
constructor() {}
88
async changeIcon(iconColor: string): Promise<void> {
99
if (iconColor === 'light') {
10-
chrome.action.setIcon({ path: await getResourcePath('icon-dark.png') });
10+
chrome.action.setIcon({ path: await getResourcePath('icons/icon-dark.png') });
1111
} else {
12-
chrome.action.setIcon({ path: await getResourcePath('icon.png') });
12+
chrome.action.setIcon({ path: await getResourcePath('icons/icon.png') });
1313
}
1414
}
1515
async initContextMenu(): Promise<void> {

webpack/plugin/webpack-create-extension-manifest-plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async function getChromeManifest() {
3737
service_worker: './background.js',
3838
},
3939
icons: {
40-
128: 'icon.png',
40+
128: 'icons/icon.png',
4141
},
4242
commands: {
4343
'save-selection': {
@@ -76,7 +76,7 @@ async function getFirefoxManifest() {
7676
scripts: ['chrome/background.js'],
7777
},
7878
icons: {
79-
128: 'chrome/icon.png',
79+
128: 'chrome/icons/icon.png',
8080
},
8181
commands: {
8282
'save-selection': {

0 commit comments

Comments
 (0)