Skip to content

Commit

Permalink
improved IPFS
Browse files Browse the repository at this point in the history
  • Loading branch information
1M4nt0 committed Feb 15, 2024
1 parent e95d207 commit cfc5f59
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 61 deletions.
8 changes: 4 additions & 4 deletions apps/web/pages/api/v0/course/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const handlePostRequest = async (
.json({ status: ApiResponseStatus.error, message: 'Bad request.' })
}

const { mimetype, filepath, originalFilename } = imageFile
const { mimetype, filepath } = imageFile
const courseImageBuffer = fs.readFileSync(filepath)

const nameFormatted = name.replace(' ', '_')
const ipfsCourseImageData = await IpfsConnector.upload({
fileContent: courseImageBuffer,
fileName: originalFilename ?? '',
fileName: `ci_${nameFormatted}`,
mimeType: mimetype ?? '',
})

Expand All @@ -49,7 +49,7 @@ const handlePostRequest = async (
'snapshot-ens': snapshotEns,
'media-channel': mediaChannel,
},
fileName: '',
fileName: `cd_${nameFormatted}`,
mimeType: 'application/json',
})

Expand Down
80 changes: 39 additions & 41 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
{
"name": "dae",
"dependencies": {
"dotenv-cli": "latest",
"eslint": "8.29.0",
"prettier": "2.8.8",
"turbo": "1.10.12"
},
"devDependencies": {
"husky": "~8.0.3",
"lint-staged": "~13.2.2",
"rome": "~12.1.3",
"syncpack": "~11.2.1"
},
"engines": {
"node": ">=18.x",
"pnpm": ">=7.x"
},
"packageManager": "[email protected]",
"private": true,
"scripts": {
"build": "dotenv -- turbo run build",
"build-config": "dotenv -- turbo run build --filter={./config/*}",
"build-packages": "dotenv -- turbo run build --filter={./packages/*}...",
"check": "dotenv -- turbo run check",
"clean": "turbo run clean && rm -rf node_modules",
"db:format": "dotenv -- turbo run db:format",
"db:migrate:deploy": "dotenv -- turbo run db:migrate:deploy",
"db:migrate:dev": "dotenv -- turbo run db:migrate:dev",
"db:migrate:reset": "dotenv -- turbo run db:migrate:reset",
"db:push": "dotenv -- turbo run db:push",
"db:studio": "dotenv -- turbo run db:studio",
"deploy": "dotenv -- turbo run deploy",
"dev": "dotenv -- turbo run dev --no-cache --parallel --continue",
"format": "rome format .",
"format:fix": "rome format . --write",
"lint": "rome check .",
"lint:fix": "rome check . --apply",
"prepare": "husky install",
"start": "dotenv -- turbo run start"
}
}
"name": "dae",
"dependencies": {
"dotenv-cli": "latest",
"turbo": "1.10.12"
},
"devDependencies": {
"husky": "~8.0.3",
"lint-staged": "~13.2.2",
"rome": "~12.1.3",
"syncpack": "~11.2.1"
},
"engines": {
"node": ">=18.x",
"pnpm": ">=7.x"
},
"packageManager": "[email protected]",
"private": true,
"scripts": {
"build": "dotenv -- turbo run build",
"build-config": "dotenv -- turbo run build --filter={./config/*}",
"build-packages": "dotenv -- turbo run build --filter={./packages/*}...",
"check": "dotenv -- turbo run check",
"clean": "turbo run clean && rm -rf node_modules",
"db:format": "dotenv -- turbo run db:format",
"db:migrate:deploy": "dotenv -- turbo run db:migrate:deploy",
"db:migrate:dev": "dotenv -- turbo run db:migrate:dev",
"db:migrate:reset": "dotenv -- turbo run db:migrate:reset",
"db:push": "dotenv -- turbo run db:push",
"db:studio": "dotenv -- turbo run db:studio",
"deploy": "dotenv -- turbo run deploy",
"dev": "dotenv -- turbo run dev --no-cache --parallel --continue",
"format": "rome format .",
"format:fix": "rome format . --write",
"lint": "rome check .",
"lint:fix": "rome check . --apply",
"prepare": "husky install",
"start": "dotenv -- turbo run start"
}
}
10 changes: 9 additions & 1 deletion packages/ipfs/src/connectors/pinata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ export class PinataIpfsConnector
}): Promise<IpfsUploadResult> {
const formData = new FormData()
let response: AxiosResponse | undefined = undefined
const pinataMetadata = JSON.stringify({
keyvalues: { environment: process.env.NEXT_PUBLIC_DAE_ENVIRONMENT },
name: fileName,
})

if (fileContent instanceof Buffer) {
const stream = Readable.from(fileContent)
formData.append('file', stream, {
filepath: fileName,
contentType: mimeType,
})
formData.append('pinataMetadata', pinataMetadata)
response = await axios.post(
'https://api.pinata.cloud/pinning/pinFileToIPFS',
formData,
Expand All @@ -46,7 +51,10 @@ export class PinataIpfsConnector
} else if (typeof fileContent === 'object') {
response = await axios.post(
'https://api.pinata.cloud/pinning/pinJSONToIPFS',
fileContent,
JSON.stringify({
pinataContent: fileContent,
pinataMetadata: pinataMetadata,
}),
{
headers: {
accept: 'application/json',
Expand Down
Loading

0 comments on commit cfc5f59

Please sign in to comment.