123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- if (!OCA.Sharing) {
- OCA.Sharing = {};
- }
- if (!OCA.Files) {
- OCA.Files = {};
- }
- OCA.Sharing.PublicApp = {
- _initialized: false,
-
- initialize: function ($el) {
- var self = this;
- var fileActions;
- if (this._initialized) {
- return;
- }
- fileActions = new OCA.Files.FileActions();
-
- fileActions.registerDefaultActions();
-
- fileActions.merge(OCA.Files.fileActions);
-
-
- OCA.Files.fileActions = fileActions;
- this._initialized = true;
- var urlParams = OC.Util.History.parseUrlQuery();
- this.initialDir = urlParams.path || '/';
- var token = $('#sharingToken').val();
- var hideDownload = $('#hideDownload').val();
-
- if (hideDownload === 'true') {
- window.oncontextmenu = function(event) {
- event.preventDefault();
- event.stopPropagation();
- return false;
- };
- }
-
- if ($el.find('.files-filestable').length) {
-
- this.$showGridView = $('input#showgridview');
- this.$showGridView.on('change', _.bind(this._onGridviewChange, this));
- var filesClient = new OC.Files.Client({
- host: OC.getHost(),
- port: OC.getPort(),
- userName: token,
-
-
- root: OC.getRootPath() + '/public.php/webdav',
- useHTTPS: OC.getProtocol() === 'https'
- });
- this.fileList = new OCA.Files.FileList(
- $el,
- {
- id: 'files.public',
- dragOptions: dragOptions,
- folderDropOptions: folderDropOptions,
- fileActions: fileActions,
- detailsViewEnabled: false,
- filesClient: filesClient,
- enableUpload: true,
- multiSelectMenu: [
- {
- name: 'copyMove',
- displayName: t('files', 'Move or copy'),
- iconClass: 'icon-external',
- },
- {
- name: 'download',
- displayName: t('files', 'Download'),
- iconClass: 'icon-download',
- },
- {
- name: 'delete',
- displayName: t('files', 'Delete'),
- iconClass: 'icon-delete',
- }
- ]
- }
- );
- if (hideDownload === 'true') {
- this.fileList._allowSelection = false;
- }
- this.files = OCA.Files.Files;
- this.files.initialize();
-
-
- OC.Plugins.attach('OCA.Sharing.PublicFileList', this.fileList);
- }
- var mimetype = $('#mimetype').val();
- var mimetypeIcon = $('#mimetypeIcon').val();
- mimetypeIcon = mimetypeIcon.substring(0, mimetypeIcon.length - 3);
- mimetypeIcon = mimetypeIcon + 'svg';
- var previewSupported = $('#previewSupported').val();
- if (typeof FileActions !== 'undefined') {
-
- if (mimetype.substr(0, mimetype.indexOf('/')) !== 'image' && $('.publicpreview').length === 0) {
-
- var spec = FileActions.getDefaultFileAction(mimetype, 'file', OC.PERMISSION_READ);
- if (spec && spec.action) {
- spec.action($('#filename').val());
- }
- }
- }
-
- var bottomMargin = 350;
- var previewWidth = $(window).width();
- var previewHeight = $(window).height() - bottomMargin;
- previewHeight = Math.max(200, previewHeight);
- var params = {
- x: Math.ceil(previewWidth * window.devicePixelRatio),
- y: Math.ceil(previewHeight * window.devicePixelRatio),
- a: 'true',
- file: encodeURIComponent(this.initialDir + $('#filename').val()),
- scalingup: 0
- };
- var imgcontainer = $('<img class="publicpreview" alt="">');
- if (hideDownload === 'false') {
- imgcontainer = $('<a href="' + $('#previewURL').val() + '" target="_blank"></a>').append(imgcontainer);
- }
- var img = imgcontainer.hasClass('publicpreview')? imgcontainer: imgcontainer.find('.publicpreview');
- img.css({
- 'max-width': previewWidth,
- 'max-height': previewHeight
- });
- if (OCA.Viewer && OCA.Viewer.mimetypes.includes(mimetype)
- && (mimetype.startsWith('image/') || mimetype.startsWith('video/') || mimetype.startsWith('audio'))) {
- OCA.Viewer.setRootElement('#imgframe')
- OCA.Viewer.open({ path: '/' })
- } else if (mimetype.substr(0, mimetype.indexOf('/')) === 'text' && window.btoa) {
- if (OC.appswebroots['files_texteditor'] !== undefined ||
- OC.appswebroots['text'] !== undefined) {
-
- return;
- }
-
- var url = parent.location.protocol + '//' + location.host + OC.linkTo('', 'public.php/webdav');
- $.ajax({
- url: url,
- headers: {
- Authorization: 'Basic ' + btoa(token + ':'),
- Range: 'bytes=0-10000'
- }
- }).then(function (data) {
- self._showTextPreview(data, previewHeight);
- });
- } else if ((previewSupported === 'true' && mimetype.substr(0, mimetype.indexOf('/')) !== 'video') ||
- mimetype.substr(0, mimetype.indexOf('/')) === 'image' &&
- mimetype !== 'image/svg+xml') {
- img.attr('src', OC.generateUrl('/apps/files_sharing/publicpreview/' + token + '?' + OC.buildQueryString(params)));
- imgcontainer.appendTo('#imgframe');
- } else if (mimetype.substr(0, mimetype.indexOf('/')) !== 'video') {
- img.attr('src', mimetypeIcon);
- img.attr('width', 128);
-
-
-
- imgcontainer.prependTo('#imgframe');
- } else if (previewSupported === 'true') {
- $('#imgframe > video').attr('poster', OC.generateUrl('/apps/files_sharing/publicpreview/' + token + '?' + OC.buildQueryString(params)));
- }
- if (this.fileList) {
-
- this.fileList.getDownloadUrl = function (filename, dir, isDir) {
- var path = dir || this.getCurrentDirectory();
- if (_.isArray(filename)) {
- filename = JSON.stringify(filename);
- }
- var params = {
- path: path
- };
- if (filename) {
- params.files = filename;
- }
- return OC.generateUrl('/s/' + token + '/download') + '?' + OC.buildQueryString(params);
- };
- this.fileList._createRow = function(fileData) {
- var $tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments);
- if (hideDownload === 'true') {
- this.fileActions.currentFile = $tr.find('td');
-
- $tr.find('a.name').attr('href', '#');
- delete this.fileActions.actions.all.Download;
- }
- return $tr;
- };
- this.fileList.isSelectedDownloadable = function () {
- return hideDownload !== 'true';
- };
- this.fileList.getUploadUrl = function(fileName, dir) {
- if (_.isUndefined(dir)) {
- dir = this.getCurrentDirectory();
- }
- var pathSections = dir.split('/');
- if (!_.isUndefined(fileName)) {
- pathSections.push(fileName);
- }
- var encodedPath = '';
- _.each(pathSections, function(section) {
- if (section !== '') {
- encodedPath += '/' + encodeURIComponent(section);
- }
- });
- var base = '';
- if (!this._uploader.isXHRUpload()) {
-
- base = OC.getProtocol() + '://' + token + '@' + OC.getHost() + (OC.getPort() ? ':' + OC.getPort() : '');
- }
- return base + OC.getRootPath() + '/public.php/webdav' + encodedPath;
- };
- this.fileList.getAjaxUrl = function (action, params) {
- params = params || {};
- params.t = token;
- return OC.filePath('files_sharing', 'ajax', action + '.php') + '?' + OC.buildQueryString(params);
- };
- this.fileList.linkTo = function (dir) {
- return OC.generateUrl('/s/' + token + '') + '?' + OC.buildQueryString({path: dir});
- };
- this.fileList.generatePreviewUrl = function (urlSpec) {
- urlSpec = urlSpec || {};
- if (!urlSpec.x) {
- urlSpec.x = this.$table.data('preview-x') || 250;
- }
- if (!urlSpec.y) {
- urlSpec.y = this.$table.data('preview-y') || 250;
- }
- urlSpec.x *= window.devicePixelRatio;
- urlSpec.y *= window.devicePixelRatio;
- urlSpec.x = Math.ceil(urlSpec.x);
- urlSpec.y = Math.ceil(urlSpec.y);
- var token = $('#dirToken').val();
- return OC.generateUrl('/apps/files_sharing/publicpreview/' + token + '?' + OC.buildQueryString(urlSpec));
- };
- this.fileList.updateEmptyContent = function() {
- this.$el.find('.emptycontent .uploadmessage').text(
- t('files_sharing', 'You can upload into this folder')
- );
- OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments);
- };
- this.fileList._uploader.on('fileuploadadd', function(e, data) {
- if (!data.headers) {
- data.headers = {};
- }
- data.headers.Authorization = 'Basic ' + btoa(token + ':');
- });
-
- delete this.fileList.fileActions.actions.all.Share;
- this.fileList.changeDirectory(this.initialDir || '/', false, true);
-
- this.fileList.$el.on('changeDirectory', _.bind(this._onDirectoryChanged, this));
- OC.Util.History.addOnPopStateHandler(_.bind(this._onUrlChanged, this));
- $('#download').click(function (e) {
- e.preventDefault();
- OC.redirect(FileList.getDownloadUrl());
- });
- if (hideDownload === 'true') {
- this.fileList.$el.find('.summary').find('td:first-child').remove();
- }
- }
- $(document).on('click', '#directLink', function () {
- $(this).focus();
- $(this).select();
- });
- $('.save-form').submit(function (event) {
- event.preventDefault();
- var remote = $(this).find('#remote_address').val();
- var token = $('#sharingToken').val();
- var owner = $('#save-external-share').data('owner');
- var ownerDisplayName = $('#save-external-share').data('owner-display-name');
- var name = $('#save-external-share').data('name');
- var isProtected = $('#save-external-share').data('protected') ? 1 : 0;
- OCA.Sharing.PublicApp._createFederatedShare(remote, token, owner, ownerDisplayName, name, isProtected);
- });
- $('#remote_address').on("keyup paste", function() {
- if ($(this).val() === '' || $('#save-external-share > .icon.icon-loading-small').length > 0) {
- $('#save-button-confirm').prop('disabled', true);
- } else {
- $('#save-button-confirm').prop('disabled', false);
- }
- });
- self._bindShowTermsAction();
-
- window.FileList = this.fileList;
- },
-
- _bindShowTermsAction: function() {
- $('#show-terms-dialog').on('click', function() {
- OC.dialogs.info($('#disclaimerText').val(), t('files_sharing', 'Terms of service'));
- });
- },
- _showTextPreview: function (data, previewHeight) {
- var textDiv = $('<div></div>').addClass('text-preview');
- textDiv.text(data);
- textDiv.appendTo('#imgframe');
- var divHeight = textDiv.height();
- if (data.length > 999) {
- var ellipsis = $('<div></div>').addClass('ellipsis');
- ellipsis.html('(…)');
- ellipsis.appendTo('#imgframe');
- }
- if (divHeight > previewHeight) {
- textDiv.height(previewHeight);
- }
- },
-
- _onGridviewChange: function() {
- const isGridView = this.$showGridView.is(':checked');
- this.$showGridView.next('#view-toggle')
- .removeClass('icon-toggle-filelist icon-toggle-pictures')
- .addClass(isGridView ? 'icon-toggle-filelist' : 'icon-toggle-pictures')
- this.$showGridView.next('#view-toggle').attr(
- 'title',
- isGridView ? t('files', 'Show list view') : t('files', 'Show grid view'),
- )
- if (this.fileList) {
- this.fileList.setGridView(isGridView);
- }
- },
- _onDirectoryChanged: function (e) {
- OC.Util.History.pushState({
-
- path: e.dir
- });
- },
- _onUrlChanged: function (params) {
- this.fileList.changeDirectory(params.path || params.dir, false, true);
- },
-
- _legacyCreateFederatedShare: function (remote, token, owner, ownerDisplayName, name, isProtected) {
- var self = this;
- var location = window.location.protocol + '//' + window.location.host + OC.getRootPath();
- if(remote.substr(-1) !== '/') {
- remote += '/'
- }
- var url = remote + 'index.php/apps/files#' + 'remote=' + encodeURIComponent(location)
- + "&token=" + encodeURIComponent(token) + "&owner=" + encodeURIComponent(owner) +"&ownerDisplayName=" + encodeURIComponent(ownerDisplayName) + "&name=" + encodeURIComponent(name) + "&protected=" + isProtected;
- if (remote.indexOf('://') > 0) {
- OC.redirect(url);
- } else {
-
-
- $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) {
- if (protocol !== 'http' && protocol !== 'https') {
- self._toggleLoading();
- OC.dialogs.alert(t('files_sharing', 'No compatible server found at {remote}', {remote: remote}),
- t('files_sharing', 'Invalid server URL'));
- } else {
- OC.redirect(protocol + '://' + url);
- }
- });
- }
- },
- _toggleLoading: function() {
- var loading = $('#save-external-share > .icon.icon-loading-small').length === 0;
- if (loading) {
- $('#save-external-share > .icon-external')
- .removeClass("icon-external")
- .addClass("icon-loading-small");
- $('#save-external-share #save-button-confirm').prop("disabled", true);
- } else {
- $('#save-external-share > .icon-loading-small')
- .addClass("icon-external")
- .removeClass("icon-loading-small");
- $('#save-external-share #save-button-confirm').prop("disabled", false);
- }
- },
- _createFederatedShare: function (remote, token, owner, ownerDisplayName, name, isProtected) {
- var self = this;
- this._toggleLoading();
- if (remote.indexOf('@') === -1) {
- this._legacyCreateFederatedShare(remote, token, owner, ownerDisplayName, name, isProtected);
- return;
- }
- $.post(
- OC.generateUrl('/apps/federatedfilesharing/createFederatedShare'),
- {
- 'shareWith': remote,
- 'token': token
- }
- ).done(
- function (data) {
- var url = data.remoteUrl;
- if (url.indexOf('://') > 0) {
- OC.redirect(url);
- } else {
- OC.redirect('http://' + url);
- }
- }
- ).fail(
- function (jqXHR) {
- OC.dialogs.alert(JSON.parse(jqXHR.responseText).message,
- t('files_sharing', 'Failed to add the public link to your Nextcloud'));
- self._toggleLoading();
- }
- );
- }
- };
- window.addEventListener('DOMContentLoaded', function () {
-
- if (window.TESTING) {
- return;
- }
- var App = OCA.Sharing.PublicApp;
-
- _.defer(function () {
- App.initialize($('#preview'));
- });
- if (window.Files) {
-
- Files.generatePreviewUrl = function (urlSpec) {
- return App.fileList.generatePreviewUrl(urlSpec);
- };
- }
- });
|