file-preview.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. import Ember from 'ember';
  19. import FileOperationMixin from '../mixins/file-operation';
  20. export default Ember.Service.extend(FileOperationMixin, {
  21. fileSelectionService: Ember.inject.service('files-selection'),
  22. selectedFiles: Ember.computed.alias('fileSelectionService.files'),
  23. selected: Ember.computed('selectedFiles', function () {
  24. return this.get('selectedFiles').objectAt(0);
  25. }),
  26. selectedFilePath: Ember.computed('selected.path', function () {
  27. return this.get('selected.path');
  28. }),
  29. filesDownloadService: Ember.inject.service('files-download'),
  30. fileContent: '',
  31. startIndex: 0,
  32. offset: 5000,
  33. path: '',
  34. isLoading: false,
  35. fileFetchFinished: false,
  36. hasError: false,
  37. endIndex: function () {
  38. return this.get('startIndex') + this.get('offset');
  39. }.property('startIndex'),
  40. reset: function () {
  41. this.set('fileContent', '');
  42. this.set('startIndex', 0);
  43. this.set('offset', 5000);
  44. this.set('path', '');
  45. this.set('isLoading', false);
  46. this.set('hasError', false);
  47. this.set('fileFetchFinished', false);
  48. },
  49. getNextContent: function () {
  50. return this._getContent();
  51. },
  52. _getContent: function () {
  53. var _self = this;
  54. if (this.get('fileFetchFinished')) {
  55. return false;
  56. }
  57. var adapter = this.get('store').adapterFor('file');
  58. var baseURL = adapter.buildURL('file');
  59. var renameUrl = baseURL.substring(0, baseURL.lastIndexOf('/'));
  60. var previewUrl = renameUrl.substring(0, renameUrl.lastIndexOf('/')) + "/preview/file?path=";
  61. var currentFetchPath = previewUrl + this.get('selected.path') + '&start=' + this.get('startIndex') + '&end=' + this.get('endIndex');
  62. this.set('isLoading', true);
  63. Ember.$.ajax({
  64. url: currentFetchPath,
  65. dataType: 'json',
  66. type: 'get',
  67. contentType: 'application/json',
  68. success: this._fetchSuccess,
  69. beforeSend: function (xhr) {
  70. xhr.setRequestHeader('X-Requested-By', 'ambari');
  71. xhr.setRequestHeader('Authorization', 'Basic YWRtaW46YWRtaW4=');
  72. },
  73. success: function (response, textStatus, jQxhr) {
  74. _self.set('fileContent', _self.get('fileContent') + response.data);
  75. _self.set('fileFetchFinished', response.isFileEnd);
  76. _self.set('isLoading', false);
  77. },
  78. error: function (jQxhr, textStatus, errorThrown) {
  79. console.log("Preview Fail pagecontent: " + errorThrown);
  80. _self.set('hasError', true);
  81. _self.set('isLoading', false);
  82. }
  83. })
  84. this.set('startIndex', (this.get('startIndex') + this.get('offset')));
  85. },
  86. _fetchSuccess: function (response, textStatus, jQxhr) {
  87. this.set('fileContent', this.get('fileContent') + response.data);
  88. this.set('fileFetchFinished', response.isFileEnd);
  89. this.set('isLoading', false);
  90. },
  91. _fetchError: function (jQxhr, textStatus, errorThrown) {
  92. console.log("Preview Fail pagecontent: " + errorThrown);
  93. this.set('hasError', true);
  94. this.set('isLoading', false);
  95. },
  96. download: function (event) {
  97. this.get('filesDownloadService').download();
  98. }
  99. });