resource.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <div v-if="focus">
  3. <video
  4. v-if="type === MetaType.video"
  5. controls
  6. autoplay
  7. playsinline
  8. webkit-playsinline
  9. >
  10. <source :src="url" />
  11. </video>
  12. <iframe v-else-if="type === MetaType.other" :src="url"></iframe>
  13. <iframe
  14. v-else-if="type === MetaType.xfile"
  15. :src="`./xfile-viewer/index.html?file=${url}&time=${Date.now()}`"
  16. ></iframe>
  17. <img :src="url" v-if="type === MetaType.image" />
  18. <audio
  19. :src="url"
  20. v-if="type === MetaType.audio"
  21. controls
  22. autoplay
  23. playsinline
  24. webkit-playsinline
  25. />
  26. </div>
  27. </template>
  28. <script lang="ts" setup>
  29. import { getResource } from "@/env";
  30. import { computed } from "vue";
  31. import { getUrlType, MetaType } from "@/utils/meta";
  32. const props = withDefaults(
  33. defineProps<{
  34. data: string | Blob | File;
  35. type?: MetaType;
  36. focus?: boolean;
  37. }>(),
  38. { focus: true }
  39. );
  40. const url = computed(() =>
  41. typeof props.data === "string"
  42. ? getResource(props.data)
  43. : URL.createObjectURL(props.data)
  44. );
  45. const type = computed(() => {
  46. if (props.type) {
  47. return props.type;
  48. } else if (props.data instanceof File || typeof props.data === "string") {
  49. const d = props.data instanceof File ? props.data.name : props.data;
  50. return getUrlType(d);
  51. } else {
  52. return MetaType.other;
  53. }
  54. });
  55. </script>
  56. <style scoped>
  57. div {
  58. width: 100%;
  59. height: 100%;
  60. display: flex;
  61. align-items: center;
  62. justify-content: center;
  63. }
  64. audio,
  65. iframe {
  66. width: 100%;
  67. height: 100%;
  68. display: block;
  69. }
  70. video,
  71. img {
  72. max-width: 100%;
  73. max-height: 100%;
  74. display: block;
  75. object-fit: cover;
  76. }
  77. iframe {
  78. border: none;
  79. }
  80. </style>