From 54a0eded492d99ff86f5f72d1d8d20d1a1fb401d Mon Sep 17 00:00:00 2001 From: vaultec <47548474+vaultec81@users.noreply.github.com> Date: Fri, 4 Feb 2022 22:50:49 -0800 Subject: [PATCH] fix: partial ipfs thumbnail --- package.json | 1 + src/renderer/components/video/VideoWidget.tsx | 3 +- src/renderer/services/video.service.ts | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 5e39b46..c23fe30 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/renderer/components/video/VideoWidget.tsx b/src/renderer/components/video/VideoWidget.tsx index cc3c150..6e689a4 100644 --- a/src/renderer/components/video/VideoWidget.tsx +++ b/src/renderer/components/video/VideoWidget.tsx @@ -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) diff --git a/src/renderer/services/video.service.ts b/src/renderer/services/video.service.ts index 67380ee..0242d2a 100644 --- a/src/renderer/services/video.service.ts +++ b/src/renderer/services/video.service.ts @@ -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 @@ -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 + } + } }