public.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2014
  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. /* global FileActions, Files, FileList */
  11. /* global dragOptions, folderDropOptions */
  12. if (!OCA.Sharing) {
  13. OCA.Sharing = {};
  14. }
  15. if (!OCA.Files) {
  16. OCA.Files = {};
  17. }
  18. /**
  19. * @namespace
  20. */
  21. OCA.Sharing.PublicApp = {
  22. _initialized: false,
  23. /**
  24. * Initializes the public share app.
  25. *
  26. * @param $el container
  27. */
  28. initialize: function ($el) {
  29. var self = this;
  30. var fileActions;
  31. if (this._initialized) {
  32. return;
  33. }
  34. fileActions = new OCA.Files.FileActions();
  35. // default actions
  36. fileActions.registerDefaultActions();
  37. // legacy actions
  38. fileActions.merge(window.FileActions);
  39. // regular actions
  40. fileActions.merge(OCA.Files.fileActions);
  41. // in case apps would decide to register file actions later,
  42. // replace the global object with this one
  43. OCA.Files.fileActions = fileActions;
  44. this._initialized = true;
  45. this.initialDir = $('#dir').val();
  46. // file list mode ?
  47. if ($el.find('#filestable').length) {
  48. this.fileList = new OCA.Files.FileList(
  49. $el,
  50. {
  51. id: 'files.public',
  52. scrollContainer: $(window),
  53. dragOptions: dragOptions,
  54. folderDropOptions: folderDropOptions,
  55. fileActions: fileActions
  56. }
  57. );
  58. this.files = OCA.Files.Files;
  59. this.files.initialize();
  60. // TODO: move to PublicFileList.initialize() once
  61. // the code was split into a separate class
  62. OC.Plugins.attach('OCA.Sharing.PublicFileList', this.fileList);
  63. }
  64. var mimetype = $('#mimetype').val();
  65. var mimetypeIcon = $('#mimetypeIcon').val();
  66. mimetypeIcon = mimetypeIcon.substring(0, mimetypeIcon.length - 3);
  67. mimetypeIcon = mimetypeIcon + 'svg';
  68. var previewSupported = $('#previewSupported').val();
  69. if (typeof FileActions !== 'undefined') {
  70. // Show file preview if previewer is available, images are already handled by the template
  71. if (mimetype.substr(0, mimetype.indexOf('/')) !== 'image' && $('.publicpreview').length === 0) {
  72. // Trigger default action if not download TODO
  73. var action = FileActions.getDefault(mimetype, 'file', OC.PERMISSION_READ);
  74. if (typeof action !== 'undefined') {
  75. action($('#filename').val());
  76. }
  77. }
  78. }
  79. // dynamically load image previews
  80. var params = {
  81. x: $(document).width() * window.devicePixelRatio,
  82. y: $(document).height() * window.devicePixelRatio,
  83. a: 'true',
  84. file: encodeURIComponent(this.initialDir + $('#filename').val()),
  85. t: $('#sharingToken').val(),
  86. scalingup: 0
  87. };
  88. var img = $('<img class="publicpreview" alt="">');
  89. var fileSize = parseInt($('#filesize').val(), 10);
  90. var maxGifSize = parseInt($('#maxSizeAnimateGif').val(), 10);
  91. if (mimetype === 'image/gif' &&
  92. (maxGifSize === -1 || fileSize <= (maxGifSize * 1024 * 1024))) {
  93. img.attr('src', $('#downloadURL').val());
  94. img.appendTo('#imgframe');
  95. } else if (previewSupported === 'true' ||
  96. mimetype.substr(0, mimetype.indexOf('/')) === 'image' &&
  97. mimetype !== 'image/svg+xml') {
  98. img.attr('src', OC.filePath('files_sharing', 'ajax', 'publicpreview.php') + '?' + OC.buildQueryString(params));
  99. img.appendTo('#imgframe');
  100. } else if (mimetype.substr(0, mimetype.indexOf('/')) !== 'video') {
  101. img.attr('src', OC.Util.replaceSVGIcon(mimetypeIcon));
  102. img.attr('width', 128);
  103. img.appendTo('#imgframe');
  104. }
  105. if (this.fileList) {
  106. // TODO: move this to a separate PublicFileList class that extends OCA.Files.FileList (+ unit tests)
  107. this.fileList.getDownloadUrl = function (filename, dir) {
  108. if ($.isArray(filename)) {
  109. filename = JSON.stringify(filename);
  110. }
  111. var path = dir || FileList.getCurrentDirectory();
  112. var token = $('#sharingToken').val();
  113. var params = {
  114. path: path,
  115. files: filename
  116. };
  117. return OC.generateUrl('/s/'+token+'/download') + '?' + OC.buildQueryString(params);
  118. };
  119. this.fileList.getAjaxUrl = function (action, params) {
  120. params = params || {};
  121. params.t = $('#sharingToken').val();
  122. return OC.filePath('files_sharing', 'ajax', action + '.php') + '?' + OC.buildQueryString(params);
  123. };
  124. this.fileList.linkTo = function (dir) {
  125. var token = $('#sharingToken').val();
  126. var params = {
  127. dir: dir
  128. };
  129. return OC.generateUrl('/s/'+token+'') + '?' + OC.buildQueryString(params);
  130. };
  131. this.fileList.generatePreviewUrl = function (urlSpec) {
  132. urlSpec.t = $('#dirToken').val();
  133. return OC.generateUrl('/apps/files_sharing/ajax/publicpreview.php?') + $.param(urlSpec);
  134. };
  135. var file_upload_start = $('#file_upload_start');
  136. file_upload_start.on('fileuploadadd', function (e, data) {
  137. var fileDirectory = '';
  138. if (typeof data.files[0].relativePath !== 'undefined') {
  139. fileDirectory = data.files[0].relativePath;
  140. }
  141. // Add custom data to the upload handler
  142. data.formData = {
  143. requesttoken: $('#publicUploadRequestToken').val(),
  144. dirToken: $('#dirToken').val(),
  145. subdir: data.targetDir || self.fileList.getCurrentDirectory(),
  146. file_directory: fileDirectory
  147. };
  148. });
  149. // do not allow sharing from the public page
  150. delete this.fileList.fileActions.actions.all.Share;
  151. this.fileList.changeDirectory(this.initialDir || '/', false, true);
  152. // URL history handling
  153. this.fileList.$el.on('changeDirectory', _.bind(this._onDirectoryChanged, this));
  154. OC.Util.History.addOnPopStateHandler(_.bind(this._onUrlChanged, this));
  155. $('#download').click(function (e) {
  156. e.preventDefault();
  157. OC.redirect(FileList.getDownloadUrl());
  158. });
  159. }
  160. $(document).on('click', '#directLink', function () {
  161. $(this).focus();
  162. $(this).select();
  163. });
  164. $('.save-form').submit(function (event) {
  165. event.preventDefault();
  166. var remote = $(this).find('input[type="text"]').val();
  167. var token = $('#sharingToken').val();
  168. var owner = $('#save').data('owner');
  169. var name = $('#save').data('name');
  170. var isProtected = $('#save').data('protected') ? 1 : 0;
  171. OCA.Sharing.PublicApp._saveToOwnCloud(remote, token, owner, name, isProtected);
  172. });
  173. $('#save #save-button').click(function () {
  174. $(this).hide();
  175. $('.save-form').css('display', 'inline');
  176. $('#remote_address').focus();
  177. });
  178. // legacy
  179. window.FileList = this.fileList;
  180. },
  181. _onDirectoryChanged: function (e) {
  182. OC.Util.History.pushState({
  183. // arghhhh, why is this not called "dir" !?
  184. path: e.dir
  185. });
  186. },
  187. _onUrlChanged: function (params) {
  188. this.fileList.changeDirectory(params.path || params.dir, false, true);
  189. },
  190. _saveToOwnCloud: function(remote, token, owner, name, isProtected) {
  191. var location = window.location.protocol + '//' + window.location.host + OC.webroot;
  192. var url = remote + '/index.php/apps/files#' + 'remote=' + encodeURIComponent(location) // our location is the remote for the other server
  193. + "&token=" + encodeURIComponent(token) + "&owner=" + encodeURIComponent(owner) + "&name=" + encodeURIComponent(name) + "&protected=" + isProtected;
  194. if (remote.indexOf('://') > 0) {
  195. OC.redirect(url);
  196. } else {
  197. // if no protocol is specified, we automatically detect it by testing https and http
  198. // this check needs to happen on the server due to the Content Security Policy directive
  199. $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) {
  200. if (protocol !== 'http' && protocol !== 'https') {
  201. OC.dialogs.alert(t('files_sharing', 'No ownCloud installation (7 or higher) found at {remote}', {remote: remote}),
  202. t('files_sharing', 'Invalid ownCloud url'));
  203. } else {
  204. OC.redirect(protocol + '://' + url);
  205. }
  206. });
  207. }
  208. }
  209. };
  210. $(document).ready(function () {
  211. var App = OCA.Sharing.PublicApp;
  212. // defer app init, to give a chance to plugins to register file actions
  213. _.defer(function () {
  214. App.initialize($('#preview'));
  215. });
  216. if (window.Files) {
  217. // HACK: for oc-dialogs previews that depends on Files:
  218. Files.lazyLoadPreview = function (path, mime, ready, width, height, etag) {
  219. return App.fileList.lazyLoadPreview({
  220. path: path,
  221. mime: mime,
  222. callback: ready,
  223. width: width,
  224. height: height,
  225. etag: etag
  226. });
  227. };
  228. }
  229. });