If you mean in posts, then if you have download remote images to local set, Discourse will download them. There are sidekiq jobs that should run to do that.
You can look at other scripts for “Permalink”.
In short, the p=123 values should be stored in TopicCustomFields or PostCustomFields with name import_id and you can then run a script to create the permalinks.