index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { AuthType, FileStat, createClient } from "webdav";
  2. interface ICachedData {
  3. url?: string;
  4. username?: string;
  5. password?: string;
  6. searchPath?: string;
  7. searchPathList?: string[];
  8. cacheFileList?: FileStat[];
  9. }
  10. let cachedData: ICachedData = {};
  11. function getClient() {
  12. const { url, username, password, searchPath } =
  13. env?.getUserVariables?.() ?? {};
  14. if (!(url && username && password)) {
  15. return null;
  16. }
  17. if (
  18. !(
  19. cachedData.url === url &&
  20. cachedData.username === username &&
  21. cachedData.password === password &&
  22. cachedData.searchPath === searchPath
  23. )
  24. ) {
  25. cachedData.url = url;
  26. cachedData.username = username;
  27. cachedData.password = password;
  28. cachedData.searchPath = searchPath;
  29. cachedData.searchPathList = searchPath?.split?.(",");
  30. cachedData.cacheFileList = null;
  31. }
  32. return createClient(url, {
  33. authType: AuthType.Password,
  34. username,
  35. password,
  36. });
  37. }
  38. async function searchMusic(query: string) {
  39. const client = getClient();
  40. if (!cachedData.cacheFileList) {
  41. const searchPathList = cachedData.searchPathList?.length
  42. ? cachedData.searchPathList
  43. : ["/"];
  44. let result: FileStat[] = [];
  45. for (let search of searchPathList) {
  46. try {
  47. const fileItems = (
  48. (await client.getDirectoryContents(search)) as FileStat[]
  49. ).filter((it) => it.type === "file" && it.mime.startsWith("audio"));
  50. result = [...result, ...fileItems];
  51. } catch {}
  52. }
  53. cachedData.cacheFileList = result;
  54. }
  55. return {
  56. isEnd: true,
  57. data: (cachedData.cacheFileList ?? [])
  58. .filter((it) => it.basename.includes(query))
  59. .map((it) => ({
  60. title: it.basename,
  61. id: it.filename,
  62. artist: "未知作者",
  63. album: "未知专辑",
  64. })),
  65. };
  66. }
  67. async function getTopLists() {
  68. getClient();
  69. const data = {
  70. title: "全部歌曲",
  71. data: (cachedData.searchPathList || []).map((it) => ({
  72. title: it,
  73. id: it,
  74. })),
  75. };
  76. return [data];
  77. }
  78. async function getTopListDetail(topListItem: IMusicSheet.IMusicSheetItem) {
  79. const client = getClient();
  80. const fileItems = (
  81. (await client.getDirectoryContents(topListItem.id)) as FileStat[]
  82. ).filter((it) => it.type === "file" && it.mime.startsWith("audio"));
  83. return {
  84. musicList: fileItems.map((it) => ({
  85. title: it.basename,
  86. id: it.filename,
  87. artist: "未知作者",
  88. album: "未知专辑",
  89. })),
  90. };
  91. }
  92. module.exports = {
  93. platform: "WebDAV",
  94. author: "猫头猫",
  95. description: "使用此插件前先配置用户变量",
  96. userVariables: [
  97. {
  98. key: "url",
  99. name: "WebDAV地址",
  100. },
  101. {
  102. key: "username",
  103. name: "用户名",
  104. },
  105. {
  106. key: "password",
  107. name: "密码",
  108. type: "password",
  109. },
  110. {
  111. key: "searchPath",
  112. name: "存放歌曲的路径",
  113. },
  114. ],
  115. version: "0.0.2",
  116. supportedSearchType: ["music"],
  117. srcUrl:
  118. "https://gitee.com/maotoumao/MusicFreePlugins/raw/v0.1/dist/webdav/index.js",
  119. cacheControl: "no-cache",
  120. search(query, page, type) {
  121. if (type === "music") {
  122. return searchMusic(query);
  123. }
  124. },
  125. getTopLists,
  126. getTopListDetail,
  127. getMediaSource(musicItem) {
  128. const client = getClient();
  129. return {
  130. url: client.getFileDownloadLink(musicItem.id),
  131. };
  132. },
  133. };