Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NadirRoGue committed Jun 29, 2016
1 parent 8a356fe commit c5bca2e
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ajax/loadfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

// Check if we are a user
OCP\JSON::checkLoggedIn();
OCP\JSON::callCheck();

\OC::$server->getSession()->close();

// Set the session key for the file we are about to edit.
$dir = isset($_GET['dir']) ? $_GET['dir'] : '';
$filename = isset($_GET['file']) ? $_GET['file'] : '';
$token = isset($_GET['token']) ? $_GET['token'] : '';
if(!empty($filename))
{
header('Content-Type: application/pdf');

if(!empty($token))
{
$linkItem = \OC::$server->getShareManager()->getShareByToken($token);
$owner = $linkItem->getShareOwner();
\OC\Files\Filesystem::init($owner, '/' . $owner . '/files');
$dir = '/' . \OC\Files\Filesystem::getPath($linkItem->getNodeId());
$dir = rtrim($dir, '/');
}

$path = $dir.'/'.$filename;
$filecontents = \OC\Files\Filesystem::file_get_contents($path);

if($filecontents)
{
echo $filecontents;
}
else
{
\OCP\Util::writeLog('files_pdfviewer', 'Error when opening PDF ' . $path, \OCP\Util::ERROR);
OCP\JSON::error(['data' => ['message' => 'A problem occoured while loading the PDF']]);
}
return;

}
else
{
OCP\JSON::error(['data' => ['message' => 'Invalid file path supplied.']]);
}
38 changes: 38 additions & 0 deletions ajax/loadpublicfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

// Check if we are a user
//OCP\JSON::checkLoggedIn();
OCP\JSON::callCheck();

\OC::$server->getSession()->close();

// Set the session key for the file we are about to edit.
$token = isset($_GET['token']) ? $_GET['token'] : '';
if(!empty($token))
{
header('Content-Type: application/pdf');

$linkItem = \OC::$server->getShareManager()->getShareByToken($token);
$owner = $linkItem->getShareOwner();

\OC\Files\Filesystem::tearDown();
\OC\Files\Filesystem::init($owner, '/' . $owner . '/files');

$path = '/' . \OC\Files\Filesystem::getPath($linkItem->getNodeId());

$filecontents = \OC\Files\Filesystem::file_get_contents($path);

if($filecontents)
{
echo $filecontents;
}
else
{
\OCP\Util::writeLog('files_nbviewer', 'Error while opening PDF ' . $path, \OCP\Util::ERROR);
OCP\JSON::error(['data' => ['message' => 'A problem occoured while loading the PDF']]);
}
return;

} else {
OCP\JSON::error(['data' => ['message' => 'Invalid file path supplied.']]);
}
4 changes: 4 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

OCP\Util::addscript('files_pdfviewer', 'pdfobject.min');
OCP\Util::addscript('files_pdfviewer', 'app');
11 changes: 11 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<info>
<id>files_nbviewer</id>
<name>CERNBox NB Viewer</name>
<description>This application integrates the IPython/Jupyter Notebook framework into CERNBox files application</description>
<licence>AGPL</licence>
<author>Nadir Roman Guerrero</author>
<requiremin>8.2.0</requiremin>
<shipped>true</shipped>
<default_enable/>
</info>
7 changes: 7 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/** @var $this OC\Route\Router */
$this->create('files_pdfviewer_load', '/ajax/loadfile.php')
->actionInclude('files_pdfviewer/ajax/loadfile.php');

$this->create('files_pdfviewer_loadpublic', '/ajax/loadpublicfile.php')
->actionInclude('files_pdfviewer/ajax/loadpublicfile.php');
1 change: 1 addition & 0 deletions appinfo/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0
153 changes: 153 additions & 0 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
function PDF_closePublicFileCallback()
{
$('#imgframe').empty();
$('#imgframe').text('Reloading...');
window.location.reload();
}

