ipfs gateway后端列表,自动拉取与排序
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import React, { Component, PureComponent } from 'react';
|
||||
import { format_time, Time, TitleLine } from './infrastructure/widgets';
|
||||
|
||||
import HtmlToReact from 'html-to-react';
|
||||
|
||||
import './Common.css';
|
||||
import {
|
||||
URL_PID_RE,
|
||||
@@ -12,7 +10,7 @@ import {
|
||||
TAG_RE,
|
||||
split_text,
|
||||
} from './text_splitter';
|
||||
|
||||
import { save_config } from './Config';
|
||||
import renderMd from './Markdown';
|
||||
|
||||
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!');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -31,7 +31,21 @@ const DEFAULT_CONFIG = {
|
||||
color_scheme: 'default',
|
||||
block_words_v2: ['#天火', '#桃花石'],
|
||||
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() {
|
||||
@@ -59,9 +73,9 @@ export function load_config() {
|
||||
console.log('config loaded', config);
|
||||
window.config = config;
|
||||
}
|
||||
export function save_config() {
|
||||
export function save_config(need_load = true) {
|
||||
localStorage['hole_config'] = JSON.stringify(window.config);
|
||||
load_config();
|
||||
if (need_load) load_config();
|
||||
}
|
||||
|
||||
export function bgimg_style(img, color) {
|
||||
@@ -315,7 +329,7 @@ export class ConfigUI extends PureComponent {
|
||||
Object.keys(chg).forEach((key) => {
|
||||
window.config[key] = chg[key];
|
||||
});
|
||||
save_config();
|
||||
save_config(false);
|
||||
}
|
||||
|
||||
reset_settings() {
|
||||
@@ -386,10 +400,12 @@ export class ConfigUI extends PureComponent {
|
||||
/>
|
||||
<hr />
|
||||
<ConfigTextArea
|
||||
id="ipfs_gateway"
|
||||
id="ipfs_gateway_list"
|
||||
callback={this.save_changes_bound}
|
||||
name="默认ipfs网关"
|
||||
description={'<hash>表示要替换的哈希值。只会使用第一行的。'}
|
||||
name="候选ipfs网关"
|
||||
description={
|
||||
'<hash>表示要替换的哈希值。下次上传文件会使用第一行的,上传后根据速度调整。'
|
||||
}
|
||||
display={(array) => array.join('\n')}
|
||||
sift={(array) => array.filter((v) => v)}
|
||||
parse={(string) => string.split('\n')}
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
SafeTextarea,
|
||||
PromotionBar,
|
||||
HighlightedMarkdown,
|
||||
test_ipfs,
|
||||
} from './Common';
|
||||
import { MessageViewer } from './Message';
|
||||
import { LoginPopup } from './infrastructure/widgets';
|
||||
@@ -603,13 +604,14 @@ export class PostForm extends Component {
|
||||
update_text_after_upload(data) {
|
||||
const { file_name, file_type } = this.state;
|
||||
let url =
|
||||
(window.config.ipfs_gateway[0] || '<hash>(无ipfs网关)').replaceAll(
|
||||
(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 new_text =
|
||||
this.state.text +
|
||||
'\n' +
|
||||
|
||||
Reference in New Issue
Block a user