explorer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. (function() {
  19. "use strict";
  20. // The chunk size of tailing the files, i.e., how many bytes will be shown
  21. // in the preview.
  22. var TAIL_CHUNK_SIZE = 32768;
  23. var helpers = {
  24. 'helper_to_permission': function(chunk, ctx, bodies, params) {
  25. var p = ctx.current().permission;
  26. var dir = ctx.current().type == 'DIRECTORY' ? 'd' : '-';
  27. var symbols = [ '---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx' ];
  28. var sticky = p > 1000;
  29. var res = "";
  30. for (var i = 0; i < 3; ++i) {
  31. res = symbols[(p % 10)] + res;
  32. p = Math.floor(p / 10);
  33. }
  34. if (sticky) {
  35. var otherExec = ((ctx.current().permission % 10) & 1) == 1;
  36. res = res.substr(0, res.length - 1) + (otherExec ? 't' : 'T');
  37. }
  38. chunk.write(dir + res);
  39. return chunk;
  40. }
  41. };
  42. var base = dust.makeBase(helpers);
  43. var current_directory = "";
  44. function show_err_msg(msg) {
  45. $('#alert-panel-body').html(msg);
  46. $('#alert-panel').show();
  47. }
  48. function network_error_handler(url) {
  49. return function (jqxhr, text, err) {
  50. var msg = '<p>Failed to retreive data from ' + url + ', cause: ' + err + '</p>';
  51. if (url.indexOf('/webhdfs/v1') === 0) {
  52. msg += '<p>WebHDFS might be disabled. WebHDFS is required to browse the filesystem.</p>';
  53. }
  54. show_err_msg(msg);
  55. };
  56. }
  57. function append_path(prefix, s) {
  58. var l = prefix.length;
  59. var p = l > 0 && prefix[l - 1] == '/' ? prefix.substring(0, l - 1) : prefix;
  60. return p + '/' + s;
  61. }
  62. function get_response(data, type) {
  63. return data[type] !== undefined ? data[type] : null;
  64. }
  65. function get_response_err_msg(data) {
  66. var msg = data.RemoteException !== undefined ? data.RemoteException.message : "";
  67. return msg;
  68. }
  69. function view_file_details(path, abs_path) {
  70. function show_block_info(blocks) {
  71. var menus = $('#file-info-blockinfo-list');
  72. menus.empty();
  73. menus.data("blocks", blocks);
  74. menus.change(function() {
  75. var d = $(this).data('blocks')[$(this).val()];
  76. if (d === undefined) {
  77. return;
  78. }
  79. dust.render('block-info', d, function(err, out) {
  80. $('#file-info-blockinfo-body').html(out);
  81. });
  82. });
  83. for (var i = 0; i < blocks.length; ++i) {
  84. var item = $('<option value="' + i + '">Block ' + i + '</option>');
  85. menus.append(item);
  86. }
  87. menus.change();
  88. }
  89. var url = '/webhdfs/v1' + abs_path + '?op=GET_BLOCK_LOCATIONS';
  90. $.ajax({"url": url, "crossDomain": true}).done(function(data) {
  91. var d = get_response(data, "LocatedBlocks");
  92. if (d === null) {
  93. show_err_msg(get_response_err_msg(data));
  94. return;
  95. }
  96. $('#file-info-tail').hide();
  97. $('#file-info-title').text("File information - " + path);
  98. var download_url = '/webhdfs/v1' + abs_path + '/?op=OPEN';
  99. $('#file-info-download').attr('href', download_url);
  100. $('#file-info-preview').click(function() {
  101. var offset = d.fileLength - TAIL_CHUNK_SIZE;
  102. var url = offset > 0 ? download_url + '&offset=' + offset : download_url;
  103. $.get(url, function(t) {
  104. $('#file-info-preview-body').val(t);
  105. $('#file-info-tail').show();
  106. }, "text").error(network_error_handler(url));
  107. });
  108. if (d.fileLength > 0) {
  109. show_block_info(d.locatedBlocks);
  110. $('#file-info-blockinfo-panel').show();
  111. } else {
  112. $('#file-info-blockinfo-panel').hide();
  113. }
  114. $('#file-info').modal();
  115. }).error(network_error_handler(url));
  116. }
  117. function browse_directory(dir) {
  118. var url = '/webhdfs/v1' + dir + '?op=LISTSTATUS';
  119. $.get(url, function(data) {
  120. var d = get_response(data, "FileStatuses");
  121. if (d === null) {
  122. show_err_msg(get_response_err_msg(data));
  123. return;
  124. }
  125. current_directory = dir;
  126. $('#directory').val(dir);
  127. dust.render('explorer', base.push(d), function(err, out) {
  128. $('#panel').html(out);
  129. $('.explorer-browse-links').click(function() {
  130. var type = $(this).attr('inode-type');
  131. var path = $(this).attr('inode-path');
  132. var abs_path = append_path(current_directory, path);
  133. if (type == 'DIRECTORY') {
  134. browse_directory(abs_path);
  135. } else {
  136. view_file_details(path, abs_path);
  137. }
  138. });
  139. });
  140. }).error(network_error_handler(url));
  141. }
  142. function init() {
  143. dust.loadSource(dust.compile($('#tmpl-explorer').html(), 'explorer'));
  144. dust.loadSource(dust.compile($('#tmpl-block-info').html(), 'block-info'));
  145. var b = function() { browse_directory($('#directory').val()); };
  146. $('#btn-nav-directory').click(b);
  147. browse_directory('/');
  148. }
  149. init();
  150. })();