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.

131 lines
4.3KB

  1. import { h, Component } from 'preact';
  2. import WBTable from 'wb-table';
  3. import WBBreadcrumbs from 'wb-breadcrumbs';
  4. import { WBManifestReader } from 'wb-collection-manifest';
  5. import WBPagination from 'wb-pagination';
  6. import makeArvadosRequest from 'make-arvados-request';
  7. import wbDownloadFile from 'wb-download-file';
  8. class WBCollectionContent extends Component {
  9. constructor(...args) {
  10. super(...args);
  11. this.state.rows = [];
  12. this.state.manifestReader = null;
  13. this.state.loaded = 0;
  14. this.state.total = 0;
  15. }
  16. getUrl(params) {
  17. let res = '/collection-browse/' +
  18. ('uuid' in params ? params.uuid : this.props.uuid) + '/' +
  19. encodeURIComponent('collectionPath' in params ? params.collectionPath : this.props.collectionPath) + '/' +
  20. ('page' in params ? params.page : this.props.page);
  21. return res;
  22. }
  23. componentDidMount() {
  24. let { arvHost, arvToken } = this.props.app.state;
  25. let { uuid } = this.props;
  26. let select = [ 'manifest_text' ];
  27. let prom = makeArvadosRequest(arvHost, arvToken,
  28. '/arvados/v1/collections/' + uuid +
  29. '?select=' + encodeURIComponent(JSON.stringify(select)),
  30. { 'onProgress': e => {
  31. this.setState({ 'loaded': e.loaded, 'total': e.total });
  32. } });
  33. prom = prom.then(xhr => {
  34. this.state.manifestReader = new WBManifestReader(xhr.response.manifest_text);
  35. this.prepareRows();
  36. });
  37. }
  38. componentWillReceiveProps(nextProps) {
  39. this.props = nextProps;
  40. this.prepareRows();
  41. }
  42. prepareRows() {
  43. let { manifestReader } = this.state;
  44. let { collectionPath, page, itemsPerPage } = this.props;
  45. let { arvHost, arvToken } = this.props.app.state;
  46. //path = path.split('/');
  47. //path = [ '.' ].concat(path);
  48. let listing = manifestReader.listDirectory('.' + collectionPath)
  49. const numPages = Math.ceil(listing.length / itemsPerPage);
  50. listing = listing.slice(page * itemsPerPage,
  51. page * itemsPerPage + itemsPerPage);
  52. this.setState({
  53. 'numPages': numPages,
  54. 'rows': listing.map(item => (
  55. (item[0] === 'd') ? [
  56. (<a href={ this.getUrl({ 'collectionPath': collectionPath + '/' + item[1], 'page': 0 }) }>{ item[1] }/</a>),
  57. 'Directory',
  58. null,
  59. (<div></div>)
  60. ] : [
  61. item[1],
  62. 'File',
  63. filesize(item[2]),
  64. (<div>
  65. <button class="btn btn-outline-primary mx-1" title="Download"
  66. onclick={ () => {
  67. let prom = wbDownloadFile(arvHost, arvToken, manifestReader,
  68. '.' + collectionPath + '/' + item[1]);
  69. prom = prom.then(blocks => {
  70. const blob = new Blob(blocks);
  71. const a = document.createElement('a');
  72. a.name = item[1];
  73. a.href = window.URL.createObjectURL(blob);
  74. a.click();
  75. });
  76. } }><i class="fas fa-download"></i></button>
  77. <button class="btn btn-outline-primary mx-1" title="View"
  78. onclick={ () => {
  79. let prom = wbDownloadFile(arvHost, arvToken, manifestReader,
  80. '.' + collectionPath + '/' + item[1]);
  81. prom = prom.then(blocks => {
  82. const blob = new Blob(blocks);
  83. window.open(window.URL.createObjectURL(blob));
  84. });
  85. } }><i class="far fa-eye"></i></button>
  86. </div>)
  87. ]
  88. ))
  89. });
  90. }
  91. render({ collectionPath, page }, { manifestReader, rows, numPages, loaded, total }) {
  92. return (
  93. <div>
  94. <WBBreadcrumbs items={ ('.' + collectionPath).split('/') } />
  95. { manifestReader ? (
  96. <div>
  97. <WBTable columns={ [ 'Name', 'Type', 'Size', 'Actions' ] }
  98. rows={ rows } />
  99. <WBPagination activePage={ page } numPages={ numPages }
  100. getPageUrl={ page => this.getUrl({ 'page': page }) } />
  101. </div>
  102. ) : (
  103. <div>Downloading manifest: { filesize(loaded) }</div>
  104. ) }
  105. </div>
  106. );
  107. }
  108. }
  109. WBCollectionContent.defaultProps = {
  110. 'collectionPath': '',
  111. 'page': 0,
  112. 'itemsPerPage': 20
  113. };
  114. export default WBCollectionContent;