UploadFileAction.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Content Explorer
  5. *| (C) Copyright IBM Corp. 2015, 2018
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or disclosure
  8. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *+------------------------------------------------------------------------+
  10. */
  11. import { GlassContextHelper } from '@businessanalytics/content-nav';
  12. import AjaxService from '../services/AjaxService';
  13. export class UploadFileAction {
  14. constructor(options) {
  15. const ajaxOptions = {
  16. glassContext: options.glassContext
  17. };
  18. this.ajaxService = new AjaxService(ajaxOptions);
  19. }
  20. getFileUploader = (glassContext) => {
  21. let fUploaderPromise = glassContext.getSvc('.FileUpload')
  22. .then((fileUploader) => {
  23. return fileUploader;
  24. })
  25. .catch((err) => {
  26. glassContext.appController.showErrorMessage(err);
  27. return null;
  28. });
  29. return fUploaderPromise;
  30. }
  31. getSearchPath = (parentObj, glassContext, ancestors) => {
  32. const targetFolder = ancestors[ancestors.length -1];
  33. const url = targetFolder._meta.links.self.url;
  34. const data = {
  35. 'fields': 'searchPath'
  36. };
  37. return this.ajaxService.getData(url, data)
  38. .then((response) => {
  39. parentObj = response.data.data[0];
  40. if ((url.indexOf('.my_folders') > -1)) {
  41. parentObj.searchPath = null;
  42. }
  43. })
  44. .catch((err) => {
  45. GlassContextHelper.showAjaxServiceError(glassContext, err);
  46. return Promise.reject(err);
  47. });
  48. }
  49. onFileUpload = (parentObj, glassContext, ancestors) => {
  50. this.getFileUploader(glassContext)
  51. .then(fileUploader => {
  52. var fileDestination;
  53. if (!parentObj || !parentObj.searchPath) {
  54. this.getSearchPath(parentObj, glassContext, ancestors)
  55. .then(() => {
  56. fileDestination = parentObj.searchPath || '.my_folders';
  57. fileUploader.showFilePicker(null, null, null, { destination: fileDestination });
  58. });
  59. } else {
  60. fileDestination = parentObj.searchPath || '.my_folders';
  61. fileUploader.showFilePicker(null, null, null, { destination: fileDestination });
  62. }
  63. });
  64. }
  65. }