IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
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.

218 lines
7.8KB

  1. import { h, Component } from 'preact';
  2. import WBTable from 'wb-table';
  3. import WBBreadcrumbs from 'wb-breadcrumbs';
  4. import WBPagination from 'wb-pagination';
  5. import makeArvadosRequest from 'make-arvados-request';
  6. import wbDownloadFile from 'wb-download-file';
  7. import WBManifestWorkerWrapper from 'wb-manifest-worker-wrapper';
  8. function unescapeName(name) {
  9. return name.replace(/(\\\\|\\[0-9]{3})/g,
  10. (_, $1) => ($1 === '\\\\' ? '\\' : String.fromCharCode(parseInt($1.substr(1), 8))));
  11. }
  12. function encodeURIComponentIncludingDots(s) {
  13. return encodeURIComponent(s).replace('.', '%2E');
  14. }
  15. class WBCollectionContent extends Component {
  16. constructor(...args) {
  17. super(...args);
  18. this.state.rows = [];
  19. this.state.manifestWorker = new WBManifestWorkerWrapper();
  20. this.state.loaded = 0;
  21. this.state.total = 0;
  22. this.state.mode = 'manifestDownload';
  23. this.state.parsedStreams = 0;
  24. this.state.totalStreams = 1;
  25. }
  26. getUrl(params) {
  27. let res = '/collection-browse/' +
  28. ('uuid' in params ? params.uuid : this.props.uuid) + '/' +
  29. encodeURIComponentIncludingDots('collectionPath' in params ? params.collectionPath : this.props.collectionPath) + '/' +
  30. ('page' in params ? params.page : this.props.page);
  31. return res;
  32. }
  33. componentDidMount() {
  34. let { arvHost, arvToken } = this.props.app.state;
  35. let { uuid, collectionPath } = this.props;
  36. let { manifestWorker } = this.state;
  37. let select = [ 'manifest_text' ];
  38. let prom = makeArvadosRequest(arvHost, arvToken,
  39. '/arvados/v1/collections/' + uuid +
  40. '?select=' + encodeURIComponent(JSON.stringify(select)),
  41. { 'onProgress': e => {
  42. this.setState({ 'loaded': e.loaded, 'total': e.total });
  43. } });
  44. prom = prom.then(xhr => {
  45. const streams = xhr.response.manifest_text.split('\n');
  46. const paths = streams.filter(s => s).map(s => {
  47. const n = s.indexOf(' ');
  48. return unescapeName(s.substr(0, n));
  49. });
  50. let prom_1 = new Promise(accept => accept());
  51. prom_1 = prom_1.then(() => {
  52. this.setState({
  53. 'totalStreams': streams.length,
  54. 'parsedStreams': 0,
  55. 'mode': 'manifestParse'
  56. });
  57. return manifestWorker.postMessage([ 'precreatePaths', paths ]);
  58. });
  59. for (let i = 0; i < streams.length; i++) {
  60. prom_1 = prom_1.then(() => manifestWorker.postMessage([ 'parseStream', streams[i] ]));
  61. prom_1 = prom_1.then(() => manifestWorker.postMessage([
  62. 'listDirectory', '.' + this.props.collectionPath, true
  63. ]));
  64. prom_1 = prom_1.then(e => {
  65. this.prepareRows(e.data[1]);
  66. this.setState({ 'parsedStreams': (i + 1) });
  67. });
  68. }
  69. prom_1 = prom_1.then(() => manifestWorker.postMessage([ 'listDirectory',
  70. '.' + this.props.collectionPath, true ]));
  71. prom_1 = prom_1.then(e => {
  72. this.state.mode = 'browsingReady';
  73. this.prepareRows(e.data[1]);
  74. });
  75. return prom_1;
  76. });
  77. }
  78. componentWillReceiveProps(nextProps) {
  79. this.props = nextProps;
  80. const { manifestWorker, mode } = this.state;
  81. const { collectionPath } = this.props;
  82. if (mode === 'browsingReady') {
  83. let prom = manifestWorker.postMessage([ 'listDirectory', '.' + collectionPath, true ]);
  84. prom = prom.then(e => this.prepareRows(e.data[1]));
  85. }
  86. }
  87. prepareRows(listing) {
  88. let { manifestWorker, mode } = this.state;
  89. let { collectionPath, page, itemsPerPage, app } = this.props;
  90. let { arvHost, arvToken } = app.state;
  91. const numPages = Math.ceil(listing.length / itemsPerPage);
  92. listing = listing.slice(page * itemsPerPage,
  93. page * itemsPerPage + itemsPerPage);
  94. this.setState({
  95. 'numPages': numPages,
  96. 'rows': listing.map(item => (
  97. (item[0] === 'd') ? [
  98. (<a href={ this.getUrl({ 'collectionPath': collectionPath + '/' + item[1], 'page': 0 }) }>{ item[1] }/</a>),
  99. 'Directory',
  100. null,
  101. (<div></div>)
  102. ] : [
  103. item[1],
  104. 'File',
  105. filesize(item[2]),
  106. ( (mode === 'browsingReady') ? (
  107. <div>
  108. <button class="btn btn-outline-primary mx-1" title="Download"
  109. onclick={ () => manifestWorker.postMessage([ 'getFile',
  110. '.' + collectionPath + '/' + item[1] ]).then(e => {
  111. const file = e.data[1];
  112. const blob = new Blob([
  113. JSON.stringify([ arvHost, arvToken, item[1], file ])
  114. ]);
  115. const blocksBlobUrl = URL.createObjectURL(blob);
  116. window.open('/download/' + encodeURIComponent(blocksBlobUrl), '_blank');
  117. }) }><i class="fas fa-download"></i></button>
  118. <button class="btn btn-outline-primary mx-1" title="View"
  119. onclick={ () => {
  120. alert('Not implemented.')
  121. } }><i class="far fa-eye"></i></button>
  122. { item[1].toLowerCase().endsWith('.nii') ? (
  123. <button class="btn btn-outline-primary mx-1" title="View Image"
  124. onclick={ () => manifestWorker.postMessage([ 'getFile',
  125. '.' + collectionPath + '/' + item[1] ]).then(e => {
  126. const file = e.data[1];
  127. const blob = new Blob([
  128. JSON.stringify({ 'name': item[1], 'file': file })
  129. ]);
  130. const blocksBlobUrl = URL.createObjectURL(blob);
  131. window.open('/image-viewer/' + encodeURIComponent(blocksBlobUrl), '_blank');
  132. }) }><i class="far fa-eye"></i></button>
  133. ) : null }
  134. </div>
  135. ) : null)
  136. ]
  137. ))
  138. });
  139. }
  140. render({ collectionPath, page }, { manifestReader, rows,
  141. numPages, loaded, total, mode, parsedStreams, totalStreams }) {
  142. return (
  143. <div>
  144. <WBBreadcrumbs items={ ('.' + collectionPath).split('/').map((name, index) => ({ name, index })) }
  145. getItemUrl={ it => this.getUrl({
  146. collectionPath: ('.' + collectionPath).split('/').slice(0, it.index + 1).join('/').substr(1),
  147. page: 0
  148. }) } />
  149. { (mode === 'manifestDownload') ?
  150. (
  151. <div class="container-fluid">
  152. <div>Downloading manifest: { filesize(loaded) }</div>
  153. <div class="progress">
  154. <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar"
  155. aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div>
  156. </div>
  157. </div>
  158. ) : (
  159. <div>
  160. { mode === 'manifestParse' ? (
  161. <div class="container-fluid mb-2">
  162. <div>Parsing manifest: { parsedStreams }/{ totalStreams }</div>
  163. <div class="progress">
  164. <div class="progress-bar progress-bar-striped progress-bar-animated bg-success" role="progressbar"
  165. aria-valuenow={ totalStreams } aria-valuemin="0" aria-valuemax={ parsedStreams } style={ 'width: ' + Math.round(parsedStreams * 100 / totalStreams) + '%' }></div>
  166. </div>
  167. </div>
  168. ) : null }
  169. <WBTable columns={ [ 'Name', 'Type', 'Size', 'Actions' ] }
  170. rows={ rows } />
  171. <WBPagination activePage={ page } numPages={ numPages }
  172. getPageUrl={ page => this.getUrl({ 'page': page }) } />
  173. </div>
  174. ) }
  175. </div>
  176. );
  177. }
  178. }
  179. WBCollectionContent.defaultProps = {
  180. 'collectionPath': '',
  181. 'page': 0,
  182. 'itemsPerPage': 20
  183. };
  184. export default WBCollectionContent;