fileinfomodel.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. (function(OC, OCA) {
  7. /**
  8. * @class OC.Files.FileInfo
  9. * @classdesc File information
  10. *
  11. * @param {Object} attributes file data
  12. * @param {number} attributes.id file id
  13. * @param {string} attributes.name file name
  14. * @param {string} attributes.path path leading to the file,
  15. * without the file name and with a leading slash
  16. * @param {number} attributes.size size
  17. * @param {string} attributes.mimetype mime type
  18. * @param {string} attributes.icon icon URL
  19. * @param {number} attributes.permissions permissions
  20. * @param {Date} attributes.mtime modification time
  21. * @param {string} attributes.etag etag
  22. * @param {string} mountType mount type
  23. *
  24. * @since 8.2
  25. */
  26. var FileInfoModel = OC.Backbone.Model.extend({
  27. defaults: {
  28. mimetype: 'application/octet-stream',
  29. path: ''
  30. },
  31. _filesClient: null,
  32. initialize: function(data, options) {
  33. if (!_.isUndefined(data.id)) {
  34. data.id = parseInt(data.id, 10);
  35. }
  36. if( options ){
  37. if (options.filesClient) {
  38. this._filesClient = options.filesClient;
  39. }
  40. }
  41. },
  42. /**
  43. * Returns whether this file is a directory
  44. *
  45. * @return {boolean} true if this is a directory, false otherwise
  46. */
  47. isDirectory: function() {
  48. return this.get('mimetype') === 'httpd/unix-directory';
  49. },
  50. /**
  51. * Returns whether this file is an image
  52. *
  53. * @return {boolean} true if this is an image, false otherwise
  54. */
  55. isImage: function() {
  56. if (!this.has('mimetype')) {
  57. return false;
  58. }
  59. return this.get('mimetype').substr(0, 6) === 'image/'
  60. || this.get('mimetype') === 'application/postscript'
  61. || this.get('mimetype') === 'application/illustrator'
  62. || this.get('mimetype') === 'application/x-photoshop';
  63. },
  64. /**
  65. * Returns the full path to this file
  66. *
  67. * @return {string} full path
  68. */
  69. getFullPath: function() {
  70. return OC.joinPaths(this.get('path'), this.get('name'));
  71. },
  72. /**
  73. * Returns the mimetype of the file
  74. *
  75. * @return {string} mimetype
  76. */
  77. getMimeType: function() {
  78. return this.get('mimetype');
  79. },
  80. /**
  81. * Reloads missing properties from server and set them in the model.
  82. * @param properties array of properties to be reloaded
  83. * @return ajax call object
  84. */
  85. reloadProperties: function(properties) {
  86. if( !this._filesClient ){
  87. return;
  88. }
  89. var self = this;
  90. var deferred = $.Deferred();
  91. var targetPath = OC.joinPaths(this.get('path') + '/', this.get('name'));
  92. this._filesClient.getFileInfo(targetPath, {
  93. properties: properties
  94. })
  95. .then(function(status, data) {
  96. // the following lines should be extracted to a mapper
  97. if( properties.indexOf(OC.Files.Client.PROPERTY_GETCONTENTLENGTH) !== -1
  98. || properties.indexOf(OC.Files.Client.PROPERTY_SIZE) !== -1 ) {
  99. self.set('size', data.size);
  100. }
  101. deferred.resolve(status, data);
  102. })
  103. .fail(function(status) {
  104. OC.Notification.show(t('files', 'Could not load info for file "{file}"', {file: self.get('name')}), {type: 'error'});
  105. deferred.reject(status);
  106. });
  107. return deferred.promise();
  108. },
  109. canDownload: function() {
  110. for (const i in this.attributes.shareAttributes) {
  111. const attr = this.attributes.shareAttributes[i]
  112. if (attr.scope === 'permissions' && attr.key === 'download') {
  113. return attr.enabled
  114. }
  115. }
  116. return true
  117. },
  118. });
  119. if (!OCA.Files) {
  120. OCA.Files = {};
  121. }
  122. OCA.Files.FileInfoModel = FileInfoModel;
  123. })(OC, OCA);