mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-10 04:22:09 +10:00
Combined fixes (#116)
* fix: missing CheckIcon import in LanguageSelector * fix: #114 and error handling * fix: #97 * fix: lint * feat: #104 * fix: #72
This commit is contained in:
@ -1,8 +1,16 @@
|
||||
import { ArkErrors, type } from "arktype";
|
||||
import { defineEventHandler, createError } from "h3";
|
||||
import aclManager from "~/server/internal/acls";
|
||||
import newsManager from "~/server/internal/news";
|
||||
import { handleFileUpload } from "~/server/internal/utils/handlefileupload";
|
||||
|
||||
const CreateNews = type({
|
||||
title: "string",
|
||||
description: "string",
|
||||
content: "string",
|
||||
tags: "string = '[]'",
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (h3) => {
|
||||
const allowed = await aclManager.allowSystemACL(h3, ["news:create"]);
|
||||
if (!allowed) throw createError({ statusCode: 403 });
|
||||
@ -23,24 +31,25 @@ export default defineEventHandler(async (h3) => {
|
||||
|
||||
const [imageIds, options, pull, _dump] = uploadResult;
|
||||
|
||||
const title = options.title;
|
||||
const description = options.description;
|
||||
const content = options.content;
|
||||
const tags = options.tags ? (JSON.parse(options.tags) as string[]) : [];
|
||||
const imageId = imageIds.at(0);
|
||||
const body = await CreateNews(options);
|
||||
if (body instanceof ArkErrors)
|
||||
throw createError({ statusCode: 400, statusMessage: body.summary });
|
||||
|
||||
if (!title || !description || !content)
|
||||
const parsedTags = JSON.parse(body.tags);
|
||||
if (typeof parsedTags !== "object" || !Array.isArray(parsedTags))
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: "Missing or invalid title, description or content.",
|
||||
statusMessage: "Tags must be an array",
|
||||
});
|
||||
|
||||
const article = await newsManager.create({
|
||||
title: title,
|
||||
description: description,
|
||||
content: content,
|
||||
const imageId = imageIds.at(0);
|
||||
|
||||
tags: tags,
|
||||
const article = await newsManager.create({
|
||||
title: body.title,
|
||||
description: body.description,
|
||||
content: body.content,
|
||||
|
||||
tags: parsedTags,
|
||||
|
||||
...(imageId && { image: imageId }),
|
||||
authorId: "system",
|
||||
|
||||
@ -39,7 +39,7 @@ export class ObjectTransactionalHandler {
|
||||
if (!transaction) return;
|
||||
|
||||
let progress = 0;
|
||||
const increment = (1 / transaction.size) * 100;
|
||||
const increment = 100 / transaction.size;
|
||||
|
||||
for (const [id, data] of transaction) {
|
||||
if (typeof data === "string") {
|
||||
|
||||
@ -374,8 +374,15 @@ export function wrapTaskContext(
|
||||
): TaskRunContext {
|
||||
return {
|
||||
progress(progress) {
|
||||
const scalar = 100 / (options.max - options.min);
|
||||
const adjustedProgress = progress * scalar + options.min;
|
||||
if (progress > 100 || progress < 0) {
|
||||
console.warn("[wrapTaskContext] progress must be between 0 and 100");
|
||||
}
|
||||
|
||||
// I was too tired to figure this out
|
||||
// https://stackoverflow.com/a/929107
|
||||
const oldRange = 100;
|
||||
const newRange = options.max - options.min;
|
||||
const adjustedProgress = (progress * newRange) / oldRange + options.min;
|
||||
return context.progress(adjustedProgress);
|
||||
},
|
||||
log(message) {
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
import { H3Error } from "h3";
|
||||
import sessionHandler from "../internal/session";
|
||||
|
||||
export default defineNitroPlugin((nitro) => {
|
||||
nitro.hooks.hook("error", async (error, { event }) => {
|
||||
if (!event) return;
|
||||
|
||||
// Don't handle for API routes
|
||||
if (event.path.startsWith("/api")) return;
|
||||
if (event.path.startsWith("/auth")) return;
|
||||
|
||||
// Make sure it's a web error
|
||||
if (!(error instanceof H3Error)) return;
|
||||
|
||||
switch (error.statusCode) {
|
||||
case 401:
|
||||
case 403: {
|
||||
const user = await sessionHandler.getSession(event);
|
||||
if (user) break;
|
||||
return sendRedirect(
|
||||
event,
|
||||
`/auth/signin?redirect=${encodeURIComponent(event.path)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user