You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2018 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_web_utils.files', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var utils = require('web.utils');
  23. var QWeb = core.qweb;
  24. var _t = core._t;
  25. var traverse_items = function(items, structure) {
  26. var $result = $.Deferred();
  27. var files = [];
  28. var events = [];
  29. function traverse_item() {
  30. var $get = $.Deferred();
  31. var item, file;
  32. events.push($get);
  33. if (items[i] instanceof DataTransferItem) {
  34. item = items[i].webkitGetAsEntry();
  35. file = items[i].getAsFile();
  36. } else {
  37. item = items[i];
  38. }
  39. if (item) {
  40. if (item.isFile) {
  41. files.push(item);
  42. $get.resolve();
  43. } else if (item.isDirectory) {
  44. var dirReader = item.createReader();
  45. dirReader.readEntries(function (entries) {
  46. traverse_items(entries, structure).then(function (result) {
  47. if(structure) {
  48. files.push({
  49. name: item.name,
  50. fullPath: item.fullPath,
  51. files: result,
  52. isDirectory: true,
  53. isFile: false,
  54. });
  55. } else {
  56. files = _.union(files, result);
  57. }
  58. $get.resolve();
  59. });
  60. });
  61. }
  62. } else if (file) {
  63. if(file.size) {
  64. file.isFileItem = !!file.size;
  65. files.push(file);
  66. }
  67. $get.resolve();
  68. } else {
  69. console.warn("Your browser doesn't support Drag and Drop!");
  70. $get.resolve();
  71. }
  72. };
  73. for (var i = 0; i < items.length; i++) {
  74. traverse_item();
  75. }
  76. $.when.apply($, events).then(function () {
  77. $result.resolve(files);
  78. });
  79. return $result;
  80. };
  81. var count_files_in_structure = function(structure) {
  82. var counter = 0;
  83. _.each(structure, function(item, index, structure) {
  84. if(item.isFile || item.isFileItem) {
  85. counter++;
  86. } else if(item.isDirectory) {
  87. counter += count_files_in_structure(item.files);
  88. }
  89. });
  90. return counter;
  91. }
  92. var get_file_list = function(items) {
  93. return traverse_items(items, false);
  94. };
  95. var get_file_structure = function(items) {
  96. return traverse_items(items, true);
  97. };
  98. var load_file = function(file, callback) {
  99. var fileReader = new FileReader();
  100. fileReader.readAsDataURL(file);
  101. fileReader.onloadend = callback;
  102. };
  103. var read_file = function(file, callback) {
  104. if(file.isFile) {
  105. file.file(function(file) {
  106. load_file(file, callback);
  107. });
  108. } else {
  109. load_file(file, callback);
  110. }
  111. };
  112. return {
  113. traverse_items: traverse_items,
  114. count_files_in_structure: count_files_in_structure,
  115. get_file_list: get_file_list,
  116. get_file_structure: get_file_structure,
  117. load_file: load_file,
  118. read_file: read_file,
  119. };
  120. });