mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add file size and redirect status to file model
This commit is contained in:
@@ -3,3 +3,7 @@ import {ReactNode} from "react";
|
|||||||
export default function Badge({children, className, onClick }: { children: ReactNode, className?: string, onClick?: () => void }) {
|
export default function Badge({children, className, onClick }: { children: ReactNode, className?: string, onClick?: () => void }) {
|
||||||
return <span className={`badge badge-primary text-sm ${className}`} onClick={onClick}>{children}</span>
|
return <span className={`badge badge-primary text-sm ${className}`} onClick={onClick}>{children}</span>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function BadgeAccent({children, className, onClick }: { children: ReactNode, className?: string, onClick?: () => void }) {
|
||||||
|
return <span className={`badge badge-accent text-sm ${className}`} onClick={onClick}>{children}</span>
|
||||||
|
}
|
@@ -82,6 +82,9 @@ export interface RFile {
|
|||||||
id: string;
|
id: string;
|
||||||
filename: string;
|
filename: string;
|
||||||
description: string;
|
description: string;
|
||||||
|
size: number;
|
||||||
|
is_redirect: boolean;
|
||||||
|
user_id: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UploadingFile {
|
export interface UploadingFile {
|
||||||
|
@@ -22,7 +22,7 @@ import Pagination from "../components/pagination.tsx";
|
|||||||
import showPopup, {useClosePopup} from "../components/popup.tsx";
|
import showPopup, {useClosePopup} from "../components/popup.tsx";
|
||||||
import {Turnstile} from "@marsidev/react-turnstile";
|
import {Turnstile} from "@marsidev/react-turnstile";
|
||||||
import Button from "../components/button.tsx";
|
import Button from "../components/button.tsx";
|
||||||
import Badge from "../components/badge.tsx";
|
import Badge, {BadgeAccent} from "../components/badge.tsx";
|
||||||
|
|
||||||
export default function ResourcePage() {
|
export default function ResourcePage() {
|
||||||
const params = useParams()
|
const params = useParams()
|
||||||
@@ -226,16 +226,33 @@ function Article({article}: { article: string }) {
|
|||||||
</article>
|
</article>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fileSizeToString(size: number) {
|
||||||
|
if (size < 1024) {
|
||||||
|
return size + "B"
|
||||||
|
} else if (size < 1024 * 1024) {
|
||||||
|
return (size / 1024).toFixed(2) + "KB"
|
||||||
|
} else if (size < 1024 * 1024 * 1024) {
|
||||||
|
return (size / 1024 / 1024).toFixed(2) + "MB"
|
||||||
|
} else {
|
||||||
|
return (size / 1024 / 1024 / 1024).toFixed(2) + "GB"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function FileTile({file}: { file: RFile }) {
|
function FileTile({file}: { file: RFile }) {
|
||||||
const buttonRef = createRef<HTMLButtonElement>()
|
const buttonRef = createRef<HTMLButtonElement>()
|
||||||
|
|
||||||
|
const {t} = useTranslation()
|
||||||
|
|
||||||
return <div className={"card card-border border-base-300 my-2"}>
|
return <div className={"card card-border border-base-300 my-2"}>
|
||||||
<div className={"p-4 flex flex-row items-center"}>
|
<div className={"p-4 flex flex-row items-center"}>
|
||||||
<div className={"grow"}>
|
<div className={"grow"}>
|
||||||
<h4 className={"font-bold py-1"}>{file.filename}</h4>
|
<h4 className={"font-bold py-1"}>{file.filename}</h4>
|
||||||
<p className={"text-sm"}>{file.description}</p>
|
<p className={"text-sm"}>{file.description}</p>
|
||||||
|
<p>
|
||||||
|
<BadgeAccent className={"mt-1"}>{file.is_redirect ? t("Redirect") : fileSizeToString(file.size)}</BadgeAccent>
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div className={"flex flex-row items-center"}>
|
||||||
<button ref={buttonRef} className={"btn btn-primary btn-soft btn-square"} onClick={() => {
|
<button ref={buttonRef} className={"btn btn-primary btn-soft btn-square"} onClick={() => {
|
||||||
if (!app.cloudflareTurnstileSiteKey) {
|
if (!app.cloudflareTurnstileSiteKey) {
|
||||||
const link = network.getFileDownloadLink(file.id, "");
|
const link = network.getFileDownloadLink(file.id, "");
|
||||||
@@ -246,7 +263,7 @@ function FileTile({file}: { file: RFile }) {
|
|||||||
}}>
|
}}>
|
||||||
<MdOutlineDownload size={24}/>
|
<MdOutlineDownload size={24}/>
|
||||||
</button>
|
</button>
|
||||||
<DeleteFileDialog fileId={file.id}/>
|
<DeleteFileDialog fileId={file.id} uploaderId={file.user_id}/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -591,7 +608,7 @@ function CommentTile({comment}: { comment: Comment }) {
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
function DeleteFileDialog({fileId}: { fileId: string }) {
|
function DeleteFileDialog({fileId, uploaderId}: { fileId: string, uploaderId: number }) {
|
||||||
const [isLoading, setLoading] = useState(false)
|
const [isLoading, setLoading] = useState(false)
|
||||||
|
|
||||||
const id = `delete_file_dialog_${fileId}`
|
const id = `delete_file_dialog_${fileId}`
|
||||||
@@ -617,7 +634,7 @@ function DeleteFileDialog({fileId}: { fileId: string }) {
|
|||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!app.isAdmin()) {
|
if (!app.isAdmin() && app.user?.id !== uploaderId) {
|
||||||
return <></>
|
return <></>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -24,6 +24,9 @@ type FileView struct {
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
IsRedirect bool `json:"is_redirect"`
|
||||||
|
UserID uint `json:"user_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) ToView() *FileView {
|
func (f *File) ToView() *FileView {
|
||||||
@@ -31,5 +34,8 @@ func (f *File) ToView() *FileView {
|
|||||||
ID: f.UUID,
|
ID: f.UUID,
|
||||||
Filename: f.Filename,
|
Filename: f.Filename,
|
||||||
Description: f.Description,
|
Description: f.Description,
|
||||||
|
Size: f.Size,
|
||||||
|
IsRedirect: f.RedirectUrl != "",
|
||||||
|
UserID: f.UserID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user