토픽에 이미지를 업로드하는 플러그인을 만들었습니다. 파일 경로를 custom_fields에 저장하고 있습니다. 하지만 Discourse는 업로드된 이미지가 토픽과 연결되어 있지 않다고 판단하여 고아 이미지(orphaned images)로 간주하고 모든 이미지를 삭제합니다. 따라서 업로드된 이미지를 각 토픽에 연결하는 방법에 대해 조언을 구할 수 있을까요?
image_upload_id를 업데이트해 보았지만, 어떤 이유로인지 topic 테이블의 컬럼 값이 업데이트되지 않습니다.
죄송합니다. 방금 답변은 일부만 포함된 것이었습니다. 이 방법은 이미지가 고아 데이터가 되는 것만 방지할 뿐입니다. 코드에서 topic.image_upload_id를 설정할 수는 있지만, 게시물의 HTML이 다시 생성(rebake)되면 이 값이 덮어쓰기됩니다. 따라서 rebake 코드에도 후킹을 적용해야 합니다.
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
게시물과 토픽의 image_upload_id를 업데이트했지만 이미지는 여전히 삭제되고 있습니다. register_upload_in_use API 함수를 사용했습니다.
아래 코드를 추가하여 토픽의 custom_fields에서 topic_file_upload 필드 이름을 확인하고 있습니다. 이것이 올바른 방법인가요?
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
TopicCustomField에는 무엇이 포함되어 있나요? 업로드 ID가 포함된 것으로 추정됩니다.
그렇다면 다음과 같은 형태가 더 적절할 것 같습니다.
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