index.js 3.8 KB

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