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

This commit is contained in:
2022-04-02 23:53:39 +08:00
parent 5acd48bf4d
commit 4f5d324ad6
3 changed files with 53 additions and 11 deletions

View File

@@ -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!');
});
});
}