Skip to content

Commit

Permalink
fix: partial ipfs thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
vaultec81 committed Feb 5, 2022
1 parent 54e63b1 commit 54a0ede
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"app-root-path": "^3.0.0",
"arraysearch": "^1.2.0",
"axios": "^0.21.1",
"base58-js": "^1.0.0",
"bootstrap": "^4.5.3",
"brace": "^0.11.1",
"byte-size": "^8.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/video/VideoWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function VideoWidget(props: any) {
if (props.isNsfw === true) {
thumbnail = nsfwWarning
} else {
thumbnail = await VideoService.getThumbnailURL(props.reflink)
const [, author, permlink] = props.reflink.split(':')
thumbnail = await VideoService.getNewThumbnailURL(author, permlink)
}

setThumbnailUrl(thumbnail)
Expand Down
33 changes: 33 additions & 0 deletions src/renderer/services/video.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { AccountService } from './account.service'

import DefaultThumbnail from '../assets/img/default-thumbnail.jpg'
import { IpfsService } from './ipfs.service'
import hive from '@hiveio/hive-js'
import { binary_to_base58 } from 'base58-js'

const Finder = ArraySearch.Finder

Expand Down Expand Up @@ -83,4 +85,35 @@ export class VideoService {
return DefaultThumbnail
}
}
static async getNewThumbnailURL(author, permlink) {
try {
const content = await hive.api.getContentAsync(author, permlink)

console.log(content)
const parsedMeta = JSON.parse(content.json_metadata)

if (parsedMeta && typeof parsedMeta === 'object' && typeof parsedMeta.image[0] === 'string') {
let url = parsedMeta.image[0]

if (parsedMeta.image[0].includes('ipfs-3speak.b-cdn.net')) {
const pathArray = url.split('/')
const protocol = pathArray[3]
const host = pathArray[4]
url = `https://images.hive.blog/p/${binary_to_base58(
Buffer.from('https://ipfs.io/' + protocol + '/' + host),
)}?format=jpeg&mode=cover&width=340&height=191`
} else {
url = `https://img.3speakcontent.co/${permlink}/thumbnails/default.png`
console.log(url, permlink)
}

return url
} else {
return DefaultThumbnail
//throw new Error("Invalid post metadata");
}
} catch {
return DefaultThumbnail
}
}
}

0 comments on commit 54a0ede

Please sign in to comment.