Skip to content

Commit

Permalink
Public pages compatibility
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Signed-off-by: npmbuildbot[bot] <npmbuildbot[bot]@users.noreply.github.com>
  • Loading branch information
skjnldsv committed Mar 12, 2020
1 parent 41dc52f commit d67331a
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 27 deletions.
46 changes: 33 additions & 13 deletions js/viewer.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion js/viewer.js.map

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

namespace OCA\Viewer\AppInfo;

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Viewer\Event\LoadViewer;
use OCA\Viewer\Listener\LoadViewerScript;
use OCP\AppFramework\App;
Expand All @@ -41,6 +40,5 @@ public function __construct() {
$server = $this->getContainer()->getServer();
$eventDispatcher = $server->query(IEventDispatcher::class);
$eventDispatcher->addServiceListener(LoadViewer::class, LoadViewerScript::class);
$eventDispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadViewerScript::class);
}
}
19 changes: 15 additions & 4 deletions src/mixins/PreviewUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { generateUrl, generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { generateUrl } from '@nextcloud/router'
import { getRootPath, getToken, isPublic } from '../utils/davUtils'

export default {
computed: {
Expand All @@ -32,16 +32,23 @@ export default {
previewpath() {
return this.getPreviewIfAny({
fileid: this.fileid,
filename: this.filename,
hasPreview: this.hasPreview,
davPath: this.davPath,
})
},

/**
* Absolute dav remote path of the file
* @returns {string}
*/
davPath() {
return generateRemoteUrl(`dav/files/${getCurrentUser().uid}${this.filename}`)
// TODO: allow proper dav access without the need of basic auth
// https://github.com/nextcloud/server/issues/19700
if (isPublic()) {
return generateUrl(`/s/${getToken()}/download?path=${this.filename.replace(this.basename, '')}&files=${this.basename}`)
}
return getRootPath() + this.filename
},
},
methods: {
Expand All @@ -55,8 +62,12 @@ export default {
* @param {string} data.davPath the absolute dav path
* @returns {String} the absolute url
*/
getPreviewIfAny({ fileid, hasPreview, davPath }) {
getPreviewIfAny({ fileid, filename, hasPreview, davPath }) {
if (hasPreview) {
// TODO: find a nicer standard way of doing this?
if (isPublic()) {
return generateUrl(`/apps/files_sharing/publicpreview/${getToken()}?fileId=${fileid}&file=${filename}&x=${screen.width}&y=${screen.height}`)
}
return generateUrl(`/core/preview?fileId=${fileid}&x=${screen.width}&y=${screen.height}&a=true`)
}
return davPath
Expand Down
8 changes: 5 additions & 3 deletions src/services/DavClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@

import webdav from 'webdav'
import axios from '@nextcloud/axios'
import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { getRootPath, getToken, isPublic } from '../utils/davUtils'

// force our axios
const patcher = webdav.getPatcher()
patcher.patch('request', axios)

// init webdav client
const client = webdav.createClient(generateRemoteUrl(`dav/files/${getCurrentUser().uid}`))
const client = webdav.createClient(getRootPath(), isPublic()
? { username: getToken(), password: '' }
: {}
)

export default client
42 changes: 42 additions & 0 deletions src/utils/davUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'

const getRootPath = function() {
if (getCurrentUser()) {
return generateRemoteUrl(`dav/files/${getCurrentUser().uid}`)
} else {
return generateRemoteUrl(`webdav`).replace('/remote.php', '/public.php')
}
}

const isPublic = function() {
return !getCurrentUser()
}

const getToken = function() {
return document.getElementById('sharingToken') && document.getElementById('sharingToken').value
}

export { getRootPath, getToken, isPublic }
10 changes: 6 additions & 4 deletions src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</template>

<script>
import { generateRemoteUrl } from '@nextcloud/router'
import { getRootPath } from '../utils/davUtils'
import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
import isFullscreen from '@nextcloud/vue/dist/Mixins/isFullscreen'
import isMobile from '@nextcloud/vue/dist/Mixins/isMobile'
Expand Down Expand Up @@ -158,7 +158,7 @@ export default {

standalone: !(OCA && OCA.Files && 'fileActions' in OCA.Files),

root: generateRemoteUrl(`dav/files/${OC.getCurrentUser().uid}`),
root: getRootPath(),
}),

computed: {
Expand Down Expand Up @@ -376,6 +376,8 @@ export default {
console.error(`The following file could not be displayed`, fileName, fileInfo)
this.close()
}

// if sidebar was opened before, let's update the file
this.changeSidebar()
} catch (error) {
console.error(error)
Expand All @@ -396,10 +398,10 @@ export default {
},

/**
* Show sidebar if sidebar is already opened
* Show sidebar if available and a file is already opened
*/
changeSidebar() {
if (this.sidebarFile !== '') {
if (OCA.Files.Sidebar && this.sidebarFile !== '') {
this.showSidebar()
}
},
Expand Down

0 comments on commit d67331a

Please sign in to comment.