From ead0667320a5b15bcd99c7ae7ba2a3d648d8b3ea Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Mon, 25 Sep 2023 01:02:16 +0200 Subject: [PATCH] fix: upload media on masto api --- .../server/api/mastodon/MastodonApiServerService.ts | 4 ++-- packages/megalodon/src/misskey.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts index 0e35974847..128cb3491a 100644 --- a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts +++ b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts @@ -143,7 +143,7 @@ export class MastodonApiServerService { const data = await client.uploadMedia(multipartData); reply.send(convertAttachment(data.data as Entity.Attachment)); } catch (e: any) { - console.error(e); + /* console.error(e); */ reply.code(401).send(e.response.data); } }); @@ -161,7 +161,7 @@ export class MastodonApiServerService { const data = await client.uploadMedia(multipartData, _request.body!); reply.send(convertAttachment(data.data as Entity.Attachment)); } catch (e: any) { - console.error(e); + /* console.error(e); */ reply.code(401).send(e.response.data); } }); diff --git a/packages/megalodon/src/misskey.ts b/packages/megalodon/src/misskey.ts index 3a154b68ea..39c7685915 100644 --- a/packages/megalodon/src/misskey.ts +++ b/packages/megalodon/src/misskey.ts @@ -1,5 +1,5 @@ import FormData from 'form-data' - +import fs from 'fs'; import MisskeyAPI from './misskey/api_client' import { DEFAULT_UA } from './default' import { ProxyConfig } from './proxy_config' @@ -1311,7 +1311,13 @@ export default class Misskey implements MegalodonInterface { */ public async uploadMedia(file: any, _options?: { description?: string; focus?: string }): Promise> { const formData = new FormData() - formData.append('file', file) + formData.append('file', fs.createReadStream(file.path), { + contentType: file.mimetype, + }); + + if (file.originalname != null && file.originalname !== "file") formData.append("name", file.originalname); + + if (_options?.description != null) formData.append("comment", _options.description); let headers: { [key: string]: string } = {} if (typeof formData.getHeaders === 'function') { headers = formData.getHeaders()