From d87d1d62aae5267de2f3b808f2f7ac9cdf6f0fd9 Mon Sep 17 00:00:00 2001 From: smolgrrr Date: Tue, 31 Oct 2023 14:39:30 +1100 Subject: [PATCH] switch to void.cat thank you lord kieran --- client/src/components/Modals/QuoteEmbed.tsx | 10 +-- .../src/components/PostCard/NewThreadCard.tsx | 14 +--- client/src/components/PostCard/PostCard.tsx | 10 +-- client/src/components/Thread/OPCard.tsx | 29 +------ client/src/components/Thread/ReplyCard.tsx | 11 +-- client/src/components/Thread/ThreadPost.tsx | 2 +- client/src/utils/FileUpload.tsx | 83 +++++++++++++++++++ client/src/utils/ImgUpload.ts | 80 ------------------ 8 files changed, 96 insertions(+), 143 deletions(-) create mode 100644 client/src/utils/FileUpload.tsx delete mode 100644 client/src/utils/ImgUpload.ts diff --git a/client/src/components/Modals/QuoteEmbed.tsx b/client/src/components/Modals/QuoteEmbed.tsx index f0eac77..327718d 100644 --- a/client/src/components/Modals/QuoteEmbed.tsx +++ b/client/src/components/Modals/QuoteEmbed.tsx @@ -2,6 +2,7 @@ import { parseContent } from '../../utils/content'; import { Event } from 'nostr-tools'; import { getMetadata, uniqBy } from '../../utils/utils'; import ContentPreview from './TextModal'; +import { renderMedia } from '../../utils/FileUpload'; const colorCombos = [ 'from-red-400 to-yellow-500', @@ -81,14 +82,7 @@ const QuoteEmbed = ({ event, metadata }: { event: Event, metadata: Event | null}
- {file !== "" && ( -
- -
- )} + {renderMedia(file)} ); diff --git a/client/src/components/PostCard/NewThreadCard.tsx b/client/src/components/PostCard/NewThreadCard.tsx index ecc9ac1..b6ac183 100644 --- a/client/src/components/PostCard/NewThreadCard.tsx +++ b/client/src/components/PostCard/NewThreadCard.tsx @@ -3,7 +3,8 @@ import { ArrowUpTrayIcon, CpuChipIcon } from '@heroicons/react/24/outline'; import { useState, useEffect, useMemo } from 'react'; import { generatePrivateKey, getPublicKey, finishEvent } from 'nostr-tools'; import { publish } from '../../utils/relays'; -import NostrImg from '../../utils/ImgUpload'; +import FileUpload from '../../utils/FileUpload'; +import { renderMedia } from '../../utils/FileUpload'; const NewThreadCard: React.FC = () => { const [comment, setComment] = useState(""); @@ -73,7 +74,7 @@ const NewThreadCard: React.FC = () => { async function attachFile(file_input: File | null) { try { if (file_input) { - const rx = await NostrImg(file_input); + const rx = await FileUpload(file_input); if (rx.url) { setFile(rx.url); } else if (rx?.error) { @@ -115,14 +116,7 @@ const NewThreadCard: React.FC = () => { />
- {file !== "" && ( -
- -
- )} + {renderMedia(file)}
diff --git a/client/src/components/PostCard/PostCard.tsx b/client/src/components/PostCard/PostCard.tsx index 5a52024..a6f88ae 100644 --- a/client/src/components/PostCard/PostCard.tsx +++ b/client/src/components/PostCard/PostCard.tsx @@ -5,6 +5,7 @@ import { Event } from 'nostr-tools'; import { nip19 } from 'nostr-tools'; import { getMetadata } from '../../utils/utils'; import ContentPreview from'../Modals/TextModal'; +import { renderMedia } from '../../utils/FileUpload'; const colorCombos = [ 'from-red-400 to-yellow-500', @@ -92,14 +93,7 @@ const PostCard = ({ key, event, metadata, replyCount }: { key: string, event: Ev
- {file !== "" && ( -
- -
- )} + {renderMedia(file)}
diff --git a/client/src/components/Thread/OPCard.tsx b/client/src/components/Thread/OPCard.tsx index 247d33b..f60989c 100644 --- a/client/src/components/Thread/OPCard.tsx +++ b/client/src/components/Thread/OPCard.tsx @@ -4,6 +4,7 @@ import { parseContent } from '../../utils/content'; import { Event } from 'nostr-tools'; import { getMetadata } from '../../utils/utils'; import ContentPreview from '../Modals/TextModal'; +import { renderMedia } from '../../utils/FileUpload'; const colorCombos = [ 'from-red-400 to-yellow-500', @@ -89,37 +90,11 @@ const OPCard = ({ event, metadata, replyCount }: { event: Event, metadata: Event
- {file !== "" && ( -
- -
- )} + {renderMedia(file)}
); }; -interface LinkPreview { - url: string; - title: string; - siteName?: string; - description?: string; - mediaType: string; - contentType?: string; - images: string[]; - videos: { - url?: string; - secureUrl?: string; - type?: string; - width?: string; - height?: string; - [key: string]: any; - }[]; - [key: string]: any; -} - export default OPCard; \ No newline at end of file diff --git a/client/src/components/Thread/ReplyCard.tsx b/client/src/components/Thread/ReplyCard.tsx index 818f01d..20bbb93 100644 --- a/client/src/components/Thread/ReplyCard.tsx +++ b/client/src/components/Thread/ReplyCard.tsx @@ -5,7 +5,7 @@ import { Event } from 'nostr-tools'; import { nip19 } from 'nostr-tools'; import { getMetadata, uniqBy } from '../../utils/utils'; import ContentPreview from '../Modals/TextModal'; -import { useEffect, useState } from 'react'; +import { renderMedia } from '../../utils/FileUpload'; const colorCombos = [ 'from-red-400 to-yellow-500', @@ -108,14 +108,7 @@ const ReplyCard = ({ event, metadata, replyCount, repliedTo }: { event: Event, m
- {file !== "" && ( -
- -
- )} + {renderMedia(file)} diff --git a/client/src/components/Thread/ThreadPost.tsx b/client/src/components/Thread/ThreadPost.tsx index 89032a3..8a6dbe1 100644 --- a/client/src/components/Thread/ThreadPost.tsx +++ b/client/src/components/Thread/ThreadPost.tsx @@ -3,7 +3,7 @@ import { useState, useMemo, useEffect } from "react"; import { ArrowUpTrayIcon, CpuChipIcon } from '@heroicons/react/24/outline'; import { generatePrivateKey, getPublicKey, finishEvent, Event as NostrEvent } from 'nostr-tools'; import { publish } from '../../utils/relays'; -import NostrImg from '../../utils/ImgUpload'; +import NostrImg from '../../utils/FileUpload'; import { nip19 } from 'nostr-tools'; diff --git a/client/src/utils/FileUpload.tsx b/client/src/utils/FileUpload.tsx new file mode 100644 index 0000000..31c60ee --- /dev/null +++ b/client/src/utils/FileUpload.tsx @@ -0,0 +1,83 @@ +export interface UploadResult { + url?: string; + error?: string; +} + +/** +* Upload file to void.cat +* https://void.cat/swagger/index.html +*/ + +export default async function FileUpload(file: File ): Promise { + const buf = await file.arrayBuffer(); + + const req = await fetch("https://void.cat/upload", { + body: buf, + method: "POST", + headers: { + "Content-Type": "application/octet-stream", + "V-Content-Type": file.type, // Extracting the mime type + "V-Filename": file.name, // Extracting the filename + "V-Description": "Upload from https://tao-green.vercel.app/", + "V-Strip-Metadata": "true", // Here's the new header + }, + }); + if (req.ok) { + let rsp: VoidUploadResponse = await req.json(); + const fileExtension = file.name.split('.').pop(); // Extracting the file extension + const resultUrl = `https://void.cat/d/${rsp.file?.id}.${fileExtension}`; + return {url: resultUrl}; + } + return { + error: "Upload failed", + }; +} + +export const renderMedia = (file: string) => { + if (file && (file.endsWith(".mp4") || file.endsWith(".webm"))) { + return ( + + ); + } else if (!file.includes("http")) { + return ( + <> + ); + } else { + return ( + Invalid thread + ); + } +}; + +export interface UploadResult { +url?: string; +error?: string; +} + +export type VoidUploadResponse = { + ok: boolean, + file?: VoidFile, + errorMessage?: string, +} + +export type VoidFile = { + id: string, + meta?: VoidFileMeta +} + +export type VoidFileMeta = { + version: number, + id: string, + name?: string, + size: number, + uploaded: Date, + description?: string, + mimeType?: string, + digest?: string, + url?: string, + expires?: Date, + storage?: string, + encryptionParams?: string, +} \ No newline at end of file diff --git a/client/src/utils/ImgUpload.ts b/client/src/utils/ImgUpload.ts deleted file mode 100644 index ac5725e..0000000 --- a/client/src/utils/ImgUpload.ts +++ /dev/null @@ -1,80 +0,0 @@ -export interface UploadResult { - url?: string; - error?: string; - } - -/** - * Upload file to void.cat - * https://void.cat/swagger/index.html - */ - -export default async function NostrImg(file: File ): Promise { - const fd = new FormData(); - fd.append("image", file); - - const req = await fetch("https://nostrimg.com/api/upload", { - body: fd, - method: "POST", - headers: { - accept: "application/json", - }, - }); - if (req.ok) { - const data: UploadResponse = await req.json(); - if (typeof data?.imageUrl === "string" && data.success) { - return { - url: new URL(data.imageUrl).toString(), - }; - } - } - return { - error: "Upload failed", - }; - } - -export interface UploadResult { - url?: string; - error?: string; -} - -export type VoidUploadResponse = { - ok: boolean, - file?: VoidFile, - errorMessage?: string, -} - -interface UploadResponse { - fileID?: string; - fileName?: string; - imageUrl?: string; - lightningDestination?: string; - lightningPaymentLink?: string; - message?: string; - route?: string; - status: number; - success: boolean; - url?: string; - data?: { - url?: string; - }; -} - -export type VoidFile = { - id: string, - meta?: VoidFileMeta -} - -export type VoidFileMeta = { - version: number, - id: string, - name?: string, - size: number, - uploaded: Date, - description?: string, - mimeType?: string, - digest?: string, - url?: string, - expires?: Date, - storage?: string, - encryptionParams?: string, -} \ No newline at end of file