Browse Source

stop use ipfs

pull/16/head
hole-thu 3 years ago
parent
commit
c5eaf8b810
  1. 28
      src/Common.js
  2. 23
      src/Config.js
  3. 42
      src/UserAction.js

28
src/Common.js

@ -10,13 +10,13 @@ import {
TAG_RE,
split_text,
} from './text_splitter';
import { save_config } from './Config';
import renderMd from './Markdown';
export { format_time, Time, TitleLine };
export const API_BASE = `${process.env.REACT_APP_BACKEND || '/'}_api/v1`;
export const API_BASE_2 = `${process.env.REACT_APP_BACKEND || '/'}_api/v2`;
export const STORAGE_BASE = `${process.env.REACT_APP_STORAGE || ''}`;
// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
function escape_regex(string) {
@ -429,29 +429,3 @@ export class ClickHandler extends PureComponent {
);
}
}
export function test_ipfs(hash) {
let key = 'testing_' + hash;
window[key] = { curr: 0 };
window.config.ipfs_gateway_list.forEach((url) => {
fetch(url.replaceAll('<hash>', hash), { method: 'HEAD' })
.then((res) => {
if (res.ok) {
console.log(url, 'success!');
if (window[key]) {
window[key].curr += 1;
window[key][url] = window[key].curr;
let list = window.config.ipfs_gateway_list;
list.sort(
(x, y) => (window[key][x] || 9999) - (window[key][y] || 999),
);
window.config.ipfs_gateway_list = list;
save_config(false);
}
}
})
.catch((e) => {
console.log(url, 'fail!');
});
});
}

23
src/Config.js

@ -33,16 +33,6 @@ const DEFAULT_CONFIG = {
block_cw: ['xxg', 'zzxg'],
block_words_v4: ['🕷', '[系统自动代发]'],
whitelist_cw: [],
ipfs_gateway_list: [
'https://<hash>.ipfs.dweb.link/',
'https://<hash>.ipfs.infura-ipfs.io/',
'https://gateway.pinata.cloud/ipfs/<hash>',
'https://ipfs.eth.aragon.network/ipfs/<hash>',
'https://gateway.ipfs.io/ipfs/<hash>',
'https://ipfs.fleek.co/ipfs/<hash>',
'https://cloudflare-ipfs.com/ipfs/<hash>',
'https://ipfs.2read.net/ipfs/<hash>',
],
};
export function load_config() {
@ -70,6 +60,7 @@ export function load_config() {
console.log('config loaded', config);
window.config = config;
}
export function save_config(need_load = true) {
localStorage['hole_config'] = JSON.stringify(window.config);
if (need_load) load_config();
@ -411,18 +402,6 @@ export class ConfigUI extends PureComponent {
parse={(string) => string.split('\n')}
/>
<hr />
<ConfigTextArea
id="ipfs_gateway_list"
callback={this.save_changes_bound}
name="候选ipfs网关"
description={
'<hash>表示要替换的哈希值。下次上传文件会使用第一行的,上传后根据速度调整。'
}
display={(array) => array.join('\n')}
sift={(array) => array.filter((v) => v)}
parse={(string) => string.split('\n')}
/>
<hr />
<ConfigSwitch
callback={this.save_changes_bound}
id="pressure"

42
src/UserAction.js

@ -2,10 +2,10 @@ import React, { Component } from 'react';
import {
API_BASE,
API_BASE_2,
STORAGE_BASE,
SafeTextarea,
PromotionBar,
HighlightedMarkdown,
test_ipfs,
} from './Common';
import { MessageViewer } from './Message';
import { LoginPopup } from './infrastructure/widgets';
@ -671,7 +671,7 @@ export class PostForm extends Component {
xh.addEventListener('load', this.upload_complete.bind(this), false);
xh.addEventListener('error', this.upload_error.bind(this), false);
xh.addEventListener('abort', this.upload_abort.bind(this), false);
xh.open('POST', API_BASE + '/upload');
xh.open('POST', API_BASE_2 + '/upload');
xh.setRequestHeader('User-Token', this.props.token);
xh.send(f);
}
@ -679,20 +679,13 @@ export class PostForm extends Component {
update_text_after_upload(data) {
const { file_name, file_type } = this.state;
let url =
(window.config.ipfs_gateway_list[0] || '<hash>(无ipfs网关)').replaceAll(
'<hash>',
data.hash,
) +
`?filename=${encodeURIComponent(file_name)}&filetype=${encodeURIComponent(
file_type,
)}`;
test_ipfs(data.hash);
let url = `${STORAGE_BASE}/${data.path}?filename=${encodeURIComponent(
file_name,
)}&filetype=${encodeURIComponent(file_type)}`;
let new_text =
this.state.text +
'\n' +
(file_type.startsWith('image/') ? `![](${url})` : url) +
` [加载失败请点击此](${window.location.origin}/ipfs.html#${data.hash})\n\n---\n`;
(file_type.startsWith('image/') ? `![](${url})` : url);
this.setState({ text: new_text });
this.area_ref.current.set(new_text);
}
@ -884,28 +877,7 @@ export class PostForm extends Component {
</small>
</p>
<p>
<small>
首选ipfs网关可以在设置中修改如效果不佳仍可使用图床例如
<a href="https://imgtu.com/" target="_blank">
路过图床
</a>
<a href="https://sm.ms/" target="_blank">
sm.ms
</a>
<a
href="https://bbs.pku.edu.cn/v2/post-read.php?bid=154&threadid=3743"
target="_blank"
>
未名BBS
</a>
<a href="https://zhuanlan.zhihu.com/write" target="_blank">
知乎
</a>
</small>
<small>上传文件限制200M保留至少一个月也可使用第三方图床</small>
</p>
</form>
);

Loading…
Cancel
Save