Skip to content

Commit c5bca2e

Browse files
committed
Initial commit
1 parent 8a356fe commit c5bca2e

File tree

8 files changed

+266
-0
lines changed

8 files changed

+266
-0
lines changed

ajax/loadfile.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
// Check if we are a user
4+
OCP\JSON::checkLoggedIn();
5+
OCP\JSON::callCheck();
6+
7+
\OC::$server->getSession()->close();
8+
9+
// Set the session key for the file we are about to edit.
10+
$dir = isset($_GET['dir']) ? $_GET['dir'] : '';
11+
$filename = isset($_GET['file']) ? $_GET['file'] : '';
12+
$token = isset($_GET['token']) ? $_GET['token'] : '';
13+
if(!empty($filename))
14+
{
15+
header('Content-Type: application/pdf');
16+
17+
if(!empty($token))
18+
{
19+
$linkItem = \OC::$server->getShareManager()->getShareByToken($token);
20+
$owner = $linkItem->getShareOwner();
21+
\OC\Files\Filesystem::init($owner, '/' . $owner . '/files');
22+
$dir = '/' . \OC\Files\Filesystem::getPath($linkItem->getNodeId());
23+
$dir = rtrim($dir, '/');
24+
}
25+
26+
$path = $dir.'/'.$filename;
27+
$filecontents = \OC\Files\Filesystem::file_get_contents($path);
28+
29+
if($filecontents)
30+
{
31+
echo $filecontents;
32+
}
33+
else
34+
{
35+
\OCP\Util::writeLog('files_pdfviewer', 'Error when opening PDF ' . $path, \OCP\Util::ERROR);
36+
OCP\JSON::error(['data' => ['message' => 'A problem occoured while loading the PDF']]);
37+
}
38+
return;
39+
40+
}
41+
else
42+
{
43+
OCP\JSON::error(['data' => ['message' => 'Invalid file path supplied.']]);
44+
}

ajax/loadpublicfile.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
// Check if we are a user
4+
//OCP\JSON::checkLoggedIn();
5+
OCP\JSON::callCheck();
6+
7+
\OC::$server->getSession()->close();
8+
9+
// Set the session key for the file we are about to edit.
10+
$token = isset($_GET['token']) ? $_GET['token'] : '';
11+
if(!empty($token))
12+
{
13+
header('Content-Type: application/pdf');
14+
15+
$linkItem = \OC::$server->getShareManager()->getShareByToken($token);
16+
$owner = $linkItem->getShareOwner();
17+
18+
\OC\Files\Filesystem::tearDown();
19+
\OC\Files\Filesystem::init($owner, '/' . $owner . '/files');
20+
21+
$path = '/' . \OC\Files\Filesystem::getPath($linkItem->getNodeId());
22+
23+
$filecontents = \OC\Files\Filesystem::file_get_contents($path);
24+
25+
if($filecontents)
26+
{
27+
echo $filecontents;
28+
}
29+
else
30+
{
31+
\OCP\Util::writeLog('files_nbviewer', 'Error while opening PDF ' . $path, \OCP\Util::ERROR);
32+
OCP\JSON::error(['data' => ['message' => 'A problem occoured while loading the PDF']]);
33+
}
34+
return;
35+
36+
} else {
37+
OCP\JSON::error(['data' => ['message' => 'Invalid file path supplied.']]);
38+
}

appinfo/app.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
OCP\Util::addscript('files_pdfviewer', 'pdfobject.min');
4+
OCP\Util::addscript('files_pdfviewer', 'app');

appinfo/info.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<info>
3+
<id>files_nbviewer</id>
4+
<name>CERNBox NB Viewer</name>
5+
<description>This application integrates the IPython/Jupyter Notebook framework into CERNBox files application</description>
6+
<licence>AGPL</licence>
7+
<author>Nadir Roman Guerrero</author>
8+
<requiremin>8.2.0</requiremin>
9+
<shipped>true</shipped>
10+
<default_enable/>
11+
</info>

appinfo/routes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
/** @var $this OC\Route\Router */
3+
$this->create('files_pdfviewer_load', '/ajax/loadfile.php')
4+
->actionInclude('files_pdfviewer/ajax/loadfile.php');
5+
6+
$this->create('files_pdfviewer_loadpublic', '/ajax/loadpublicfile.php')
7+
->actionInclude('files_pdfviewer/ajax/loadpublicfile.php');

appinfo/version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0

