feat: use portainer hook instead of pushing image to gitea package repository
This commit is contained in:
24
server/api/files/[name]/index.delete.ts
Normal file
24
server/api/files/[name]/index.delete.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { unlink } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { z } from "zod";
|
||||
|
||||
const paramsSchema = z.object({
|
||||
name: z.string().min(1).regex(/^[^/\\]+$/, "Invalid filename"),
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const params = await getValidatedRouterParams(event, paramsSchema.parse);
|
||||
const dir = resolve(process.cwd(), "public/files");
|
||||
const filePath = resolve(dir, params.name);
|
||||
|
||||
if (!filePath.startsWith(dir + "/")) {
|
||||
throw createError({ statusCode: 400, message: "Invalid filename" });
|
||||
}
|
||||
|
||||
try {
|
||||
await unlink(filePath);
|
||||
return { success: true };
|
||||
} catch {
|
||||
throw createError({ statusCode: 404, message: "File not found" });
|
||||
}
|
||||
});
|
||||
31
server/api/files/[name]/index.patch.ts
Normal file
31
server/api/files/[name]/index.patch.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { rename } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { z } from "zod";
|
||||
|
||||
const paramsSchema = z.object({
|
||||
name: z.string().min(1).regex(/^[^/\\]+$/, "Invalid filename"),
|
||||
});
|
||||
|
||||
const bodySchema = z.object({
|
||||
name: z.string().min(1).regex(/^[^/\\]+$/, "Invalid filename"),
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const params = await getValidatedRouterParams(event, paramsSchema.parse);
|
||||
const body = await readValidatedBody(event, bodySchema.parse);
|
||||
|
||||
const dir = resolve(process.cwd(), "public/files");
|
||||
const oldPath = resolve(dir, params.name);
|
||||
const newPath = resolve(dir, body.name);
|
||||
|
||||
if (!oldPath.startsWith(dir + "/") || !newPath.startsWith(dir + "/")) {
|
||||
throw createError({ statusCode: 400, message: "Invalid filename" });
|
||||
}
|
||||
|
||||
try {
|
||||
await rename(oldPath, newPath);
|
||||
return { name: body.name };
|
||||
} catch {
|
||||
throw createError({ statusCode: 404, message: "File not found" });
|
||||
}
|
||||
});
|
||||
20
server/api/files/index.get.ts
Normal file
20
server/api/files/index.get.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { readdir, stat } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
const filesDir = () => resolve(process.cwd(), "public/files");
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
const dir = filesDir();
|
||||
try {
|
||||
const names = await readdir(dir);
|
||||
const files = await Promise.all(
|
||||
names.map(async (name) => {
|
||||
const s = await stat(resolve(dir, name));
|
||||
return { name, size: s.size, modifiedAt: s.mtime.toISOString() };
|
||||
}),
|
||||
);
|
||||
return files;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
22
server/api/files/index.post.ts
Normal file
22
server/api/files/index.post.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { writeFile, mkdir } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const parts = await readMultipartFormData(event);
|
||||
if (!parts || parts.length === 0) {
|
||||
throw createError({ statusCode: 400, message: "No file provided" });
|
||||
}
|
||||
|
||||
const filePart = parts.find((p) => p.name === "file");
|
||||
if (!filePart || !filePart.filename) {
|
||||
throw createError({ statusCode: 400, message: "No file provided" });
|
||||
}
|
||||
|
||||
const filename = filePart.filename.replace(/[^a-zA-Z0-9._-]/g, "_");
|
||||
const dir = resolve(process.cwd(), "public/files");
|
||||
|
||||
await mkdir(dir, { recursive: true });
|
||||
await writeFile(resolve(dir, filename), filePart.data);
|
||||
|
||||
return { name: filename, size: filePart.data.length };
|
||||
});
|
||||
Reference in New Issue
Block a user