diff --git a/src/configuration/configuration.service.ts b/src/configuration/configuration.service.ts index ca6f9e6c44a78e2259c3b3e70c8a5168d3d30d95..9af45d3b25b2e31d7f2664c87819589fa377f8f6 100644 --- a/src/configuration/configuration.service.ts +++ b/src/configuration/configuration.service.ts @@ -23,6 +23,10 @@ export class ConfigurationService { dotenv.config(); } + public isLocalConf(): boolean { + return process.env.NODE_ENV === 'local'; + } + get config() { return this._config; } diff --git a/src/posts/posts.controller.ts b/src/posts/posts.controller.ts index 2f919b97474661f31fd42052ef998e3fe58fde46..a5241f25b29e21687b77163dba8fb48623cd479a 100644 --- a/src/posts/posts.controller.ts +++ b/src/posts/posts.controller.ts @@ -6,10 +6,15 @@ import { Post } from './schemas/post.schema'; import { PostsService } from './posts.service'; import { Tag } from './schemas/tag.schema'; import { PostWithMeta } from './schemas/postWithMeta.schema'; +import { ConfigurationService } from '../configuration/configuration.service'; @Controller('posts') export class PostsController { - constructor(private readonly httpService: HttpService, private readonly postsService: PostsService) {} + constructor( + private readonly httpService: HttpService, + private readonly postsService: PostsService, + private readonly configService: ConfigurationService + ) {} @Get() @ApiQuery({ name: 'include', type: String, required: false }) @@ -20,7 +25,7 @@ export class PostsController { @ApiQuery({ name: 'page', type: String, required: false }) @ApiQuery({ name: 'order', type: String, required: false }) public async findAll(@Query() query): Promise<PostWithMeta> { - const result = await this.httpService + const result = this.httpService .get(`${process.env.GHOST_HOST_AND_PORT}/ghost/api/v3/content/posts`, { params: { key: process.env.GHOST_CONTENT_API_KEY, @@ -40,9 +45,21 @@ export class PostsController { }) ); return new Promise((resolve) => { - result.subscribe((result: PostWithMeta) => { - result.posts.map((post: Post) => (!post.custom_excerpt ? (post.excerpt = 'Inconnu') : '')); - resolve(result); + 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] + }`; + } + return post; + }); + resolve(postData); }); }); } diff --git a/src/posts/schemas/post.schema.ts b/src/posts/schemas/post.schema.ts index 6d5b65ab2df871c3335a296796c8476c71fe3838..3e2ac2ca992fc0be11e97e73b2e13f6730a6aa75 100644 --- a/src/posts/schemas/post.schema.ts +++ b/src/posts/schemas/post.schema.ts @@ -5,7 +5,7 @@ export class Post { slug: string; html: string; comment_id: string; - feature_image: null; + feature_image: string; featured: false; visibility: string; email_recipient_filter: string;