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.

226 lines
8.1KB

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