# How to avoid uploading attachments multiple times in a migration

**URL:** https://meta.discourse.org/t/how-to-avoid-uploading-attachments-multiple-times-in-a-migration/70613
**Category:** Development
**Created:** [Septembre 22, 2017, 10:35 UTC](https://meta.discourse.org/t/how-to-avoid-uploading-attachments-multiple-times-in-a-migration/70613 "2017-09-22T22:35:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [Septembre 22, 2017, 10:35 UTC](https://meta.discourse.org/t/how-to-avoid-uploading-attachments-multiple-times-in-a-migration/70613/1 "2017-09-22T22:35:48Z")

</div>

The vbulletin importer currently includes only attachments that are included in `[attach]` tags in the text of the message. But sometimes there are attachments not included in the text, and they don’t get imported.

I’ve fixed this with a function that after posts are processed, reads all the attachments, uploads them, replaces the `[attach]` codes, and if the attachment was not mentioned in the text adds a link to it at the end of the post.

The problem is how to avoid re-uploading and including them with subsequent runs of the importer. My current solution is to put `<div class="vbulletin-attachments-imported"></div>` at the end of the post and to check for that string before handling attachments.

It works, but is there a more elegant way? (And, I suppose if I were more clever, I’d have a solution that did this on a per-attachment rather than per-post level, but it’s hard to imagine that such a feature would ever be used.)

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [Septembre 23, 2017, 7:25 UTC](https://meta.discourse.org/t/how-to-avoid-uploading-attachments-multiple-times-in-a-migration/70613/2 "2017-09-23T07:25:20Z")

</div>

Why don’t you just re-upload the files and let Discourse figure this out? Since storage is based on an SHA checksum, this won’t duplicate the files 🙂  
Is this about making subsequent runs faster?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [Septembre 25, 2017, 4:20 UTC](https://meta.discourse.org/t/how-to-avoid-uploading-attachments-multiple-times-in-a-migration/70613/3 "2017-09-25T16:20:59Z")

</div>

> [@fefrei](#):
>
> Since storage is based on an SHA checksum, this won’t duplicate the files

DOH! So **that’s** why those checksums are in the model?!?!?! 😊

In that case, my problem is only making sure that I don’t add links to the un-linked attachments multiple times.

Thanks very much.
