list.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. const copy = require("./API/copy");
  2. const directory = require("./API/directory");
  3. const directory_PUT = require("./API/directory_PUT");
  4. const download = require("./API/download");
  5. const object_delete = require("./API/object_delete");
  6. const object_patch = require("./API/object_patch");
  7. const preview = require("./API/preview");
  8. const property = require("./API/property");
  9. const rename = require("./API/rename");
  10. const share = require("./API/share");
  11. // var cookie;
  12. function formatUtcTime(v) { // 时间格式化
  13. if (!v) {
  14. return ''
  15. }
  16. let date = new Date(v);
  17. date = new Date(date.valueOf());
  18. return date.getFullYear() +
  19. "-" + ((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) +
  20. "-" + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) +
  21. " " + (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) +
  22. ":" + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
  23. ":" + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  24. }
  25. async function attribute(m) { // 属性
  26. if (m.type == "dir") {
  27. var info = await property(m.id, true, cookie);
  28. $ui.showCode(`文件夹名: ${m.name}\n文件夹大小: ${(info.size/1024/1024).toFixed(2)}M\n子文件夹: ${info.child_folder_num}\n子文件: ${info.child_file_num}\n创建时间: ${formatUtcTime(info.created_at)}\n更新时间: ${formatUtcTime(info.updated_at)}\n查询时间: ${formatUtcTime(info.query_date)}`);
  29. } else {
  30. var info = await property(m.id, false, cookie);
  31. $ui.showCode(`文件名: ${m.name}\n文件大小: ${(info.size/1024/1024).toFixed(2)}M\n创建时间: ${formatUtcTime(info.created_at)}\n更新时间: ${formatUtcTime(info.updated_at)}\n查询时间: ${formatUtcTime(info.query_date)}\n储存节点: ${info.policy}`);
  32. }
  33. }
  34. async function make_share(m) { // 分享文件、分享文件夹
  35. var new_pwd = "";
  36. var char = 'abcdefghijklmnopqrstuvwxyz1234567890';
  37. for (var i = 0; i < 6; i++) {
  38. j = Math.floor(Math.random() * char.length);
  39. new_pwd += char.charAt(j);
  40. }
  41. $ui.toast("已随机生成密码, 可自行修改");
  42. var password = await $input.text({
  43. title: '输入分享密码',
  44. hint: "分享密码",
  45. value: new_pwd
  46. })
  47. if (password != null) {
  48. if (m.type == "dir") {
  49. var data = await share(m.id, true, password, true, 0, cookie);
  50. } else {
  51. var data = await share(m.id, false, password, true, 0, cookie);
  52. }
  53. if (data != false) {
  54. $ui.toast("分享成功");
  55. $ui.showCode(`链接: ${data}\n密码: ${password}`);
  56. } else {
  57. $ui.toast("分享失败");
  58. }
  59. } else {
  60. $ui.toast("取消分享");
  61. }
  62. }
  63. async function file_rename(m) { // 重新命名文件
  64. var new_name = await $input.text({
  65. title: '修改文件名',
  66. hint: "新文件名",
  67. value: m.name
  68. })
  69. var data = await rename(m.id, true, new_name, cookie);
  70. if (data) {
  71. $ui.toast("重新命名成功!");
  72. } else {
  73. $ui.toast("重新命名失败!");
  74. }
  75. }
  76. async function dir_create(m) { // 创建文件夹
  77. // var path = m.path[m.path.length-1]=="/" ? m.path.substring(0, m.path.length-1) : "";
  78. var dir_name = await $input.text({
  79. title: '创建文件夹',
  80. hint: '文件夹名',
  81. value: ''
  82. })
  83. var data = await directory_PUT(`${path}/${dir_name}`, cookie);
  84. if (data) {
  85. $ui.toast("创建成功!");
  86. } else {
  87. $ui.toast("创建失败!");
  88. }
  89. }
  90. async function dir_rename(m) { // 重新命名文件夹
  91. var new_name = await $input.text({
  92. title: '修改文件夹名',
  93. hint: "新文件夹名",
  94. value: m.name
  95. })
  96. var data = await rename(m.id, false, new_name, cookie);
  97. if (data) {
  98. $ui.toast("重新命名成功!");
  99. } else {
  100. $ui.toast("重新命名失败!");
  101. }
  102. }
  103. async function file_delete(m) { // 删除文件
  104. let pd = await $input.confirm({
  105. title: "确认框",
  106. message: `是否删除文件: ${m.name}`,
  107. okBtn: "删除"
  108. })
  109. if (pd) {
  110. var data = await object_delete(m.id, true, cookie);
  111. if (data) {
  112. $ui.toast("删除文件成功!");
  113. } else {
  114. $ui.toast("删除文件失败!");
  115. }
  116. } else {
  117. $ui.toast("取消删除");
  118. }
  119. }
  120. async function dir_delete(m) { // 删除文件夹
  121. let pd = await $input.confirm({
  122. title: "确认框",
  123. message: `是否删除文件夹: ${m.name}`,
  124. okBtn: "删除"
  125. })
  126. if (pd) {
  127. var data = await object_delete(m.id, false, cookie);
  128. if (data) {
  129. $ui.toast("删除文件夹成功!");
  130. } else {
  131. $ui.toast("删除文件夹失败!");
  132. }
  133. } else {
  134. $ui.toast("取消删除");
  135. }
  136. }
  137. async function file_down(m) { // 文件下载
  138. var url = await download(m.id, cookie);
  139. if (url != false) {
  140. $ui.browser(url);
  141. $ui.toast("开始下载...");
  142. } else {
  143. $ui.toast("获取失败");
  144. }
  145. }
  146. async function copy_move_file(m) { // 复制移动文件
  147. src_dir = m.path;
  148. mid = m.id;
  149. isFile = true;
  150. $ui.toast("复制移动...");
  151. }
  152. async function copy_move_folder(m) { // 复制移动文件
  153. src_dir = m.path!="" ? m.path : "/";
  154. mid = m.id;
  155. isFile = false;
  156. $ui.toast("复制移动...");
  157. }
  158. async function copy_to(m) { // 复制
  159. let pd = await $input.confirm({
  160. title: "确认框",
  161. message: `是否复制到该目录`,
  162. okBtn: "复制"
  163. })
  164. if (pd) {
  165. if (await copy(path=="" ? "/" : path, mid, src_dir, isFile, cookie)) {
  166. $ui.toast("复制成功");
  167. } else {
  168. $ui.toast("复制失败");
  169. }
  170. } else {
  171. $ui.toast("取消复制");
  172. }
  173. src_dir = "";
  174. }
  175. async function move_to(m) { // 移动到
  176. let pd = await $input.confirm({
  177. title: "确认框",
  178. message: "是否移动到该目录",
  179. okBtn: "移动"
  180. })
  181. if (pd) {
  182. if (await object_patch("move", path=="" ? "/" : path, mid, src_dir, isFile, cookie)) {
  183. $ui.toast("移动成功");
  184. } else {
  185. $ui.toast("移动失败");
  186. }
  187. } else {
  188. $ui.toast("取消移动");
  189. }
  190. src_dir = "";
  191. }
  192. module.exports = {
  193. type: 'list',
  194. beforeCreate() {
  195. getCookie();
  196. },
  197. async fetch({args}) {
  198. this.title = args.title;
  199. path = args.path;
  200. var list = await directory(args.path, cookie);
  201. if (list == false && typeof(list) == "boolean") {
  202. $router.to($route('login'));
  203. $ui.toast("Cookie已失效,请登录!");
  204. } else {
  205. var dir = [];
  206. list.forEach(m => {
  207. if (m.type == "dir") {
  208. m.path.substring(m.path.length-1) == "/" ? m.path = m.path.substring(0, m.path.length-1) : null;
  209. dir.push({
  210. title: m.name,
  211. route: $route("list", {
  212. path: m.path + '/' + m.name,
  213. ppath: m.path,
  214. title: m.name
  215. }),
  216. onLongClick: async () => {
  217. var options = [];
  218. options.push({title: '创建文件夹', fun: dir_create});
  219. options.push({title: '重新命名', fun: dir_rename});
  220. src_dir=="" ? options.push({title: '复制移动', fun: copy_move_folder}) : null;
  221. src_dir!="" ? options.push({title: '复制到', fun: copy_to}) : null;
  222. src_dir!="" ? options.push({title: '移动到', fun: move_to}) : null;
  223. options.push({title: '分享文件夹', fun: make_share});
  224. options.push({title: '删除文件夹', fun: dir_delete});
  225. options.push({title: '属性', fun: attribute});
  226. var selected = await $input.select({
  227. title: '选择哪一个',
  228. options: options
  229. })
  230. selected != null ? selected.fun(m) : null;
  231. }
  232. });
  233. }
  234. });
  235. if (dir.length > 0) {
  236. dir.splice(0, 0, {
  237. title: '文件夹 (单点打开、长按更多操作)',
  238. style: 'category'
  239. })
  240. }
  241. var file = [];
  242. list.forEach(m => {
  243. if (m.type == "file") {
  244. file.push({
  245. title: m.name,
  246. onClick: async () => {
  247. var type = ['.flv', '.mp4', '.ts', '.mkv'];
  248. var video = false;
  249. type.forEach(f => {m.name.includes(f) ? video=true : null});
  250. if (video) {
  251. var url = await preview(m.id, cookie);
  252. $router.to($route('@video', {url: url, title: m.name}));
  253. } else {
  254. $ui.toast("不是指定文件格式");
  255. }
  256. },
  257. onLongClick: async () => {
  258. var options = [];
  259. options.push({title: '创建文件夹', fun: dir_create});
  260. options.push({title: '文件下载', fun: file_down});
  261. options.push({title: '重新命名', fun: file_rename});
  262. src_dir=="" ? options.push({title: "复制移动", fun: copy_move_file}) : null;
  263. src_dir!="" ? options.push({title: '复制到', fun: copy_to}) : null;
  264. src_dir!="" ? options.push({title: '移动到', fun: move_to}) : null;
  265. options.push({title: '分享文件', fun: make_share});
  266. options.push({title: '删除文件', fun: file_delete});
  267. options.push({title: '属性', fun: attribute});
  268. var selected = await $input.select({
  269. title: '选择哪一个',
  270. options: options
  271. })
  272. selected != null ? selected.fun(m) : null;
  273. }
  274. });
  275. }
  276. });
  277. if (file.length > 0) {
  278. file.splice(0, 0, {
  279. title: '文件 (单点预览、长按更多操作)',
  280. style: 'category'
  281. })
  282. }
  283. file.forEach(f => {
  284. dir.push(f);
  285. })
  286. this.actions = [{title: '搜索分享', route: $route("share_search")}];
  287. if (list.length == 0) {
  288. let actions = this.actions==undefined ? [] : this.actions;
  289. actions.push({
  290. title: '创建文件夹',
  291. onClick: () => {
  292. dir_create();
  293. }
  294. })
  295. if (src_dir != "") {
  296. actions.push({
  297. title: '复制到',
  298. onClick: () => {
  299. copy_to();
  300. }
  301. })
  302. actions.push({
  303. title: '移动到',
  304. onClick: () => {
  305. move_to();
  306. }
  307. })
  308. }
  309. this.actions = actions;
  310. }
  311. return dir;
  312. }
  313. },
  314. async beforeDestroy() {
  315. path = this.args.ppath;
  316. }
  317. }