mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15:51:14 +00:00
Compare commits
33 Commits
23269ad9d1
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 9cd743af7f | |||
| 79bae828d8 | |||
| a42087ce5c | |||
| a9d2f05562 | |||
| 31b9fb5d45 | |||
| 116efcdf93 | |||
| 9ad8d9d7e9 | |||
| 8e2ab62297 | |||
| cb61ce99bf | |||
| 2767f8a30f | |||
| 5a5c2edfda | |||
| d860bdf06a | |||
| a3de195eca | |||
| 6cabff8f8d | |||
| 78f6130b23 | |||
| ddd856529b | |||
| 48638111ec | |||
| d255ecc503 | |||
| 00321b01c3 | |||
| 59904223b4 | |||
| b732e1be83 | |||
| fd86d6c221 | |||
| fbe8ac27bf | |||
| fb1f47c0c0 | |||
| ecfea63edd | |||
| ae547522ed | |||
| 96cdd2c41c | |||
| 566234c30c | |||
| 4a6c214709 | |||
| 4550720cbb | |||
| e833783da1 | |||
| 1406f76fbb | |||
| 6040f88034 |
@@ -33,3 +33,6 @@ BACKUP_RETENTION_DAYS=30
|
||||
|
||||
# Download Configuration
|
||||
DOWNLOAD_SECRET_KEY=your_download_secret_key_here
|
||||
|
||||
# Access Key for Development API
|
||||
DEV_ACCESS_KEY=your_dev_access_key_here
|
||||
@@ -19,6 +19,7 @@ import CreateCollectionPage from "./pages/create_collection_page.tsx";
|
||||
import CollectionPage from "./pages/collection_page.tsx";
|
||||
import { i18nData } from "./i18n.ts";
|
||||
import { i18nContext } from "./utils/i18n.ts";
|
||||
import NotificationPage from "./pages/notification_page.tsx";
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
@@ -49,6 +50,7 @@ export default function App() {
|
||||
element={<CreateCollectionPage />}
|
||||
/>
|
||||
<Route path={"/collection/:id"} element={<CollectionPage />} />
|
||||
<Route path={"/notifications"} element={<NotificationPage />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
|
||||
@@ -2,11 +2,10 @@ import { app } from "../app.ts";
|
||||
import { network } from "../network/network.ts";
|
||||
import { useNavigate, useOutlet } from "react-router";
|
||||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
import { MdArrowUpward, MdOutlinePerson, MdSearch } from "react-icons/md";
|
||||
import { MdArrowUpward, MdOutlinePerson, MdSearch, MdNotifications } from "react-icons/md";
|
||||
import { useTranslation } from "../utils/i18n";
|
||||
import UploadingSideBar from "./uploading_side_bar.tsx";
|
||||
import { ThemeSwitcher } from "./theme_switcher.tsx";
|
||||
import { IoLogoGithub } from "react-icons/io";
|
||||
import { useAppContext } from "./AppContext.tsx";
|
||||
import { AnimatePresence, motion } from "framer-motion";
|
||||
|
||||
@@ -234,16 +233,7 @@ export default function Navigator() {
|
||||
<SearchBar />
|
||||
<UploadingSideBar />
|
||||
<ThemeSwitcher />
|
||||
<a
|
||||
className={"hidden sm:inline"}
|
||||
href="https://github.com/wgh136/nysoure"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<button className={"btn btn-circle btn-ghost"}>
|
||||
<IoLogoGithub size={24} />
|
||||
</button>
|
||||
</a>
|
||||
{app.isLoggedIn() && <NotificationButton />}
|
||||
{app.isLoggedIn() ? (
|
||||
<UserButton />
|
||||
) : (
|
||||
@@ -554,3 +544,41 @@ function FloatingToTopButton() {
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
function NotificationButton() {
|
||||
const [count, setCount] = useState(0);
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
const fetchCount = async () => {
|
||||
if (!app.isLoggedIn()) {
|
||||
return;
|
||||
}
|
||||
const res = await network.getUserNotificationsCount();
|
||||
if (res.success && res.data !== undefined) {
|
||||
setCount(res.data);
|
||||
}
|
||||
};
|
||||
|
||||
fetchCount();
|
||||
const interval = setInterval(fetchCount, 60000); // 每分钟请求一次
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="indicator">
|
||||
{count > 0 && <span className="bg-error text-white text-xs rounded-full px-1 indicator-item">
|
||||
{count > 99 ? "99+" : count}
|
||||
</span>}
|
||||
<button
|
||||
className="btn btn-ghost btn-circle"
|
||||
onClick={() => {
|
||||
navigate("/notifications");
|
||||
}}
|
||||
>
|
||||
<MdNotifications size={24} />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -156,8 +156,8 @@ export const i18nData = {
|
||||
"Cloudflare Turnstile Secret Key": "Cloudflare Turnstile 密钥",
|
||||
"If the cloudflare turnstile keys are not empty, the turnstile will be used for register and download.":
|
||||
"如果设置了 Cloudflare Turnstile 密钥,将在注册和下载时启用验证",
|
||||
"The first image will be used as the cover image":
|
||||
"第一张图片将用作封面图片",
|
||||
"You can select a cover image using the radio button in the Cover column":
|
||||
"您可以使用封面列中的单选按钮选择封面图片",
|
||||
"Please enter a search keyword": "请输入搜索关键词",
|
||||
"Searching...": "搜索中...",
|
||||
"Create Tag": "创建标签",
|
||||
@@ -179,6 +179,8 @@ export const i18nData = {
|
||||
"Views Descending": "浏览量降序",
|
||||
"Downloads Ascending": "下载量升序",
|
||||
"Downloads Descending": "下载量降序",
|
||||
"Release Date Ascending": "发布日期升序",
|
||||
"Release Date Descending": "发布日期降序",
|
||||
"File Url": "文件链接",
|
||||
"Provide a file url for the server to download, and the file will be moved to the selected storage.":
|
||||
"提供一个文件链接供服务器下载,文件将被移动到选定的存储中。",
|
||||
@@ -262,6 +264,8 @@ export const i18nData = {
|
||||
"Tag": "标签",
|
||||
"Optional": "可选",
|
||||
"Download": "下载",
|
||||
"Notifications": "通知",
|
||||
"Release Date": "发售日期",
|
||||
},
|
||||
},
|
||||
"zh-TW": {
|
||||
@@ -421,8 +425,8 @@ export const i18nData = {
|
||||
"Cloudflare Turnstile Secret Key": "Cloudflare Turnstile 密鑰",
|
||||
"If the cloudflare turnstile keys are not empty, the turnstile will be used for register and download.":
|
||||
"如果設置了 Cloudflare Turnstile 密鑰,將在註冊和下載時啟用驗證",
|
||||
"The first image will be used as the cover image":
|
||||
"第一張圖片將用作封面圖片",
|
||||
"You can select a cover image using the radio button in the Cover column":
|
||||
"您可以使用封面列中的單選按鈕選擇封面圖片",
|
||||
"Please enter a search keyword": "請輸入搜尋關鍵字",
|
||||
"Searching...": "搜尋中...",
|
||||
"Create Tag": "創建標籤",
|
||||
@@ -444,6 +448,8 @@ export const i18nData = {
|
||||
"Views Descending": "瀏覽量降序",
|
||||
"Downloads Ascending": "下載量升序",
|
||||
"Downloads Descending": "下載量降序",
|
||||
"Release Date Ascending": "發布日期升序",
|
||||
"Release Date Descending": "發布日期降序",
|
||||
"File Url": "檔案連結",
|
||||
"Provide a file url for the server to download, and the file will be moved to the selected storage.":
|
||||
"提供一個檔案連結供伺服器下載,檔案將被移動到選定的儲存中。",
|
||||
@@ -527,6 +533,8 @@ export const i18nData = {
|
||||
"Tag": "標籤",
|
||||
"Optional": "可選",
|
||||
"Download": "下載",
|
||||
"Notifications": "通知",
|
||||
"Release Date": "發售日期",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -44,9 +44,11 @@ export interface CreateResourceParams {
|
||||
title: string;
|
||||
alternative_titles: string[];
|
||||
links: RLink[];
|
||||
release_date?: string;
|
||||
tags: number[];
|
||||
article: string;
|
||||
images: number[];
|
||||
cover_id?: number;
|
||||
gallery: number[];
|
||||
gallery_nsfw: number[];
|
||||
characters: CharacterParams[];
|
||||
@@ -77,6 +79,7 @@ export interface Resource {
|
||||
id: number;
|
||||
title: string;
|
||||
created_at: string;
|
||||
release_date?: string;
|
||||
tags: Tag[];
|
||||
image?: Image;
|
||||
author: User;
|
||||
@@ -89,8 +92,10 @@ export interface ResourceDetails {
|
||||
links: RLink[];
|
||||
article: string;
|
||||
createdAt: string;
|
||||
releaseDate?: string;
|
||||
tags: Tag[];
|
||||
images: Image[];
|
||||
coverId?: number;
|
||||
files: RFile[];
|
||||
author: User;
|
||||
views: number;
|
||||
@@ -194,6 +199,8 @@ export enum RSort {
|
||||
ViewsDesc = 3,
|
||||
DownloadsAsc = 4,
|
||||
DownloadsDesc = 5,
|
||||
ReleaseDateAsc = 6,
|
||||
ReleaseDateDesc = 7,
|
||||
}
|
||||
|
||||
export enum ActivityType {
|
||||
|
||||
@@ -730,6 +730,26 @@ class Network {
|
||||
);
|
||||
}
|
||||
|
||||
async getUserNotifications(page: number = 1): Promise<PageResponse<Activity>> {
|
||||
return this._callApi(() =>
|
||||
axios.get(`${this.apiBaseUrl}/notification`, {
|
||||
params: { page },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async resetUserNotificationsCount(): Promise<Response<void>> {
|
||||
return this._callApi(() =>
|
||||
axios.post(`${this.apiBaseUrl}/notification/reset`),
|
||||
);
|
||||
}
|
||||
|
||||
async getUserNotificationsCount(): Promise<Response<number>> {
|
||||
return this._callApi(() =>
|
||||
axios.get(`${this.apiBaseUrl}/notification/count`),
|
||||
);
|
||||
}
|
||||
|
||||
async createCollection(
|
||||
title: string,
|
||||
article: string,
|
||||
|
||||
@@ -25,9 +25,11 @@ import CharacterEditer, { FetchVndbCharactersButton } from "../components/charac
|
||||
export default function EditResourcePage() {
|
||||
const [title, setTitle] = useState<string>("");
|
||||
const [altTitles, setAltTitles] = useState<string[]>([]);
|
||||
const [releaseDate, setReleaseDate] = useState<string | undefined>(undefined);
|
||||
const [tags, setTags] = useState<Tag[]>([]);
|
||||
const [article, setArticle] = useState<string>("");
|
||||
const [images, setImages] = useState<number[]>([]);
|
||||
const [coverId, setCoverId] = useState<number | undefined>(undefined);
|
||||
const [links, setLinks] = useState<{ label: string; url: string }[]>([]);
|
||||
const [galleryImages, setGalleryImages] = useState<number[]>([]);
|
||||
const [galleryNsfw, setGalleryNsfw] = useState<number[]>([]);
|
||||
@@ -58,9 +60,11 @@ export default function EditResourcePage() {
|
||||
setTags(data.tags);
|
||||
setArticle(data.article);
|
||||
setImages(data.images.map((i) => i.id));
|
||||
setCoverId(data.coverId);
|
||||
setLinks(data.links ?? []);
|
||||
setGalleryImages(data.gallery ?? []);
|
||||
setGalleryNsfw(data.galleryNsfw ?? []);
|
||||
setReleaseDate(data.releaseDate?.split("T")[0] ?? undefined);
|
||||
setCharacters(data.characters ?? []);
|
||||
setLoading(false);
|
||||
} else {
|
||||
@@ -104,10 +108,12 @@ export default function EditResourcePage() {
|
||||
tags: tags.map((tag) => tag.id),
|
||||
article: article,
|
||||
images: images,
|
||||
cover_id: coverId,
|
||||
links: links,
|
||||
gallery: galleryImages,
|
||||
gallery_nsfw: galleryNsfw,
|
||||
characters: characters,
|
||||
release_date: releaseDate,
|
||||
});
|
||||
if (res.success) {
|
||||
setSubmitting(false);
|
||||
@@ -194,6 +200,14 @@ export default function EditResourcePage() {
|
||||
{t("Add Alternative Title")}
|
||||
</button>
|
||||
<div className={"h-2"}></div>
|
||||
<p className={"my-1"}>{t("Release Date")}</p>
|
||||
<input
|
||||
type="date"
|
||||
className="input"
|
||||
value={releaseDate || ""}
|
||||
onChange={(e) => setReleaseDate(e.target.value || undefined)}
|
||||
/>
|
||||
<div className={"h-4"}></div>
|
||||
<p className={"my-1"}>{t("Links")}</p>
|
||||
<div className={"flex flex-col"}>
|
||||
{links.map((link, index) => {
|
||||
@@ -317,7 +331,7 @@ export default function EditResourcePage() {
|
||||
"Images will not be displayed automatically, you need to reference them in the description",
|
||||
)}
|
||||
</p>
|
||||
<p>{t("The first image will be used as the cover image")}</p>
|
||||
<p>{t("You can select a cover image using the radio button in the Cover column")}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
@@ -328,6 +342,7 @@ export default function EditResourcePage() {
|
||||
<tr>
|
||||
<td>{t("Preview")}</td>
|
||||
<td>{"Markdown"}</td>
|
||||
<td>{t("Cover")}</td>
|
||||
<td>{t("Gallery")}</td>
|
||||
<td>{"Nsfw"}</td>
|
||||
<td>{t("Action")}</td>
|
||||
@@ -357,6 +372,15 @@ export default function EditResourcePage() {
|
||||
<MdContentCopy />
|
||||
</button>
|
||||
</td>
|
||||
<td>
|
||||
<input
|
||||
type="radio"
|
||||
name="cover"
|
||||
className="radio radio-accent"
|
||||
checked={coverId === image}
|
||||
onChange={() => setCoverId(image)}
|
||||
/>
|
||||
</td>
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
@@ -398,6 +422,9 @@ export default function EditResourcePage() {
|
||||
const newImages = [...images];
|
||||
newImages.splice(index, 1);
|
||||
setImages(newImages);
|
||||
if (coverId === id) {
|
||||
setCoverId(undefined);
|
||||
}
|
||||
network.deleteImage(id);
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -5,7 +5,6 @@ import { app } from "../app.ts";
|
||||
import { Resource, RSort, Statistics } from "../network/models.ts";
|
||||
import { useTranslation } from "../utils/i18n";
|
||||
import { useAppContext } from "../components/AppContext.tsx";
|
||||
import Select from "../components/select.tsx";
|
||||
import { useNavigate } from "react-router";
|
||||
import { useNavigator } from "../components/navigator.tsx";
|
||||
import {
|
||||
@@ -40,23 +39,32 @@ export default function HomePage() {
|
||||
<>
|
||||
<HomeHeader />
|
||||
<div className={"flex pt-4 px-4 items-center"}>
|
||||
<Select
|
||||
values={[
|
||||
<select
|
||||
value={order}
|
||||
className="select select-primary max-w-72"
|
||||
onChange={(e) => {
|
||||
const order = parseInt(e.target.value);
|
||||
setOrder(order);
|
||||
if (appContext) {
|
||||
appContext.set("home_page_order", order);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{[
|
||||
t("Time Ascending"),
|
||||
t("Time Descending"),
|
||||
t("Views Ascending"),
|
||||
t("Views Descending"),
|
||||
t("Downloads Ascending"),
|
||||
t("Downloads Descending"),
|
||||
]}
|
||||
current={order}
|
||||
onSelected={(index) => {
|
||||
setOrder(index);
|
||||
if (appContext) {
|
||||
appContext.set("home_page_order", index);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
t("Release Date Ascending"),
|
||||
t("Release Date Descending"),
|
||||
].map((label, idx) => (
|
||||
<option key={idx} value={idx}>
|
||||
{label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<ResourcesView
|
||||
key={`home_page_${order}`}
|
||||
|
||||
212
frontend/src/pages/notification_page.tsx
Normal file
212
frontend/src/pages/notification_page.tsx
Normal file
@@ -0,0 +1,212 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { Activity, ActivityType } from "../network/models.ts";
|
||||
import { network } from "../network/network.ts";
|
||||
import showToast from "../components/toast.ts";
|
||||
import { useTranslation } from "../utils/i18n";
|
||||
import { useNavigate } from "react-router";
|
||||
import Loading from "../components/loading.tsx";
|
||||
import { CommentContent } from "../components/comment_tile.tsx";
|
||||
import { MdOutlineArchive, MdOutlinePhotoAlbum } from "react-icons/md";
|
||||
import Badge from "../components/badge.tsx";
|
||||
import Markdown from "react-markdown";
|
||||
import { ErrorAlert } from "../components/alert.tsx";
|
||||
import { app } from "../app.ts";
|
||||
import { useNavigator } from "../components/navigator.tsx";
|
||||
|
||||
export default function NotificationPage() {
|
||||
const [activities, setActivities] = useState<Activity[]>([]);
|
||||
const pageRef = useRef(0);
|
||||
const maxPageRef = useRef(1);
|
||||
const isLoadingRef = useRef(false);
|
||||
const { t } = useTranslation();
|
||||
const navigator = useNavigator();
|
||||
|
||||
const fetchNextPage = useCallback(async () => {
|
||||
if (isLoadingRef.current || pageRef.current >= maxPageRef.current) return;
|
||||
isLoadingRef.current = true;
|
||||
const response = await network.getUserNotifications(pageRef.current + 1);
|
||||
if (response.success) {
|
||||
setActivities((prev) => [...prev, ...response.data!]);
|
||||
pageRef.current += 1;
|
||||
maxPageRef.current = response.totalPages!;
|
||||
} else {
|
||||
showToast({
|
||||
type: "error",
|
||||
message: response.message || "Failed to load activities",
|
||||
});
|
||||
}
|
||||
isLoadingRef.current = false;
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchNextPage();
|
||||
}, [fetchNextPage]);
|
||||
|
||||
useEffect(() => {
|
||||
network.resetUserNotificationsCount();
|
||||
navigator.refresh();
|
||||
}, [navigator]);
|
||||
|
||||
useEffect(() => {
|
||||
document.title = t("Notifications");
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const handleScroll = () => {
|
||||
if (
|
||||
window.innerHeight + window.scrollY >=
|
||||
document.documentElement.scrollHeight - 100 &&
|
||||
!isLoadingRef.current &&
|
||||
pageRef.current < maxPageRef.current
|
||||
) {
|
||||
fetchNextPage();
|
||||
}
|
||||
};
|
||||
window.addEventListener("scroll", handleScroll);
|
||||
return () => window.removeEventListener("scroll", handleScroll);
|
||||
}, [fetchNextPage]);
|
||||
|
||||
if (!app.user) {
|
||||
return (
|
||||
<ErrorAlert
|
||||
className={"m-4"}
|
||||
message={t("You are not logged in. Please log in to access this page.")}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={"pb-2"}>
|
||||
{activities.map((activity) => (
|
||||
<ActivityCard key={activity.id} activity={activity} />
|
||||
))}
|
||||
{pageRef.current < maxPageRef.current && <Loading />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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 ActivityCard({ activity }: { activity: Activity }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const messages = [
|
||||
"Unknown activity",
|
||||
t("Published a resource"),
|
||||
t("Updated a resource"),
|
||||
t("Posted a comment"),
|
||||
t("Added a new file"),
|
||||
];
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
let content = <></>;
|
||||
|
||||
if (
|
||||
activity.type === ActivityType.ResourcePublished ||
|
||||
activity.type === ActivityType.ResourceUpdated
|
||||
) {
|
||||
content = (
|
||||
<div className={"mx-1"}>
|
||||
<div className={"font-bold my-4 break-all"}>
|
||||
{activity.resource?.title}
|
||||
</div>
|
||||
{activity.resource?.image && (
|
||||
<div>
|
||||
<img
|
||||
className={"object-contain max-h-52 mt-2 rounded-lg"}
|
||||
src={network.getResampledImageUrl(activity.resource.image.id)}
|
||||
alt={activity.resource.title}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
} else if (activity.type === ActivityType.NewComment) {
|
||||
content = (
|
||||
<div className="comment_tile">
|
||||
<CommentContent content={activity.comment!.content} />
|
||||
</div>
|
||||
);
|
||||
} else if (activity.type === ActivityType.NewFile) {
|
||||
content = (
|
||||
<div>
|
||||
<h4 className={"font-bold py-2 break-all"}>
|
||||
{activity.file!.filename}
|
||||
</h4>
|
||||
<div className={"text-sm my-1 comment_tile"}>
|
||||
<Markdown>
|
||||
{activity.file!.description.replaceAll("\n", " \n")}
|
||||
</Markdown>
|
||||
</div>
|
||||
<p className={"pt-1"}>
|
||||
<Badge className={"badge-soft badge-secondary text-xs mr-2"}>
|
||||
<MdOutlineArchive size={16} className={"inline-block"} />
|
||||
{activity.file!.is_redirect
|
||||
? t("Redirect")
|
||||
: fileSizeToString(activity.file!.size)}
|
||||
</Badge>
|
||||
<Badge className={"badge-soft badge-accent text-xs mr-2"}>
|
||||
<MdOutlinePhotoAlbum size={16} className={"inline-block"} />
|
||||
{(() => {
|
||||
let title = activity.resource!.title;
|
||||
if (title.length > 20) {
|
||||
title = title.slice(0, 20) + "...";
|
||||
}
|
||||
return title;
|
||||
})()}
|
||||
</Badge>
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={
|
||||
"card shadow m-4 p-4 hover:shadow-md transition-shadow cursor-pointer bg-base-100-tr82"
|
||||
}
|
||||
onClick={() => {
|
||||
if (
|
||||
activity.type === ActivityType.ResourcePublished ||
|
||||
activity.type === ActivityType.ResourceUpdated
|
||||
) {
|
||||
navigate(`/resources/${activity.resource?.id}`);
|
||||
} else if (activity.type === ActivityType.NewComment) {
|
||||
navigate(`/comments/${activity.comment?.id}`);
|
||||
} else if (activity.type === ActivityType.NewFile) {
|
||||
navigate(`/resources/${activity.resource?.id}#files`);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className={"flex items-center"}>
|
||||
<div className={"avatar w-9 h-9 rounded-full"}>
|
||||
<img
|
||||
className={"rounded-full"}
|
||||
alt={"avatar"}
|
||||
src={network.getUserAvatar(activity.user!)}
|
||||
/>
|
||||
</div>
|
||||
<span className={"mx-2 font-bold text-sm"}>
|
||||
{activity.user?.username}
|
||||
</span>
|
||||
<span
|
||||
className={"ml-2 badge-sm sm:badge-md badge badge-primary badge-soft"}
|
||||
>
|
||||
{messages[activity.type]}
|
||||
</span>
|
||||
</div>
|
||||
{content}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -24,9 +24,11 @@ import CharacterEditer, { FetchVndbCharactersButton } from "../components/charac
|
||||
export default function PublishPage() {
|
||||
const [title, setTitle] = useState<string>("");
|
||||
const [altTitles, setAltTitles] = useState<string[]>([]);
|
||||
const [releaseDate, setReleaseDate] = useState<string | undefined>(undefined);
|
||||
const [tags, setTags] = useState<Tag[]>([]);
|
||||
const [article, setArticle] = useState<string>("");
|
||||
const [images, setImages] = useState<number[]>([]);
|
||||
const [coverId, setCoverId] = useState<number | undefined>(undefined);
|
||||
const [links, setLinks] = useState<{ label: string; url: string }[]>([]);
|
||||
const [galleryImages, setGalleryImages] = useState<number[]>([]);
|
||||
const [galleryNsfw, setGalleryNsfw] = useState<number[]>([]);
|
||||
@@ -43,9 +45,15 @@ export default function PublishPage() {
|
||||
const data = JSON.parse(oldData);
|
||||
setTitle(data.title || "");
|
||||
setAltTitles(data.alternative_titles || []);
|
||||
setReleaseDate(data.release_date || undefined);
|
||||
setTags(data.tags || []);
|
||||
setArticle(data.article || "");
|
||||
setImages(data.images || []);
|
||||
setCoverId(data.cover_id || undefined);
|
||||
setLinks(data.links || []);
|
||||
setGalleryImages(data.gallery || []);
|
||||
setGalleryNsfw(data.gallery_nsfw || []);
|
||||
setCharacters(data.characters || []);
|
||||
} catch (e) {
|
||||
console.error("Failed to parse publish_data from localStorage", e);
|
||||
}
|
||||
@@ -58,11 +66,17 @@ export default function PublishPage() {
|
||||
tags: tags,
|
||||
article: article,
|
||||
images: images,
|
||||
cover_id: coverId,
|
||||
links: links,
|
||||
gallery: galleryImages,
|
||||
gallery_nsfw: galleryNsfw,
|
||||
characters: characters,
|
||||
release_date: releaseDate,
|
||||
};
|
||||
const dataString = JSON.stringify(data);
|
||||
localStorage.setItem("publish_data", dataString);
|
||||
}
|
||||
}, [altTitles, article, images, tags, title]);
|
||||
}, [altTitles, article, images, coverId, tags, title, links, galleryImages, galleryNsfw, characters, releaseDate]);
|
||||
|
||||
const navigate = useNavigate();
|
||||
const { t } = useTranslation();
|
||||
@@ -105,9 +119,11 @@ export default function PublishPage() {
|
||||
const res = await network.createResource({
|
||||
title: title,
|
||||
alternative_titles: altTitles,
|
||||
release_date: releaseDate,
|
||||
tags: tags.map((tag) => tag.id),
|
||||
article: article,
|
||||
images: images,
|
||||
cover_id: coverId,
|
||||
links: links,
|
||||
gallery: galleryImages,
|
||||
gallery_nsfw: galleryNsfw,
|
||||
@@ -201,6 +217,14 @@ export default function PublishPage() {
|
||||
{t("Add Alternative Title")}
|
||||
</button>
|
||||
<div className={"h-2"}></div>
|
||||
<p className={"my-1"}>{t("Release Date")}</p>
|
||||
<input
|
||||
type="date"
|
||||
className="input"
|
||||
value={releaseDate || ""}
|
||||
onChange={(e) => setReleaseDate(e.target.value || undefined)}
|
||||
/>
|
||||
<div className={"h-4"}></div>
|
||||
<p className={"my-1"}>{t("Links")}</p>
|
||||
<div className={"flex flex-col"}>
|
||||
{links.map((link, index) => {
|
||||
@@ -324,7 +348,7 @@ export default function PublishPage() {
|
||||
"Images will not be displayed automatically, you need to reference them in the description",
|
||||
)}
|
||||
</p>
|
||||
<p>{t("The first image will be used as the cover image")}</p>
|
||||
<p>{t("You can select a cover image using the radio button in the Cover column")}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
@@ -335,7 +359,8 @@ export default function PublishPage() {
|
||||
<tr>
|
||||
<td>{t("Preview")}</td>
|
||||
<td>{"Markdown"}</td>
|
||||
<td>{"Gallery"}</td>
|
||||
<td>{t("Cover")}</td>
|
||||
<td>{t("Gallery")}</td>
|
||||
<td>{"Nsfw"}</td>
|
||||
<td>{t("Action")}</td>
|
||||
</tr>
|
||||
@@ -364,6 +389,15 @@ export default function PublishPage() {
|
||||
<MdContentCopy />
|
||||
</button>
|
||||
</td>
|
||||
<td>
|
||||
<input
|
||||
type="radio"
|
||||
name="cover"
|
||||
className="radio radio-accent"
|
||||
checked={coverId === image}
|
||||
onChange={() => setCoverId(image)}
|
||||
/>
|
||||
</td>
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
@@ -405,6 +439,9 @@ export default function PublishPage() {
|
||||
const newImages = [...images];
|
||||
newImages.splice(index, 1);
|
||||
setImages(newImages);
|
||||
if (coverId === id) {
|
||||
setCoverId(undefined);
|
||||
}
|
||||
network.deleteImage(id);
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -209,6 +209,14 @@ export default function ResourcePage() {
|
||||
</h2>
|
||||
);
|
||||
})}
|
||||
{
|
||||
resource.releaseDate ? (
|
||||
<div className={"px-4 py-1 text-sm text-gray-600 dark:text-gray-400 flex items-center"}>
|
||||
<MdOutlineAccessTime size={18} className={"inline-block mr-1"} />
|
||||
{t("Release Date")}: {resource.releaseDate.split("T")[0]}
|
||||
</div>
|
||||
) : null
|
||||
}
|
||||
<button
|
||||
onClick={() => {
|
||||
navigate(
|
||||
|
||||
14
go.mod
14
go.mod
@@ -14,14 +14,17 @@ require (
|
||||
github.com/blevesearch/bleve v1.0.14
|
||||
github.com/chai2010/webp v1.4.0
|
||||
github.com/disintegration/imaging v1.6.2
|
||||
github.com/jlaffaye/ftp v0.2.0
|
||||
github.com/redis/go-redis/v9 v9.17.0
|
||||
github.com/stretchr/testify v1.11.1
|
||||
github.com/wgh136/cloudflare-error-page v0.0.1
|
||||
gorm.io/driver/mysql v1.6.0
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/RoaringBitmap/roaring v0.4.23 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/blevesearch/go-porterstemmer v1.0.3 // indirect
|
||||
github.com/blevesearch/mmap-go v1.0.2 // indirect
|
||||
github.com/blevesearch/segment v0.9.0 // indirect
|
||||
@@ -37,16 +40,22 @@ require (
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2 // indirect
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
github.com/golang/protobuf v1.3.2 // indirect
|
||||
github.com/golang/protobuf v1.5.0 // indirect
|
||||
github.com/golang/snappy v0.0.1 // indirect
|
||||
github.com/hashicorp/errwrap v1.0.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/jlaffaye/ftp v0.2.0 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/mschoch/smat v0.2.0 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/prometheus/client_model v0.6.2 // indirect
|
||||
github.com/prometheus/common v0.66.1 // indirect
|
||||
github.com/prometheus/procfs v0.16.1 // indirect
|
||||
github.com/steveyen/gtreap v0.1.0 // indirect
|
||||
github.com/willf/bitset v1.1.10 // indirect
|
||||
go.etcd.io/bbolt v1.3.5 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
google.golang.org/protobuf v1.36.8 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -75,6 +84,7 @@ require (
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.28 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/prometheus/client_golang v1.23.2
|
||||
github.com/tinylib/msgp v1.3.0 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasthttp v1.65.0 // indirect
|
||||
|
||||
40
go.sum
40
go.sum
@@ -6,6 +6,8 @@ github.com/RoaringBitmap/roaring v0.4.23/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06
|
||||
github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ=
|
||||
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
|
||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/blevesearch/bleve v1.0.14 h1:Q8r+fHTt35jtGXJUM0ULwM3Tzg+MRfyai4ZkWDy2xO4=
|
||||
github.com/blevesearch/bleve v1.0.14/go.mod h1:e/LJTr+E7EaoVdkQZTfoz7dt4KoDNvDbLb8MSKuNTLQ=
|
||||
github.com/blevesearch/blevex v1.0.0 h1:pnilj2Qi3YSEGdWgLj1Pn9Io7ukfXPoQcpAI1Bv8n/o=
|
||||
@@ -45,6 +47,7 @@ github.com/couchbase/moss v0.1.0/go.mod h1:9MaHIaRuy9pvLPUJxB8sh8OrLfyDczECVL37g
|
||||
github.com/couchbase/vellum v1.0.2 h1:BrbP0NKiyDdndMPec8Jjhy0U47CZ0Lgx3xUC2r9rZqw=
|
||||
github.com/couchbase/vellum v1.0.2/go.mod h1:FcwrEivFpNi24R3jLOs3n+fs5RnuQnQqCLBJ1uAg1W4=
|
||||
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d h1:SwD98825d6bdB+pEuTxWOXiSjBrHdOl/UVp75eI7JT8=
|
||||
github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d/go.mod h1:URriBxXwVq5ijiJ12C7iIZqlA69nTlI+LgI6/pwftG8=
|
||||
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM=
|
||||
@@ -83,13 +86,17 @@ github.com/gofiber/utils/v2 v2.0.0-rc.1/go.mod h1:Y1g08g7gvST49bbjHJ1AVqcsmg9391
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a h1:l7A0loSszR5zHd/qK53ZIHMO8b3bBSmENnQ6eKnUT0A=
|
||||
github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
@@ -121,6 +128,12 @@ github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/kljensen/snowball v0.6.0/go.mod h1:27N7E8fVU5H68RlUmnWwZCfxgt4POBJfENGMvNRhldw=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
@@ -139,6 +152,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
|
||||
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
|
||||
github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM=
|
||||
github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
@@ -148,10 +163,20 @@ github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
|
||||
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
|
||||
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||
github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs=
|
||||
github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA=
|
||||
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
|
||||
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
|
||||
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
|
||||
github.com/redis/go-redis/v9 v9.17.0 h1:K6E+ZlYN95KSMmZeEQPbU/c++wfmEvfFB17yEAq/VhM=
|
||||
github.com/redis/go-redis/v9 v9.17.0/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
@@ -187,6 +212,8 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.65.0 h1:j/u3uzFEGFfRxw79iYzJN+TteTJwbYkru9uDp3d0Yf8=
|
||||
github.com/valyala/fasthttp v1.65.0/go.mod h1:P/93/YkKPMsKSnATEeELUCkG8a7Y+k99uxNHVbKINr4=
|
||||
github.com/wgh136/cloudflare-error-page v0.0.1 h1:OZ2JWfEF85JlwSVE71Jx0f+++HkotvZZ1Fb6YUyoFcQ=
|
||||
github.com/wgh136/cloudflare-error-page v0.0.1/go.mod h1:/0dw1xavAlZLFlJla5qeLIh1/hv0irtR8oN7SBVMD8s=
|
||||
github.com/willf/bitset v1.1.10 h1:NotGKqX0KwQ72NUzqrjZq5ipPNDQex9lo3WpaS8L2sc=
|
||||
github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
|
||||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
|
||||
@@ -196,6 +223,10 @@ github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZ
|
||||
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
||||
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
|
||||
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||
@@ -221,8 +252,13 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
|
||||
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
7
main.go
7
main.go
@@ -6,7 +6,9 @@ import (
|
||||
"nysoure/server/middleware"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/adaptor"
|
||||
"github.com/gofiber/fiber/v3/middleware/logger"
|
||||
prom "github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -29,6 +31,10 @@ func main() {
|
||||
|
||||
app.Use(middleware.FrontendMiddleware)
|
||||
|
||||
app.Use(middleware.StatMiddleware)
|
||||
|
||||
app.Get("/metrics", adaptor.HTTPHandler(prom.Handler()))
|
||||
|
||||
apiG := app.Group("/api")
|
||||
{
|
||||
api.AddUserRoutes(apiG)
|
||||
@@ -42,6 +48,7 @@ func main() {
|
||||
api.AddActivityRoutes(apiG)
|
||||
api.AddCollectionRoutes(apiG)
|
||||
api.AddProxyRoutes(apiG)
|
||||
api.AddDevAPI(apiG)
|
||||
}
|
||||
|
||||
log.Fatal(app.Listen(":3000"))
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func handleGetActivity(c fiber.Ctx) error {
|
||||
@@ -28,6 +29,68 @@ func handleGetActivity(c fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func handleGetUserNotifications(c fiber.Ctx) error {
|
||||
uid, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewUnAuthorizedError("Unauthorized")
|
||||
}
|
||||
pageStr := c.Query("page", "1")
|
||||
page, err := strconv.Atoi(pageStr)
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid page number")
|
||||
}
|
||||
notifications, totalPages, err := service.GetUserNotifications(uid, page)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if notifications == nil {
|
||||
notifications = []model.ActivityView{}
|
||||
}
|
||||
return c.JSON(model.PageResponse[model.ActivityView]{
|
||||
Success: true,
|
||||
Data: notifications,
|
||||
TotalPages: totalPages,
|
||||
Message: "User notifications retrieved successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func handleResetUserNotificationsCount(c fiber.Ctx) error {
|
||||
uid, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewUnAuthorizedError("Unauthorized")
|
||||
}
|
||||
err := service.ResetUserNotificationsCount(uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(model.Response[any]{
|
||||
Success: true,
|
||||
Message: "User notifications count reset successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func handleGetUserNotificationsCount(c fiber.Ctx) error {
|
||||
uid, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewUnAuthorizedError("Unauthorized")
|
||||
}
|
||||
count, err := service.GetUserNotificationsCount(uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(model.Response[uint]{
|
||||
Success: true,
|
||||
Data: count,
|
||||
Message: "User notifications count retrieved successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func AddActivityRoutes(router fiber.Router) {
|
||||
router.Get("/activity", handleGetActivity)
|
||||
notificationrouter := router.Group("/notification")
|
||||
{
|
||||
notificationrouter.Get("/", handleGetUserNotifications)
|
||||
notificationrouter.Post("/reset", handleResetUserNotificationsCount)
|
||||
notificationrouter.Get("/count", handleGetUserNotificationsCount)
|
||||
}
|
||||
}
|
||||
|
||||
62
server/api/dev.go
Normal file
62
server/api/dev.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/middleware"
|
||||
"time"
|
||||
|
||||
"nysoure/server/search"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func rebuildSearchIndex(c fiber.Ctx) error {
|
||||
err := search.RebuildSearchIndex()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "Failed to rebuild search index: " + err.Error(),
|
||||
})
|
||||
}
|
||||
return c.JSON(fiber.Map{
|
||||
"message": "Search index rebuilt successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func updateResourceReleaseDate(c fiber.Ctx) error {
|
||||
type Request struct {
|
||||
ResourceID uint `json:"resource_id"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
}
|
||||
var req Request
|
||||
if err := c.Bind().JSON(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"error": "Invalid request body: " + err.Error(),
|
||||
})
|
||||
}
|
||||
date, err := time.Parse("2006-01-02", req.ReleaseDate)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"error": "Invalid date format: " + err.Error(),
|
||||
})
|
||||
}
|
||||
err = dao.UpdateResourceReleaseDate(req.ResourceID, date)
|
||||
if err != nil {
|
||||
slog.Error("Failed to update release date", "error", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "Failed to update release date",
|
||||
})
|
||||
}
|
||||
return c.JSON(fiber.Map{
|
||||
"message": "Release date updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func AddDevAPI(router fiber.Router) {
|
||||
devGroup := router.Group("/dev")
|
||||
devGroup.Use(middleware.DevMiddleware())
|
||||
{
|
||||
devGroup.Post("/rebuild_search_index", rebuildSearchIndex)
|
||||
devGroup.Post("/update_resource_release_date", updateResourceReleaseDate)
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"nysoure/server/middleware"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
"nysoure/server/stat"
|
||||
"nysoure/server/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -229,13 +230,18 @@ func downloadFile(c fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
q := uri.Query()
|
||||
if len(q) != 0 {
|
||||
// If there are already query parameters, assume the URL is signed
|
||||
return c.Redirect().Status(fiber.StatusFound).To(uri.String())
|
||||
}
|
||||
token, err := utils.GenerateDownloadToken(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
q := uri.Query()
|
||||
q.Set("token", token)
|
||||
uri.RawQuery = q.Encode()
|
||||
stat.RecordDownload()
|
||||
return c.Redirect().Status(fiber.StatusFound).To(uri.String())
|
||||
}
|
||||
data := map[string]string{
|
||||
@@ -247,6 +253,7 @@ func downloadFile(c fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return model.NewInternalServerError("Failed to generate download token")
|
||||
}
|
||||
stat.RecordDownload()
|
||||
return c.Redirect().Status(fiber.StatusFound).To(fmt.Sprintf("%s/api/files/download/local?token=%s", c.BaseURL(), token))
|
||||
}
|
||||
|
||||
|
||||
@@ -88,10 +88,6 @@ func handleGetResampledImage(c fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if image == nil {
|
||||
// No resampled image, redirect to original
|
||||
return c.Redirect().To("/api/image/" + idStr)
|
||||
}
|
||||
contentType := http.DetectContentType(image)
|
||||
c.Set("Content-Type", contentType)
|
||||
c.Set("Cache-Control", "public, max-age=31536000")
|
||||
|
||||
@@ -109,7 +109,7 @@ func handleListResources(c fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid sort parameter")
|
||||
}
|
||||
if sortInt < 0 || sortInt > 5 {
|
||||
if sortInt < 0 || sortInt > 7 {
|
||||
return model.NewRequestError("Sort parameter out of range")
|
||||
}
|
||||
sort := model.RSort(sortInt)
|
||||
@@ -287,7 +287,11 @@ func handleGetCharactersFromVndb(c fiber.Ctx) error {
|
||||
if vnID == "" {
|
||||
return model.NewRequestError("VNDB ID is required")
|
||||
}
|
||||
characters, err := service.GetCharactersFromVndb(vnID)
|
||||
uid, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewUnAuthorizedError("You must be logged in to get characters from VNDB")
|
||||
}
|
||||
characters, err := service.GetCharactersFromVndb(vnID, uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"nysoure/server/middleware"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
"nysoure/server/stat"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -24,6 +25,7 @@ func handleUserRegister(c fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stat.RecordRegister()
|
||||
return c.Status(fiber.StatusOK).JSON(model.Response[model.UserViewWithToken]{
|
||||
Success: true,
|
||||
Data: user,
|
||||
|
||||
@@ -2,9 +2,10 @@ package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"nysoure/server/model"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func AddNewResourceActivity(userID, resourceID uint) error {
|
||||
@@ -42,13 +43,20 @@ func AddUpdateResourceActivity(userID, resourceID uint) error {
|
||||
return db.Create(activity).Error
|
||||
}
|
||||
|
||||
func AddNewCommentActivity(userID, commentID uint) error {
|
||||
func AddNewCommentActivity(userID, commentID, notifyTo uint) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
activity := &model.Activity{
|
||||
UserID: userID,
|
||||
Type: model.ActivityTypeNewComment,
|
||||
RefID: commentID,
|
||||
NotifyTo: notifyTo,
|
||||
}
|
||||
return db.Create(activity).Error
|
||||
err := tx.Create(activity).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&model.User{}).Where("id = ?", notifyTo).UpdateColumn("unread_notifications_count", gorm.Expr("unread_notifications_count + ?", 1)).Error
|
||||
})
|
||||
}
|
||||
|
||||
func AddNewFileActivity(userID, fileID uint) error {
|
||||
@@ -82,3 +90,18 @@ func GetActivityList(offset, limit int) ([]model.Activity, int, error) {
|
||||
|
||||
return activities, int(total), nil
|
||||
}
|
||||
|
||||
func GetUserNotifications(userID uint, offset, limit int) ([]model.Activity, int, error) {
|
||||
var activities []model.Activity
|
||||
var total int64
|
||||
|
||||
if err := db.Model(&model.Activity{}).Where("notify_to = ?", userID).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := db.Where("notify_to = ?", userID).Offset(offset).Limit(limit).Order("id DESC").Find(&activities).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return activities, int(total), nil
|
||||
}
|
||||
|
||||
@@ -99,6 +99,10 @@ func GetResourceList(page, pageSize int, sort model.RSort) ([]model.Resource, in
|
||||
order = "downloads ASC"
|
||||
case model.RSortDownloadsDesc:
|
||||
order = "downloads DESC"
|
||||
case model.RSortReleaseDateAsc:
|
||||
order = "release_date ASC"
|
||||
case model.RSortReleaseDateDesc:
|
||||
order = "release_date DESC"
|
||||
default:
|
||||
order = "modified_time DESC" // Default sort order
|
||||
}
|
||||
@@ -693,3 +697,25 @@ func UpdateResourceImage(resourceID, oldImageID, newImageID uint) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func GetResourceOwnerID(resourceID uint) (uint, error) {
|
||||
var uid uint
|
||||
if err := db.Model(&model.Resource{}).Select("user_id").Where("id = ?", resourceID).First(&uid).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
func UpdateResourceReleaseDate(resourceID uint, releaseDate time.Time) error {
|
||||
result := db.Model(&model.Resource{}).Where("id = ?", resourceID).Update("release_date", releaseDate)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,8 +2,9 @@ package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"nysoure/server/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CreateUser(username string, hashedPassword []byte) (model.User, error) {
|
||||
@@ -21,9 +22,7 @@ func CreateUser(username string, hashedPassword []byte) (model.User, error) {
|
||||
return user, err
|
||||
}
|
||||
if exists {
|
||||
return user, &model.RequestError{
|
||||
Message: "User already exists",
|
||||
}
|
||||
return user, model.NewRequestError("User already exists")
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
return user, err
|
||||
@@ -132,3 +131,15 @@ func DeleteUser(id uint) error {
|
||||
}
|
||||
return db.Delete(&model.User{}, id).Error
|
||||
}
|
||||
|
||||
func ResetUserNotificationsCount(userID uint) error {
|
||||
return db.Model(&model.User{}).Where("id = ?", userID).Update("unread_notifications_count", 0).Error
|
||||
}
|
||||
|
||||
func GetUserNotificationCount(userID uint) (uint, error) {
|
||||
var count uint
|
||||
if err := db.Model(&model.User{}).Where("id = ?", userID).Select("unread_notifications_count").Scan(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
22
server/middleware/dev_middleware.go
Normal file
22
server/middleware/dev_middleware.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"nysoure/server/model"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func DevMiddleware() func(c fiber.Ctx) error {
|
||||
AccessKey := os.Getenv("DEV_ACCESS_KEY")
|
||||
return func(c fiber.Ctx) error {
|
||||
if AccessKey == "" {
|
||||
return model.NewUnAuthorizedError("Unauthorized")
|
||||
}
|
||||
providedKey := c.Get("X-DEV-ACCESS-KEY")
|
||||
if providedKey != AccessKey {
|
||||
return model.NewUnAuthorizedError("Unauthorized")
|
||||
}
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"nysoure/server/model"
|
||||
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
@@ -13,64 +12,14 @@ import (
|
||||
func ErrorHandler(c fiber.Ctx) error {
|
||||
err := c.Next()
|
||||
if err != nil {
|
||||
var requestErr *model.RequestError
|
||||
var unauthorizedErr *model.UnAuthorizedError
|
||||
var notFoundErr *model.NotFoundError
|
||||
var fiberErr *fiber.Error
|
||||
if errors.As(err, &requestErr) {
|
||||
log.Error("Request Error: ", err)
|
||||
return c.Status(fiber.StatusBadRequest).JSON(model.Response[any]{
|
||||
Success: false,
|
||||
Data: nil,
|
||||
Message: requestErr.Error(),
|
||||
})
|
||||
} else if errors.As(err, &unauthorizedErr) {
|
||||
log.Error("Unauthorized Error: ", err)
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(model.Response[any]{
|
||||
Success: false,
|
||||
Data: nil,
|
||||
Message: unauthorizedErr.Error(),
|
||||
})
|
||||
} else if errors.As(err, ¬FoundErr) {
|
||||
log.Error("Not Found Error: ", err)
|
||||
return c.Status(fiber.StatusNotFound).JSON(model.Response[any]{
|
||||
Success: false,
|
||||
Data: nil,
|
||||
Message: notFoundErr.Error(),
|
||||
})
|
||||
} else if errors.Is(err, fiber.ErrNotFound) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(model.Response[any]{
|
||||
Success: false,
|
||||
Data: nil,
|
||||
Message: "Not found",
|
||||
})
|
||||
} else if errors.Is(err, fiber.ErrMethodNotAllowed) {
|
||||
return c.Status(fiber.StatusMethodNotAllowed).JSON(model.Response[any]{
|
||||
Success: false,
|
||||
Data: nil,
|
||||
Message: "Method not allowed",
|
||||
})
|
||||
} else if errors.As(err, &fiberErr) && fiberErr.Message != "" {
|
||||
if errors.As(err, &fiberErr) {
|
||||
if fiberErr.Code != fiber.StatusInternalServerError {
|
||||
return c.Status(fiberErr.Code).JSON(model.Response[any]{
|
||||
Success: false,
|
||||
Data: nil,
|
||||
Message: fiberErr.Message,
|
||||
})
|
||||
} else if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(model.Response[any]{
|
||||
Success: false,
|
||||
Data: nil,
|
||||
Message: "Not found",
|
||||
})
|
||||
} else {
|
||||
var fiberErr *fiber.Error
|
||||
if errors.As(err, &fiberErr) {
|
||||
if fiberErr.Code == fiber.StatusNotFound {
|
||||
return c.Status(fiber.StatusNotFound).JSON(model.Response[any]{
|
||||
Success: false,
|
||||
Data: nil,
|
||||
Message: "Not found",
|
||||
})
|
||||
}
|
||||
}
|
||||
log.Error("Internal Server Error: ", err)
|
||||
@@ -80,6 +29,5 @@ func ErrorHandler(c fiber.Ctx) error {
|
||||
Message: "Internal server error",
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ func FrontendMiddleware(c fiber.Ctx) error {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
if strings.HasPrefix(c.Path(), "/metrics") {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
path := c.Path()
|
||||
file := "static" + path
|
||||
|
||||
|
||||
21
server/middleware/stat_middleware.go
Normal file
21
server/middleware/stat_middleware.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"nysoure/server/stat"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func StatMiddleware(c fiber.Ctx) error {
|
||||
err := c.Next()
|
||||
status := "200"
|
||||
if err != nil {
|
||||
if e, ok := err.(*fiber.Error); ok {
|
||||
status = string(rune(e.Code))
|
||||
} else {
|
||||
status = "500"
|
||||
}
|
||||
}
|
||||
stat.RecordRequest(c.Method(), c.Route().Path, status)
|
||||
return err
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
|
||||
errorpage "github.com/wgh136/cloudflare-error-page"
|
||||
)
|
||||
|
||||
func UnsupportedRegionMiddleware(c fiber.Ctx) error {
|
||||
@@ -20,10 +22,39 @@ func UnsupportedRegionMiddleware(c fiber.Ctx) error {
|
||||
}
|
||||
|
||||
if string(c.Request().Header.Peek("Unsupported-Region")) == "true" {
|
||||
// Return a 403 Forbidden response with an empty html for unsupported regions
|
||||
h, err := generateForbiddenPage(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Response().Header.Add("Content-Type", "text/html")
|
||||
c.Status(fiber.StatusForbidden)
|
||||
return c.SendString("<html></html>")
|
||||
return c.SendString(h)
|
||||
}
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func generateForbiddenPage(c fiber.Ctx) (string, error) {
|
||||
params := errorpage.Params{
|
||||
"error_code": 403,
|
||||
"title": "Forbidden",
|
||||
"browser_status": map[string]interface{}{
|
||||
"status": "error",
|
||||
"status_text": "Error",
|
||||
},
|
||||
"cloudflare_status": map[string]interface{}{
|
||||
"status": "ok",
|
||||
"status_text": "Working",
|
||||
},
|
||||
"host_status": map[string]interface{}{
|
||||
"status": "ok",
|
||||
"location": c.Hostname(),
|
||||
},
|
||||
"error_source": "cloudflare",
|
||||
|
||||
"what_happened": "<p>The service is not available in your region.</p>",
|
||||
"what_can_i_do": "<p>Please try again in a few minutes.</p>",
|
||||
"client_ip": c.IP(),
|
||||
}
|
||||
|
||||
return errorpage.Render(params, nil)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ type Activity struct {
|
||||
UserID uint `gorm:"not null"`
|
||||
Type ActivityType `gorm:"not null;index:idx_type_refid"`
|
||||
RefID uint `gorm:"not null;index:idx_type_refid"`
|
||||
NotifyTo uint `gorm:"default:null;index"`
|
||||
}
|
||||
|
||||
type ActivityView struct {
|
||||
|
||||
@@ -2,78 +2,31 @@ package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
type RequestError struct {
|
||||
Message string `json:"message"`
|
||||
func NewRequestError(message string) error {
|
||||
return fiber.NewError(400, message)
|
||||
}
|
||||
|
||||
func (e *RequestError) Error() string {
|
||||
return e.Message
|
||||
func NewUnAuthorizedError(message string) error {
|
||||
return fiber.NewError(403, message)
|
||||
}
|
||||
|
||||
func NewRequestError(message string) *RequestError {
|
||||
return &RequestError{
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func IsRequestError(err error) bool {
|
||||
var requestError *RequestError
|
||||
ok := errors.As(err, &requestError)
|
||||
return ok
|
||||
}
|
||||
|
||||
type UnAuthorizedError struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (e *UnAuthorizedError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func NewUnAuthorizedError(message string) *UnAuthorizedError {
|
||||
return &UnAuthorizedError{
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func IsUnAuthorizedError(err error) bool {
|
||||
var unAuthorizedError *UnAuthorizedError
|
||||
ok := errors.As(err, &unAuthorizedError)
|
||||
return ok
|
||||
}
|
||||
|
||||
type NotFoundError struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (e *NotFoundError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func NewNotFoundError(message string) *NotFoundError {
|
||||
return &NotFoundError{
|
||||
Message: message,
|
||||
}
|
||||
func NewNotFoundError(message string) error {
|
||||
return fiber.NewError(404, message)
|
||||
}
|
||||
|
||||
func IsNotFoundError(err error) bool {
|
||||
var notFoundError *NotFoundError
|
||||
ok := errors.As(err, ¬FoundError)
|
||||
return ok
|
||||
var fiberError *fiber.Error
|
||||
ok := errors.As(err, &fiberError)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return fiberError.Code == 404
|
||||
}
|
||||
|
||||
type InternalServerError struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (e *InternalServerError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func NewInternalServerError(message string) *InternalServerError {
|
||||
return &InternalServerError{
|
||||
Message: message,
|
||||
}
|
||||
func NewInternalServerError(message string) error {
|
||||
return fiber.NewError(500, message)
|
||||
}
|
||||
|
||||
@@ -11,8 +11,10 @@ type Resource struct {
|
||||
Title string
|
||||
AlternativeTitles []string `gorm:"serializer:json"`
|
||||
Links []Link `gorm:"serializer:json"`
|
||||
ReleaseDate *time.Time
|
||||
Article string
|
||||
Images []Image `gorm:"many2many:resource_images;"`
|
||||
CoverID *uint
|
||||
Tags []Tag `gorm:"many2many:resource_tags;"`
|
||||
Files []File `gorm:"foreignKey:ResourceID"`
|
||||
UserID uint
|
||||
@@ -35,6 +37,7 @@ type ResourceView struct {
|
||||
ID uint `json:"id"`
|
||||
Title string `json:"title"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ReleaseDate *time.Time `json:"release_date,omitempty"`
|
||||
Tags []TagView `json:"tags"`
|
||||
Image *ImageView `json:"image"`
|
||||
Author UserView `json:"author"`
|
||||
@@ -47,8 +50,10 @@ type ResourceDetailView struct {
|
||||
Links []Link `json:"links"`
|
||||
Article string `json:"article"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
ReleaseDate *time.Time `json:"releaseDate,omitempty"`
|
||||
Tags []TagView `json:"tags"`
|
||||
Images []ImageView `json:"images"`
|
||||
CoverID *uint `json:"coverId,omitempty"`
|
||||
Files []FileView `json:"files"`
|
||||
Author UserView `json:"author"`
|
||||
Views uint `json:"views"`
|
||||
@@ -75,7 +80,18 @@ func (r *Resource) ToView() ResourceView {
|
||||
}
|
||||
|
||||
var image *ImageView
|
||||
if len(r.Images) > 0 {
|
||||
if r.CoverID != nil {
|
||||
// Use the cover image if specified
|
||||
for _, img := range r.Images {
|
||||
if img.ID == *r.CoverID {
|
||||
v := img.ToView()
|
||||
image = &v
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// If no cover is set or cover image not found, use the first image
|
||||
if image == nil && len(r.Images) > 0 {
|
||||
v := r.Images[0].ToView()
|
||||
image = &v
|
||||
}
|
||||
@@ -84,6 +100,7 @@ func (r *Resource) ToView() ResourceView {
|
||||
ID: r.ID,
|
||||
Title: r.Title,
|
||||
CreatedAt: r.CreatedAt,
|
||||
ReleaseDate: r.ReleaseDate,
|
||||
Tags: tags,
|
||||
Image: image,
|
||||
Author: r.User.ToView(),
|
||||
@@ -115,8 +132,10 @@ func (r *Resource) ToDetailView() ResourceDetailView {
|
||||
Links: r.Links,
|
||||
Article: r.Article,
|
||||
CreatedAt: r.CreatedAt,
|
||||
ReleaseDate: r.ReleaseDate,
|
||||
Tags: tags,
|
||||
Images: images,
|
||||
CoverID: r.CoverID,
|
||||
Files: files,
|
||||
Author: r.User.ToView(),
|
||||
Views: r.Views,
|
||||
|
||||
@@ -9,4 +9,6 @@ const (
|
||||
RSortViewsDesc
|
||||
RSortDownloadsAsc
|
||||
RSortDownloadsDesc
|
||||
RSortReleaseDateAsc
|
||||
RSortReleaseDateDesc
|
||||
)
|
||||
|
||||
@@ -19,6 +19,7 @@ type User struct {
|
||||
CommentsCount int
|
||||
Resources []Resource `gorm:"foreignKey:UserID"`
|
||||
Bio string
|
||||
UnreadNotificationsCount uint `gorm:"not null;default:0"`
|
||||
}
|
||||
|
||||
type UserView struct {
|
||||
|
||||
@@ -3,30 +3,54 @@ package search
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/utils"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/blevesearch/bleve"
|
||||
)
|
||||
|
||||
var (
|
||||
index bleve.Index
|
||||
mu = sync.RWMutex{}
|
||||
)
|
||||
|
||||
type ResourceParams struct {
|
||||
Id uint
|
||||
Title string
|
||||
Subtitles []string
|
||||
Time time.Time
|
||||
Characters []ResourceCharacter
|
||||
}
|
||||
|
||||
var index bleve.Index
|
||||
type ResourceCharacter struct {
|
||||
Name string
|
||||
Alias []string
|
||||
CV string
|
||||
}
|
||||
|
||||
func AddResourceToIndex(r model.Resource) error {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
cs := make([]ResourceCharacter, 0, len(r.Characters))
|
||||
for _, c := range r.Characters {
|
||||
cs = append(cs, ResourceCharacter{
|
||||
Name: c.Name,
|
||||
Alias: c.Alias,
|
||||
CV: c.CV,
|
||||
})
|
||||
}
|
||||
return index.Index(fmt.Sprintf("%d", r.ID), ResourceParams{
|
||||
Id: r.ID,
|
||||
Title: r.Title,
|
||||
Subtitles: r.AlternativeTitles,
|
||||
Time: r.CreatedAt,
|
||||
Characters: cs,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -40,16 +64,25 @@ func createIndex() error {
|
||||
}
|
||||
page := 1
|
||||
total := 1
|
||||
current := 0
|
||||
for page <= total {
|
||||
res, totalPages, err := dao.GetResourceList(page, 100, model.RSortTimeAsc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, r := range res {
|
||||
err := AddResourceToIndex(r)
|
||||
r, err := dao.GetResourceByID(r.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = AddResourceToIndex(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
current++
|
||||
if current%20 == 0 {
|
||||
slog.Info("Rebuilding search index", "current", current, "total", totalPages*100)
|
||||
}
|
||||
}
|
||||
page++
|
||||
total = totalPages
|
||||
@@ -80,6 +113,8 @@ func init() {
|
||||
}
|
||||
|
||||
func SearchResource(keyword string) ([]uint, error) {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
query := bleve.NewMatchQuery(keyword)
|
||||
searchRequest := bleve.NewSearchRequest(query)
|
||||
searchResults, err := index.Search(searchRequest)
|
||||
@@ -112,3 +147,24 @@ func IsStopWord(word string) bool {
|
||||
tokens := analyzer.Analyze([]byte(word))
|
||||
return len(tokens) == 0
|
||||
}
|
||||
|
||||
func RebuildSearchIndex() error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
err := index.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to close search index: %w", err)
|
||||
}
|
||||
indexPath := utils.GetStoragePath() + "/resource_index.bleve"
|
||||
err = os.RemoveAll(indexPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove search index: %w", err)
|
||||
}
|
||||
mapping := bleve.NewIndexMapping()
|
||||
index, err = bleve.New(indexPath, mapping)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create search index: %w", err)
|
||||
}
|
||||
go createIndex()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -68,3 +68,67 @@ func GetActivityList(page int) ([]model.ActivityView, int, error) {
|
||||
|
||||
return views, totalPages, nil
|
||||
}
|
||||
|
||||
func GetUserNotifications(userID uint, page int) ([]model.ActivityView, int, error) {
|
||||
offset := (page - 1) * pageSize
|
||||
limit := pageSize
|
||||
|
||||
activities, total, err := dao.GetUserNotifications(userID, offset, limit)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var views []model.ActivityView
|
||||
for _, activity := range activities {
|
||||
user, err := dao.GetUserByID(activity.UserID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var comment *model.CommentView
|
||||
var resource *model.ResourceView
|
||||
var file *model.FileView
|
||||
switch activity.Type {
|
||||
case model.ActivityTypeNewComment:
|
||||
c, err := dao.GetCommentByID(activity.RefID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
comment = c.ToView()
|
||||
comment.Content, comment.ContentTruncated = restrictCommentLength(c.Content)
|
||||
case model.ActivityTypeNewResource, model.ActivityTypeUpdateResource:
|
||||
r, err := dao.GetResourceByID(activity.RefID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rv := r.ToView()
|
||||
resource = &rv
|
||||
case model.ActivityTypeNewFile:
|
||||
f, err := dao.GetFileByID(activity.RefID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
fv := f.ToView()
|
||||
file = fv
|
||||
r, err := dao.GetResourceByID(f.ResourceID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rv := r.ToView()
|
||||
resource = &rv
|
||||
}
|
||||
view := model.ActivityView{
|
||||
ID: activity.ID,
|
||||
User: user.ToView(),
|
||||
Type: activity.Type,
|
||||
Time: activity.CreatedAt,
|
||||
Comment: comment,
|
||||
Resource: resource,
|
||||
File: file,
|
||||
}
|
||||
views = append(views, view)
|
||||
}
|
||||
|
||||
totalPages := (total + pageSize - 1) / pageSize
|
||||
|
||||
return views, totalPages, nil
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ func CreateComment(req CommentRequest, userID uint, refID uint, ip string, cType
|
||||
return nil, model.NewRequestError("Comment content exceeds maximum length of 1024 characters")
|
||||
}
|
||||
|
||||
var notifyTo uint
|
||||
|
||||
switch cType {
|
||||
case model.CommentTypeResource:
|
||||
resourceExists, err := dao.ExistsResource(refID)
|
||||
@@ -39,12 +41,18 @@ func CreateComment(req CommentRequest, userID uint, refID uint, ip string, cType
|
||||
if !resourceExists {
|
||||
return nil, model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
notifyTo, err = dao.GetResourceOwnerID(refID)
|
||||
if err != nil {
|
||||
log.Error("Error getting resource owner ID:", err)
|
||||
return nil, model.NewInternalServerError("Error getting resource owner ID")
|
||||
}
|
||||
case model.CommentTypeReply:
|
||||
_, err := dao.GetCommentByID(refID)
|
||||
comment, err := dao.GetCommentByID(refID)
|
||||
if err != nil {
|
||||
log.Error("Error getting reply comment:", err)
|
||||
return nil, model.NewNotFoundError("Reply comment not found")
|
||||
}
|
||||
notifyTo = comment.UserID
|
||||
}
|
||||
|
||||
userExists, err := dao.ExistsUserByID(userID)
|
||||
@@ -63,7 +71,7 @@ func CreateComment(req CommentRequest, userID uint, refID uint, ip string, cType
|
||||
log.Error("Error creating comment:", err)
|
||||
return nil, model.NewInternalServerError("Error creating comment")
|
||||
}
|
||||
err = dao.AddNewCommentActivity(userID, c.ID)
|
||||
err = dao.AddNewCommentActivity(userID, c.ID, notifyTo)
|
||||
if err != nil {
|
||||
log.Error("Error creating comment activity:", err)
|
||||
}
|
||||
|
||||
@@ -31,8 +31,78 @@ import (
|
||||
|
||||
const (
|
||||
resampledMaxPixels = 1280 * 720
|
||||
subdirsCount = 256 // Number of subdirectories (0-255)
|
||||
)
|
||||
|
||||
// getImageSubdir returns the subdirectory name for an image filename
|
||||
// Uses the first 2 characters of the filename to distribute across 256 subdirs
|
||||
func getImageSubdir(filename string) string {
|
||||
if len(filename) < 2 {
|
||||
return "00"
|
||||
}
|
||||
// Use first 2 hex chars to determine subdir (e.g., "a1b2c3..." -> "a1")
|
||||
return filename[:2]
|
||||
}
|
||||
|
||||
// getImagePath returns the full path to an image, checking new subdirectory structure first,
|
||||
// then falling back to legacy flat structure for backward compatibility
|
||||
func getImagePath(filename string) string {
|
||||
baseDir := utils.GetStoragePath() + "/images/"
|
||||
|
||||
// Try new subdirectory structure first
|
||||
subdir := getImageSubdir(filename)
|
||||
newPath := baseDir + subdir + "/" + filename
|
||||
if _, err := os.Stat(newPath); err == nil {
|
||||
return newPath
|
||||
}
|
||||
|
||||
// Fall back to legacy flat structure
|
||||
legacyPath := baseDir + filename
|
||||
return legacyPath
|
||||
}
|
||||
|
||||
// ensureImageSubdir creates the subdirectory for a filename if it doesn't exist
|
||||
func ensureImageSubdir(filename string) error {
|
||||
baseDir := utils.GetStoragePath() + "/images/"
|
||||
subdir := getImageSubdir(filename)
|
||||
subdirPath := baseDir + subdir
|
||||
|
||||
if _, err := os.Stat(subdirPath); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(subdirPath, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getResampledImagePath returns the full path to a resampled image using subdirectory structure
|
||||
// Subdirectory is based on image ID modulo 256 (e.g., id=1234 -> subdir="d2" from 1234%256=210=0xd2)
|
||||
func getResampledImagePath(imageID uint) string {
|
||||
baseDir := utils.GetStoragePath() + "/resampled/"
|
||||
subdir := strconv.FormatUint(uint64(imageID%subdirsCount), 16)
|
||||
if len(subdir) == 1 {
|
||||
subdir = "0" + subdir
|
||||
}
|
||||
return baseDir + subdir + "/" + strconv.Itoa(int(imageID)) + ".webp"
|
||||
}
|
||||
|
||||
// ensureResampledSubdir creates the subdirectory for a resampled image if it doesn't exist
|
||||
func ensureResampledSubdir(imageID uint) error {
|
||||
baseDir := utils.GetStoragePath() + "/resampled/"
|
||||
subdir := strconv.FormatUint(uint64(imageID%subdirsCount), 16)
|
||||
if len(subdir) == 1 {
|
||||
subdir = "0" + subdir
|
||||
}
|
||||
subdirPath := baseDir + subdir
|
||||
|
||||
if _, err := os.Stat(subdirPath); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(subdirPath, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Start a goroutine to delete unused images every hour
|
||||
go func() {
|
||||
@@ -108,13 +178,24 @@ func CreateImage(uid uint, ip string, data []byte) (uint, error) {
|
||||
}
|
||||
|
||||
filename := uuid.New().String()
|
||||
if err := os.WriteFile(imageDir+filename, data, 0644); err != nil {
|
||||
|
||||
// Create subdirectory for new storage structure
|
||||
if err := ensureImageSubdir(filename); err != nil {
|
||||
return 0, errors.New("failed to create image subdirectory")
|
||||
}
|
||||
|
||||
// Save to new subdirectory structure
|
||||
subdir := getImageSubdir(filename)
|
||||
filepath := imageDir + subdir + "/" + filename
|
||||
if err := os.WriteFile(filepath, data, 0644); err != nil {
|
||||
return 0, errors.New("failed to save image file")
|
||||
}
|
||||
|
||||
i, err := dao.CreateImage(filename, img.Bounds().Dx(), img.Bounds().Dy())
|
||||
if err != nil {
|
||||
_ = os.Remove(imageDir + filename)
|
||||
// Clean up the file if database creation fails
|
||||
subdir := getImageSubdir(filename)
|
||||
_ = os.Remove(imageDir + subdir + "/" + filename)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -127,11 +208,11 @@ func GetImage(id uint) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
imageDir := utils.GetStoragePath() + "/images/"
|
||||
if _, err := os.Stat(imageDir); os.IsNotExist(err) {
|
||||
filepath := getImagePath(i.FileName)
|
||||
if _, err := os.Stat(filepath); os.IsNotExist(err) {
|
||||
return nil, model.NewNotFoundError("Image not found")
|
||||
}
|
||||
data, err := os.ReadFile(imageDir + i.FileName)
|
||||
data, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read image file")
|
||||
}
|
||||
@@ -161,11 +242,13 @@ func deleteImage(id uint) error {
|
||||
return err
|
||||
}
|
||||
|
||||
imageDir := utils.GetStoragePath() + "/images/"
|
||||
_ = os.Remove(imageDir + i.FileName)
|
||||
// Delete from both potential locations (new subdir and legacy flat)
|
||||
filepath := getImagePath(i.FileName)
|
||||
_ = os.Remove(filepath)
|
||||
|
||||
resampledDir := utils.GetStoragePath() + "/resampled/"
|
||||
_ = os.Remove(resampledDir + strconv.Itoa(int(i.ID)) + ".webp")
|
||||
// Delete resampled image from subdirectory structure
|
||||
resampledPath := getResampledImagePath(i.ID)
|
||||
_ = os.Remove(resampledPath)
|
||||
|
||||
if err := dao.DeleteImage(id); err != nil {
|
||||
return err
|
||||
@@ -180,10 +263,6 @@ func GetResampledImage(id uint) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if i.Width*i.Height <= resampledMaxPixels {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
data, err := getOrCreateResampledImage(i)
|
||||
if err != nil {
|
||||
log.Error("Error getting or creating resampled image:", err)
|
||||
@@ -194,14 +273,8 @@ func GetResampledImage(id uint) ([]byte, error) {
|
||||
}
|
||||
|
||||
func getOrCreateResampledImage(i model.Image) ([]byte, error) {
|
||||
baseDir := utils.GetStoragePath() + "/resampled/"
|
||||
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
resampledFilepath := baseDir + strconv.Itoa(int(i.ID)) + ".webp"
|
||||
// Check if resampled image already exists
|
||||
resampledFilepath := getResampledImagePath(i.ID)
|
||||
if _, err := os.Stat(resampledFilepath); err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
@@ -210,7 +283,7 @@ func getOrCreateResampledImage(i model.Image) ([]byte, error) {
|
||||
return os.ReadFile(resampledFilepath)
|
||||
}
|
||||
|
||||
originalFilepath := utils.GetStoragePath() + "/images/" + i.FileName
|
||||
originalFilepath := getImagePath(i.FileName)
|
||||
if _, err := os.Stat(originalFilepath); os.IsNotExist(err) {
|
||||
return nil, model.NewNotFoundError("Original image not found")
|
||||
}
|
||||
@@ -241,6 +314,12 @@ func getOrCreateResampledImage(i model.Image) ([]byte, error) {
|
||||
if err := webp.Encode(buf, dstImg, &webp.Options{Quality: 80}); err != nil {
|
||||
return nil, errors.New("failed to encode resampled image data to webp format")
|
||||
}
|
||||
|
||||
// Ensure subdirectory exists before saving
|
||||
if err := ensureResampledSubdir(i.ID); err != nil {
|
||||
return nil, errors.New("failed to create resampled image subdirectory")
|
||||
}
|
||||
|
||||
if err := os.WriteFile(resampledFilepath, buf.Bytes(), 0644); err != nil {
|
||||
return nil, errors.New("failed to save resampled image file")
|
||||
}
|
||||
|
||||
@@ -30,9 +30,11 @@ type ResourceParams struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
AlternativeTitles []string `json:"alternative_titles"`
|
||||
Links []model.Link `json:"links"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
Tags []uint `json:"tags"`
|
||||
Article string `json:"article"`
|
||||
Images []uint `json:"images"`
|
||||
CoverID *uint `json:"cover_id"`
|
||||
Gallery []uint `json:"gallery"`
|
||||
GalleryNsfw []uint `json:"gallery_nsfw"`
|
||||
Characters []CharacterParams `json:"characters"`
|
||||
@@ -101,12 +103,30 @@ func CreateResource(uid uint, params *ResourceParams) (uint, error) {
|
||||
ImageID: imageID,
|
||||
}
|
||||
}
|
||||
var date *time.Time
|
||||
if params.ReleaseDate != "" {
|
||||
parsedDate, err := time.Parse("2006-01-02", params.ReleaseDate)
|
||||
if err != nil {
|
||||
return 0, model.NewRequestError("Invalid release date format, expected YYYY-MM-DD")
|
||||
}
|
||||
date = &parsedDate
|
||||
}
|
||||
// Validate CoverID if provided
|
||||
var coverID *uint
|
||||
if params.CoverID != nil && *params.CoverID != 0 {
|
||||
if !slices.Contains(params.Images, *params.CoverID) {
|
||||
return 0, model.NewRequestError("Cover ID must be one of the resource images")
|
||||
}
|
||||
coverID = params.CoverID
|
||||
}
|
||||
r := model.Resource{
|
||||
Title: params.Title,
|
||||
AlternativeTitles: params.AlternativeTitles,
|
||||
Article: params.Article,
|
||||
Links: params.Links,
|
||||
ReleaseDate: date,
|
||||
Images: images,
|
||||
CoverID: coverID,
|
||||
Tags: tags,
|
||||
UserID: uid,
|
||||
Gallery: gallery,
|
||||
@@ -529,10 +549,30 @@ func UpdateResource(uid, rid uint, params *ResourceParams) error {
|
||||
}
|
||||
}
|
||||
|
||||
var date *time.Time
|
||||
if params.ReleaseDate != "" {
|
||||
parsedDate, err := time.Parse("2006-01-02", params.ReleaseDate)
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid release date format, expected YYYY-MM-DD")
|
||||
}
|
||||
date = &parsedDate
|
||||
}
|
||||
|
||||
// Validate CoverID if provided
|
||||
var coverID *uint
|
||||
if params.CoverID != nil && *params.CoverID != 0 {
|
||||
if !slices.Contains(params.Images, *params.CoverID) {
|
||||
return model.NewRequestError("Cover ID must be one of the resource images")
|
||||
}
|
||||
coverID = params.CoverID
|
||||
}
|
||||
|
||||
r.Title = params.Title
|
||||
r.AlternativeTitles = params.AlternativeTitles
|
||||
r.Article = params.Article
|
||||
r.Links = params.Links
|
||||
r.ReleaseDate = date
|
||||
r.CoverID = coverID
|
||||
r.Gallery = gallery
|
||||
r.GalleryNsfw = nsfw
|
||||
r.Characters = characters
|
||||
@@ -615,7 +655,15 @@ func GetPinnedResources() ([]model.ResourceView, error) {
|
||||
return views, nil
|
||||
}
|
||||
|
||||
func GetCharactersFromVndb(vnID string) ([]CharacterParams, error) {
|
||||
func GetCharactersFromVndb(vnID string, uid uint) ([]CharacterParams, error) {
|
||||
canUpload, err := checkUserCanUpload(uid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !canUpload {
|
||||
return nil, model.NewUnAuthorizedError("You have not permission to fetch characters from VNDB")
|
||||
}
|
||||
|
||||
client := http.Client{}
|
||||
jsonStr := fmt.Sprintf(`
|
||||
{
|
||||
|
||||
@@ -390,3 +390,11 @@ func validateUsername(username string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ResetUserNotificationsCount(userID uint) error {
|
||||
return dao.ResetUserNotificationsCount(userID)
|
||||
}
|
||||
|
||||
func GetUserNotificationsCount(userID uint) (uint, error) {
|
||||
return dao.GetUserNotificationCount(userID)
|
||||
}
|
||||
|
||||
52
server/stat/stat.go
Normal file
52
server/stat/stat.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package stat
|
||||
|
||||
import (
|
||||
prom "github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
RequestCount = prom.NewCounterVec(
|
||||
prom.CounterOpts{
|
||||
Name: "http_requests_total",
|
||||
Help: "Total number of HTTP requests",
|
||||
},
|
||||
[]string{"path", "status"},
|
||||
)
|
||||
RegisterCount = prom.NewCounterVec(
|
||||
prom.CounterOpts{
|
||||
Name: "register_requests_total",
|
||||
Help: "Total number of registration requests",
|
||||
},
|
||||
[]string{},
|
||||
)
|
||||
DownloadCount = prom.NewCounterVec(
|
||||
prom.CounterOpts{
|
||||
Name: "download_requests_total",
|
||||
Help: "Total number of download requests",
|
||||
},
|
||||
[]string{},
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
prom.MustRegister(RequestCount)
|
||||
prom.MustRegister(RegisterCount)
|
||||
prom.MustRegister(DownloadCount)
|
||||
}
|
||||
|
||||
func RecordRequest(method, path string, status string) {
|
||||
if status == "404" {
|
||||
// Aggregate all 404s under a single label
|
||||
path = "NOT_FOUND"
|
||||
}
|
||||
path = method + " " + path
|
||||
RequestCount.WithLabelValues(path, status).Inc()
|
||||
}
|
||||
|
||||
func RecordRegister() {
|
||||
RegisterCount.WithLabelValues().Inc()
|
||||
}
|
||||
|
||||
func RecordDownload() {
|
||||
DownloadCount.WithLabelValues().Inc()
|
||||
}
|
||||
@@ -22,7 +22,7 @@ type FTPStorage struct {
|
||||
|
||||
func (f *FTPStorage) Upload(filePath string, fileName string) (string, error) {
|
||||
// 连接到FTP服务器
|
||||
conn, err := ftp.Dial(f.Host, ftp.DialWithTimeout(10*time.Second))
|
||||
conn, err := ftp.Dial(f.Host, ftp.DialWithTimeout(10*time.Second), ftp.DialWithExplicitTLS(nil))
|
||||
if err != nil {
|
||||
log.Error("Failed to connect to FTP server: ", err)
|
||||
return "", errors.New("failed to connect to FTP server")
|
||||
@@ -76,7 +76,7 @@ func (f *FTPStorage) Download(storageKey string, fileName string) (string, error
|
||||
|
||||
func (f *FTPStorage) Delete(storageKey string) error {
|
||||
// 连接到FTP服务器
|
||||
conn, err := ftp.Dial(f.Host, ftp.DialWithTimeout(10*time.Second))
|
||||
conn, err := ftp.Dial(f.Host, ftp.DialWithTimeout(10*time.Second), ftp.DialWithExplicitTLS(nil))
|
||||
if err != nil {
|
||||
log.Error("Failed to connect to FTP server: ", err)
|
||||
return errors.New("failed to connect to FTP server")
|
||||
|
||||
Reference in New Issue
Block a user