Skip to content
Snippets Groups Projects
Commit bc80a95f authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

fix: images path in getById

parent 21f5955c
No related branches found
No related tags found
2 merge requests!96release V1.10.0,!48Dev
import { Controller, Get, HttpException, HttpService, HttpStatus, Logger, Param, Query } from '@nestjs/common';
import { Controller, Get, HttpException, HttpService, HttpStatus, Param, Query } from '@nestjs/common';
import { Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { ApiQuery } from '@nestjs/swagger';
......@@ -10,11 +10,7 @@ import { ConfigurationService } from '../configuration/configuration.service';
@Controller('posts')
export class PostsController {
constructor(
private readonly httpService: HttpService,
private readonly postsService: PostsService,
private readonly configService: ConfigurationService
) {}
constructor(private readonly httpService: HttpService, private readonly postsService: PostsService) {}
@Get()
@ApiQuery({ name: 'include', type: String, required: false })
......@@ -47,19 +43,7 @@ export class PostsController {
return new Promise((resolve) => {
result.subscribe((postData: PostWithMeta) => {
postData.posts.forEach((post: Post) => {
// Handle excerpt field in case of no author
if (!post.custom_excerpt) {
post.excerpt = 'Inconnu';
}
// Handle image display. Rewrite image URL to fit ghost infra issue.
if (post.feature_image && !this.configService.isLocalConf()) {
post.feature_image = `https://${this.configService.config.host}/blog/content${
post.feature_image.split('/content')[1]
}`;
const regex = /(https?:\/\/ghost):(\d*)?/g; // (https?://ghost):(\d*)?/
post.html = post.html.replace(regex, `https://${this.configService.config.host}`);
}
return post;
return this.postsService.formatPosts(post);
});
resolve(postData);
});
......@@ -78,7 +62,7 @@ export class PostsController {
}
@Get(':id')
public async getPostbyId(@Param('id') id: string): Promise<Observable<{ posts: Post }>> {
public async getPostbyId(@Param('id') id: string): Promise<Observable<{ posts: Post[] }>> {
return this.httpService
.get(`${process.env.GHOST_HOST_AND_PORT}/ghost/api/v3/content/posts/` + id, {
params: {
......@@ -86,6 +70,10 @@ export class PostsController {
include: 'tags,authors',
},
})
.pipe(map((response) => response.data));
.pipe(
map((response) => {
return { posts: [this.postsService.formatPosts(response.data.posts[0])] };
})
);
}
}
......@@ -3,7 +3,9 @@ import { Logger } from '@nestjs/common';
import * as GhostAdminAPI from '@tryghost/admin-api';
import * as _ from 'lodash';
import { map } from 'rxjs/operators';
import { ConfigurationService } from '../configuration/configuration.service';
import { TagEnum } from './enums/tag.enum';
import { Post } from './schemas/post.schema';
import { Tag } from './schemas/tag.schema';
@Injectable()
......@@ -13,7 +15,7 @@ export class PostsService {
public readonly publicCategory = 'public';
private api: any;
constructor() {
constructor(private readonly configService: ConfigurationService) {
// Configure Ghost client
this.api = new GhostAdminAPI({
url: process.env.GHOST_HOST_AND_PORT,
......@@ -66,4 +68,20 @@ export class PostsService {
arr.splice(toIndex, 0, element);
return arr;
}
public formatPosts(postData: Post): Post {
// Handle excerpt field in case of no author
if (!postData.custom_excerpt) {
postData.excerpt = 'Inconnu';
}
// Handle image display. Rewrite image URL to fit ghost infra issue.
if (postData.feature_image && !this.configService.isLocalConf()) {
postData.feature_image = `https://${this.configService.config.host}/blog/content${
postData.feature_image.split('/content')[1]
}`;
const regex = /(https?:\/\/ghost):(\d*)?/g;
postData.html = postData.html.replace(regex, `https://${this.configService.config.host}`);
}
return postData;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment