Skip to content

Commit

Permalink
iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Sep 26, 2024
1 parent e0802b0 commit a3e6419
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 207 deletions.
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@lit-protocol/types": "6.0.0-beta.4",
"@lit-protocol/uint8arrays": "4.1.1",
"@mailchimp/mailchimp_marketing": "^3.0.80",
"@pinata/sdk": "^2.1.0",
"aws-sdk": "^2.1327.0",
"cid": "multiformats/cid",
"codeco": "^1.2.1",
Expand Down Expand Up @@ -61,6 +60,7 @@
"objection": "^3.1.4",
"pg": "^8.12.0",
"pgvector": "^0.2.0",
"pinata": "^1.4.1",
"redis": "^4.6.5",
"striptags": "^3.2.0",
"uint8arrays": "^4.0.3",
Expand Down
74 changes: 30 additions & 44 deletions api/src/controllers/file.js
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.");
}
};
2 changes: 2 additions & 0 deletions api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ app.get(
);
app.get("/ens/:ensName", metaController.getWalletByENSHandler);

app.get("/avatar/:cid", fileController.getAvatar);

app.post(
"/profile/upload_avatar",
isImage.single("file"),
Expand Down
7 changes: 4 additions & 3 deletions api/src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Ed25519Provider } from "key-did-provider-ed25519";
import { getResolver } from "key-did-resolver";
import { fromString } from "uint8arrays/from-string";

import pinataSDK from "@pinata/sdk";
// import pinataSDK from "@pinata/sdk";

import { Readable } from "stream";
//import { Readable } from "stream";

import RedisClient from "../clients/redis.js";

Expand Down Expand Up @@ -61,7 +61,7 @@ export const generateLITAction = async (conditions, definition) => {
});

actionStr = actionStr.replace("__REPLACE_THIS_AS_MODELS_OBJECT__", models);

/*
const pinata = new pinataSDK({ pinataJWTKey: process.env.PINATA_JWT_KEY });
const buffer = Buffer.from(actionStr, "utf8");
Expand All @@ -73,6 +73,7 @@ export const generateLITAction = async (conditions, definition) => {
pinataMetadata: { name: "signerFunction" },
});
return resp.IpfsHash;
*/
};

export const flattenSources = async (sources, didService) => {
Expand Down
Loading

0 comments on commit a3e6419

Please sign in to comment.