Skip to content

Commit

Permalink
refacto ipfs + solve issues (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksibisamir committed May 15, 2023
2 parents 03bbe11 + 1e5e93d commit c9e2306
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions controllers/campaign.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,15 @@ module.exports.launchBounty = async (req, res) => {
exports.uploadPictureToIPFS = async (req, res) => {
// using IPFS
try {

if (req.file) {
const { id } = req.params

// SEARCH COMPAIGN ID
const campaign = await Campaigns.findOne({_id: id, idNode: '0'+req.user._id});

if(campaign) {

// IPFS CONNECTION
const ipfs = await ipfsConnect()

Expand All @@ -479,15 +487,20 @@ exports.uploadPictureToIPFS = async (req, res) => {
// ADD TO IPFS
let buffer = Buffer.from(x)
let result = await ipfs.add({ content: buffer })

// REMOVE FILE FROM UPLOADS DIR
fs.unlinkSync('uploads/' + req.file.filename)

return responseHandler.makeResponseData(res, 200, result, true)

} else return responseHandler.makeResponseData(res, 400, "campaign not found / you are not the owner", false)



} else
return responseHandler.makeResponseData(
res,
200,
400,
'required picture',
false
)
Expand Down Expand Up @@ -1756,7 +1769,7 @@ exports.update = async (req, res) => {
let campaign = req.body
campaign.updatedAt = Date.now()
let updatedCampaign = await Campaigns.findOneAndUpdate(
{ _id: req.params.idCampaign },
{ _id: req.params.idCampaign , idNode: '0'+req.user._id},
{ $set: campaign },
{ new: true }
)
Expand Down
2 changes: 1 addition & 1 deletion routes/campaign.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ router.post('/launchBounty', verifyAuth, launchBounty)
router.get('/campaigns', campaigns)


router.post('/ipfs', verifyAuth,campaignsPictureUpload ,uploadPictureToIPFS)
router.post('/ipfs/:id', verifyAuth,campaignsPictureUpload ,uploadPictureToIPFS)

/**
* @swagger
Expand Down

0 comments on commit c9e2306

Please sign in to comment.