feat: use ipfs protocol in content and change ipfs service

This commit is contained in:
DIYgod
2022-08-23 15:18:48 +01:00
parent 4fd30a0d63
commit 52c185eabf
8 changed files with 419 additions and 70 deletions
+3 -5
View File
@@ -4,6 +4,7 @@ import { Profile, Note } from "~/lib/types"
import { Disclosure } from "@headlessui/react"
import { ChevronUpIcon } from "@heroicons/react/solid"
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
import { toIPFS, toGateway } from "~/lib/ipfs-parser"
export const BlockchainInfo: React.FC<{
site?: Profile
@@ -97,7 +98,7 @@ export const BlockchainInfo: React.FC<{
href={url}
key={url}
>
{url.replace(IPFS_GATEWAY, "ipfs://")}
{toIPFS(url)}
</a>
)
})
@@ -106,10 +107,7 @@ export const BlockchainInfo: React.FC<{
target="_blank"
rel="noreferrer"
className="inline-block mr-4 break-all"
href={site?.metadata?.uri.replace(
"ipfs://",
IPFS_GATEWAY,
)}
href={toGateway(site?.metadata?.uri)}
key={site?.metadata?.uri}
>
{site?.metadata?.uri}
+2 -4
View File
@@ -1,6 +1,7 @@
import clsx from "clsx"
import React, { useMemo } from "react"
import { IPFS_GATEWAY } from "~/lib/env"
import { toGateway } from "~/lib/ipfs-parser"
export const Avatar: React.FC<
{
@@ -22,10 +23,7 @@ export const Avatar: React.FC<
const image = useMemo(() => {
for (const image of images) {
if (image)
return image
.replace("ipfs://", IPFS_GATEWAY)
.replace("https://gateway.ipfs.io/ipfs/", IPFS_GATEWAY)
if (image) return toGateway(image)
}
}, [images])
+13
View File
@@ -0,0 +1,13 @@
import { IPFS_GATEWAY } from "~/lib/env"
export const toGateway = (url: string) => {
return url
?.replace("ipfs://", IPFS_GATEWAY)
.replace("https://gateway.ipfs.io/ipfs/", IPFS_GATEWAY)
.replace("https://ipfs.io/ipfs/", IPFS_GATEWAY)
.replace("https://cf-ipfs.com/ipfs/", IPFS_GATEWAY)
}
export const toIPFS = (url: string) => {
return url?.replace(IPFS_GATEWAY, "ipfs://")
}
+15 -14
View File
@@ -1,18 +1,19 @@
import { Web3Storage } from "web3.storage"
import { IPFS_GATEWAY } from "../lib/env"
import axios from "axios"
export const UploadFile = async (blob: Blob, filename: string) => {
const file = new File([blob], filename)
const web3Storage = new Web3Storage({
token:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDAyMDIwODZmRjU5OUU0Y0YyMzM4MkUzNjg1Y0NmZUEyOGNBODBCOTAiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NTIzNjM1Njk3NDUsIm5hbWUiOiJVbmlkYXRhIn0.XmsAuXvbTj4BFhZlJK4xXfbd0ltVZJCEhqdYcW_kLOo",
} as any)
const cid = await web3Storage.put([file], {
name: file.name,
maxRetries: 3,
wrapWithDirectory: false,
})
export const UploadFile = async (blob: Blob) => {
const formData = new FormData()
formData.append("file", blob)
const res = await axios.post(
"https://ipfs-relay.crossbell.io/upload",
formData,
{
headers: {
"Content-Type": "multipart/form-data",
},
},
)
return {
key: `${IPFS_GATEWAY}${cid}`,
key: res.data.url,
}
}
+6 -1
View File
@@ -2,6 +2,7 @@ import { Root } from "rehype-raw"
import { Plugin } from "unified"
import { visit } from "unist-util-visit"
import { getUserContentsUrl } from "~/lib/user-contents"
import { toGateway } from "~/lib/ipfs-parser"
const isExternLink = (url: string) => /^https?:\/\//.test(url)
@@ -10,12 +11,16 @@ export const rehypeImage: Plugin<Array<void>, Root> = () => {
visit(tree, { tagName: "img" }, (node) => {
if (!node.properties) return
const url = node.properties.src
let url = node.properties.src
if (!url || typeof url !== "string") {
return
}
const ipfsUrl = toGateway(url)
node.properties.src = ipfsUrl
url = ipfsUrl
if (isExternLink(url)) {
if (!url.startsWith("https:")) {
throw new Error(`External image url must start with https`)
+6 -1
View File
@@ -1,6 +1,7 @@
import { Root } from "rehype-raw"
import { Plugin } from "unified"
import { visit } from "unist-util-visit"
import { toGateway } from "~/lib/ipfs-parser"
const isExternLink = (url: string) => /^https?:\/\//.test(url)
@@ -9,12 +10,16 @@ export const rehypeExternalLink: Plugin<Array<void>, Root> = () => {
visit(tree, { type: "element", tagName: "a" }, (node) => {
if (!node.properties) return
const url = node.properties.href
let url = node.properties.href
if (!url || typeof url !== "string") {
return
}
const ipfsUrl = toGateway(url)
node.properties.href = ipfsUrl
url = ipfsUrl
if (isExternLink(url)) {
node.properties.target = "_blank"
node.properties.rel = "noopener noreferrer"