improve compatibility with multipart/form-data mastodon API requests
This commit is contained in:
@@ -3,12 +3,8 @@
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import querystring from 'querystring';
|
||||
import multer from 'fastify-multer';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import type { Config } from '@/config.js';
|
||||
import { getErrorData, getErrorStatus, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
|
||||
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
import { ApiAccountMastodon } from '@/server/api/mastodon/endpoints/account.js';
|
||||
@@ -20,6 +16,7 @@ import { ApiNotificationsMastodon } from '@/server/api/mastodon/endpoints/notifi
|
||||
import { ApiTimelineMastodon } from '@/server/api/mastodon/endpoints/timeline.js';
|
||||
import { ApiSearchMastodon } from '@/server/api/mastodon/endpoints/search.js';
|
||||
import { ApiError } from '@/server/api/error.js';
|
||||
import { ServerUtilityService } from '@/server/ServerUtilityService.js';
|
||||
import { parseTimelineArgs, TimelineArgs, toBoolean } from './argsUtils.js';
|
||||
import { convertAnnouncement, convertAttachment, MastodonConverters, convertRelationship } from './MastodonConverters.js';
|
||||
import type { Entity } from 'megalodon';
|
||||
@@ -28,9 +25,6 @@ import type { FastifyInstance, FastifyPluginOptions } from 'fastify';
|
||||
@Injectable()
|
||||
export class MastodonApiServerService {
|
||||
constructor(
|
||||
@Inject(DI.config)
|
||||
private readonly config: Config,
|
||||
|
||||
private readonly mastoConverters: MastodonConverters,
|
||||
private readonly logger: MastodonLogger,
|
||||
private readonly clientService: MastodonClientService,
|
||||
@@ -42,97 +36,15 @@ export class MastodonApiServerService {
|
||||
private readonly apiSearchMastodon: ApiSearchMastodon,
|
||||
private readonly apiStatusMastodon: ApiStatusMastodon,
|
||||
private readonly apiTimelineMastodon: ApiTimelineMastodon,
|
||||
private readonly serverUtilityService: ServerUtilityService,
|
||||
) {}
|
||||
|
||||
@bindThis
|
||||
public createServer(fastify: FastifyInstance, _options: FastifyPluginOptions, done: (err?: Error) => void) {
|
||||
const upload = multer({
|
||||
storage: multer.diskStorage({}),
|
||||
limits: {
|
||||
fileSize: this.config.maxFileSize || 262144000,
|
||||
files: 1,
|
||||
},
|
||||
});
|
||||
|
||||
fastify.addHook('onRequest', (_, reply, done) => {
|
||||
// Allow web-based clients to connect from other origins.
|
||||
reply.header('Access-Control-Allow-Origin', '*');
|
||||
|
||||
// Mastodon uses all types of request methods.
|
||||
reply.header('Access-Control-Allow-Methods', '*');
|
||||
|
||||
// Allow web-based clients to access Link header - required for mastodon pagination.
|
||||
// https://stackoverflow.com/a/54928828
|
||||
// https://docs.joinmastodon.org/api/guidelines/#pagination
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Expose-Headers
|
||||
reply.header('Access-Control-Expose-Headers', 'Link');
|
||||
|
||||
// Cache to avoid extra pre-flight requests
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Max-Age
|
||||
reply.header('Access-Control-Max-Age', 60 * 60 * 24); // 1 day in seconds
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
fastify.addContentTypeParser('application/x-www-form-urlencoded', (_, payload, done) => {
|
||||
let body = '';
|
||||
payload.on('data', (data) => {
|
||||
body += data;
|
||||
});
|
||||
payload.on('end', () => {
|
||||
try {
|
||||
const parsed = querystring.parse(body);
|
||||
done(null, parsed);
|
||||
} catch (e) {
|
||||
done(e as Error);
|
||||
}
|
||||
});
|
||||
payload.on('error', done);
|
||||
});
|
||||
|
||||
// Remove trailing "[]" from query params
|
||||
fastify.addHook('preValidation', (request, _reply, done) => {
|
||||
if (!request.query || typeof(request.query) !== 'object') {
|
||||
return done();
|
||||
}
|
||||
|
||||
// Same object aliased with a different type
|
||||
const query = request.query as Record<string, string | string[] | undefined>;
|
||||
|
||||
for (const key of Object.keys(query)) {
|
||||
if (!key.endsWith('[]')) {
|
||||
continue;
|
||||
}
|
||||
if (query[key] == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const newKey = key.substring(0, key.length - 2);
|
||||
const newValue = query[key];
|
||||
const oldValue = query[newKey];
|
||||
|
||||
// Move the value to the correct key
|
||||
if (oldValue != null) {
|
||||
if (Array.isArray(oldValue)) {
|
||||
// Works for both array and single values
|
||||
query[newKey] = oldValue.concat(newValue);
|
||||
} else if (Array.isArray(newValue)) {
|
||||
// Preserve order
|
||||
query[newKey] = [oldValue, ...newValue];
|
||||
} else {
|
||||
// Preserve order
|
||||
query[newKey] = [oldValue, newValue];
|
||||
}
|
||||
} else {
|
||||
query[newKey] = newValue;
|
||||
}
|
||||
|
||||
// Remove the invalid key
|
||||
delete query[key];
|
||||
}
|
||||
|
||||
return done();
|
||||
});
|
||||
this.serverUtilityService.addMultipartFormDataContentType(fastify);
|
||||
this.serverUtilityService.addFormUrlEncodedContentType(fastify);
|
||||
this.serverUtilityService.addCORS(fastify);
|
||||
this.serverUtilityService.addFlattenedQueryType(fastify);
|
||||
|
||||
fastify.setErrorHandler((error, request, reply) => {
|
||||
const data = getErrorData(error);
|
||||
@@ -143,14 +55,12 @@ export class MastodonApiServerService {
|
||||
reply.code(status).send(data);
|
||||
});
|
||||
|
||||
fastify.register(multer.contentParser);
|
||||
|
||||
// External endpoints
|
||||
this.apiAccountMastodon.register(fastify, upload);
|
||||
this.apiAppsMastodon.register(fastify, upload);
|
||||
this.apiFilterMastodon.register(fastify, upload);
|
||||
this.apiAccountMastodon.register(fastify);
|
||||
this.apiAppsMastodon.register(fastify);
|
||||
this.apiFilterMastodon.register(fastify);
|
||||
this.apiInstanceMastodon.register(fastify);
|
||||
this.apiNotificationsMastodon.register(fastify, upload);
|
||||
this.apiNotificationsMastodon.register(fastify);
|
||||
this.apiSearchMastodon.register(fastify);
|
||||
this.apiStatusMastodon.register(fastify);
|
||||
this.apiTimelineMastodon.register(fastify);
|
||||
@@ -178,11 +88,10 @@ export class MastodonApiServerService {
|
||||
reply.send(data.data);
|
||||
});
|
||||
|
||||
fastify.post('/v1/media', { preHandler: upload.single('file') }, async (_request, reply) => {
|
||||
const multipartData = await _request.file();
|
||||
fastify.post('/v1/media', async (_request, reply) => {
|
||||
const multipartData = _request.savedRequestFiles?.[0];
|
||||
if (!multipartData) {
|
||||
reply.code(401).send({ error: 'No image' });
|
||||
return;
|
||||
return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'No image' });
|
||||
}
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
@@ -192,11 +101,10 @@ export class MastodonApiServerService {
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<{ Body: { description?: string; focus?: string } }>('/v2/media', { preHandler: upload.single('file') }, async (_request, reply) => {
|
||||
const multipartData = await _request.file();
|
||||
fastify.post<{ Body: { description?: string; focus?: string } }>('/v2/media', async (_request, reply) => {
|
||||
const multipartData = _request.savedRequestFiles?.[0];
|
||||
if (!multipartData) {
|
||||
reply.code(401).send({ error: 'No image' });
|
||||
return;
|
||||
return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'No image' });
|
||||
}
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
@@ -294,7 +202,7 @@ export class MastodonApiServerService {
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<{ Querystring: TimelineArgs, Params: { id?: string } }>('/v1/follow_requests/:id/authorize', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/follow_requests/:id/authorize', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
@@ -304,7 +212,7 @@ export class MastodonApiServerService {
|
||||
reply.send(response);
|
||||
});
|
||||
|
||||
fastify.post<{ Querystring: TimelineArgs, Params: { id?: string } }>('/v1/follow_requests/:id/reject', { preHandler: upload.single('none') }, async (_request, reply) => {
|
||||
fastify.post<{ Params: { id?: string } }>('/v1/follow_requests/:id/reject', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const client = this.clientService.getClient(_request);
|
||||
@@ -325,7 +233,7 @@ export class MastodonApiServerService {
|
||||
focus?: string,
|
||||
is_sensitive?: string,
|
||||
},
|
||||
}>('/v1/media/:id', { preHandler: upload.none() }, async (_request, reply) => {
|
||||
}>('/v1/media/:id', async (_request, reply) => {
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
|
||||
|
||||
const options = {
|
||||
|
||||
Reference in New Issue
Block a user