From 8cb6b699e565f1e2a83f85cc902bb4b0b0d1b83d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 5 Feb 2024 20:02:37 +0800 Subject: [PATCH] :sparkles: External attachments --- pkg/models/attachments.go | 17 ++-- pkg/view/src/components/PostAttachments.tsx | 6 +- pkg/view/src/components/PostPublish.tsx | 94 ++++++++++++++++----- pkg/view/src/layouts/shared/Navbar.tsx | 3 +- 4 files changed, 87 insertions(+), 33 deletions(-) diff --git a/pkg/models/attachments.go b/pkg/models/attachments.go index aa31c1b..da1de5c 100644 --- a/pkg/models/attachments.go +++ b/pkg/models/attachments.go @@ -9,14 +9,15 @@ import ( type Attachment struct { BaseModel - FileID string `json:"file_id"` - Filesize int64 `json:"filesize"` - Filename string `json:"filename"` - Mimetype string `json:"mimetype"` - Post *Post `json:"post"` - Author Account `json:"author"` - PostID *uint `json:"post_id"` - AuthorID uint `json:"author_id"` + FileID string `json:"file_id"` + Filesize int64 `json:"filesize"` + Filename string `json:"filename"` + Mimetype string `json:"mimetype"` + ExternalUrl string `json:"external_url"` + Post *Post `json:"post"` + Author Account `json:"author"` + PostID *uint `json:"post_id"` + AuthorID uint `json:"author_id"` } func (v Attachment) GetStoragePath() string { diff --git a/pkg/view/src/components/PostAttachments.tsx b/pkg/view/src/components/PostAttachments.tsx index 9009cf8..2aad7b6 100644 --- a/pkg/view/src/components/PostAttachments.tsx +++ b/pkg/view/src/components/PostAttachments.tsx @@ -14,7 +14,7 @@ export default function PostAttachments(props: { attachments: any[] }) { } function getUrl(item: any): string { - return `/api/attachments/o/${item.file_id}`; + return item.external_url ?? `/api/attachments/o/${item.file_id}`; } createEffect(() => { @@ -36,8 +36,8 @@ export default function PostAttachments(props: { attachments: any[] }) {

{item().filename}

-
-

{item().filesize} Bytes

+
+

{item().filesize <= 0 ? "Unknown" : item().filesize} Bytes

{item().mimetype}

diff --git a/pkg/view/src/components/PostPublish.tsx b/pkg/view/src/components/PostPublish.tsx index 6c32154..9a8d0a2 100644 --- a/pkg/view/src/components/PostPublish.tsx +++ b/pkg/view/src/components/PostPublish.tsx @@ -1,4 +1,4 @@ -import { createEffect, createSignal, For, Show } from "solid-js"; +import { createEffect, createSignal, For, Match, Show, Switch } from "solid-js"; import { getAtk, useUserinfo } from "../stores/userinfo.tsx"; import styles from "./PostPublish.module.css"; @@ -22,6 +22,8 @@ export default function PostPublish(props: { const [categories, setCategories] = createSignal<{ alias: string, name: string }[]>([]); const [tags, setTags] = createSignal<{ alias: string, name: string }[]>([]); + const [attachmentMode, setAttachmentMode] = createSignal(0); + createEffect(() => { setAttachments(props.editing?.attachments ?? []); setCategories(props.editing?.categories ?? []); @@ -101,7 +103,7 @@ export default function PostPublish(props: { setSubmitting(false); } - async function uploadAttachments(evt: SubmitEvent) { + async function uploadAttachment(evt: SubmitEvent) { evt.preventDefault(); const form = evt.target as HTMLFormElement; @@ -125,6 +127,19 @@ export default function PostPublish(props: { setUploading(false); } + function addAttachment(evt: SubmitEvent) { + evt.preventDefault(); + + const form = evt.target as HTMLFormElement; + const data = Object.fromEntries(new FormData(form)); + + setAttachments(attachments().concat([{ + ...data, + author_id: userinfo?.profiles?.id, + }])); + form.reset(); + } + function addCategory(evt: SubmitEvent) { evt.preventDefault(); @@ -138,7 +153,7 @@ export default function PostPublish(props: { } function removeCategory(target: any) { - setCategories(categories().filter(item => item.alias !== target.alias)) + setCategories(categories().filter(item => item.alias !== target.alias)); } function addTag(evt: SubmitEvent) { @@ -154,7 +169,7 @@ export default function PostPublish(props: { } function removeTag(target: any) { - setTags(tags().filter(item => item.alias !== target.alias)) + setTags(tags().filter(item => item.alias !== target.alias)); } function resetForm() { @@ -270,23 +285,60 @@ export default function PostPublish(props: {