1
0

fileinfomodel.js 3.5 KB

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