dirReader.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. const iconConfig = [
  2. {
  3. type: ["", undefined, null],
  4. mime: "",
  5. icon: "/devTools/page/static/fileSys/weizhiwenjian.png",
  6. },
  7. {
  8. type: ["dwg"],
  9. mime: "dwg",
  10. icon: "/devTools/page/static/fileSys/DWG.png",
  11. },
  12. {
  13. type: ["xls", "xlsx", "csv"],
  14. mime: "xls",
  15. icon: "/devTools/page/static/fileSys/excel.png",
  16. },
  17. {
  18. type: ["exe"],
  19. mime: "exe",
  20. icon: "/devTools/page/static/fileSys/EXE.png",
  21. },
  22. {
  23. type: ["gif"],
  24. mime: "gif",
  25. icon: "/devTools/page/static/fileSys/GIF.png",
  26. },
  27. {
  28. type: ["html"],
  29. mime: "html",
  30. icon: "/devTools/page/static/fileSys/HTML.png",
  31. },
  32. {
  33. type: ["pdf"],
  34. mime: "pdf",
  35. icon: "/devTools/page/static/fileSys/pdf.png",
  36. },
  37. {
  38. type: ["ppt"],
  39. mime: "ppt",
  40. icon: "/devTools/page/static/fileSys/pptl.png",
  41. },
  42. {
  43. type: ["psd"],
  44. mime: "psd",
  45. icon: "/devTools/page/static/fileSys/PSD.png",
  46. },
  47. {
  48. type: ["rvt"],
  49. mime: "rvt",
  50. icon: "/devTools/page/static/fileSys/RVT.png",
  51. },
  52. {
  53. type: [
  54. "mp4",
  55. "avi",
  56. "wmv",
  57. "mpg",
  58. "mpeg",
  59. "mov",
  60. "flv",
  61. "3gp",
  62. "mp3gp",
  63. "mkv",
  64. "rmvb",
  65. ],
  66. mime: "mp4",
  67. icon: "/devTools/page/static/fileSys/shipin.png",
  68. },
  69. {
  70. type: ["skp"],
  71. mime: "skp",
  72. icon: "/devTools/page/static/fileSys/SKP.png",
  73. },
  74. {
  75. type: ["svg"],
  76. mime: "svg",
  77. icon: "/devTools/page/static/fileSys/SVG.png",
  78. },
  79. {
  80. type: ["png", "jpeg", "jpg", "webp", "bmp"],
  81. mime: "img",
  82. icon: "/devTools/page/static/fileSys/tupian.png",
  83. },
  84. {
  85. type: ["txt", "sql", "js", "css", "log", "json"],
  86. mime: "txt",
  87. icon: "/devTools/page/static/fileSys/txt.png",
  88. },
  89. {
  90. type: ["word"],
  91. mime: "word",
  92. icon: "/devTools/page/static/fileSys/word.png",
  93. },
  94. {
  95. type: ["zip", "rar", "gz", "7z"],
  96. mime: "zip",
  97. icon: "/devTools/page/static/fileSys/yasuo.png",
  98. },
  99. {
  100. type: ["mp3", "wma", "wav", "aac", "flac"],
  101. mime: "",
  102. icon: "/devTools/page/static/fileSys/yinpin.png",
  103. },
  104. ];
  105. export default {
  106. /**
  107. * 获取文件和目录列表
  108. */
  109. getDirFileList(path) {
  110. return new Promise((yes) => {
  111. // #ifdef APP-PLUS
  112. plus.io.resolveLocalFileSystemURL(path, function (entry) {
  113. if (entry.isDirectory) {
  114. let reader = entry.createReader();
  115. reader.readEntries(
  116. async (entries) => {
  117. let dirList = [];
  118. let fileList = [];
  119. for (let i = 0; i < entries.length; i++) {
  120. /**
  121. * @type {PlusIoDirectoryEntry}
  122. */
  123. const item = entries[i];
  124. let meta = await getMetaData(item)
  125. let row = {
  126. type: item.isDirectory ? 'dir' : 'file',
  127. name: item.name,
  128. fileType: getFileType(item.name),
  129. ...meta,
  130. }
  131. if (item.isDirectory) {
  132. dirList.push(row)
  133. } else {
  134. fileList.push(row)
  135. }
  136. }
  137. dirList = dirList.sort((a, b) => a.time > b.time)
  138. fileList = fileList.sort((a, b) => a.time > b.time)
  139. yes([
  140. ...dirList,
  141. ...fileList,
  142. ])
  143. },
  144. (e) => {
  145. console.log("readEntries error", e);
  146. uni.showToast({
  147. title: "文件读取失败: " + e.message,
  148. icon: "none",
  149. });
  150. yes([])
  151. }
  152. );
  153. } else {
  154. uni.showToast({
  155. title: "路径读取失败_b",
  156. icon: "none",
  157. });
  158. yes([])
  159. }
  160. }, () => {
  161. uni.showToast({
  162. title: "路径读取失败_a",
  163. icon: "none",
  164. });
  165. yes([])
  166. });
  167. // #endif
  168. })
  169. },
  170. /**
  171. * 获取文件图片
  172. */
  173. getFileIcon(type) {
  174. for (let i = 0; i < iconConfig.length; i++) {
  175. const item = iconConfig[i];
  176. for (let _i = 0; _i < item.type.length; _i++) {
  177. const typeName = item.type[_i];
  178. if (type == typeName) {
  179. return item.icon;
  180. }
  181. }
  182. }
  183. return "/devTools/page/static/fileSys/weizhiwenjian.png";
  184. }
  185. }
  186. /**
  187. * @param {PlusIoDirectoryEntry} entry
  188. */
  189. function getMetaData(entry) {
  190. return new Promise((yes) => {
  191. entry.getMetadata(function (metadata) {
  192. yes({
  193. size: metadata.size,
  194. time: metadata.modificationTime.getTime(),
  195. fileCount: metadata.fileCount,
  196. directoryCount: metadata.directoryCount,
  197. })
  198. }, function () {
  199. yes({
  200. size: 0,
  201. time: 0,
  202. fileCount: 0,
  203. directoryCount: 0,
  204. })
  205. });
  206. })
  207. }
  208. function getFileType(name) {
  209. if (typeof name == "string") {
  210. let tList = name.split(".");
  211. if (tList.length > 1) {
  212. return tList.pop().toLocaleLowerCase()
  213. } else {
  214. return ""
  215. }
  216. } else {
  217. return ""
  218. }
  219. }