function PDF_loadFile(file, dir)
{
var urlD = OC.filePath('files_pdfviewer', 'ajax', 'loadfile.php') + "?file=" + encodeURIComponent(file)
+ "&dir=" + encodeURIComponent(dir) + "&requesttoken=" + encodeURIComponent(oc_requesttoken);

if($('#isPublic').attr('value') == '1')
{
urlD = urlD + "&token=" + encodeURIComponent($('#sharingToken').attr('value'));
}

$('#pdfviewer-loader').remove();

var frame = $('#pdfviewer-frame');
frame.attr('type', 'application/pdf');
frame.attr('src', urlD);
frame.contents().find('title').html(file);
frame.on('load', function()
{
document.title = file;
});
}

function PDF_loadPublicFile(token)
{
var urlD = OC.filePath('files_pdfviewer', 'ajax', 'loadpublicfile.php') + "?token=" + encodeURIComponent(token)
+ "&requesttoken=" + encodeURIComponent(oc_requesttoken);

$('#pdfviewer-loader').remove();

var frame = $('#pdfviewer-frame');
frame.attr('type', 'application/pdf');
frame.attr('src', urlD);
}

/**
* Set up all html elements needed to display the notebook
*/
function PDF_setUpEditor(closeCallBack)
{
isPDFViewerOpen = true;
var mainDiv = $('#pdfviewer');
if(mainDiv.length < 1)
{
mainDiv = $('<div id="pdfviewer"></div>');
mainDiv.css('position', 'absolute');
mainDiv.css('top', '0');
mainDiv.css('left', '0');
mainDiv.css('width', '100%');
mainDiv.css('height', '100%');
mainDiv.css('z-index', '200');
mainDiv.css('background-color', '#fff');

var frame = $('<iframe id="pdfviewer-frame"></iframe>');
frame.css('position', 'absolute');
frame.css('top', '0');
frame.css('left', '0');
frame.css('width', '100%');
frame.css('height', '100%');

mainDiv.append(frame);
$('#content').append(mainDiv);
}

var loadingImg = $('<div id="pdfviewer-loader"></div>');
loadingImg.css('position', 'absolute');
loadingImg.css('top', '50%');
loadingImg.css('left', '50%');
loadingImg.css('width', 'auto');
loadingImg.css('height', 'auto');
var img = OC.imagePath('core', 'loading-dark.gif');
var imgContent = $('<img></img>');
imgContent.attr('src',img);
loadingImg.append(imgContent);

var closeButton = $('<div></div>');
closeButton.css('position', 'absolute');
closeButton.css('top', '0');
closeButton.css('left', '95%');
closeButton.css('width', 'auto');
closeButton.css('height', 'auto');
closeButton.css('z-index', '200');
closeButton.css('background-color', '#f00');
var closeImg = OC.imagePath('core', 'actions/close.svg');
var closeImgContent = $('<img></img>');
closeImgContent.attr('src', closeImg);
closeButton.append(closeImgContent);

closeButton.click(function() { PDF_closeFile(closeCallBack); });

$('#app-navigation').hide();
$('#app-content').hide();

mainDiv.append(loadingImg);
mainDiv.append(closeButton);
}

/**
* Open the notebook viewer and displays the notebook given the directory where it belongs and it's name
* @param directory
* @param file
*/
function PDF_openFile(directory, file)
{
PDF_setUpEditor();
PDF_loadFile(file, directory);
}

/**
* Open the notebook viewer and displays the notebook given by a publically shared by link
* @param token
*/
function PDF_openPublicFile(token)
{
PDF_setUpEditor(closePublicFileCallback);
PDF_loadPublicFile(token);
}


function PDF_closeFile(callback)
{
if(isPDFViewerOpen)
{
$('#pdfviewer').remove();
$('#app-navigation').show();
$('#app-content').show();
isPDFViewerOpen = false;

if(callback)
{
callback();
}
}
}

var isPDFViewerOpen = false;
$(document).ready(function ()
{
if (typeof FileActions !== 'undefined')
{
FileActions.setDefault('application/pdf', 'Edit');
OCA.Files.fileActions.register('application/pdf', 'Edit', OC.PERMISSION_READ, '', function (filename)
{
PDF_openFile(FileList.getCurrentDirectory(), filename);
});
}
});
8 changes: 8 additions & 0 deletions js/pdfobject.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5bca2e

Please sign in to comment.