mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
Add more routes for media upload
This commit is contained in:
parent
4afd939b18
commit
930b84826b
|
|
@ -27,9 +27,28 @@ export const attachmentToAPI = (
|
|||
width: attachment.width || undefined,
|
||||
height: attachment.height || undefined,
|
||||
fps: attachment.fps || undefined,
|
||||
size: attachment.size?.toString() || undefined,
|
||||
size:
|
||||
attachment.width && attachment.height
|
||||
? `${attachment.width}x${attachment.height}`
|
||||
: undefined,
|
||||
duration: attachment.duration || undefined,
|
||||
length: attachment.size?.toString() || undefined,
|
||||
aspect:
|
||||
attachment.width && attachment.height
|
||||
? attachment.width / attachment.height
|
||||
: undefined,
|
||||
original: {
|
||||
width: attachment.width || undefined,
|
||||
height: attachment.height || undefined,
|
||||
size:
|
||||
attachment.width && attachment.height
|
||||
? `${attachment.width}x${attachment.height}`
|
||||
: undefined,
|
||||
aspect:
|
||||
attachment.width && attachment.height
|
||||
? attachment.width / attachment.height
|
||||
: undefined,
|
||||
},
|
||||
// Idk whether size or length is the right value
|
||||
},
|
||||
description: attachment.description,
|
||||
|
|
|
|||
104
server/api/api/v1/media/[id]/index.ts
Normal file
104
server/api/api/v1/media/[id]/index.ts
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
import { applyConfig } from "@api";
|
||||
import { errorResponse, jsonResponse } from "@response";
|
||||
import { client } from "~database/datasource";
|
||||
import { getFromRequest } from "~database/entities/User";
|
||||
import { APIRouteMeta } from "~types/api";
|
||||
import { uploadFile } from "~classes/media";
|
||||
import { getConfig } from "@config";
|
||||
import { attachmentToAPI, getUrl } from "~database/entities/Attachment";
|
||||
import { MatchedRoute } from "bun";
|
||||
import { parseRequest } from "@request";
|
||||
|
||||
export const meta: APIRouteMeta = applyConfig({
|
||||
allowedMethods: ["GET", "PUT"],
|
||||
ratelimits: {
|
||||
max: 10,
|
||||
duration: 60,
|
||||
},
|
||||
route: "/api/v1/media/:id",
|
||||
auth: {
|
||||
required: true,
|
||||
oauthPermissions: ["write:media"],
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Get media information
|
||||
*/
|
||||
export default async (
|
||||
req: Request,
|
||||
matchedRoute: MatchedRoute
|
||||
): Promise<Response> => {
|
||||
const { user } = await getFromRequest(req);
|
||||
|
||||
if (!user) {
|
||||
return errorResponse("Unauthorized", 401);
|
||||
}
|
||||
|
||||
const id = matchedRoute.params.id;
|
||||
|
||||
const attachment = await client.attachment.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!attachment) {
|
||||
return errorResponse("Media not found", 404);
|
||||
}
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
switch (req.method) {
|
||||
case "GET": {
|
||||
if (attachment.url) {
|
||||
return jsonResponse(attachmentToAPI(attachment));
|
||||
} else {
|
||||
return new Response(null, {
|
||||
status: 206,
|
||||
});
|
||||
}
|
||||
}
|
||||
case "PUT": {
|
||||
const { description, thumbnail } = await parseRequest<{
|
||||
thumbnail?: File;
|
||||
description?: string;
|
||||
focus?: string;
|
||||
}>(req);
|
||||
|
||||
let thumbnailUrl = attachment.thumbnail_url;
|
||||
|
||||
if (thumbnail) {
|
||||
const hash = await uploadFile(
|
||||
thumbnail as unknown as File,
|
||||
config
|
||||
);
|
||||
|
||||
thumbnailUrl = hash ? getUrl(hash, config) : "";
|
||||
}
|
||||
|
||||
const descriptionText = description || attachment.description;
|
||||
|
||||
if (
|
||||
descriptionText !== attachment.description ||
|
||||
thumbnailUrl !== attachment.thumbnail_url
|
||||
) {
|
||||
const newAttachment = await client.attachment.update({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
description: descriptionText,
|
||||
thumbnail_url: thumbnailUrl,
|
||||
},
|
||||
});
|
||||
|
||||
return jsonResponse(attachmentToAPI(newAttachment));
|
||||
}
|
||||
|
||||
return jsonResponse(attachmentToAPI(attachment));
|
||||
}
|
||||
}
|
||||
|
||||
return errorResponse("Method not allowed", 405);
|
||||
};
|
||||
|
|
@ -18,11 +18,12 @@ export const meta: APIRouteMeta = applyConfig({
|
|||
route: "/api/v2/media",
|
||||
auth: {
|
||||
required: true,
|
||||
oauthPermissions: ["write:media"],
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch a user
|
||||
* Upload new media
|
||||
*/
|
||||
export default async (req: Request): Promise<Response> => {
|
||||
const { user } = await getFromRequest(req);
|
||||
|
|
@ -96,8 +97,15 @@ export default async (req: Request): Promise<Response> => {
|
|||
|
||||
// TODO: Add job to process videos and other media
|
||||
|
||||
return jsonResponse({
|
||||
if (isImage) {
|
||||
return jsonResponse(attachmentToAPI(newAttachment));
|
||||
} else {
|
||||
return jsonResponse(
|
||||
{
|
||||
...attachmentToAPI(newAttachment),
|
||||
url: undefined,
|
||||
});
|
||||
url: null,
|
||||
},
|
||||
202
|
||||
);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
BIN
tests/test-image.webp
Executable file
BIN
tests/test-image.webp
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 75 KiB |
|
|
@ -8,5 +8,6 @@ export interface APIRouteMeta {
|
|||
auth: {
|
||||
required: boolean;
|
||||
requiredOnMethods?: ("GET" | "POST" | "PUT" | "DELETE" | "PATCH")[];
|
||||
oauthPermissions?: string[];
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue