How to link images with topic

Hello @Discourse,

I have created a plugin which uploads an image to topics and I am saving file path in custom_fields. but discourse does deletes all the images that are uploaded for the topics by considering orphaned images as images are not linked with the topics. Hence, Can anyone guide me how can I link the uploaded images to the respective topics?

I tried updating the image_upload_id but somehow it is not upading the columns value in topic table.

Any advice would be a great help!

Thanks!

There is a plugin API function register_upload_in_use

For an example on how to use it, see the code in the Chat plugin.

1 Like

Sorry, that was only a partial reply, it will only prevent the images from being orphaned. You should be able to set topic.image_upload_id from your code, but it will be overwritten when the HTML for a post is rebaked. So you should hook into the rebake code as well.

can you please guide me how can I do that If you can share some example because I don’t know much ruby but I can work with javascript and new to discourse as well so don’t know much about discourse customization :sweat_smile:

Here is the code that updates the topic image_upload_id, it’s in CookedPostProcessor::update_post_image

So what you could do is monkey patch that function and then do something like

def update_post_image
  super # call original
  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

It has updated the image_upload_id for post and topic but the image are still being deleted. I did have used register_upload_in_use API function

I have added below code to check in topic’s custom_fields where topic_file_upload is field name. Is this right way to do it?

  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

What does the TopicCustomField contain? I assume it contains the upload ID ?

In that case it should be more like

  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
1 Like

It has image url but I have added new field in custom fields for the image id and now checking the id as you mentioned in code. I belive If I would have used below code that should work may be?

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

Strange - if it had the URL it should have worked before, since value: [upload.url, upload.sha1] would match the URL as well as the SHA1 :thinking:

Thanks @RGJ for the help! Id worked for me instead of url

1 Like

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