-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0802b0
commit a3e6419
Showing
5 changed files
with
87 additions
and
207 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,32 @@ | ||
import fs from 'fs' | ||
import pinataSDK from '@pinata/sdk'; | ||
import { Readable } from "stream"; | ||
|
||
|
||
const bufferToStream = (buffer) => { | ||
const stream = new Readable(); | ||
stream.push(buffer); | ||
stream.push(null); // Indicate end of stream | ||
return stream; | ||
import { PinataSDK } from "pinata"; | ||
|
||
const pinata = new PinataSDK({ | ||
pinataJwt: process.env.PINATA_JWT_KEY, | ||
pinataGateway: "ipfs.index.network", | ||
}); | ||
|
||
export const getAvatar = async (req, res, next) => { | ||
const url = await pinata.gateways.createSignedURL({ | ||
cid: req.params.cid, | ||
expires: 30000, | ||
}); | ||
return res.redirect(302, url); | ||
}; | ||
|
||
export const uploadAvatar = async (req, res, next) => { | ||
|
||
try { | ||
|
||
const pinata = new pinataSDK({ pinataJWTKey: process.env.PINATA_JWT_KEY}); | ||
// Assuming multer is used to handle file uploads and the file is stored temporarily | ||
|
||
|
||
const readableStreamForFile = bufferToStream(req.file.buffer); | ||
|
||
// Add the file to IPFS | ||
const options = { | ||
pinataMetadata: { | ||
name: "profilePicture.jpg", | ||
keyvalues: { | ||
// Add any key-values here you wish to associate with the upload | ||
} | ||
}, | ||
pinataOptions: { | ||
cidVersion: 0 | ||
} | ||
}; | ||
|
||
const result = await pinata.pinFileToIPFS(readableStreamForFile, options); | ||
|
||
|
||
// Respond with the IPFS hash | ||
res.json({ cid: result.IpfsHash }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('An error occurred while uploading the file to IPFS.'); | ||
} | ||
|
||
} | ||
try { | ||
// Assuming multer is used to handle file uploads and the file is stored temporarily | ||
|
||
const file = new File([req.file.buffer], "profilePicture.jpg", { | ||
type: req.file.mimetype, | ||
}); | ||
|
||
// Upload the file to IPFS via Pinata | ||
const result = await pinata.upload.file(file); | ||
|
||
// Respond with the IPFS CID | ||
res.json({ cid: result.cid }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send("An error occurred while uploading the file to IPFS."); | ||
} | ||
}; |
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
Oops, something went wrong.