-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve ipfs media download when file its in folder #7752
Open
evavirseda
wants to merge
8
commits into
develop
Choose a base branch
from
feat/improve-ipfs-media-download
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8665596
feat: add getIpfsUri function
evavirseda 885abae
feat: display ipfs media
evavirseda 10d6735
fix types
evavirseda 2edca0e
fix: check dowloadUrl and replace with constant
evavirseda 14db8a8
fix: check if ipfsHash exists
evavirseda 0e5b108
Merge branch 'develop' into feat/improve-ipfs-media-download
evavirseda 085399e
fix: ipfs hash and ipfs uri
brancoder a073190
Merge branch 'develop' into feat/improve-ipfs-media-download
brancoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
interface IIpfsLink { | ||
Hash: string | ||
Name: string | ||
Size: number | ||
Target: string | ||
Type: number | ||
Mode?: string | ||
Mtime?: number | ||
MtimeNsecs?: number | ||
} | ||
|
||
interface IIPfsEntry { | ||
readonly type: 'dir' | 'file' | ||
readonly cid: string | ||
readonly name: string | ||
readonly path: string | ||
mode?: number | ||
mtime?: { | ||
secs: number | ||
nsecs?: number | ||
} | ||
size: number | ||
} | ||
|
||
interface IIpfsObject { | ||
Hash: string | ||
Links: IIpfsLink[] | ||
} | ||
|
||
enum typeOfLink { | ||
Dir = 'dir', | ||
File = 'file', | ||
} | ||
|
||
const IPFS_ENDPOINT = 'https://ipfs.io' | ||
const IPFS_PATH = '/api/v0/ls' | ||
const IPFS_PREFIX = '/ipfs/' | ||
|
||
export async function getIpfsUri(link: { path?: string; hash: string }): Promise<string | undefined> { | ||
try { | ||
const slicedLink = link.hash.slice(IPFS_ENDPOINT.length) | ||
const ipfsEntry = await ls(slicedLink) | ||
if (ipfsEntry) { | ||
if (ipfsEntry.type === 'dir') { | ||
const path = `${link.path ?? ''}/${ipfsEntry.name}` | ||
return await getIpfsUri({ hash: link.hash, path }) | ||
} | ||
return `${link.hash}/${encodeURIComponent(ipfsEntry.name)}` | ||
} | ||
} catch (error) { | ||
console.error('error', error) | ||
} | ||
} | ||
|
||
async function ls(path: string): Promise<IIPfsEntry | undefined> { | ||
let ipfsEntry: IIPfsEntry | undefined | ||
|
||
try { | ||
const baseUrl = IPFS_ENDPOINT | ||
const method = 'get' | ||
const payload = undefined | ||
let headers = {} | ||
const timeout = undefined | ||
|
||
headers ??= {} | ||
|
||
let controller: AbortController | undefined | ||
let timerId: NodeJS.Timeout | undefined | ||
|
||
if (timeout !== undefined) { | ||
controller = new AbortController() | ||
timerId = setTimeout(() => { | ||
if (controller) { | ||
controller.abort() | ||
} | ||
}, timeout) | ||
} | ||
|
||
try { | ||
if (path.includes('ipfs')) { | ||
const response = await fetch(`${baseUrl}${IPFS_PATH}?arg=/${path}`, { | ||
method, | ||
headers, | ||
body: payload ? JSON.stringify(payload) : undefined, | ||
signal: controller ? controller.signal : undefined, | ||
}) | ||
const lsResponse = (await response.json()) as { Objects: IIpfsObject[] } | ||
const result = lsResponse.Objects[0] | ||
if (result) { | ||
const links = result.Links | ||
if (links.length > 0) { | ||
ipfsEntry = mapLinkToIpfsEntry(links[0], path) | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
console.error('error', error) | ||
} finally { | ||
if (timerId) { | ||
clearTimeout(timerId) | ||
} | ||
} | ||
} catch (error) { | ||
console.error('error', error) | ||
} | ||
|
||
return ipfsEntry | ||
} | ||
|
||
function mapLinkToIpfsEntry(link: IIpfsLink, path: string): IIPfsEntry { | ||
const hash = link.Hash.startsWith(IPFS_PREFIX) ? link.Hash.slice(IPFS_PREFIX.length) : link.Hash | ||
const entry: IIPfsEntry = { | ||
name: link.Name, | ||
path: path + (link.Name ? `/${link.Name}` : ''), | ||
size: link.Size, | ||
cid: hash, | ||
type: typeOf(link), | ||
} | ||
if (link.Mode) { | ||
entry.mode = Number.parseInt(link.Mode, 8) | ||
} | ||
|
||
if (link.Mtime !== undefined && link.Mtime !== null) { | ||
entry.mtime = { | ||
secs: link.Mtime, | ||
} | ||
|
||
if (link.MtimeNsecs !== undefined && link.MtimeNsecs !== null) { | ||
entry.mtime.nsecs = link.MtimeNsecs | ||
} | ||
} | ||
|
||
return entry | ||
} | ||
|
||
function typeOf(link: IIpfsLink): typeOfLink { | ||
switch (link.Type) { | ||
case 1: | ||
case 5: { | ||
return typeOfLink.Dir | ||
} | ||
case 2: { | ||
return typeOfLink.File | ||
} | ||
default: { | ||
return typeOfLink.File | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking something along the lines like this check we have in explorer: https://github.com/iotaledger/explorer/blob/dev/client/src/helpers/hooks/useNftMetadataUri.ts#L22
Looks to me that we now try to
getIpfsUri
even ifdownloadUrl
is not ipfs link.