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.

shareitemmodel.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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() {
  11. if(!OC.Share) {
  12. OC.Share = {};
  13. OC.Share.Types = {};
  14. }
  15. /**
  16. * @typedef {object} OC.Share.Types.LinkShareInfo
  17. * @property {bool} isLinkShare
  18. * @property {string} token
  19. * @property {string|null} password
  20. * @property {string} link
  21. * @property {number} permissions
  22. * @property {Date} expiration
  23. * @property {number} stime share time
  24. */
  25. /**
  26. * @typedef {object} OC.Share.Types.Collection
  27. * @property {string} item_type
  28. * @property {string} path
  29. * @property {string} item_source TODO: verify
  30. */
  31. /**
  32. * @typedef {object} OC.Share.Types.Reshare
  33. * @property {string} uid_owner
  34. * @property {number} share_type
  35. * @property {string} share_with
  36. * @property {string} displayname_owner
  37. * @property {number} permissions
  38. */
  39. /**
  40. * @typedef {object} OC.Share.Types.ShareInfo
  41. * @property {number} share_type
  42. * @property {number} permissions
  43. * @property {number} file_source optional
  44. * @property {number} item_source
  45. * @property {string} token
  46. * @property {string} share_with
  47. * @property {string} share_with_displayname
  48. * @property {string} mail_send
  49. * @property {OC.Share.Types.Collection|undefined} collection
  50. * @property {Date} expiration optional?
  51. * @property {number} stime optional?
  52. */
  53. /**
  54. * @typedef {object} OC.Share.Types.ShareItemInfo
  55. * @property {OC.Share.Types.Reshare} reshare
  56. * @property {OC.Share.Types.ShareInfo[]} shares
  57. * @property {OC.Share.Types.LinkShareInfo|undefined} linkShare
  58. */
  59. /**
  60. * These properties are sometimes returned by the server as strings instead
  61. * of integers, so we need to convert them accordingly...
  62. */
  63. var SHARE_RESPONSE_INT_PROPS = [
  64. 'id', 'file_parent', 'mail_send', 'file_source', 'item_source', 'permissions',
  65. 'storage', 'share_type', 'parent', 'stime'
  66. ];
  67. /**
  68. * @class OCA.Share.ShareItemModel
  69. * @classdesc
  70. *
  71. * Represents the GUI of the share dialogue
  72. *
  73. * // FIXME: use OC Share API once #17143 is done
  74. *
  75. * // TODO: this really should be a collection of share item models instead,
  76. * where the link share is one of them
  77. */
  78. var ShareItemModel = OC.Backbone.Model.extend({
  79. initialize: function(attributes, options) {
  80. if(!_.isUndefined(options.configModel)) {
  81. this.configModel = options.configModel;
  82. }
  83. if(!_.isUndefined(options.fileInfoModel)) {
  84. /** @type {OC.Files.FileInfo} **/
  85. this.fileInfoModel = options.fileInfoModel;
  86. }
  87. _.bindAll(this, 'addShare');
  88. },
  89. defaults: {
  90. allowPublicUploadStatus: false,
  91. permissions: 0,
  92. linkShare: {}
  93. },
  94. /**
  95. * Saves the current link share information.
  96. *
  97. * This will trigger an ajax call and refetch the model afterwards.
  98. *
  99. * TODO: this should be a separate model
  100. */
  101. saveLinkShare: function(attributes, options) {
  102. var model = this;
  103. var itemType = this.get('itemType');
  104. var itemSource = this.get('itemSource');
  105. // TODO: use backbone's default value mechanism once this is a separate model
  106. var requiredAttributes = [
  107. { name: 'password', defaultValue: '' },
  108. { name: 'passwordChanged', defaultValue: false },
  109. { name: 'permissions', defaultValue: OC.PERMISSION_READ },
  110. { name: 'expiration', defaultValue: this.configModel.getDefaultExpirationDateString() }
  111. ];
  112. attributes = attributes || {};
  113. // get attributes from the model and fill in with default values
  114. _.each(requiredAttributes, function(attribute) {
  115. // a provided options overrides a present value of the link
  116. // share. If neither is given, the default value is used.
  117. if(_.isUndefined(attribute[attribute.name])) {
  118. attributes[attribute.name] = attribute.defaultValue;
  119. var currentValue = model.get('linkShare')[attribute.name];
  120. if(!_.isUndefined(currentValue)) {
  121. attributes[attribute.name] = currentValue;
  122. }
  123. }
  124. });
  125. var password = {
  126. password: attributes.password,
  127. passwordChanged: attributes.passwordChanged
  128. };
  129. OC.Share.share(
  130. itemType,
  131. itemSource,
  132. OC.Share.SHARE_TYPE_LINK,
  133. password,
  134. attributes.permissions,
  135. this.fileInfoModel.get('name'),
  136. attributes.expiration,
  137. function(result) {
  138. if (!result || result.status !== 'success') {
  139. model.fetch({
  140. success: function() {
  141. if (options && _.isFunction(options.success)) {
  142. options.success(model);
  143. }
  144. }
  145. });
  146. } else {
  147. if (options && _.isFunction(options.error)) {
  148. options.error(model);
  149. }
  150. }
  151. },
  152. function(result) {
  153. var msg = t('core', 'Error');
  154. if (result.data && result.data.message) {
  155. msg = result.data.message;
  156. }
  157. if (options && _.isFunction(options.error)) {
  158. options.error(model, msg);
  159. } else {
  160. OC.dialogs.alert(msg, t('core', 'Error while sharing'));
  161. }
  162. }
  163. );
  164. },
  165. removeLinkShare: function() {
  166. this.removeShare(OC.Share.SHARE_TYPE_LINK, '');
  167. },
  168. /**
  169. * Sets the public upload flag
  170. *
  171. * @param {bool} allow whether public upload is allowed
  172. */
  173. setPublicUpload: function(allow) {
  174. var permissions = OC.PERMISSION_READ;
  175. if(allow) {
  176. permissions = OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ;
  177. }
  178. this.get('linkShare').permissions = permissions;
  179. },
  180. /**
  181. * Sets the expiration date of the public link
  182. *
  183. * @param {string} expiration expiration date
  184. */
  185. setExpirationDate: function(expiration) {
  186. this.get('linkShare').expiration = expiration;
  187. },
  188. /**
  189. * Set password of the public link share
  190. *
  191. * @param {string} password
  192. */
  193. setPassword: function(password) {
  194. this.get('linkShare').password = password;
  195. this.get('linkShare').passwordChanged = true;
  196. },
  197. addShare: function(attributes, options) {
  198. var shareType = attributes.shareType;
  199. var shareWith = attributes.shareWith;
  200. var fileName = this.fileInfoModel.get('name');
  201. options = options || {};
  202. // Default permissions are Edit (CRUD) and Share
  203. // Check if these permissions are possible
  204. var permissions = OC.PERMISSION_READ;
  205. if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  206. permissions = OC.PERMISSION_CREATE | OC.PERMISSION_UPDATE | OC.PERMISSION_READ;
  207. } else {
  208. if (this.updatePermissionPossible()) {
  209. permissions = permissions | OC.PERMISSION_UPDATE;
  210. }
  211. if (this.createPermissionPossible()) {
  212. permissions = permissions | OC.PERMISSION_CREATE;
  213. }
  214. if (this.deletePermissionPossible()) {
  215. permissions = permissions | OC.PERMISSION_DELETE;
  216. }
  217. if (this.configModel.get('isResharingAllowed') && (this.sharePermissionPossible())) {
  218. permissions = permissions | OC.PERMISSION_SHARE;
  219. }
  220. }
  221. var model = this;
  222. var itemType = this.get('itemType');
  223. var itemSource = this.get('itemSource');
  224. OC.Share.share(itemType, itemSource, shareType, shareWith, permissions, fileName, options.expiration, function() {
  225. model.fetch();
  226. });
  227. },
  228. setPermissions: function(shareType, shareWith, permissions) {
  229. var itemType = this.get('itemType');
  230. var itemSource = this.get('itemSource');
  231. // TODO: in the future, only set the permissions on the model but don't save directly
  232. OC.Share.setPermissions(itemType, itemSource, shareType, shareWith, permissions);
  233. },
  234. removeShare: function(shareType, shareWith) {
  235. var model = this;
  236. var itemType = this.get('itemType');
  237. var itemSource = this.get('itemSource');
  238. OC.Share.unshare(itemType, itemSource, shareType, shareWith, function() {
  239. model.fetch();
  240. });
  241. },
  242. /**
  243. * @returns {boolean}
  244. */
  245. isPublicUploadAllowed: function() {
  246. return this.get('allowPublicUploadStatus');
  247. },
  248. /**
  249. * @returns {boolean}
  250. */
  251. isFolder: function() {
  252. return this.get('itemType') === 'folder';
  253. },
  254. /**
  255. * @returns {boolean}
  256. */
  257. isFile: function() {
  258. return this.get('itemType') === 'file';
  259. },
  260. /**
  261. * whether this item has reshare information
  262. * @returns {boolean}
  263. */
  264. hasReshare: function() {
  265. var reshare = this.get('reshare');
  266. return _.isObject(reshare) && !_.isUndefined(reshare.uid_owner);
  267. },
  268. /**
  269. * whether this item has user share information
  270. * @returns {boolean}
  271. */
  272. hasUserShares: function() {
  273. return this.getSharesWithCurrentItem().length > 0;
  274. },
  275. /**
  276. * Returns whether this item has a link share
  277. *
  278. * @return {bool} true if a link share exists, false otherwise
  279. */
  280. hasLinkShare: function() {
  281. var linkShare = this.get('linkShare');
  282. if (linkShare && linkShare.isLinkShare) {
  283. return true;
  284. }
  285. return false;
  286. },
  287. /**
  288. * @param {number} shareIndex
  289. * @returns {string}
  290. */
  291. getCollectionType: function(shareIndex) {
  292. /** @type OC.Share.Types.ShareInfo **/
  293. var share = this.get('shares')[shareIndex];
  294. if(!_.isObject(share)) {
  295. throw "Unknown Share";
  296. } else if(_.isUndefined(share.collection)) {
  297. throw "Share is not a collection";
  298. }
  299. return share.collection.item_type;
  300. },
  301. /**
  302. * @param {number} shareIndex
  303. * @returns {string}
  304. */
  305. getCollectionPath: function(shareIndex) {
  306. /** @type OC.Share.Types.ShareInfo **/
  307. var share = this.get('shares')[shareIndex];
  308. if(!_.isObject(share)) {
  309. throw "Unknown Share";
  310. } else if(_.isUndefined(share.collection)) {
  311. throw "Share is not a collection";
  312. }
  313. return share.collection.path;
  314. },
  315. /**
  316. * @param {number} shareIndex
  317. * @returns {string}
  318. */
  319. getCollectionSource: function(shareIndex) {
  320. /** @type OC.Share.Types.ShareInfo **/
  321. var share = this.get('shares')[shareIndex];
  322. if(!_.isObject(share)) {
  323. throw "Unknown Share";
  324. } else if(_.isUndefined(share.collection)) {
  325. throw "Share is not a collection";
  326. }
  327. return share.collection.item_source;
  328. },
  329. /**
  330. * @param {number} shareIndex
  331. * @returns {boolean}
  332. */
  333. isCollection: function(shareIndex) {
  334. /** @type OC.Share.Types.ShareInfo **/
  335. var share = this.get('shares')[shareIndex];
  336. if(!_.isObject(share)) {
  337. throw "Unknown Share";
  338. }
  339. if(_.isUndefined(share.collection)) {
  340. return false;
  341. }
  342. return true;
  343. },
  344. /**
  345. * @returns {string}
  346. */
  347. getReshareOwner: function() {
  348. return this.get('reshare').uid_owner;
  349. },
  350. /**
  351. * @returns {string}
  352. */
  353. getReshareOwnerDisplayname: function() {
  354. return this.get('reshare').displayname_owner;
  355. },
  356. /**
  357. * @returns {string}
  358. */
  359. getReshareWith: function() {
  360. return this.get('reshare').share_with;
  361. },
  362. /**
  363. * @returns {number}
  364. */
  365. getReshareType: function() {
  366. return this.get('reshare').share_type;
  367. },
  368. /**
  369. * Returns all share entries that only apply to the current item
  370. * (file/folder)
  371. *
  372. * @return {Array.<OC.Share.Types.ShareInfo>}
  373. */
  374. getSharesWithCurrentItem: function() {
  375. var shares = this.get('shares') || [];
  376. var fileId = this.fileInfoModel.get('id');
  377. return _.filter(shares, function(share) {
  378. return share.item_source === fileId;
  379. });
  380. },
  381. /**
  382. * @param shareIndex
  383. * @returns {string}
  384. */
  385. getShareWith: function(shareIndex) {
  386. /** @type OC.Share.Types.ShareInfo **/
  387. var share = this.get('shares')[shareIndex];
  388. if(!_.isObject(share)) {
  389. throw "Unknown Share";
  390. }
  391. return share.share_with;
  392. },
  393. /**
  394. * @param shareIndex
  395. * @returns {string}
  396. */
  397. getShareWithDisplayName: function(shareIndex) {
  398. /** @type OC.Share.Types.ShareInfo **/
  399. var share = this.get('shares')[shareIndex];
  400. if(!_.isObject(share)) {
  401. throw "Unknown Share";
  402. }
  403. return share.share_with_displayname;
  404. },
  405. getShareType: function(shareIndex) {
  406. /** @type OC.Share.Types.ShareInfo **/
  407. var share = this.get('shares')[shareIndex];
  408. if(!_.isObject(share)) {
  409. throw "Unknown Share";
  410. }
  411. return share.share_type;
  412. },
  413. /**
  414. * whether a share from shares has the requested permission
  415. *
  416. * @param {number} shareIndex
  417. * @param {number} permission
  418. * @returns {boolean}
  419. * @private
  420. */
  421. _shareHasPermission: function(shareIndex, permission) {
  422. /** @type OC.Share.Types.ShareInfo **/
  423. var share = this.get('shares')[shareIndex];
  424. if(!_.isObject(share)) {
  425. throw "Unknown Share";
  426. }
  427. if( share.share_type === OC.Share.SHARE_TYPE_REMOTE
  428. && ( permission === OC.PERMISSION_SHARE
  429. || permission === OC.PERMISSION_DELETE))
  430. {
  431. return false;
  432. }
  433. return (share.permissions & permission) === permission;
  434. },
  435. notificationMailWasSent: function(shareIndex) {
  436. /** @type OC.Share.Types.ShareInfo **/
  437. var share = this.get('shares')[shareIndex];
  438. if(!_.isObject(share)) {
  439. throw "Unknown Share";
  440. }
  441. return share.mail_send === 1;
  442. },
  443. /**
  444. * Sends an email notification for the given share
  445. *
  446. * @param {int} shareType share type
  447. * @param {string} shareWith recipient
  448. * @param {bool} state whether to set the notification flag or remove it
  449. */
  450. sendNotificationForShare: function(shareType, shareWith, state) {
  451. var itemType = this.get('itemType');
  452. var itemSource = this.get('itemSource');
  453. return $.post(
  454. OC.generateUrl('core/ajax/share.php'),
  455. {
  456. action: state ? 'informRecipients' : 'informRecipientsDisabled',
  457. recipient: shareWith,
  458. shareType: shareType,
  459. itemSource: itemSource,
  460. itemType: itemType
  461. },
  462. function(result) {
  463. if (result.status !== 'success') {
  464. // FIXME: a model should not show dialogs
  465. OC.dialogs.alert(t('core', result.data.message), t('core', 'Warning'));
  466. }
  467. }
  468. );
  469. },
  470. /**
  471. * Send the link share information by email
  472. *
  473. * @param {string} recipientEmail recipient email address
  474. */
  475. sendEmailPrivateLink: function(recipientEmail) {
  476. var deferred = $.Deferred();
  477. var itemType = this.get('itemType');
  478. var itemSource = this.get('itemSource');
  479. var linkShare = this.get('linkShare');
  480. $.post(
  481. OC.generateUrl('core/ajax/share.php'), {
  482. action: 'email',
  483. toaddress: recipientEmail,
  484. link: linkShare.link,
  485. itemType: itemType,
  486. itemSource: itemSource,
  487. file: this.fileInfoModel.get('name'),
  488. expiration: linkShare.expiration || ''
  489. },
  490. function(result) {
  491. if (!result || result.status !== 'success') {
  492. // FIXME: a model should not show dialogs
  493. OC.dialogs.alert(result.data.message, t('core', 'Error while sending notification'));
  494. deferred.reject();
  495. } else {
  496. deferred.resolve();
  497. }
  498. });
  499. return deferred.promise();
  500. },
  501. /**
  502. * @returns {boolean}
  503. */
  504. sharePermissionPossible: function() {
  505. return (this.get('permissions') & OC.PERMISSION_SHARE) === OC.PERMISSION_SHARE;
  506. },
  507. /**
  508. * @param {number} shareIndex
  509. * @returns {boolean}
  510. */
  511. hasSharePermission: function(shareIndex) {
  512. return this._shareHasPermission(shareIndex, OC.PERMISSION_SHARE);
  513. },
  514. /**
  515. * @returns {boolean}
  516. */
  517. createPermissionPossible: function() {
  518. return (this.get('permissions') & OC.PERMISSION_CREATE) === OC.PERMISSION_CREATE;
  519. },
  520. /**
  521. * @param {number} shareIndex
  522. * @returns {boolean}
  523. */
  524. hasCreatePermission: function(shareIndex) {
  525. return this._shareHasPermission(shareIndex, OC.PERMISSION_CREATE);
  526. },
  527. /**
  528. * @returns {boolean}
  529. */
  530. updatePermissionPossible: function() {
  531. return (this.get('permissions') & OC.PERMISSION_UPDATE) === OC.PERMISSION_UPDATE;
  532. },
  533. /**
  534. * @param {number} shareIndex
  535. * @returns {boolean}
  536. */
  537. hasUpdatePermission: function(shareIndex) {
  538. return this._shareHasPermission(shareIndex, OC.PERMISSION_UPDATE);
  539. },
  540. /**
  541. * @returns {boolean}
  542. */
  543. deletePermissionPossible: function() {
  544. return (this.get('permissions') & OC.PERMISSION_DELETE) === OC.PERMISSION_DELETE;
  545. },
  546. /**
  547. * @param {number} shareIndex
  548. * @returns {boolean}
  549. */
  550. hasDeletePermission: function(shareIndex) {
  551. return this._shareHasPermission(shareIndex, OC.PERMISSION_DELETE);
  552. },
  553. /**
  554. * @returns {boolean}
  555. */
  556. editPermissionPossible: function() {
  557. return this.createPermissionPossible()
  558. || this.updatePermissionPossible()
  559. || this.deletePermissionPossible();
  560. },
  561. /**
  562. * @returns {boolean}
  563. */
  564. hasEditPermission: function(shareIndex) {
  565. return this.hasCreatePermission(shareIndex)
  566. || this.hasUpdatePermission(shareIndex)
  567. || this.hasDeletePermission(shareIndex);
  568. },
  569. fetch: function() {
  570. var model = this;
  571. this.trigger('request', this);
  572. OC.Share.loadItem(this.get('itemType'), this.get('itemSource'), function(data) {
  573. model.trigger('sync', 'GET', this);
  574. model.set(model.parse(data));
  575. });
  576. },
  577. /**
  578. * Updates OC.Share.itemShares and OC.Share.statuses.
  579. *
  580. * This is required in case the user navigates away and comes back,
  581. * the share statuses from the old arrays are still used to fill in the icons
  582. * in the file list.
  583. */
  584. _legacyFillCurrentShares: function(shares) {
  585. var fileId = this.fileInfoModel.get('id');
  586. if (!shares || !shares.length) {
  587. delete OC.Share.statuses[fileId];
  588. OC.Share.currentShares = {};
  589. OC.Share.itemShares = [];
  590. return;
  591. }
  592. var currentShareStatus = OC.Share.statuses[fileId];
  593. if (!currentShareStatus) {
  594. currentShareStatus = {link: false};
  595. OC.Share.statuses[fileId] = currentShareStatus;
  596. }
  597. currentShareStatus.link = false;
  598. OC.Share.currentShares = {};
  599. OC.Share.itemShares = [];
  600. _.each(shares,
  601. /**
  602. * @param {OC.Share.Types.ShareInfo} share
  603. */
  604. function(share) {
  605. if (share.share_type === OC.Share.SHARE_TYPE_LINK) {
  606. OC.Share.itemShares[share.share_type] = true;
  607. currentShareStatus.link = true;
  608. } else {
  609. if (!OC.Share.itemShares[share.share_type]) {
  610. OC.Share.itemShares[share.share_type] = [];
  611. }
  612. OC.Share.itemShares[share.share_type].push(share.share_with);
  613. }
  614. }
  615. );
  616. },
  617. parse: function(data) {
  618. if(data === false) {
  619. console.warn('no data was returned');
  620. trigger('fetchError');
  621. return {};
  622. }
  623. var permissions = this.get('possiblePermissions');
  624. if(!_.isUndefined(data.reshare) && !_.isUndefined(data.reshare.permissions) && data.reshare.uid_owner !== OC.currentUser) {
  625. permissions = permissions & data.reshare.permissions;
  626. }
  627. var allowPublicUploadStatus = false;
  628. if(!_.isUndefined(data.shares)) {
  629. $.each(data.shares, function (key, value) {
  630. if (value.share_type === OC.Share.SHARE_TYPE_LINK) {
  631. allowPublicUploadStatus = (value.permissions & OC.PERMISSION_CREATE) ? true : false;
  632. return true;
  633. }
  634. });
  635. }
  636. /** @type {OC.Share.Types.ShareInfo[]} **/
  637. var shares = _.map(data.shares, function(share) {
  638. // properly parse some values because sometimes the server
  639. // returns integers as string...
  640. var i;
  641. for (i = 0; i < SHARE_RESPONSE_INT_PROPS.length; i++) {
  642. var prop = SHARE_RESPONSE_INT_PROPS[i];
  643. if (!_.isUndefined(share[prop])) {
  644. share[prop] = parseInt(share[prop], 10);
  645. }
  646. }
  647. return share;
  648. });
  649. this._legacyFillCurrentShares(shares);
  650. var linkShare = { isLinkShare: false };
  651. // filter out the share by link
  652. shares = _.reject(shares,
  653. /**
  654. * @param {OC.Share.Types.ShareInfo} share
  655. */
  656. function(share) {
  657. var isShareLink =
  658. share.share_type === OC.Share.SHARE_TYPE_LINK
  659. && ( share.file_source === this.get('itemSource')
  660. || share.item_source === this.get('itemSource'));
  661. if (isShareLink) {
  662. var link = window.location.protocol + '//' + window.location.host;
  663. if (!share.token) {
  664. // pre-token link
  665. var fullPath = this.fileInfoModel.get('path') + '/' +
  666. this.fileInfoModel.get('name');
  667. var location = '/' + OC.currentUser + '/files' + fullPath;
  668. var type = this.fileInfoModel.isDirectory() ? 'folder' : 'file';
  669. link += OC.linkTo('', 'public.php') + '?service=files&' +
  670. type + '=' + encodeURIComponent(location);
  671. } else {
  672. link += OC.generateUrl('/s/') + share.token;
  673. }
  674. linkShare = {
  675. isLinkShare: true,
  676. token: share.token,
  677. password: share.share_with,
  678. link: link,
  679. permissions: share.permissions,
  680. // currently expiration is only effective for link shares.
  681. expiration: share.expiration,
  682. stime: share.stime
  683. };
  684. return share;
  685. }
  686. },
  687. this
  688. );
  689. return {
  690. reshare: data.reshare,
  691. shares: shares,
  692. linkShare: linkShare,
  693. permissions: permissions,
  694. allowPublicUploadStatus: allowPublicUploadStatus
  695. };
  696. },
  697. /**
  698. * Parses a string to an valid integer (unix timestamp)
  699. * @param time
  700. * @returns {*}
  701. * @internal Only used to work around a bug in the backend
  702. */
  703. _parseTime: function(time) {
  704. if (_.isString(time)) {
  705. // skip empty strings and hex values
  706. if (time === '' || (time.length > 1 && time[0] === '0' && time[1] === 'x')) {
  707. return null;
  708. }
  709. time = parseInt(time, 10);
  710. if(isNaN(time)) {
  711. time = null;
  712. }
  713. }
  714. return time;
  715. }
  716. });
  717. OC.Share.ShareItemModel = ShareItemModel;
  718. })();