js/app.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
function PDF_closePublicFileCallback()
2+
{
3+
$('#imgframe').empty();
4+
$('#imgframe').text('Reloading...');
5+
window.location.reload();
6+
}
7+
8+
function PDF_loadFile(file, dir)
9+
{
10+
var urlD = OC.filePath('files_pdfviewer', 'ajax', 'loadfile.php') + "?file=" + encodeURIComponent(file)
11+
+ "&dir=" + encodeURIComponent(dir) + "&requesttoken=" + encodeURIComponent(oc_requesttoken);
12+
13+
if($('#isPublic').attr('value') == '1')
14+
{
15+
urlD = urlD + "&token=" + encodeURIComponent($('#sharingToken').attr('value'));
16+
}
17+
18+
$('#pdfviewer-loader').remove();
19+
20+
var frame = $('#pdfviewer-frame');
21+
frame.attr('type', 'application/pdf');
22+
frame.attr('src', urlD);
23+
frame.contents().find('title').html(file);
24+
frame.on('load', function()
25+
{
26+
document.title = file;
27+
});
28+
}
29+
30+
function PDF_loadPublicFile(token)
31+
{
32+
var urlD = OC.filePath('files_pdfviewer', 'ajax', 'loadpublicfile.php') + "?token=" + encodeURIComponent(token)
33+
+ "&requesttoken=" + encodeURIComponent(oc_requesttoken);
34+
35+
$('#pdfviewer-loader').remove();
36+
37+
var frame = $('#pdfviewer-frame');
38+
frame.attr('type', 'application/pdf');
39+
frame.attr('src', urlD);
40+
}
41+
42+
/**
43+
* Set up all html elements needed to display the notebook
44+
*/
45+
function PDF_setUpEditor(closeCallBack)
46+
{
47+
isPDFViewerOpen = true;
48+
var mainDiv = $('#pdfviewer');
49+
if(mainDiv.length < 1)
50+
{
51+
mainDiv = $('<div id="pdfviewer"></div>');
52+
mainDiv.css('position', 'absolute');
53+
mainDiv.css('top', '0');
54+
mainDiv.css('left', '0');
55+
mainDiv.css('width', '100%');
56+
mainDiv.css('height', '100%');
57+
mainDiv.css('z-index', '200');
58+
mainDiv.css('background-color', '#fff');
59+
60+
var frame = $('<iframe id="pdfviewer-frame"></iframe>');
61+
frame.css('position', 'absolute');
62+
frame.css('top', '0');
63+
frame.css('left', '0');
64+
frame.css('width', '100%');
65+
frame.css('height', '100%');
66+
67+
mainDiv.append(frame);
68+
$('#content').append(mainDiv);
69+
}
70+
71+
var loadingImg = $('<div id="pdfviewer-loader"></div>');
72+
loadingImg.css('position', 'absolute');
73+
loadingImg.css('top', '50%');
74+
loadingImg.css('left', '50%');
75+
loadingImg.css('width', 'auto');
76+
loadingImg.css('height', 'auto');
77+
var img = OC.imagePath('core', 'loading-dark.gif');
78+
var imgContent = $('<img></img>');
79+
imgContent.attr('src',img);
80+
loadingImg.append(imgContent);
81+
82+
var closeButton = $('<div></div>');
83+
closeButton.css('position', 'absolute');
84+
closeButton.css('top', '0');
85+
closeButton.css('left', '95%');
86+
closeButton.css('width', 'auto');
87+
closeButton.css('height', 'auto');
88+
closeButton.css('z-index', '200');
89+
closeButton.css('background-color', '#f00');
90+
var closeImg = OC.imagePath('core', 'actions/close.svg');
91+
var closeImgContent = $('<img></img>');
92+
closeImgContent.attr('src', closeImg);
93+
closeButton.append(closeImgContent);
94+
95+
closeButton.click(function() { PDF_closeFile(closeCallBack); });
96+
97+
$('#app-navigation').hide();
98+
$('#app-content').hide();
99+
100+
mainDiv.append(loadingImg);
101+
mainDiv.append(closeButton);
102+
}
103+
104+
/**
105+
* Open the notebook viewer and displays the notebook given the directory where it belongs and it's name
106+
* @param directory
107+
* @param file
108+
*/
109+
function PDF_openFile(directory, file)
110+
{
111+
PDF_setUpEditor();
112+
PDF_loadFile(file, directory);
113+
}
114+
115+
/**
116+
* Open the notebook viewer and displays the notebook given by a publically shared by link
117+
* @param token
118+
*/
119+
function PDF_openPublicFile(token)
120+
{
121+
PDF_setUpEditor(closePublicFileCallback);
122+
PDF_loadPublicFile(token);
123+
}
124+
125+
126+
function PDF_closeFile(callback)
127+
{
128+
if(isPDFViewerOpen)
129+
{
130+
$('#pdfviewer').remove();
131+
$('#app-navigation').show();
132+
$('#app-content').show();
133+
isPDFViewerOpen = false;
134+
135+
if(callback)
136+
{
137+
callback();
138+
}
139+
}
140+
}
141+
142+
var isPDFViewerOpen = false;
143+
$(document).ready(function ()
144+
{
145+
if (typeof FileActions !== 'undefined')
146+
{
147+
FileActions.setDefault('application/pdf', 'Edit');
148+
OCA.Files.fileActions.register('application/pdf', 'Edit', OC.PERMISSION_READ, '', function (filename)
149+
{
150+
PDF_openFile(FileList.getCurrentDirectory(), filename);
151+
});
152+
}
153+
});

js/pdfobject.min.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)