2025-02-14 16:44:32 +01:00
import { apiRoute , auth , reusedResponses } from "@/api" ;
2025-02-05 21:49:39 +01:00
import { createRoute , z } from "@hono/zod-openapi" ;
2025-03-22 18:04:47 +01:00
import { Attachment as AttachmentSchema } from "@versia/client/schemas" ;
import { RolePermission } from "@versia/client/schemas" ;
2025-01-23 16:08:42 +01:00
import { Media } from "@versia/kit/db" ;
2024-09-15 14:28:47 +02:00
import { ErrorSchema } from "~/types/api" ;
2023-11-29 00:54:39 +01:00
2024-09-15 14:28:47 +02:00
const route = createRoute ( {
method : "post" ,
path : "/api/v1/media" ,
2025-02-14 16:44:32 +01:00
summary : "Upload media as an attachment (v1)" ,
description :
"Creates an attachment to be used with a new status. This method will return after the full sized media is done processing." ,
deprecated : true ,
externalDocs : {
url : "https://docs.joinmastodon.org/methods/media/#v1" ,
} ,
tags : [ "Media" ] ,
2024-12-30 19:18:31 +01:00
middleware : [
auth ( {
auth : true ,
scopes : [ "write:media" ] ,
2025-03-22 18:04:47 +01:00
permissions : [ RolePermission . ManageOwnMedia ] ,
2024-12-30 19:18:31 +01:00
} ) ,
] as const ,
2024-09-15 14:28:47 +02:00
request : {
body : {
content : {
"multipart/form-data" : {
2025-02-14 16:44:32 +01:00
schema : z.object ( {
file : z.instanceof ( File ) . openapi ( {
description :
"The file to be attached, encoded using multipart form data. The file must have a MIME type." ,
} ) ,
thumbnail : z.instanceof ( File ) . optional ( ) . openapi ( {
description :
"The custom thumbnail of the media to be attached, encoded using multipart form data." ,
} ) ,
description :
AttachmentSchema . shape . description . optional ( ) ,
focus : z
. string ( )
. optional ( )
. openapi ( {
description :
"Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. Used for media cropping on clients." ,
externalDocs : {
url : "https://docs.joinmastodon.org/api/guidelines/#focal-points" ,
} ,
} ) ,
} ) ,
2024-09-15 14:28:47 +02:00
} ,
} ,
} ,
} ,
responses : {
200 : {
2025-02-14 16:44:32 +01:00
description :
"Attachment created successfully. Note that the MediaAttachment will be created even if the file is not understood correctly due to failed processing." ,
2024-09-15 14:28:47 +02:00
content : {
"application/json" : {
2025-02-12 23:25:22 +01:00
schema : AttachmentSchema ,
2024-09-15 14:28:47 +02:00
} ,
} ,
} ,
413 : {
description : "File too large" ,
content : {
"application/json" : {
schema : ErrorSchema ,
} ,
} ,
} ,
415 : {
description : "Disallowed file type" ,
content : {
"application/json" : {
schema : ErrorSchema ,
} ,
} ,
} ,
2025-02-14 16:44:32 +01:00
. . . reusedResponses ,
2024-09-15 14:28:47 +02:00
} ,
} ) ;
export default apiRoute ( ( app ) = >
app . openapi ( route , async ( context ) = > {
const { file , thumbnail , description } = context . req . valid ( "form" ) ;
2025-01-23 16:08:42 +01:00
const attachment = await Media . fromFile ( file , {
2025-01-06 19:21:57 +01:00
thumbnail ,
2025-02-14 16:44:32 +01:00
description : description ? ? undefined ,
2024-09-15 14:28:47 +02:00
} ) ;
2025-01-06 19:21:57 +01:00
return context . json ( attachment . toApi ( ) , 200 ) ;
2024-09-15 14:28:47 +02:00
} ) ,
2024-08-19 20:06:38 +02:00
) ;