mirror of
https://github.com/luminous-devs/lume.git
synced 2024-09-18 11:13:30 +00:00
unify upload function
This commit is contained in:
parent
555c8ec08a
commit
60b803f419
@ -3,20 +3,25 @@ import { Dispatch, SetStateAction, useState } from 'react';
|
|||||||
|
|
||||||
import { LoaderIcon, MediaIcon } from '@shared/icons';
|
import { LoaderIcon, MediaIcon } from '@shared/icons';
|
||||||
|
|
||||||
|
import { useNostr } from '@utils/hooks/useNostr';
|
||||||
|
|
||||||
export function MediaUploader({
|
export function MediaUploader({
|
||||||
setState,
|
setState,
|
||||||
}: {
|
}: {
|
||||||
setState: Dispatch<SetStateAction<string>>;
|
setState: Dispatch<SetStateAction<string>>;
|
||||||
}) {
|
}) {
|
||||||
|
const { upload } = useNostr();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const uploadMedia = async () => {
|
const uploadMedia = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const image = await upload(null);
|
|
||||||
if (image.url) {
|
const image = await upload(['mp4', 'mp3', 'webm', 'mkv', 'avi', 'mov']);
|
||||||
setState((prev: string) => `${prev}\n${image.url}`);
|
|
||||||
|
if (image) {
|
||||||
|
setState((prev: string) => `${prev}\n${image}`);
|
||||||
|
setLoading(false);
|
||||||
}
|
}
|
||||||
setLoading(false);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
import { message, open } from '@tauri-apps/plugin-dialog';
|
import { message } from '@tauri-apps/plugin-dialog';
|
||||||
import { readBinaryFile } from '@tauri-apps/plugin-fs';
|
|
||||||
import { Editor } from '@tiptap/react';
|
import { Editor } from '@tiptap/react';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
import { MediaIcon } from '@shared/icons';
|
import { MediaIcon } from '@shared/icons';
|
||||||
|
|
||||||
|
import { useNostr } from '@utils/hooks/useNostr';
|
||||||
|
|
||||||
export function MediaUploader({ editor }: { editor: Editor }) {
|
export function MediaUploader({ editor }: { editor: Editor }) {
|
||||||
|
const { upload } = useNostr();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const uploadToNostrBuild = async () => {
|
const uploadToNostrBuild = async () => {
|
||||||
@ -13,52 +15,12 @@ export function MediaUploader({ editor }: { editor: Editor }) {
|
|||||||
// start loading
|
// start loading
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
const selected = await open({
|
const image = await upload(['mp4', 'mp3', 'webm', 'mkv', 'avi', 'mov']);
|
||||||
multiple: false,
|
|
||||||
filters: [
|
|
||||||
{
|
|
||||||
name: 'Media',
|
|
||||||
extensions: [
|
|
||||||
'png',
|
|
||||||
'jpeg',
|
|
||||||
'jpg',
|
|
||||||
'gif',
|
|
||||||
'mp4',
|
|
||||||
'mp3',
|
|
||||||
'webm',
|
|
||||||
'mkv',
|
|
||||||
'avi',
|
|
||||||
'mov',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!selected) {
|
if (image) {
|
||||||
setLoading(false);
|
editor.commands.setImage({ src: image });
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const file = await readBinaryFile(selected.path);
|
|
||||||
const blob = new Blob([file]);
|
|
||||||
|
|
||||||
const data = new FormData();
|
|
||||||
data.append('fileToUpload', blob);
|
|
||||||
data.append('submit', 'Upload Image');
|
|
||||||
|
|
||||||
const res = await fetch('https://nostr.build/api/v2/upload/files', {
|
|
||||||
method: 'POST',
|
|
||||||
body: data,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
const json = await res.json();
|
|
||||||
const content = json.data[0];
|
|
||||||
|
|
||||||
editor.commands.setImage({ src: content.url });
|
|
||||||
editor.commands.createParagraphNear();
|
editor.commands.createParagraphNear();
|
||||||
|
|
||||||
// stop loading
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
import { message, open } from '@tauri-apps/plugin-dialog';
|
import { message } from '@tauri-apps/plugin-dialog';
|
||||||
import { readBinaryFile } from '@tauri-apps/plugin-fs';
|
|
||||||
import { fetch } from '@tauri-apps/plugin-http';
|
|
||||||
import { Dispatch, SetStateAction, useState } from 'react';
|
import { Dispatch, SetStateAction, useState } from 'react';
|
||||||
|
|
||||||
import { LoaderIcon, PlusIcon } from '@shared/icons';
|
import { LoaderIcon, PlusIcon } from '@shared/icons';
|
||||||
|
|
||||||
|
import { useNostr } from '@utils/hooks/useNostr';
|
||||||
|
|
||||||
export function AvatarUploader({
|
export function AvatarUploader({
|
||||||
setPicture,
|
setPicture,
|
||||||
}: {
|
}: {
|
||||||
setPicture: Dispatch<SetStateAction<string>>;
|
setPicture: Dispatch<SetStateAction<string>>;
|
||||||
}) {
|
}) {
|
||||||
|
const { upload } = useNostr();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const uploadAvatar = async () => {
|
const uploadAvatar = async () => {
|
||||||
@ -17,42 +18,14 @@ export function AvatarUploader({
|
|||||||
// start loading
|
// start loading
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
const selected = await open({
|
const image = await upload();
|
||||||
multiple: false,
|
|
||||||
filters: [
|
|
||||||
{
|
|
||||||
name: 'Image',
|
|
||||||
extensions: ['png', 'jpeg', 'jpg', 'gif'],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!selected) {
|
if (image) {
|
||||||
setLoading(false);
|
setPicture(image);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const file = await readBinaryFile(selected.path);
|
|
||||||
const blob = new Blob([file]);
|
|
||||||
|
|
||||||
const data = new FormData();
|
|
||||||
data.append('fileToUpload', blob);
|
|
||||||
data.append('submit', 'Upload Image');
|
|
||||||
|
|
||||||
const res = await fetch('https://nostr.build/api/v2/upload/files', {
|
|
||||||
method: 'POST',
|
|
||||||
body: data,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
const json = await res.json();
|
|
||||||
const content = json.data[0];
|
|
||||||
|
|
||||||
setPicture(content.url);
|
|
||||||
|
|
||||||
// stop loading
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// stop loading
|
// stop loading
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
import { message, open } from '@tauri-apps/plugin-dialog';
|
import { message } from '@tauri-apps/plugin-dialog';
|
||||||
import { readBinaryFile } from '@tauri-apps/plugin-fs';
|
|
||||||
import { fetch } from '@tauri-apps/plugin-http';
|
|
||||||
import { Dispatch, SetStateAction, useState } from 'react';
|
import { Dispatch, SetStateAction, useState } from 'react';
|
||||||
|
|
||||||
import { LoaderIcon, PlusIcon } from '@shared/icons';
|
import { LoaderIcon, PlusIcon } from '@shared/icons';
|
||||||
|
|
||||||
|
import { useNostr } from '@utils/hooks/useNostr';
|
||||||
|
|
||||||
export function BannerUploader({
|
export function BannerUploader({
|
||||||
setBanner,
|
setBanner,
|
||||||
}: {
|
}: {
|
||||||
setBanner: Dispatch<SetStateAction<string>>;
|
setBanner: Dispatch<SetStateAction<string>>;
|
||||||
}) {
|
}) {
|
||||||
|
const { upload } = useNostr();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const uploadBanner = async () => {
|
const uploadBanner = async () => {
|
||||||
@ -17,42 +18,14 @@ export function BannerUploader({
|
|||||||
// start loading
|
// start loading
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
const selected = await open({
|
const image = await upload();
|
||||||
multiple: false,
|
|
||||||
filters: [
|
|
||||||
{
|
|
||||||
name: 'Image',
|
|
||||||
extensions: ['png', 'jpeg', 'jpg', 'gif'],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!selected) {
|
if (image) {
|
||||||
setLoading(false);
|
setBanner(image);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const file = await readBinaryFile(selected.path);
|
|
||||||
const blob = new Blob([file]);
|
|
||||||
|
|
||||||
const data = new FormData();
|
|
||||||
data.append('fileToUpload', blob);
|
|
||||||
data.append('submit', 'Upload Image');
|
|
||||||
|
|
||||||
const res = await fetch('https://nostr.build/api/v2/upload/files', {
|
|
||||||
method: 'POST',
|
|
||||||
body: data,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
const json = await res.json();
|
|
||||||
const content = json.data[0];
|
|
||||||
|
|
||||||
setBanner(content.url);
|
|
||||||
|
|
||||||
// stop loading
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// stop loading
|
// stop loading
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import { NDKEvent, NDKFilter, NDKKind, NDKSubscription } from '@nostr-dev-kit/ndk';
|
import { NDKEvent, NDKFilter, NDKKind, NDKSubscription } from '@nostr-dev-kit/ndk';
|
||||||
|
import { open } from '@tauri-apps/plugin-dialog';
|
||||||
|
import { readBinaryFile } from '@tauri-apps/plugin-fs';
|
||||||
|
import { fetch } from '@tauri-apps/plugin-http';
|
||||||
import { LRUCache } from 'lru-cache';
|
import { LRUCache } from 'lru-cache';
|
||||||
import { NostrEventExt } from 'nostr-fetch';
|
import { NostrEventExt } from 'nostr-fetch';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
@ -239,6 +242,41 @@ export function useNostr() {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const upload = async (ext: string[] = []) => {
|
||||||
|
const defaultExts = ['png', 'jpeg', 'jpg', 'gif'].concat(ext);
|
||||||
|
|
||||||
|
const selected = await open({
|
||||||
|
multiple: false,
|
||||||
|
filters: [
|
||||||
|
{
|
||||||
|
name: 'Image',
|
||||||
|
extensions: defaultExts,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!selected) return null;
|
||||||
|
|
||||||
|
const file = await readBinaryFile(selected.path);
|
||||||
|
const blob = new Blob([file]);
|
||||||
|
|
||||||
|
const data = new FormData();
|
||||||
|
data.append('fileToUpload', blob);
|
||||||
|
data.append('submit', 'Upload Image');
|
||||||
|
|
||||||
|
const res = await fetch('https://nostr.build/api/v2/upload/files', {
|
||||||
|
method: 'POST',
|
||||||
|
body: data,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) return null;
|
||||||
|
|
||||||
|
const json = await res.json();
|
||||||
|
const content = json.data[0];
|
||||||
|
|
||||||
|
return content.url as string;
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sub,
|
sub,
|
||||||
getAllNIP04Chats,
|
getAllNIP04Chats,
|
||||||
@ -250,5 +288,6 @@ export function useNostr() {
|
|||||||
fetchNIP04Messages,
|
fetchNIP04Messages,
|
||||||
fetchAllReplies,
|
fetchAllReplies,
|
||||||
createZap,
|
createZap,
|
||||||
|
upload,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user