From 4f5d324ad659c958531d2684c921cc4cdd190f98 Mon Sep 17 00:00:00 2001 From: hole-thu Date: Sat, 2 Apr 2022 23:53:39 +0800 Subject: [PATCH] =?UTF-8?q?ipfs=20gateway=E5=90=8E=E7=AB=AF=E5=88=97?= =?UTF-8?q?=E8=A1=A8=EF=BC=8C=E8=87=AA=E5=8A=A8=E6=8B=89=E5=8F=96=E4=B8=8E?= =?UTF-8?q?=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common.js | 30 +++++++++++++++++++++++++++--- src/Config.js | 30 +++++++++++++++++++++++------- src/UserAction.js | 4 +++- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/Common.js b/src/Common.js index 98e5a4a..2f88870 100644 --- a/src/Common.js +++ b/src/Common.js @@ -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), { 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!'); + }); + }); +} diff --git a/src/Config.js b/src/Config.js index 7a3a0b0..9c4906e 100644 --- a/src/Config.js +++ b/src/Config.js @@ -31,7 +31,21 @@ const DEFAULT_CONFIG = { color_scheme: 'default', block_words_v2: ['#天火', '#桃花石'], whitelist_cw: [], - ipfs_gateway: ['https://.ipfs.dweb.link/'], + ipfs_gateway_list: [ + 'https://.ipfs.dweb.link/', + 'https://.ipfs.hub.textile.io/', + 'https://.ipfs.infura-ipfs.io/', + 'https://ipfs.adatools.io/ipfs/', + 'https://.ipfs.astyanax.io/', + 'https://crustwebsites.net/ipfs/', + 'https://gateway.pinata.cloud/ipfs/', + 'https://ipfs.eth.aragon.network/ipfs/', + 'https://ipfs.best-practice.se/ipfs/', + 'https://gateway.ipfs.io/ipfs/', + 'https://ipfs.fleek.co/ipfs/', + 'https://cloudflare-ipfs.com/ipfs/', + 'https://via0.com/ipfs/', + ], }; 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 { />
表示要替换的哈希值。只会使用第一行的。'} + name="候选ipfs网关" + description={ + '表示要替换的哈希值。下次上传文件会使用第一行的,上传后根据速度调整。' + } display={(array) => array.join('\n')} sift={(array) => array.filter((v) => v)} parse={(string) => string.split('\n')} diff --git a/src/UserAction.js b/src/UserAction.js index d503331..a0d2469 100644 --- a/src/UserAction.js +++ b/src/UserAction.js @@ -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] || '(无ipfs网关)').replaceAll( + (window.config.ipfs_gateway_list[0] || '(无ipfs网关)').replaceAll( '', data.hash, ) + `?filename=${encodeURIComponent(file_name)}&filetype=${encodeURIComponent( file_type, )}`; + test_ipfs(data.hash); let new_text = this.state.text + '\n' +