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.

public.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright (c) 2014
  3. * @copyright Copyright (c) 2016, Björn Schießle <bjoern@schiessle.org>
  4. *
  5. * This file is licensed under the Affero General Public License version 3
  6. * or later.
  7. *
  8. * See the COPYING-README file.
  9. *
  10. */
  11. /* global FileActions, Files, FileList */
  12. /* global dragOptions, folderDropOptions */
  13. if (!OCA.Sharing) {
  14. OCA.Sharing = {};
  15. }
  16. if (!OCA.Files) {
  17. OCA.Files = {};
  18. }
  19. /**
  20. * @namespace
  21. */
  22. OCA.Sharing.PublicApp = {
  23. _initialized: false,
  24. /**
  25. * Initializes the public share app.
  26. *
  27. * @param $el container
  28. */
  29. initialize: function ($el) {
  30. var self = this;
  31. var fileActions;
  32. if (this._initialized) {
  33. return;
  34. }
  35. fileActions = new OCA.Files.FileActions();
  36. // default actions
  37. fileActions.registerDefaultActions();
  38. // regular actions
  39. fileActions.merge(OCA.Files.fileActions);
  40. // in case apps would decide to register file actions later,
  41. // replace the global object with this one
  42. OCA.Files.fileActions = fileActions;
  43. this._initialized = true;
  44. var urlParams = OC.Util.History.parseUrlQuery();
  45. this.initialDir = urlParams.path || '/';
  46. var token = $('#sharingToken').val();
  47. var hideDownload = $('#hideDownload').val();
  48. // Prevent all right-click options if hideDownload is enabled
  49. if (hideDownload === 'true') {
  50. window.oncontextmenu = function(event) {
  51. event.preventDefault();
  52. event.stopPropagation();
  53. return false;
  54. };
  55. }
  56. // file list mode ?
  57. if ($el.find('.files-filestable').length) {
  58. // Toggle for grid view
  59. this.$showGridView = $('input#showgridview');
  60. this.$showGridView.on('change', _.bind(this._onGridviewChange, this));
  61. var filesClient = new OC.Files.Client({
  62. host: OC.getHost(),
  63. port: OC.getPort(),
  64. // note: password not be required, the endpoint
  65. // will recognize previous validation from the session
  66. root: OC.getRootPath() + '/public.php/dav/files/' + token + '/',
  67. useHTTPS: OC.getProtocol() === 'https'
  68. });
  69. this.fileList = new OCA.Files.FileList(
  70. $el,
  71. {
  72. id: 'files.public',
  73. dragOptions: dragOptions,
  74. folderDropOptions: folderDropOptions,
  75. fileActions: fileActions,
  76. detailsViewEnabled: false,
  77. filesClient: filesClient,
  78. enableUpload: true,
  79. multiSelectMenu: [
  80. {
  81. name: 'copyMove',
  82. displayName: t('files', 'Move or copy'),
  83. iconClass: 'icon-external',
  84. },
  85. {
  86. name: 'download',
  87. displayName: t('files', 'Download'),
  88. iconClass: 'icon-download',
  89. },
  90. {
  91. name: 'delete',
  92. displayName: t('files', 'Delete'),
  93. iconClass: 'icon-delete',
  94. }
  95. ]
  96. }
  97. );
  98. if (hideDownload === 'true') {
  99. this.fileList._allowSelection = false;
  100. }
  101. this.files = OCA.Files.Files;
  102. this.files.initialize();
  103. // TODO: move to PublicFileList.initialize() once
  104. // the code was split into a separate class
  105. OC.Plugins.attach('OCA.Sharing.PublicFileList', this.fileList);
  106. }
  107. var mimetype = $('#mimetype').val();
  108. var mimetypeIcon = $('#mimetypeIcon').val();
  109. mimetypeIcon = mimetypeIcon.substring(0, mimetypeIcon.length - 3);
  110. mimetypeIcon = mimetypeIcon + 'svg';
  111. var previewSupported = $('#previewSupported').val();
  112. if (typeof FileActions !== 'undefined') {
  113. // Show file preview if previewer is available, images are already handled by the template
  114. if (mimetype.substr(0, mimetype.indexOf('/')) !== 'image' && $('.publicpreview').length === 0) {
  115. // Trigger default action if not download TODO
  116. var spec = FileActions.getDefaultFileAction(mimetype, 'file', OC.PERMISSION_READ);
  117. if (spec && spec.action) {
  118. spec.action($('#filename').val());
  119. }
  120. }
  121. }
  122. // dynamically load image previews
  123. var bottomMargin = 350;
  124. var previewWidth = $(window).width();
  125. var previewHeight = $(window).height() - bottomMargin;
  126. previewHeight = Math.max(200, previewHeight);
  127. var params = {
  128. x: Math.ceil(previewWidth * window.devicePixelRatio),
  129. y: Math.ceil(previewHeight * window.devicePixelRatio),
  130. a: 'true',
  131. file: encodeURIComponent(this.initialDir + $('#filename').val()),
  132. scalingup: 0
  133. };
  134. var imgcontainer = $('<img class="publicpreview" alt="">');
  135. if (hideDownload === 'false') {
  136. imgcontainer = $('<a href="' + $('#previewURL').val() + '" target="_blank"></a>').append(imgcontainer);
  137. }
  138. var img = imgcontainer.hasClass('publicpreview')? imgcontainer: imgcontainer.find('.publicpreview');
  139. img.css({
  140. 'max-width': previewWidth,
  141. 'max-height': previewHeight
  142. });
  143. if (OCA.Viewer && OCA.Viewer.mimetypes.includes(mimetype)
  144. && (mimetype.startsWith('image/') || mimetype.startsWith('video/') || mimetype.startsWith('audio'))) {
  145. OCA.Viewer.setRootElement('#imgframe')
  146. OCA.Viewer.open({ path: '/' })
  147. } else if (mimetype.substr(0, mimetype.indexOf('/')) === 'text' && window.btoa) {
  148. if (OC.appswebroots['files_texteditor'] !== undefined ||
  149. OC.appswebroots['text'] !== undefined) {
  150. // the text editor handles the previewing
  151. return;
  152. }
  153. // Undocumented Url to public WebDAV endpoint
  154. var url = parent.location.protocol + '//' + location.host + OC.linkTo('', 'public.php/dav/files/'+ token);
  155. $.ajax({
  156. url: url,
  157. headers: {
  158. Range: 'bytes=0-10000'
  159. }
  160. }).then(function (data) {
  161. self._showTextPreview(data, previewHeight);
  162. });
  163. } else if ((previewSupported === 'true' && mimetype.substr(0, mimetype.indexOf('/')) !== 'video') ||
  164. mimetype.substr(0, mimetype.indexOf('/')) === 'image' &&
  165. mimetype !== 'image/svg+xml') {
  166. img.attr('src', OC.generateUrl('/apps/files_sharing/publicpreview/' + token + '?' + OC.buildQueryString(params)));
  167. imgcontainer.appendTo('#imgframe');
  168. } else if (mimetype.substr(0, mimetype.indexOf('/')) !== 'video') {
  169. img.attr('src', mimetypeIcon);
  170. img.attr('width', 128);
  171. // "#imgframe" is either empty or it contains an audio preview that
  172. // the icon should appear before, so the container should be
  173. // prepended to the frame.
  174. imgcontainer.prependTo('#imgframe');
  175. } else if (previewSupported === 'true') {
  176. $('#imgframe > video').attr('poster', OC.generateUrl('/apps/files_sharing/publicpreview/' + token + '?' + OC.buildQueryString(params)));
  177. }
  178. if (this.fileList) {
  179. // TODO: move this to a separate PublicFileList class that extends OCA.Files.FileList (+ unit tests)
  180. this.fileList.getDownloadUrl = function (filename, dir, isDir) {
  181. var path = dir || this.getCurrentDirectory();
  182. if (_.isArray(filename)) {
  183. filename = JSON.stringify(filename);
  184. }
  185. var params = {
  186. path: path
  187. };
  188. if (filename) {
  189. params.files = filename;
  190. }
  191. return OC.generateUrl('/s/' + token + '/download') + '?' + OC.buildQueryString(params);
  192. };
  193. this.fileList._createRow = function(fileData) {
  194. var $tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments);
  195. if (hideDownload === 'true') {
  196. this.fileActions.currentFile = $tr.find('td');
  197. // Remove the link. This means that files without a default action fail hard
  198. $tr.find('a.name').attr('href', '#');
  199. delete this.fileActions.actions.all.Download;
  200. }
  201. return $tr;
  202. };
  203. this.fileList.isSelectedDownloadable = function () {
  204. return hideDownload !== 'true';
  205. };
  206. this.fileList.getUploadUrl = function(fileName, dir) {
  207. if (_.isUndefined(dir)) {
  208. dir = this.getCurrentDirectory();
  209. }
  210. var pathSections = dir.split('/');
  211. if (!_.isUndefined(fileName)) {
  212. pathSections.push(fileName);
  213. }
  214. var encodedPath = '';
  215. _.each(pathSections, function(section) {
  216. if (section !== '') {
  217. encodedPath += '/' + encodeURIComponent(section);
  218. }
  219. });
  220. var base = '';
  221. if (!this._uploader.isXHRUpload()) {
  222. // also add auth in URL due to POST workaround
  223. base = OC.getProtocol() + '://' + token + '@' + OC.getHost() + (OC.getPort() ? ':' + OC.getPort() : '');
  224. }
  225. // encodedPath starts with a leading slash
  226. return base + OC.getRootPath() + '/public.php/dav/files/' + token + encodedPath;
  227. };
  228. this.fileList.getAjaxUrl = function (action, params) {
  229. params = params || {};
  230. params.t = token;
  231. return OC.filePath('files_sharing', 'ajax', action + '.php') + '?' + OC.buildQueryString(params);
  232. };
  233. this.fileList.linkTo = function (dir) {
  234. return OC.generateUrl('/s/' + token + '') + '?' + OC.buildQueryString({path: dir});
  235. };
  236. this.fileList.generatePreviewUrl = function (urlSpec) {
  237. urlSpec = urlSpec || {};
  238. if (!urlSpec.x) {
  239. urlSpec.x = this.$table.data('preview-x') || 250;
  240. }
  241. if (!urlSpec.y) {
  242. urlSpec.y = this.$table.data('preview-y') || 250;
  243. }
  244. urlSpec.x *= window.devicePixelRatio;
  245. urlSpec.y *= window.devicePixelRatio;
  246. urlSpec.x = Math.ceil(urlSpec.x);
  247. urlSpec.y = Math.ceil(urlSpec.y);
  248. var token = $('#dirToken').val();
  249. return OC.generateUrl('/apps/files_sharing/publicpreview/' + token + '?' + OC.buildQueryString(urlSpec));
  250. };
  251. this.fileList.updateEmptyContent = function() {
  252. this.$el.find('.emptycontent .uploadmessage').text(
  253. t('files_sharing', 'You can upload into this folder')
  254. );
  255. OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments);
  256. };
  257. this.fileList._uploader.on('fileuploadadd', function(e, data) {
  258. if (!data.headers) {
  259. data.headers = {};
  260. }
  261. data.headers.Authorization = 'Basic ' + btoa(token + ':');
  262. });
  263. // do not allow sharing from the public page
  264. delete this.fileList.fileActions.actions.all.Share;
  265. this.fileList.changeDirectory(this.initialDir || '/', false, true);
  266. // URL history handling
  267. this.fileList.$el.on('changeDirectory', _.bind(this._onDirectoryChanged, this));
  268. OC.Util.History.addOnPopStateHandler(_.bind(this._onUrlChanged, this));
  269. $('#download').click(function (e) {
  270. e.preventDefault();
  271. OC.redirect(FileList.getDownloadUrl());
  272. });
  273. if (hideDownload === 'true') {
  274. this.fileList.$el.find('.summary').find('td:first-child').remove();
  275. }
  276. }
  277. $(document).on('click', '#directLink', function () {
  278. $(this).focus();
  279. $(this).select();
  280. });
  281. $('.save-form').submit(function (event) {
  282. event.preventDefault();
  283. var remote = $(this).find('#remote_address').val();
  284. var token = $('#sharingToken').val();
  285. var owner = $('#save-external-share').data('owner');
  286. var ownerDisplayName = $('#save-external-share').data('owner-display-name');
  287. var name = $('#save-external-share').data('name');
  288. var isProtected = $('#save-external-share').data('protected') ? 1 : 0;
  289. OCA.Sharing.PublicApp._createFederatedShare(remote, token, owner, ownerDisplayName, name, isProtected);
  290. });
  291. $('#remote_address').on("keyup paste", function() {
  292. if ($(this).val() === '' || $('#save-external-share > .icon.icon-loading-small').length > 0) {
  293. $('#save-button-confirm').prop('disabled', true);
  294. } else {
  295. $('#save-button-confirm').prop('disabled', false);
  296. }
  297. });
  298. self._bindShowTermsAction();
  299. // legacy
  300. window.FileList = this.fileList;
  301. },
  302. /**
  303. * Binds the click action for the "terms of service" action.
  304. * Shows an OC info dialog on click.
  305. *
  306. * @private
  307. */
  308. _bindShowTermsAction: function() {
  309. $('#show-terms-dialog').on('click', function() {
  310. OC.dialogs.info($('#disclaimerText').val(), t('files_sharing', 'Terms of service'));
  311. });
  312. },
  313. _showTextPreview: function (data, previewHeight) {
  314. var textDiv = $('<div></div>').addClass('text-preview');
  315. textDiv.text(data);
  316. textDiv.appendTo('#imgframe');
  317. var divHeight = textDiv.height();
  318. if (data.length > 999) {
  319. var ellipsis = $('<div></div>').addClass('ellipsis');
  320. ellipsis.html('(&#133;)');
  321. ellipsis.appendTo('#imgframe');
  322. }
  323. if (divHeight > previewHeight) {
  324. textDiv.height(previewHeight);
  325. }
  326. },
  327. /**
  328. * Toggle showing gridview by default or not
  329. *
  330. * @returns {undefined}
  331. */
  332. _onGridviewChange: function() {
  333. const isGridView = this.$showGridView.is(':checked');
  334. this.$showGridView.next('#view-toggle')
  335. .removeClass('icon-toggle-filelist icon-toggle-pictures')
  336. .addClass(isGridView ? 'icon-toggle-filelist' : 'icon-toggle-pictures')
  337. this.$showGridView.next('#view-toggle').attr(
  338. 'title',
  339. isGridView ? t('files', 'Show list view') : t('files', 'Show grid view'),
  340. )
  341. if (this.fileList) {
  342. this.fileList.setGridView(isGridView);
  343. }
  344. },
  345. _onDirectoryChanged: function (e) {
  346. OC.Util.History.pushState({
  347. // arghhhh, why is this not called "dir" !?
  348. path: e.dir
  349. });
  350. },
  351. _onUrlChanged: function (params) {
  352. this.fileList.changeDirectory(params.path || params.dir, false, true);
  353. },
  354. /**
  355. * fall back to old behaviour where we redirect the user to his server to mount
  356. * the public link instead of creating a dedicated federated share
  357. *
  358. * @param {any} remote -
  359. * @param {any} token -
  360. * @param {any} owner -
  361. * @param {any} ownerDisplayName -
  362. * @param {any} name -
  363. * @param {any} isProtected -
  364. * @private
  365. */
  366. _legacyCreateFederatedShare: function (remote, token, owner, ownerDisplayName, name, isProtected) {
  367. var self = this;
  368. var location = window.location.protocol + '//' + window.location.host + OC.getRootPath();
  369. if(remote.substr(-1) !== '/') {
  370. remote += '/'
  371. }
  372. var url = remote + 'index.php/apps/files#' + 'remote=' + encodeURIComponent(location) // our location is the remote for the other server
  373. + "&token=" + encodeURIComponent(token) + "&owner=" + encodeURIComponent(owner) +"&ownerDisplayName=" + encodeURIComponent(ownerDisplayName) + "&name=" + encodeURIComponent(name) + "&protected=" + isProtected;
  374. if (remote.indexOf('://') > 0) {
  375. OC.redirect(url);
  376. } else {
  377. // if no protocol is specified, we automatically detect it by testing https and http
  378. // this check needs to happen on the server due to the Content Security Policy directive
  379. $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) {
  380. if (protocol !== 'http' && protocol !== 'https') {
  381. self._toggleLoading();
  382. OC.dialogs.alert(t('files_sharing', 'No compatible server found at {remote}', {remote: remote}),
  383. t('files_sharing', 'Invalid server URL'));
  384. } else {
  385. OC.redirect(protocol + '://' + url);
  386. }
  387. });
  388. }
  389. },
  390. _toggleLoading: function() {
  391. var loading = $('#save-external-share > .icon.icon-loading-small').length === 0;
  392. if (loading) {
  393. $('#save-external-share > .icon-external')
  394. .removeClass("icon-external")
  395. .addClass("icon-loading-small");
  396. $('#save-external-share #save-button-confirm').prop("disabled", true);
  397. } else {
  398. $('#save-external-share > .icon-loading-small')
  399. .addClass("icon-external")
  400. .removeClass("icon-loading-small");
  401. $('#save-external-share #save-button-confirm').prop("disabled", false);
  402. }
  403. },
  404. _createFederatedShare: function (remote, token, owner, ownerDisplayName, name, isProtected) {
  405. var self = this;
  406. this._toggleLoading();
  407. if (remote.indexOf('@') === -1) {
  408. this._legacyCreateFederatedShare(remote, token, owner, ownerDisplayName, name, isProtected);
  409. return;
  410. }
  411. $.post(
  412. OC.generateUrl('/apps/federatedfilesharing/createFederatedShare'),
  413. {
  414. 'shareWith': remote,
  415. 'token': token
  416. }
  417. ).done(
  418. function (data) {
  419. var url = data.remoteUrl;
  420. if (url.indexOf('://') > 0) {
  421. OC.redirect(url);
  422. } else {
  423. OC.redirect('http://' + url);
  424. }
  425. }
  426. ).fail(
  427. function (jqXHR) {
  428. OC.dialogs.alert(JSON.parse(jqXHR.responseText).message,
  429. t('files_sharing', 'Failed to add the public link to your Nextcloud'));
  430. self._toggleLoading();
  431. }
  432. );
  433. }
  434. };
  435. window.addEventListener('DOMContentLoaded', function () {
  436. // FIXME: replace with OC.Plugins.register()
  437. if (window.TESTING) {
  438. return;
  439. }
  440. var App = OCA.Sharing.PublicApp;
  441. // defer app init, to give a chance to plugins to register file actions
  442. _.defer(function () {
  443. App.initialize($('#preview'));
  444. });
  445. if (window.Files) {
  446. // HACK: for oc-dialogs previews that depends on Files:
  447. Files.generatePreviewUrl = function (urlSpec) {
  448. return App.fileList.generatePreviewUrl(urlSpec);
  449. };
  450. }
  451. });