Browse Source

ipfs gateway后端列表,自动拉取与排序

pull/16/head
hole-thu 3 years ago
parent
commit
4f5d324ad6
  1. 30
      src/Common.js
  2. 30
      src/Config.js
  3. 4
      src/UserAction.js

30
src/Common.js

@ -1,8 +1,6 @@
import React, { Component, PureComponent } from 'react'; import React, { Component, PureComponent } from 'react';
import { format_time, Time, TitleLine } from './infrastructure/widgets'; import { format_time, Time, TitleLine } from './infrastructure/widgets';
import HtmlToReact from 'html-to-react'; import HtmlToReact from 'html-to-react';
import './Common.css'; import './Common.css';
import { import {
URL_PID_RE, URL_PID_RE,
@ -12,7 +10,7 @@ import {
TAG_RE, TAG_RE,
split_text, split_text,
} from './text_splitter'; } from './text_splitter';
import { save_config } from './Config';
import renderMd from './Markdown'; import renderMd from './Markdown';
export { format_time, Time, TitleLine }; export { format_time, Time, TitleLine };
@ -418,3 +416,29 @@ 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!');
});
});
}

30
src/Config.js

@ -31,7 +31,21 @@ const DEFAULT_CONFIG = {
color_scheme: 'default', color_scheme: 'default',
block_words_v2: ['#天火', '#桃花石'], block_words_v2: ['#天火', '#桃花石'],
whitelist_cw: [], whitelist_cw: [],
ipfs_gateway: ['https://<hash>.ipfs.dweb.link/'], ipfs_gateway_list: [
'https://<hash>.ipfs.dweb.link/',
'https://<hash>.ipfs.hub.textile.io/',
'https://<hash>.ipfs.infura-ipfs.io/',
'https://ipfs.adatools.io/ipfs/<hash>',
'https://<hash>.ipfs.astyanax.io/',
'https://crustwebsites.net/ipfs/<hash>',
'https://gateway.pinata.cloud/ipfs/<hash>',
'https://ipfs.eth.aragon.network/ipfs/<hash>',
'https://ipfs.best-practice.se/ipfs/<hash>',
'https://gateway.ipfs.io/ipfs/<hash>',
'https://ipfs.fleek.co/ipfs/<hash>',
'https://cloudflare-ipfs.com/ipfs/<hash>',
'https://via0.com/ipfs/<hash>',
],
}; };
export function load_config() { export function load_config() {
@ -59,9 +73,9 @@ export function load_config() {
console.log('config loaded', config); console.log('config loaded', config);
window.config = config; window.config = config;
} }
export function save_config() { export function save_config(need_load = true) {
localStorage['hole_config'] = JSON.stringify(window.config); localStorage['hole_config'] = JSON.stringify(window.config);
load_config(); if (need_load) load_config();
} }
export function bgimg_style(img, color) { export function bgimg_style(img, color) {
@ -315,7 +329,7 @@ export class ConfigUI extends PureComponent {
Object.keys(chg).forEach((key) => { Object.keys(chg).forEach((key) => {
window.config[key] = chg[key]; window.config[key] = chg[key];
}); });
save_config(); save_config(false);
} }
reset_settings() { reset_settings() {
@ -386,10 +400,12 @@ export class ConfigUI extends PureComponent {
/> />
<hr /> <hr />
<ConfigTextArea <ConfigTextArea
id="ipfs_gateway" id="ipfs_gateway_list"
callback={this.save_changes_bound} callback={this.save_changes_bound}
name="默认ipfs网关" name="候选ipfs网关"
description={'<hash>表示要替换的哈希值。只会使用第一行的。'} description={
'<hash>表示要替换的哈希值。下次上传文件会使用第一行的,上传后根据速度调整。'
}
display={(array) => array.join('\n')} display={(array) => array.join('\n')}
sift={(array) => array.filter((v) => v)} sift={(array) => array.filter((v) => v)}
parse={(string) => string.split('\n')} parse={(string) => string.split('\n')}

4
src/UserAction.js

@ -4,6 +4,7 @@ import {
SafeTextarea, SafeTextarea,
PromotionBar, PromotionBar,
HighlightedMarkdown, HighlightedMarkdown,
test_ipfs,
} from './Common'; } from './Common';
import { MessageViewer } from './Message'; import { MessageViewer } from './Message';
import { LoginPopup } from './infrastructure/widgets'; import { LoginPopup } from './infrastructure/widgets';
@ -603,13 +604,14 @@ export class PostForm extends Component {
update_text_after_upload(data) { update_text_after_upload(data) {
const { file_name, file_type } = this.state; const { file_name, file_type } = this.state;
let url = let url =
(window.config.ipfs_gateway[0] || '<hash>(无ipfs网关)').replaceAll( (window.config.ipfs_gateway_list[0] || '<hash>(无ipfs网关)').replaceAll(
'<hash>', '<hash>',
data.hash, data.hash,
) + ) +
`?filename=${encodeURIComponent(file_name)}&filetype=${encodeURIComponent( `?filename=${encodeURIComponent(file_name)}&filetype=${encodeURIComponent(
file_type, file_type,
)}`; )}`;
test_ipfs(data.hash);
let new_text = let new_text =
this.state.text + this.state.text +
'\n' + '\n' +

Loading…
Cancel
Save