From 2698302c6b558f434039c0ace1806a70b98f9721 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Mon, 22 Mar 2021 15:11:33 +0100
Subject: [PATCH] fix: host url for news image

---
 src/configuration/configuration.service.ts |  4 ++++
 src/posts/posts.controller.ts              | 27 ++++++++++++++++++----
 src/posts/schemas/post.schema.ts           |  2 +-
 3 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/configuration/configuration.service.ts b/src/configuration/configuration.service.ts
index ca6f9e6c4..9af45d3b2 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 2f919b974..a5241f25b 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 6d5b65ab2..3e2ac2ca9 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;
-- 
GitLab