Skip to content

Commit

Permalink
Add support for opening files through viewer
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
  • Loading branch information
danxuliu committed Jan 16, 2020
1 parent d809fc7 commit e2b370f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\TalkSession;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
Expand All @@ -49,6 +51,8 @@
class PageController extends Controller {
/** @var string|null */
private $userId;
/** @var IEventDispatcher */
private $eventDispatcher;
/** @var RoomController */
private $api;
/** @var TalkSession */
Expand All @@ -68,6 +72,7 @@ class PageController extends Controller {

public function __construct(string $appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
RoomController $api,
TalkSession $session,
IUserSession $userSession,
Expand All @@ -78,6 +83,7 @@ public function __construct(string $appName,
IManager $notificationManager,
Config $config) {
parent::__construct($appName, $request);
$this->eventDispatcher = $eventDispatcher;
$this->api = $api;
$this->talkSession = $session;
$this->userSession = $userSession;
Expand Down Expand Up @@ -135,6 +141,10 @@ public function index(string $token = '', string $callUser = '', string $passwor
return $this->guestEnterRoom($token, $password);
}

// Internal files that can be opened by Viewer are available only for
// registered user.
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());

if ($token !== '') {
$room = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
<a :href="data.link"
class="container"
target="_blank"
rel="noopener noreferrer">
rel="noopener noreferrer"
@click="showPreview">
<img v-if="!isLoading && !failed"
class="preview"
alt=""
Expand All @@ -50,6 +51,7 @@ export default {
return {
id: '',
name: '',
path: '',
link: '',
mimetype: '',
'preview-available': '',
Expand Down Expand Up @@ -91,6 +93,36 @@ export default {
}
img.src = this.previewUrl
},
methods: {
showPreview(event) {
if (!this.isViewerAvailable()) {
// Regular event handling by opening the link.
return
}
event.stopPropagation()
event.preventDefault()
OCA.Viewer.open({
// Viewer expects an internal absolute path starting with "/".
path: '/' + this.data.path
})
},
isViewerAvailable() {
if (!OCA.Viewer) {
return false
}
const availableHandlers = OCA.Viewer.availableHandlers
for (let i = 0; i < availableHandlers.length; i++) {
if (availableHandlers[i].mimes.includes(this.data.mimetype)) {
return true
}
}
return false
},
},
}
</script>

Expand Down

0 comments on commit e2b370f

Please sign in to comment.