fix: upload media on masto api

This commit is contained in:
Mar0xy 2023-09-25 01:02:16 +02:00
parent 3e91abe3b3
commit ead0667320
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
2 changed files with 10 additions and 4 deletions

View file

@ -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);
}
});

View file

@ -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<Response<Entity.Attachment>> {
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()