# 주제에 이미지를 연결하는 방법

**URL:** https://meta.discourse.org/t/how-to-link-images-with-topic/321246
**Category:** Development
**Created:** [8월 12, 2024, 12:38오후 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246 "2024-08-12T12:38:13Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Harshit\_Dave](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/harshit_dave/32/379059_2.png) [@Harshit\_Dave](https://meta.discourse.org/u/Harshit_Dave)
#### Post date: [8월 12, 2024, 12:38오후 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/1 "2024-08-12T12:38:13Z")

</div>

안녕하세요 @Discourse,

토픽에 이미지를 업로드하는 플러그인을 만들었습니다. 파일 경로를 custom\_fields에 저장하고 있습니다. 하지만 Discourse는 업로드된 이미지가 토픽과 연결되어 있지 않다고 판단하여 고아 이미지(orphaned images)로 간주하고 모든 이미지를 삭제합니다. 따라서 업로드된 이미지를 각 토픽에 연결하는 방법에 대해 조언을 구할 수 있을까요?

`image_upload_id를 업데이트해 보았지만, 어떤 이유로인지 topic 테이블의 컬럼 값이 업데이트되지 않습니다.`

어떤 조언이라도 큰 도움이 될 것입니다!

감사합니다!

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [8월 13, 2024, 9:00오전 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/2 "2024-08-13T09:00:46Z")

</div>

[플러그인 API 함수](https://github.com/discourse/discourse/blob/559c9dfe0afc4edc5414b9143e1e4248993a5d54/lib/plugin/instance.rb#L331-L333) `register_upload_in_use`가 있습니다.

사용 예시는 [채팅 플러그인의 코드](https://github.com/discourse/discourse/blob/559c9dfe0afc4edc5414b9143e1e4248993a5d54/plugins/chat/plugin.rb#L120-L133)를 참고하세요.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [8월 13, 2024, 9:05오전 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/3 "2024-08-13T09:05:47Z")

</div>

죄송합니다. 방금 답변은 일부만 포함된 것이었습니다. 이 방법은 이미지가 고아 데이터가 되는 것만 방지할 뿐입니다. 코드에서 topic.image\_upload\_id를 설정할 수는 있지만, 게시물의 HTML이 다시 생성(rebake)되면 이 값이 덮어쓰기됩니다. 따라서 rebake 코드에도 후킹을 적용해야 합니다.

---

<div class="post-metadata">

### Author: ![Harshit\_Dave](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/harshit_dave/32/379059_2.png) [@Harshit\_Dave](https://meta.discourse.org/u/Harshit_Dave)
#### Post date: [8월 13, 2024, 9:10오전 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/4 "2024-08-13T09:10:51Z")

</div>

혹시 방법을 알려주실 수 있을까요? 제가 Ruby를 잘 모르지만 JavaScript는 다룰 수 있고, Discourse도 처음이라 Discourse 커스터마이징에 대해 잘 모르거든요. 예시를 공유해 주실 수 있다면 정말 감사하겠습니다. 😅

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [8월 13, 2024, 10:58오전 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/5 "2024-08-13T10:58:56Z")

</div>

토픽의 `image_upload_id`를 업데이트하는 코드는 `CookedPostProcessor::update_post_image`에 있습니다.

> <https://github.com/discourse/discourse/blob/355dbb928a2b9e8319fa4c0bba38876559ae0a5d/lib/cooked_post_processor.rb#L322-L329>

따라서 [해당 함수를 몽키 패치(monkey patch)](https://medium.com/@sebastianroyer/prepend-and-super-for-elegant-monkey-patching-24a5b98f4468)한 후, 다음과 같은 작업을 수행할 수 있습니다.

```rb
def update_post_image
  super # 원본 호출
  if @post.custom_fields[:your_field_name_upload_id]
    upload_id = @post.custom_fields[:your_field_name_upload_id]
    @post.update_column(:image_upload_id, upload_id) # post
    if @post.is_first_post? # topic
        @post.topic.update_column(:image_upload_id, upload_id)
    end
  end
end

```

---

<div class="post-metadata">

### Author: ![Harshit\_Dave](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/harshit_dave/32/379059_2.png) [@Harshit\_Dave](https://meta.discourse.org/u/Harshit_Dave)
#### Post date: [8월 13, 2024, 2:47오후 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/6 "2024-08-13T14:47:45Z")

</div>

게시물과 토픽의 `image_upload_id`를 업데이트했지만 이미지는 여전히 삭제되고 있습니다. `register_upload_in_use` API 함수를 사용했습니다.

아래 코드를 추가하여 토픽의 custom\_fields에서 `topic_file_upload` 필드 이름을 확인하고 있습니다. 이것이 올바른 방법인가요?

```plaintext
  if respond_to?(:register_upload_in_use)
    register_upload_in_use do |upload|
        TopicCustomField.where(
            name: 'topic_file_upload',
            value: [upload.url, upload.sha1]
        ).exists?
    end
  end

```

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [8월 13, 2024, 3:08오후 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/7 "2024-08-13T15:08:09Z")

</div>

TopicCustomField에는 무엇이 포함되어 있나요? 업로드 ID가 포함된 것으로 추정됩니다.

그렇다면 다음과 같은 형태가 더 적절할 것 같습니다.

```rb
  if respond_to?(:register_upload_in_use)
    register_upload_in_use do |upload|
        TopicCustomField.where(
            name: 'topic_file_upload',
            value: upload.id
        ).exists?
    end
  end

```

---

<div class="post-metadata">

### Author: ![Harshit\_Dave](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/harshit_dave/32/379059_2.png) [@Harshit\_Dave](https://meta.discourse.org/u/Harshit_Dave)
#### Post date: [8월 13, 2024, 3:17오후 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/8 "2024-08-13T15:17:16Z")

</div>

이미지 URL은 있지만, 이미지 ID를 위해 커스텀 필드에 새 필드를 추가했고 이제 코드에서 언급하신 대로 ID를 확인하고 있습니다. 만약 아래 코드를 사용했다면 작동했을 수도 있지 않을까요?

```plaintext
TopicCustomField.where(
            name: 'topic_file_upload',
            value: upload.url
        ).exists?

```

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [8월 13, 2024, 3:20오후 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/9 "2024-08-13T15:20:21Z")

</div>

이상하네요 - URL이 있었다면 `value: [upload.url, upload.sha1]`가 URL과 SHA1 모두에 일치하므로 이전에도 작동했을 것 같은데 🤔

---

<div class="post-metadata">

### Author: ![Harshit\_Dave](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/harshit_dave/32/379059_2.png) [@Harshit\_Dave](https://meta.discourse.org/u/Harshit_Dave)
#### Post date: [8월 19, 2024, 7:49오전 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/10 "2024-08-19T07:49:01Z")

</div>

@RGJ님, 도움 주셔서 감사합니다! url 대신 Id가 제게는 잘 작동했습니다.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [9월 18, 2024, 7:49오전 UTC](https://meta.discourse.org/t/how-to-link-images-with-topic/321246/11 "2024-09-18T07:49:26Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
