Switch off Docs Viewer for large files

Hello!
We've added several local yearbooks to our Omeka database (http://www.jacksoncountyhistory.org) and have run into an issue with the Docs viewer. We were getting errors about not being able to display the preview for many of the yearbooks and after some googling found Docs Preview (on the google end) has a 25MB limit. Sure enough our smaller PDFs, tested right up to about 24MB, work great.

Now going from this post about not displaying PDF files at all unless through Docs viewer (http://omeka.org/forums/topic/docs-viewer-dont-show-pdf-anywhere-else) I wondered if the opposite was possible, that is to show them for everything UNLESS that file is more than 25MB and in that case to just hide Docs Viewer.

Would code similar to what was shown in that thread apply to the DocsViewer segment of show.php? Or can you even call filesize to filter it?

Thanks in advance for any advice.

Yes, something very similar should work. Instead of checking the extension, you could do something like:

if (metadata($file, 'size') < 25000 ) {
//code for less than 25MB
} else {
//code for more than 25MB
}

I had hoped to post my thanks for such a simple solution patrickmj but I can't quite figure it out. I need to figure out where how to directly call the docs viewer (I haven't even checked yet) but more importantly how to format the php. There seems to be some problem with nesting or tags but I'm not sure what. Right now i've got this:


<?php if (get_theme_option('Item FileGallery') == 0 && metadata('item', 'has files')): ?>
<?php
if (metadata($file, 'size') < 25000 ) {
//code for less than 25MB
} else {
<div class="element-text" style="inline;"><?php echo files_for_item(array('imageSize' => 'thumbnail', 'linkToMetadata' => true)); ?></div>
}
?>
<?php endif; ?>

So the first line shows a list is because I have item gallery turned off, easy enough. After that I added the <?php tag so it would recognize the if else statement, but the ?> in the files for item array line seems to break the PHP. I tried removing it but it seems just about every step results in a white screen where nothing is displayed.

The only better result I've gotten is an actual error screen, though I didn't turn debug on to find out what it was.

I guess my main question is what to do with that <?php tag in the middle of my if statement? can both the <?php and ?> be left out, since it's already in the middle of php tags?

The way you are setting it up, it would look more like this:

<?php if (metadata($file, 'size') < 25000) : ?>
...
<?php else: ?>
...

<?php endif; ?>

Basically, it's making sure that whatever is between a <?php .... ?> is PHP code, and outside of them it is HTML, though, as in your div, there might be PHP also inside the HTML.

That clarifies quite a bit patrickmj - I'm definitely making progress (I'm reliably getting error messages instead of blank pages, so that's good) but I'm not fluent enough with this to know what I don't know yet.

Right now with this code:


<?php if (get_theme_option('Item FileGallery') == 0 && metadata('item', 'has files')): ?>

<?php if (metadata($file, 'size') < 25000) : ?>
<?php fire_plugin_hook('public_items_show', array('view' => $this, 'item' => $item)); ?>
<?php else : ?>
<div class="element-text" style="inline;"><?php echo files_for_item(array('imageSize' => 'thumbnail', 'linkToMetadata' => true)); ?></div>
<?php endif; ?>
<?php endif; ?>

I'm getting this:


exception 'InvalidArgumentException' with message 'Invalid record passed to recordMetadata.' in /var/www/html/application/views/helpers/Metadata.php:57
Stack trace:
#0 [internal function]: Omeka_View_Helper_Metadata->metadata(NULL, 'size', Array)
#1 /var/www/html/application/libraries/Zend/View/Abstract.php(350): call_user_func_array(Array, Array)
#2 /var/www/html/application/libraries/globals.php(2004): Zend_View_Abstract->__call('metadata', Array)
#3 /var/www/html/application/libraries/globals.php(2004): Omeka_View->metadata(NULL, 'size', Array)
#4 /var/www/html/themes/default/items/show.php(7): metadata(NULL, 'size')
#5 /var/www/html/application/libraries/Omeka/View.php(117): include('/var/www/html/t...')
#6 /var/www/html/application/libraries/Zend/View/Abstract.php(888): Omeka_View->_run('/var/www/html/t...')
#7 /var/www/html/application/libraries/Zend/Controller/Action/Helper/ViewRenderer.php(905): Zend_View_Abstract->render('items/show.php')
#8 /var/www/html/application/libraries/Zend/Controller/Action/Helper/ViewRenderer.php(926): Zend_Controller_Action_Helper_ViewRenderer->renderScript('items/show.php', NULL)
#9 /var/www/html/application/libraries/Zend/Controller/Action/Helper/ViewRenderer.php(965): Zend_Controller_Action_Helper_ViewRenderer->render()
#10 /var/www/html/application/libraries/Zend/Controller/Action/HelperBroker.php(277): Zend_Controller_Action_Helper_ViewRenderer->postDispatch()
#11 /var/www/html/application/libraries/Zend/Controller/Action.php(527): Zend_Controller_Action_HelperBroker->notifyPostDispatch()
#12 /var/www/html/application/libraries/Zend/Controller/Dispatcher/Standard.php(308): Zend_Controller_Action->dispatch('showAction')
#13 /var/www/html/application/libraries/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#14 /var/www/html/application/libraries/Zend/Application/Bootstrap/Bootstrap.php(105): Zend_Controller_Front->dispatch()
#15 /var/www/html/application/libraries/Zend/Application.php(382): Zend_Application_Bootstrap_Bootstrap->run()
#16 /var/www/html/application/libraries/Omeka/Application.php(79): Zend_Application->run()
#17 /var/www/html/index.php(23): Omeka_Application->run()
#18 {main}

This looks to me like it's complaining about the $file variable never being set, or that it's NULL - or I'm interpreting it incorrectly.

Doing some more digging on these functions and grabbing file metadata I found this page: https://omeka.org/codex/Display_Specific_Metadata_for_an_Item_File
That seems to do exactly what I want - I tried a few different ways of calling on that function to get the size both keeping the $file variable and using the "while" loop at the bottom of that page. In both cases The page spat back an error about an unrecognized function - it's like fileMetadata() and loop_files_for_item() didn't mean anything to it. The rest of the page draws correctly (Side bars, the title of the item, our logos, etc) but the div where all the item metadata goes just says "Fatal error: Call to undefined function loop_files_for_item() in /var/www/html/themes/default/items/show.php on line 6"

Am I just missing defining that variable somewhere?

loop_files_for_item() is from the Omeka 1.x series, and doesn't exist for the Omeka 2.x series, which explains that error. The page you cite should probably be marked as something that only works in old versions. Sorry for that confusion.

I think you're quite right that it's the $file variable not being set. If you have the current item for the page in $item, then

$files = $item->getFiles();

should give you an array of files for the item. Then, just grab the first one from that array

$file = $files[0];

I _think_ something like that will get you the file you need. Of course, if the item itself doesn't have a file, you'll need to handle that, too, with another if ($file) or something similar.

hope that helps