From bc80a95ff772e4df638514544565061016ed5060 Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Tue, 23 Mar 2021 14:54:50 +0100 Subject: [PATCH] fix: images path in getById --- src/posts/posts.controller.ts | 30 +++++++++--------------------- src/posts/posts.service.ts | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/posts/posts.controller.ts b/src/posts/posts.controller.ts index 54a41f690..294107dfe 100644 --- a/src/posts/posts.controller.ts +++ b/src/posts/posts.controller.ts @@ -1,4 +1,4 @@ -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])] }; + }) + ); } } diff --git a/src/posts/posts.service.ts b/src/posts/posts.service.ts index 498c69fed..dd9e37835 100644 --- a/src/posts/posts.service.ts +++ b/src/posts/posts.service.ts @@ -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; + } } -- GitLab