Răsfoiți Sursa

修复非安全浏览器附件编号生成

LSamien 2 luni în urmă
părinte
comite
dfc3398be0

+ 2 - 1
src/client/components/composer/ComposerShell.vue

@@ -1,6 +1,7 @@
 <script setup lang="ts">
 import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
 import AppIcon from '@/components/common/AppIcon.vue'
+import { createId } from '@/utils/id'
 import type { ComposerAttachment, ComposerOption, ComposerReference, ComposerSubmit } from './types'
 
 const props = withDefaults(defineProps<{
@@ -149,7 +150,7 @@ function addFiles(files: File[]) {
       continue
     }
     accepted.push({
-      id: `${file.name}:${file.size}:${file.lastModified}:${crypto.randomUUID()}`,
+      id: `${file.name}:${file.size}:${file.lastModified}:${createId('attachment')}`,
       file,
       name: file.name,
       size: file.size,

+ 20 - 0
tests/client/composer.test.ts

@@ -41,4 +41,24 @@ describe('composer availability', () => {
     expect(activity.element.nextElementSibling).toBe(shell.element)
     expect(activity.get('[role="status"]').text()).toBe('夭夭正在输入…')
   })
+
+  it('adds attachments when randomUUID is unavailable in an HTTP browser', async () => {
+    const descriptor = Object.getOwnPropertyDescriptor(globalThis.crypto, 'randomUUID')
+    Object.defineProperty(globalThis.crypto, 'randomUUID', { configurable: true, value: undefined })
+    try {
+      const wrapper = mount(ComposerShell)
+      const input = wrapper.get<HTMLInputElement>('.composer-file-input')
+      const file = new File(['兼容内容'], '兼容.txt', { type: 'text/plain', lastModified: 123 })
+      Object.defineProperty(input.element, 'files', { configurable: true, value: [file] })
+
+      await input.trigger('change')
+
+      expect(wrapper.get('.composer-attachment strong').text()).toBe('兼容.txt')
+      expect(wrapper.find('[role="alert"]').exists()).toBe(false)
+      wrapper.unmount()
+    } finally {
+      if (descriptor) Object.defineProperty(globalThis.crypto, 'randomUUID', descriptor)
+      else Reflect.deleteProperty(globalThis.crypto, 'randomUUID')
+    }
+  })
 })