fp/packages/link2cid/README.md

91 lines
2.0 KiB
Markdown

# link2cid
## Motivation
I wish I could give [kubo](https://github.com/ipfs/kubo) or [IPFS cluster](https://ipfscluster.io/) a URI to a file and then they would download the file and add to ipfs, returning me a [CID](https://docs.ipfs.tech/concepts/glossary/#cid).
However, neither kubo nor IPFS cluster can do this.
link2cid solves this issue with a REST API for adding a file at `url` to IPFS.
## Usage
Configure environment
Create a `.env` file. See `.env.example` for an example. Important environment variables are `API_KEY`, `PORT`, and `IPFS_URL`.
Install and run
```bash
pnpm install
pnpm start
```
Make a GET REST request to `/add` with `url` as a query parameter. Expect a [SSE](https://wikipedia.org/wiki/Server-sent_events) response.
## dokku
dokku builder-dockerfile:set link2cid dockerfile-path link2cid.Dockerfile
### Examples
#### [HTTPIE](https://httpie.io)
```bash
http -A bearer -a $API_KEY --stream 'http://localhost:3939/add?url=https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png' Accept:text/event-stream
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Connection: keep-alive
Content-Type: text/event-stream; charset=utf-8
Date: Thu, 21 Dec 2023 11:20:24 GMT
Transfer-Encoding: identity
X-Powered-By: Express
:ok
event: dlProgress
data: {
"percent": 100
}
event: addProgress
data: {
"percent": 100
}
event: end
data: {
"cid": "bafkreidj3jo7efguloaixz6vgivljlmowagagjtqv4yanyqgty2hrvg6km"
}
```
#### Javascript
@todo this is incomplete/untested
```js
await fetch('http://localhost:3939/add?url=https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png', {
headers: {
'accept': 'text/event-stream',
'authorization': `Bearer ${API_KEY}`
}
});
```
## Dev notes
### Generate API_KEY
```js
require('crypto').randomBytes(64).toString('hex')
```
### `TypeError: data.split is not a function`
If you see this error, make sure data in SSE event payload is a string, not a number.