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.
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
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
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?