feat: fix uploading cover picture to wordpress
This commit is contained in:
parent
f427991d13
commit
689612d0fd
|
|
@ -10,7 +10,7 @@ import { Integration } from '@prisma/client';
|
|||
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
|
||||
import { WordpressDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/wordpress.dto';
|
||||
import slugify from 'slugify';
|
||||
import FormData from 'form-data';
|
||||
// import FormData from 'form-data';
|
||||
import axios from 'axios';
|
||||
|
||||
export class WordpressProvider
|
||||
|
|
@ -169,35 +169,30 @@ export class WordpressProvider
|
|||
|
||||
let mediaId = '';
|
||||
if (postDetails?.[0]?.settings?.main_image?.path) {
|
||||
console.log('Uploading image to WordPress', postDetails[0].settings.main_image.path);
|
||||
const imageData = await axios.get(postDetails[0].settings.main_image.path, {
|
||||
responseType: 'stream',
|
||||
});
|
||||
|
||||
const form = new FormData();
|
||||
form.append('file', imageData.data, {
|
||||
filename: postDetails[0].settings.main_image.path.split('/').pop(), // You can customize the filename
|
||||
contentType: imageData.headers['content-type'],
|
||||
});
|
||||
if (postDetails[0].settings.main_image?.alt) {
|
||||
form.append('alt_text', postDetails[0].settings.main_image.alt);
|
||||
}
|
||||
|
||||
const mediaResponse = await axios.post(
|
||||
`${body.domain}/wp-json/wp/v2/media`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: form,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Basic ${auth}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
console.log(
|
||||
'Uploading image to WordPress',
|
||||
postDetails[0].settings.main_image.path
|
||||
);
|
||||
|
||||
mediaId = mediaResponse.data.id;
|
||||
const blob = await this.fetch(
|
||||
postDetails[0].settings.main_image.path
|
||||
).then((r) => r.blob());
|
||||
|
||||
const mediaResponse = await (
|
||||
await this.fetch(`${body.domain}/wp-json/wp/v2/media`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Basic ${auth}`,
|
||||
'Content-Disposition': `attachment; filename="${postDetails[0].settings.main_image.path
|
||||
.split('/')
|
||||
.pop()}"`,
|
||||
'Content-Type': blob.type,
|
||||
},
|
||||
body: blob,
|
||||
})
|
||||
).json();
|
||||
|
||||
mediaId = mediaResponse.id;
|
||||
}
|
||||
|
||||
const submit = await (
|
||||
|
|
|
|||
Loading…
Reference in New Issue