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.

fileinfomodel.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 {int} 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 {int} attributes.size size
  21. * @param {string} attributes.mimetype mime type
  22. * @param {string} attributes.icon icon URL
  23. * @param {int} 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. * Reloads missing properties from server and set them in the model.
  78. * @param properties array of properties to be reloaded
  79. * @return ajax call object
  80. */
  81. reloadProperties: function(properties) {
  82. if( !this._filesClient ){
  83. return;
  84. }
  85. var self = this;
  86. var deferred = $.Deferred();
  87. var targetPath = OC.joinPaths(this.get('path') + '/', this.get('name'));
  88. this._filesClient.getFileInfo(targetPath, {
  89. properties: properties
  90. })
  91. .then(function(status, data) {
  92. // the following lines should be extracted to a mapper
  93. if( properties.indexOf(OC.Files.Client.PROPERTY_GETCONTENTLENGTH) !== -1
  94. || properties.indexOf(OC.Files.Client.PROPERTY_SIZE) !== -1 ) {
  95. self.set('size', data.size);
  96. }
  97. deferred.resolve(status, data);
  98. })
  99. .fail(function(status) {
  100. OC.Notification.show(t('files', 'Could not load info for file "{file}"', {file: self.get('name')}), {type: 'error'});
  101. deferred.reject(status);
  102. });
  103. return deferred.promise();
  104. }
  105. });
  106. if (!OCA.Files) {
  107. OCA.Files = {};
  108. }
  109. OCA.Files.FileInfoModel = FileInfoModel;
  110. })(OC, OCA);