# 迁移 vBulletin 5 数据库——导入附件时关于附件扩展名的错误

**URL:** https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235
**Category:** Migration
**Tags:** vbulletin5
**Created:** [2024年七月6日 13:53 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235 "2024-07-06T13:53:34Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Mubasher112](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mubasher112](https://meta.discourse.org/u/Mubasher112)
#### Post date: [2024年七月6日 13:53 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/1 "2024-07-06T13:53:34Z")

</div>

我已成功将 vBulletin 5 论坛导入 Discourse。虽然总体导入脚本运行正常，但在导入附件时遇到了错误。附件存储在我的数据库中，包含以下扩展名：`java`、`html`、`jpg`、`png`、`txt`、`rtf`、`zip`、`js` 和 `xml`。

在调试 `import_attachment` 操作时，我发现只有扩展名为 `java` 的附件被正确导入。脚本在处理其他扩展名的附件时失败。

社区中是否有人遇到过导入这些文件扩展名的附件时出现问题？有人能提供关于脚本为何可能在处理这些特定文件类型时失败的见解吗？

以下是问题的简要概述：

- 我数据库中的前三个文件扩展名为 `java`，并且已成功导入。
- 当脚本遇到扩展名为 `jpg` 的文件时会失败。

 ![image](https://global.discourse-cdn.com/meta/original/4X/e/4/2/e4266cbc1447d024bd3fa79e2acbb41982a34dab.png)

@pfaffman 任何建议或解决方案都将不胜感激！

---

<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: [2024年七月6日 18:34 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/2 "2024-07-06T18:34:06Z")

</div>

您需要添加一些调试输出语句来查看发生了什么。

---

<div class="post-metadata">

### Author: ![Mubasher112](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mubasher112](https://meta.discourse.org/u/Mubasher112)
#### Post date: [2024年七月6日 19:54 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/3 "2024-07-06T19:54:25Z")

</div>

我在 `upl_obj` 上添加了调试语句

```plaintext
     begin
        upl_obj = create_upload(post.user.id, filename, real_filename)

        if upl_obj&.persisted?
          html = html_for_upload(upl_obj, real_filename)
          if !post.raw[html]
            post.raw += "\n\n#{html}\n\n"
            post.save!
            UploadReference.ensure_exist!(upload_ids: [upl_obj.id], target: post)
          end
        else
          puts "Failed to create upload for #{filename}: #{upl_obj.errors.full_messages.join(", ")}"
          next
        end
      rescue => e
        puts "Error processing file #{filename}: #{e.message}"
        next
      end

```

然后看到了这些错误

 ![image](https://global.discourse-cdn.com/meta/original/4X/e/d/2/ed2415de0ac35f9be0215855f05fa7426e775ed1.png)

这些错误出现在 jpg、jpeg、png、PNG、gif 扩展名上

有什么关于这些错误的建议吗？@pfaffman

---

<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: [2024年七月6日 22:29 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/4 "2024-07-06T22:29:12Z")

</div>

唯一有效的只有 ascii 文件。我猜是编码错误。

---

<div class="post-metadata">

### Author: ![Mubasher112](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mubasher112](https://meta.discourse.org/u/Mubasher112)
#### Post date: [2024年七月7日 19:04 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/5 "2024-07-07T19:04:16Z")

</div>

数据库文件有问题还是导入脚本编码有问题？@pfaffman

---

<div class="post-metadata">

### Author: ![Mubasher112](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mubasher112](https://meta.discourse.org/u/Mubasher112)
#### Post date: [2024年七月7日 19:11 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/6 "2024-07-07T19:11:02Z")

</div>

![image](https://global.discourse-cdn.com/meta/original/4X/e/8/a/e8a129ab582ece4c3f6155277f0af7b55de63530.png)  
如果其他文件都经过编码，为什么像 jpg、png 等扩展名没有呢？

---

<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: [2024年七月7日 21:06 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/7 "2024-07-07T21:06:28Z")

</div>

我的猜测（可能不正确）是，换行符编码问题导致二进制文件中的数据不正确，因为换行符被编码为数据。如果只有 ASCII 文件有效，那很可能就是这个原因。

所以这不是 Discourse 的问题，而是 MySQL 的问题。

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2024年七月7日 22:29 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/8 "2024-07-07T22:29:43Z")

</div>

> [@pfaffman](#):
>
> that a problem with newline encoding makes the data in the binary files wrong

差不多——不是换行符编码问题，而是被当作文本处理，因此损坏了。

`EF BF BD` 是 UTF-8 字节序列，代表“替换字符”（U+FFFD）。这表明文件被当作文本处理而不是二进制文件。

JPEG 图像以 `ff d8 ff e0 xx xx 4a 46 49 46 00` 开头

您可以看到前四个字节都已被替换为 `EF BF BD`。

所以您的图像确实已损坏。这不是导入器的问题，而是数据库的问题，正如 @pfaffman 已经说过的。如果您是从另一个服务器复制的此数据库，您可能需要检查原始数据库中是否已存在此问题。这也可能只发生在最旧的图像上（如果这发生在很久以前）。只需删除 `exit` [行](https://github.com/discourse/discourse/blob/main/script/import_scripts/vbulletin5.rb#L446) 看看会发生什么。

---

<div class="post-metadata">

### Author: ![Mubasher112](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mubasher112](https://meta.discourse.org/u/Mubasher112)
#### Post date: [2024年七月11日 07:10 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/9 "2024-07-11T07:10:59Z")

</div>

@RGJ 感谢您的帮助。我尝试导入一个包含正确图像的新数据库，虽然导入成功了，但并非所有附件都已完全导入。我遇到了类似这样的错误：

 ![image](https://global.discourse-cdn.com/meta/original/4X/6/f/d/6fdc8f8b20004a8d87dae24c9489a7c9f2760f4d.png)

您知道这是为什么吗？@RGJ @pfaffman

---

<div class="post-metadata">

### Author: ![Mubasher112](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mubasher112](https://meta.discourse.org/u/Mubasher112)
#### Post date: [2024年七月11日 07:13 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/10 "2024-07-11T07:13:00Z")

</div>

帖子显示为这样，并且有很多实例：

 ![image](https://global.discourse-cdn.com/meta/original/4X/7/a/f/7af1e8a524162b96b8cfb6af626c6e46c2e13436.png)

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2024年七月11日 07:26 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/11 "2024-07-11T07:26:19Z")

</div>

我认为导入器无法处理那些。  
如果我没记错的话，所有的 `[ATTACH]` 标签都从帖子中移除了，因为它们是多余的。这可能在这里不起作用，因为它不期望其中有 JSON 数据。这需要查找它们被移除的地方并修改代码以处理标签内的 JSON 数据。

---

<div class="post-metadata">

### Author: ![Mubasher112](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mubasher112](https://meta.discourse.org/u/Mubasher112)
#### Post date: [2024年七月11日 08:25 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/12 "2024-07-11T08:25:15Z")

</div>

在导入附件之前，我注意到包含图片的帖子带有 [ATTACH] 标签。导入后，其中一些标签被正确填充，而另一些则为空。为什么会这样？

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2024年七月11日 09:10 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/13 "2024-07-11T09:10:41Z")

</div>

正如我所说，那些标签是多余的，但是标签移除逻辑不期望 JSON 代码，所以它没有正确地移除它们。

---

<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: [2024年七月11日 09:58 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/14 "2024-07-11T09:58:44Z")

</div>

那些图片都没显示出来，对吧？

我认为有些 vBulletin 是将它们添加到数据库中，有些则像那样包含 bbcode。我认为我以前已经修改过导入程序来处理那些。

---

<div class="post-metadata">

### Author: ![Mubasher112](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mubasher112](https://meta.discourse.org/u/Mubasher112)
#### Post date: [2024年七月11日 10:20 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/15 "2024-07-11T10:20:45Z")

</div>

这些没有显示。  
我在 12.5k 个附件中有 1k 个附件出错。  
我该如何上传它们？

---

<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: [2024年七月11日 10:36 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/16 "2024-07-11T10:36:16Z")

</div>

哦，我之前没注意到这个 json。你是指将这些 json 文件作为附件嵌入到帖子中吗？在 vBulletin 中，这些帖子看起来是什么样的？

我相信其他的错误是因为那些帖子由于某种原因（例如父主题被删除或未被导入）而未能导入。

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2024年七月11日 11:10 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/17 "2024-07-11T11:10:44Z")

</div>

> [@pfaffman](#):
>
> 您是否期望这些 json 文件是附件？

我认为这些不是 json 文件，而是 json 元数据。  
看起来 vBulletin 将附件位置的编码从

`[attach]123[/attach]`

更改为

`[attach=json]{"data-attachmentid":123}[/attach]`

并且导入器无法处理。它应该仍然附加附件，这些标签仅用于在帖子中定位它们。但标签的删除仅发生在它们包含数字 ID 时。

上面截图中的许多其他错误与此问题无关。

---

<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: [2024年七月11日 11:14 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/18 "2024-07-11T11:14:38Z")

</div>

> [@RGJ](#):
>
> 这些标签仅用于在帖子中定位它们

我以为我曾见过有时数据库会将它们链接到帖子，有时 bbcode 会，我想有时两者都会？有时它们存在于数据库中，有时它们是外部文件（但这可能是我记错了其他系统）。

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2024年七月11日 13:34 UTC](https://meta.discourse.org/t/migrating-vbulletin-5-database-import-attachments-errors-about-attachment-extension/315235/19 "2024-07-11T13:34:05Z")

</div>

是的，大致正确。但据我所知，在 vBulletin 5 中，它总是通过数据库进行的。
