-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathipfs.ts
More file actions
36 lines (32 loc) · 1.04 KB
/
ipfs.ts
File metadata and controls
36 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import axios from 'axios'
import { ipfs } from './externalLinks'
export const ipfsUrlPrefix = 'ipfs://'
/**
* Return a URL for accessing an IPFS asset via ipfs.io
*/
export const accessIpfsUrl = (url: string): string => {
if (!url.toLowerCase().startsWith(ipfsUrlPrefix)) {
console.log('Looking for', ipfsUrlPrefix, 'found', url.toLowerCase())
throw new Error(`Invalid IPFS URL, doesn't start with ${ipfsUrlPrefix}: ${url}`)
}
return `${ipfs.proxyPrefix}${url.substring(ipfsUrlPrefix.length)}`
}
/**
* Return content-type of the asset uploaded on the provided link
*/
export const checkContentType = async (url: string) => {
try {
const response = await axios.head(url);
const contentType = response.headers['content-type'];
if (contentType && contentType.startsWith('image')) {
return 'image';
} else if (contentType && contentType.startsWith('video')) {
return 'video';
} else {
return 'unknown';
}
} catch (error) {
console.error("Error fetching NFT resource:", error);
return 'error';
}
}