Source: services/FileService.js

  1. /**
  2. * @ngdoc service
  3. * @name FileService
  4. * @module s4c.services.FileService
  5. *
  6. * @description Componente para acesso a api do backend e/ou comunicação entre controllers
  7. *
  8. *
  9. */
  10. (function () {
  11. angular.module('s4c.services')
  12. .factory('FileService', FileService);
  13. FileService.$inject = [
  14. 'Base',
  15. '$q',
  16. '$http',
  17. 'API_ENDPOINT'
  18. ]
  19. /**
  20. * @method FileService
  21. * @param {*} $http
  22. * @param {*} $q
  23. * @param {*} Base
  24. * @param {*} API_ENDPOINT
  25. */
  26. function FileService(Base, $q, $http, API_ENDPOINT) {
  27. var service = {
  28. downloadFile: downloadFile,
  29. addFile: addFile,
  30. deleteFile: deleteFile
  31. };
  32. /**
  33. * @method addFile
  34. * @param {*} tipo
  35. * @param {*} arquivo
  36. */
  37. function addFile(tipo, arquivo) {
  38. return Base.salvar('filemanager/addarquivo/' + tipo, arquivo);
  39. }
  40. /**
  41. * @method downloadFile
  42. * @param {*} id
  43. */
  44. function downloadFile(id) {
  45. var deferred = $q.defer();
  46. $http
  47. .get(API_ENDPOINT + 'filemanager/' + id, { responseType: 'arraybuffer' })
  48. .then(function (res) {
  49. deferred.resolve(res.data);
  50. }, function (err) {
  51. deferred.reject(err);
  52. });
  53. return deferred.promise;
  54. }
  55. /**
  56. * @method deleteFile
  57. * @param {*} id
  58. */
  59. function deleteFile(id) {
  60. return Base.remover('filemanager/' + id);
  61. }
  62. return service;
  63. }
  64. }());