use-library.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
  2. import LibraryApi from 'src/api/LibraryApi';
  3. import { SystemMessage } from 'src/utils/message';
  4. import { until } from '@vueuse/core';
  5. import config from 'src/config';
  6. import { LocalStorage } from 'quasar';
  7. import { useSettings } from 'pages/settings/use-settings';
  8. export const getUrl = (basePath) => config.BACKEND_URL + basePath.split(/\/|\\/).join('/');
  9. // 封面规则
  10. export const coverRule = ref(LocalStorage.getItem('coverRule') ?? 'poster.jpg');
  11. export const originMovies = ref([]);
  12. export const originTvs = ref([]);
  13. const movies = computed(() =>
  14. originMovies.value.map((movie) => ({
  15. ...movie,
  16. }))
  17. );
  18. const tvs = computed(() =>
  19. originTvs.value.map((tv) => ({
  20. ...tv,
  21. }))
  22. );
  23. export const libraryRefreshStatus = ref(null);
  24. export const subtitleUploadList = ref([]);
  25. export const refreshCacheLoading = computed(() => libraryRefreshStatus.value === 'running');
  26. let getRefreshStatusTimer = null;
  27. export const getLibraryRefreshStatus = async () => {
  28. const [res] = await LibraryApi.getRefreshStatus();
  29. libraryRefreshStatus.value = res.status;
  30. };
  31. export const getLibraryList = async () => {
  32. const [res, err] = await LibraryApi.getList();
  33. if (err !== null) {
  34. SystemMessage.error(err.message);
  35. } else {
  36. originMovies.value = res.movie_infos_v2;
  37. originTvs.value = res.season_infos_v2;
  38. }
  39. };
  40. export const checkLibraryRefreshStatus = async () => {
  41. libraryRefreshStatus.value = null;
  42. await getLibraryRefreshStatus();
  43. getRefreshStatusTimer = setInterval(() => {
  44. getLibraryRefreshStatus();
  45. }, 1000);
  46. await until(libraryRefreshStatus).toBe('stopped');
  47. clearInterval(getRefreshStatusTimer);
  48. getRefreshStatusTimer = null;
  49. await getLibraryList();
  50. };
  51. export const refreshLibrary = async () => {
  52. const [, err] = await LibraryApi.refreshLibrary();
  53. if (err !== null) {
  54. SystemMessage.error(err.message);
  55. } else {
  56. await checkLibraryRefreshStatus();
  57. SystemMessage.success('更新缓存成功');
  58. }
  59. };
  60. export const getSubtitleUploadList = async () => {
  61. const [res] = await LibraryApi.getSubTitleQueueList();
  62. subtitleUploadList.value = res.jobs;
  63. };
  64. export const useLibrary = () => {
  65. useSettings();
  66. const getSubtitleUploadListTimer = setInterval(() => {
  67. getSubtitleUploadList();
  68. }, 5000);
  69. onMounted(() => {
  70. getLibraryList();
  71. getLibraryRefreshStatus();
  72. getSubtitleUploadList();
  73. checkLibraryRefreshStatus();
  74. });
  75. onBeforeUnmount(() => {
  76. clearInterval(getRefreshStatusTimer);
  77. clearInterval(getSubtitleUploadListTimer);
  78. });
  79. return {
  80. movies,
  81. tvs,
  82. refreshLibrary,
  83. refreshCacheLoading,
  84. };
  85. };
  86. export const doFixSubtitleTimeline = async (path) => {
  87. const formData = new FormData();
  88. formData.append('video_f_path', path);
  89. const subtitleUrl = getUrl(path);
  90. // 先下载字幕到内存,生成file文件
  91. const res = await fetch(subtitleUrl);
  92. if (!res.ok) {
  93. SystemMessage.error('获取字幕文件失败');
  94. return;
  95. }
  96. const blob = await res.blob();
  97. const file = new File([blob], path.split(/\/|\\/).pop());
  98. formData.append('file', file);
  99. await LibraryApi.uploadSubtitle(formData);
  100. SystemMessage.success('已提交时间轴校准', {
  101. timeout: 3000,
  102. });
  103. await getSubtitleUploadList();
  104. };