Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions src/express-api/handlers/ipfsHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as express from 'express'
import { newLogger, nonEmptyStr } from '@subsocial/utils'
import { ipfs, ipfsCluster } from '../../connections/ipfs'
import { ipfsCluster } from '../../connections/ipfs'
import { maxFileSizeBytes, maxFileSizeMB } from '../config'
import { asIpfsCid } from '@subsocial/api'

const log = newLogger('IPFS req handler')

Expand All @@ -18,23 +17,12 @@ export const addContent = async (req: express.Request, res: express.Response) =>
}
}

const getContentResponse = async (res: express.Response, cids: string[]) => {
try {
const ipfsCids = (Array.isArray(cids) ? cids : [ cids ]).map(asIpfsCid)
const contents = await ipfs.getContentArrayFromIpfs(ipfsCids)
log.debug(`${contents.length} content items loaded from IPFS`)
res.json(contents)
} catch (err) {
res.json(err)
}
export const saveData = async (req: express.Request, res: express.Response) => {
const cid = await ipfsCluster.saveData(req.body)
log.debug('Content added to IPFS with CID:', cid)
res.json(cid)
}

export const getContentAsGetRequest = async (req: express.Request, res: express.Response) =>
getContentResponse(res, req.query.cids as string[])

export const getContentAsPostRequest = async (req: express.Request, res: express.Response) =>
getContentResponse(res, req.body.cids)

export const addFile = async (req: express.Request, res: express.Response) => {
if (req.file.size > maxFileSizeBytes) {
res.statusCode = 400
Expand Down
3 changes: 1 addition & 2 deletions src/express-api/routes/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ const createIpfsRoutes = () => {

// IPFS API
router.post('/addFile', upload.single('file'), ipfsReqHandlers.addFile)
router.post('/save', ipfsReqHandlers.saveData)

router.post('/add', ipfsReqHandlers.addContent)
// router.get('/get', ipfsReqHandlers.getContentAsGetRequest)
// router.post('/get', ipfsReqHandlers.getContentAsPostRequest)
router.delete('/pins/:cid', ipfsReqHandlers.deleteContent)

return router
Expand Down
5 changes: 5 additions & 0 deletions src/ipfs/ipfsCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export class IpfsClusterApi {
return this.makeRequestAndPinCid(this.ipfsNodeRequest('dag/put', content))
}

/** Add a JSON object to IPFS via `/add` and pin it via IPFS cluster. */
async saveData (content: string): Promise<IpfsCid | undefined> {
return this.makeRequestAndPinCid(this.ipfsClusterRequest('add', content))
}

/** Delete a content from IPFS by unpinning it via IPFS cluster. */
async unpinContent (cid: IpfsCid) {
try {
Expand Down